1 package org.apache.turbine.services.intake.xmlmodel; 2 3 18 19 import java.io.IOException ; 20 import java.io.ObjectInputStream ; 21 import java.io.ObjectOutputStream ; 22 import java.io.Serializable ; 23 24 import java.util.ArrayList ; 25 import java.util.HashMap ; 26 import java.util.Iterator ; 27 import java.util.List ; 28 import java.util.Map ; 29 30 import org.apache.commons.lang.StringUtils; 31 32 import org.xml.sax.Attributes ; 33 34 42 public class XmlField 43 implements Serializable 44 { 45 private String name; 46 private String key; 47 private String type; 48 private String displayName; 49 private String multiValued; 50 private XmlGroup parent; 51 private List rules; 52 private Map ruleMap; 53 private String ifRequiredMessage; 54 private String mapToObject; 55 private String mapToProperty; 56 private String validator; 57 private String defaultValue; 58 private String emptyValue; 59 private String displaySize; 60 61 64 public XmlField() 65 { 66 rules = new ArrayList (); 67 ruleMap = new HashMap (); 68 } 69 70 73 public XmlField(String name) 74 { 75 this.name = name; 76 rules = new ArrayList (); 77 ruleMap = new HashMap (); 78 } 79 80 83 public void loadFromXML(Attributes attrib) 84 { 85 setName(attrib.getValue("name")); 86 setKey(attrib.getValue("key")); 87 setType(attrib.getValue("type")); 88 setDisplayName(attrib.getValue("displayName")); 89 setDisplaySize(attrib.getValue("displaySize")); 90 setMultiValued(attrib.getValue("multiValued")); 91 92 String mapObj = attrib.getValue("mapToObject"); 93 if (mapObj != null && mapObj.length() != 0) 94 { 95 setMapToObject(mapObj); 96 } 97 98 setMapToProperty(attrib.getValue("mapToProperty")); 99 setValidator(attrib.getValue("validator")); 100 setDefaultValue(attrib.getValue("defaultValue")); 101 setEmptyValue(attrib.getValue("emptyValue")); 102 } 103 104 107 public String getRawName() 108 { 109 return name; 110 } 111 112 115 public String getName() 116 { 117 return StringUtils.replace(name, "_", ""); 118 } 119 120 123 public void setName(String newName) 124 { 125 name = newName; 126 } 127 128 131 public String getDisplayName() 132 { 133 return displayName; 134 } 135 136 139 public void setDisplayName(String newDisplayName) 140 { 141 displayName = newDisplayName; 142 } 143 144 147 private void setDisplaySize(String size) 148 { 149 this.displaySize = size; 150 } 151 152 156 public String getDisplaySize() 157 { 158 return this.displaySize; 159 } 160 161 164 public void setKey(String newKey) 165 { 166 key = newKey; 167 } 168 169 172 public String getKey() 173 { 174 return key; 175 } 176 177 180 public void setType(String newType) 181 { 182 type = newType; 183 } 184 185 188 public String getType() 189 { 190 return type; 191 } 192 193 196 public void setMultiValued(String newMultiValued) 197 { 198 multiValued = newMultiValued; 199 } 200 201 204 public boolean isMultiValued() 205 { 206 if (multiValued != null && multiValued.equals("true")) 207 { 208 return true; 209 } 210 return false; 211 } 212 213 218 public void setMapToObject(String objectName) 219 { 220 mapToObject = objectName; 221 } 222 223 226 public String getMapToObject() 227 { 228 return mapToObject; 229 } 230 231 236 public void setMapToProperty(String prop) 237 { 238 mapToProperty = prop; 239 } 240 241 244 public String getMapToProperty() 245 { 246 if (mapToProperty == null) 247 { 248 return getName(); 249 } 250 else 251 { 252 return mapToProperty; 253 } 254 } 255 256 259 public void setValidator(String prop) 260 { 261 validator = prop; 262 } 263 264 267 public String getValidator() 268 { 269 return validator; 270 } 271 272 277 public void setDefaultValue(String prop) 278 { 279 defaultValue = prop; 280 } 281 282 287 public String getDefaultValue() 288 { 289 return defaultValue; 290 } 291 292 297 public void setEmptyValue(String prop) 298 { 299 emptyValue = prop; 300 } 301 302 307 public String getEmptyValue() 308 { 309 return emptyValue; 310 } 311 312 318 public String getVariable() 319 { 320 String firstChar = getName().substring(0, 1).toLowerCase(); 321 return firstChar + getName().substring(1); 322 } 323 324 327 public void setGroup(XmlGroup parent) 328 { 329 this.parent = parent; 330 if (mapToObject != null && mapToObject.length() != 0) 331 { 332 mapToObject = parent.getAppData().getBasePackage() + mapToObject; 333 } 334 } 335 336 339 public XmlGroup getGroup() 340 { 341 return parent; 342 } 343 344 349 public String getIfRequiredMessage() 350 { 351 return ifRequiredMessage; 352 } 353 354 359 public void setIfRequiredMessage(String v) 360 { 361 this.ifRequiredMessage = v; 362 } 363 364 368 public Rule addRule(Attributes attrib) 369 { 370 Rule rule = new Rule(); 371 rule.loadFromXML(attrib); 372 addRule(rule); 373 374 return rule; 375 } 376 377 381 public void addRule(Rule rule) 382 { 383 rule.setField(this); 384 rules.add(rule); 385 ruleMap.put(rule.getName(), rule); 386 } 387 388 393 public List getRules() 394 { 395 return rules; 396 } 397 398 404 public Map getRuleMap() 405 { 406 return ruleMap; 407 } 408 409 413 public String toString() 414 { 415 StringBuffer result = new StringBuffer (); 416 result.append(" <field name=\"" + name + "\""); 417 result.append(" key=\"" + key + "\""); 418 result.append(" type=\"" + type + "\""); 419 420 if (displayName != null) 421 { 422 result.append(" displayName=\"" + displayName + "\""); 423 } 424 if (mapToObject != null) 425 { 426 result.append(" mapToObject=\"" + mapToObject + "\""); 427 } 428 if (mapToProperty != null) 429 { 430 result.append(" mapToProperty=\"" + mapToProperty + "\""); 431 } 432 if (validator != null) 433 { 434 result.append(" validator=\"" + validator + "\""); 435 } 436 if (defaultValue != null) 437 { 438 result.append(" defaultValue=\"" + defaultValue + "\""); 439 } 440 441 if (emptyValue != null) 442 { 443 result.append(" emptyValue=\"" + emptyValue + "\""); 444 } 445 446 if (rules.size() == 0) 447 { 448 result.append(" />\n"); 449 } 450 else 451 { 452 result.append(">\n"); 453 for (Iterator i = rules.iterator(); i.hasNext();) 454 { 455 result.append(i.next()); 456 } 457 result.append("</field>\n"); 458 } 459 460 return result.toString(); 461 } 462 463 private void writeObject(ObjectOutputStream stream) 465 throws IOException 466 { 467 stream.defaultWriteObject(); 468 } 469 470 private void readObject(ObjectInputStream stream) 471 throws IOException , ClassNotFoundException 472 { 473 stream.defaultReadObject(); 474 } 475 } 476 | Popular Tags |