1 /** 2 * $Revision$ 3 * $Date$ 4 * 5 * Copyright 2003-2007 Jive Software. 6 * 7 * All rights reserved. Licensed under the Apache License, Version 2.0 (the "License"); 8 * you may not use this file except in compliance with the License. 9 * You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, software 14 * distributed under the License is distributed on an "AS IS" BASIS, 15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 * See the License for the specific language governing permissions and 17 * limitations under the License. 18 */ 19 20 package org.jivesoftware.smackx.workgroup.ext.macros; 21 22 import java.util.ArrayList; 23 import java.util.Collection; 24 import java.util.Collections; 25 import java.util.Iterator; 26 import java.util.List; 27 28 /** 29 * MacroGroup datamodel. 30 */ 31 public class MacroGroup { 32 private List<Macro> macros; 33 private List<MacroGroup> macroGroups; 34 35 36 // Define MacroGroup 37 private String title; 38 39 public MacroGroup() { 40 macros = new ArrayList<Macro>(); 41 macroGroups = new ArrayList<MacroGroup>(); 42 } 43 44 public void addMacro(Macro macro) { 45 macros.add(macro); 46 } 47 48 public void removeMacro(Macro macro) { 49 macros.remove(macro); 50 } 51 52 public Macro getMacroByTitle(String title) { 53 Collection<Macro> col = Collections.unmodifiableList(macros); 54 Iterator<Macro> iter = col.iterator(); 55 while (iter.hasNext()) { 56 Macro macro = (Macro)iter.next(); 57 if (macro.getTitle().equalsIgnoreCase(title)) { 58 return macro; 59 } 60 } 61 return null; 62 } 63 64 public void addMacroGroup(MacroGroup group) { 65 macroGroups.add(group); 66 } 67 68 public void removeMacroGroup(MacroGroup group) { 69 macroGroups.remove(group); 70 } 71 72 public Macro getMacro(int location) { 73 return (Macro)macros.get(location); 74 } 75 76 public MacroGroup getMacroGroupByTitle(String title) { 77 Collection<MacroGroup> col = Collections.unmodifiableList(macroGroups); 78 Iterator<MacroGroup> iter = col.iterator(); 79 while (iter.hasNext()) { 80 MacroGroup group = (MacroGroup)iter.next(); 81 if (group.getTitle().equalsIgnoreCase(title)) { 82 return group; 83 } 84 } 85 return null; 86 } 87 88 public MacroGroup getMacroGroup(int location) { 89 return (MacroGroup)macroGroups.get(location); 90 } 91 92 93 public List<Macro> getMacros() { 94 return macros; 95 } 96 97 public void setMacros(List<Macro> macros) { 98 this.macros = macros; 99 } 100 101 public List<MacroGroup> getMacroGroups() { 102 return macroGroups; 103 } 104 105 public void setMacroGroups(List<MacroGroup> macroGroups) { 106 this.macroGroups = macroGroups; 107 } 108 109 public String getTitle() { 110 return title; 111 } 112 113 public void setTitle(String title) { 114 this.title = title; 115 } 116 117 public String toXML() { 118 StringBuilder buf = new StringBuilder(); 119 buf.append("<macrogroup>"); 120 buf.append("<title>" + getTitle() + "</title>"); 121 buf.append("<macros>"); 122 for (Macro macro : getMacros()) 123 { 124 buf.append("<macro>"); 125 buf.append("<title>" + macro.getTitle() + "</title>"); 126 buf.append("<type>" + macro.getType() + "</type>"); 127 buf.append("<description>" + macro.getDescription() + "</description>"); 128 buf.append("<response>" + macro.getResponse() + "</response>"); 129 buf.append("</macro>"); 130 } 131 buf.append("</macros>"); 132 133 if (getMacroGroups().size() > 0) { 134 buf.append("<macroGroups>"); 135 for (MacroGroup groups : getMacroGroups()) { 136 buf.append(groups.toXML()); 137 } 138 buf.append("</macroGroups>"); 139 } 140 buf.append("</macrogroup>"); 141 return buf.toString(); 142 } 143 } 144