1 package org.apache.turbine.services.intake.xmlmodel; 2 3 18 19 import java.io.Serializable ; 20 21 import java.util.ArrayList ; 22 import java.util.Iterator ; 23 import java.util.List ; 24 25 import org.apache.commons.lang.StringUtils; 26 27 import org.xml.sax.Attributes ; 28 29 35 public class XmlGroup 36 implements Serializable 37 { 38 private List fields; 39 private List mapToObjects; 40 private String defaultMapToObject; 41 private AppData parent; 42 private String groupName; 43 private String key; 44 private String poolCapacity; 45 46 49 public XmlGroup() 50 { 51 fields = new ArrayList (); 52 mapToObjects = new ArrayList (2); 53 } 54 55 58 public void loadFromXML(Attributes attrib) 59 { 60 groupName = attrib.getValue("name"); 61 key = attrib.getValue("key"); 62 poolCapacity = attrib.getValue("pool-capacity"); 63 64 String objName = attrib.getValue("mapToObject"); 65 if (StringUtils.isNotEmpty(objName)) 66 { 67 defaultMapToObject = objName; 68 } 69 } 70 71 74 public String getName() 75 { 76 return groupName; 77 } 78 79 82 public void setName(String newGroupName) 83 { 84 groupName = newGroupName; 85 } 86 87 90 public String getKey() 91 { 92 return key; 93 } 94 95 98 public void setKey(String newKey) 99 { 100 key = newKey; 101 } 102 103 109 public String getPoolCapacity() 110 { 111 if (poolCapacity == null) 112 { 113 return "128"; 114 } 115 116 return poolCapacity; 117 } 118 119 123 public XmlField addField(Attributes attrib) 124 { 125 XmlField field = new XmlField(); 126 field.loadFromXML(attrib); 127 addField(field); 128 129 return field; 130 } 131 132 136 public void addField(XmlField field) 137 { 138 field.setGroup(this); 139 140 if (field.getMapToObject() != null) 143 { 144 boolean isNewObject = true; 145 for (int i = 0; i < mapToObjects.size(); i++) 146 { 147 if (mapToObjects.get(i).equals(field.getMapToObject())) 148 { 149 isNewObject = false; 150 break; 151 } 152 } 153 if (isNewObject) 154 { 155 mapToObjects.add(field.getMapToObject()); 156 } 157 } 158 else if (field.getMapToProperty() != null 160 && !"".equals(field.getMapToProperty()) 161 && defaultMapToObject != null) 162 { 163 field.setMapToObject(defaultMapToObject); 164 } 165 166 fields.add(field); 167 } 168 169 172 public List getFields() 173 { 174 return fields; 175 } 176 177 180 public int getNumFields() 181 { 182 return fields.size(); 183 } 184 185 189 public XmlField getField(String name) 190 { 191 String curName; 192 193 for (Iterator iter = fields.iterator(); iter.hasNext();) 194 { 195 XmlField field = (XmlField) iter.next(); 196 curName = field.getRawName(); 197 if (curName.equals(name)) 198 { 199 return field; 200 } 201 } 202 return null; 203 } 204 205 208 public boolean containsField(XmlField field) 209 { 210 return fields.contains(field); 211 } 212 213 216 public boolean containsField(String name) 217 { 218 return (getField(name) != null); 219 } 220 221 public List getMapToObjects() 222 { 223 return mapToObjects; 224 } 225 226 229 public void setAppData(AppData parent) 230 { 231 this.parent = parent; 232 if (defaultMapToObject != null) 233 { 234 defaultMapToObject = parent.getBasePackage() + defaultMapToObject; 235 mapToObjects.add(defaultMapToObject); 236 } 237 } 238 239 242 public AppData getAppData() 243 { 244 return parent; 245 } 246 247 250 public String getVariable() 251 { 252 String firstChar = getName().substring(0, 1).toLowerCase(); 253 return firstChar + getName().substring(1); 254 } 255 256 260 public String toString() 261 { 262 StringBuffer result = new StringBuffer (); 263 264 result.append("<group name=\"").append(getName()); 265 result.append(" key=\"" + key + "\""); 266 result.append(">\n"); 267 268 if (fields != null) 269 { 270 for (Iterator iter = fields.iterator(); iter.hasNext();) 271 { 272 result.append(iter.next()); 273 } 274 } 275 276 result.append("</group>\n"); 277 278 return result.toString(); 279 } 280 } 281 | Popular Tags |