1 package org.apache.fulcrum.intake.xmlmodel; 2 3 56 57 import java.io.Serializable ; 58 import java.util.HashMap ; 59 import java.util.Map ; 60 import java.util.List ; 61 import java.util.ArrayList ; 62 import java.util.Iterator ; 63 import org.apache.commons.lang.StringUtils; 64 import org.apache.fulcrum.intake.validator.DefaultValidator; 65 import org.xml.sax.Attributes ; 66 67 74 public class XmlField 75 implements Serializable 76 { 77 private static final String DEFAULT_VALIDATOR = 78 DefaultValidator.class.getName(); 79 80 private String baseClass; 81 private String name; 82 private String key; 83 private String type; 84 private String displayName; 85 private String onError; 86 private String multiValued; 87 private XmlGroup parent; 88 private List rules; 89 private Map ruleMap; 90 private String ifRequiredMessage; 91 private String mapToObject; 92 private String mapToProperty; 93 private String validator; 94 private String defaultValue; 95 96 private static HashMap defaultOnErrors; 97 private static HashMap convertHash; 98 private static HashMap convertArrayHash; 99 100 { 102 populateDefaults(); 103 } 104 105 private static void populateDefaults() 106 { 107 defaultOnErrors = new HashMap (15); 108 convertHash = new HashMap (15); 109 convertArrayHash = new HashMap (15); 110 111 defaultOnErrors.put("boolean", "false"); 112 defaultOnErrors.put("byte", "-1"); 113 defaultOnErrors.put("short", "-1"); 114 defaultOnErrors.put("int", "-1"); 115 defaultOnErrors.put("long", "-1"); 116 defaultOnErrors.put("float", "-1.0f"); 117 defaultOnErrors.put("double", "-1.0"); 118 defaultOnErrors.put("BigDecimal", "new BigDecimal(\"-1.0\")"); 119 121 convertHash.put("boolean", "getBoolean"); 122 convertHash.put("byte", "getByte"); 123 convertHash.put("short", "getShort"); 124 convertHash.put("int", "getInt"); 125 convertHash.put("long", "getLong"); 126 convertHash.put("float", "getFloat"); 127 convertHash.put("double", "getDouble"); 128 convertHash.put("Date", "getDate"); 129 convertHash.put("BigDecimal", "getBigDecimal"); 130 132 convertHash.put("boolean[]", 133 "Boolean.valueOf(stringValue[i]).booleanValue()"); 134 convertArrayHash.put("byte[]", 135 "Byte.valueOf(stringValue[i]).byteValue()"); 136 convertArrayHash.put("short[]", 137 "Short.valueOf(stringValue[i]).shortValue()"); 138 convertArrayHash.put("int[]", "Integer.parseInt(stringValue[i])"); 139 convertArrayHash.put("long[]", "Long.parseLong(stringValue[i])"); 140 convertArrayHash.put("float[]", 141 "Float.valueOf(stringValue[i]).floatValue()"); 142 convertArrayHash.put("double[]", 143 "Double.valueOf(stringValue[i]).doubleValue()"); 144 convertArrayHash.put("Date[]", "FIXME!!"); 145 convertArrayHash.put("BigDecimal[]", "new BigDecimal(stringValue[i])"); 146 } 148 149 152 public XmlField() 153 { 154 rules = new ArrayList (); 155 ruleMap = new HashMap (); 156 } 157 158 159 162 public XmlField(String name) 163 { 164 this.name = name; 165 rules = new ArrayList (); 166 ruleMap = new HashMap (); 167 } 168 169 172 public void loadFromXML (Attributes attrib) 173 { 174 setBaseClass(attrib.getValue("baseClass")); 175 setName(attrib.getValue("name")); 176 key = attrib.getValue("key"); 177 type = attrib.getValue("type"); 178 displayName = attrib.getValue("displayName"); 179 setMultiValued(attrib.getValue("multiValued")); 181 182 String mapObj = attrib.getValue("mapToObject"); 183 if ( mapObj != null && mapObj.length() != 0 ) 184 { 185 setMapToObject(mapObj); 186 } 187 188 String mapProp = attrib.getValue("mapToProperty"); 189 if ( mapProp != null ) 190 { 191 setMapToProperty(mapProp); 192 } 193 setValidator(attrib.getValue("validator")); 194 setDefaultValue(attrib.getValue("defaultValue")); 195 } 196 197 198 201 public String getRawName() 202 { 203 return name; 204 } 205 206 209 public String getName() 210 { 211 return StringUtils.replace(name, "_", ""); 212 } 213 214 217 public void setName(String newName) 218 { 219 name = newName; 220 } 221 222 225 public String getDisplayName() 226 { 227 return displayName; 228 } 229 230 233 public void setDisplayName(String newDisplayName) 234 { 235 displayName = newDisplayName; 236 } 237 238 241 public void setKey(String newKey) 242 { 243 key = newKey; 244 } 245 248 public String getKey() 249 { 250 return key; 251 } 252 253 256 public void setType(String newType) 257 { 258 type = newType; 259 } 260 261 264 public String getType() 265 { 266 272 return type; 273 } 274 275 278 public void setBaseClass(String newBaseClass) 279 { 280 baseClass = newBaseClass; 281 } 282 283 286 public String getBaseClass() 287 { 288 return baseClass; 289 } 290 291 311 312 315 public void setMultiValued(String newMultiValued) 316 { 317 multiValued = newMultiValued; 318 } 319 320 323 public boolean isMultiValued() 324 { 325 if ( multiValued != null && multiValued.equals("true") ) 326 { 327 return true; 328 } 329 return false; 330 } 331 332 335 public void setMapToObject(String obj) 336 { 337 mapToObject = obj; 338 } 339 340 343 public String getMapToObject() 344 { 345 return mapToObject; 346 } 347 348 351 public void setMapToProperty(String prop) 352 { 353 mapToProperty = prop; 354 } 355 356 359 public String getMapToProperty() 360 { 361 if ( mapToProperty == null ) 362 { 363 return getName(); 364 } 365 else 366 { 367 return mapToProperty; 368 } 369 } 370 371 374 public void setValidator(String prop) 375 { 376 validator = prop; 377 } 378 379 382 public String getValidator() 383 { 384 return validator; 385 } 386 387 392 public void setDefaultValue(String prop) 393 { 394 defaultValue = prop; 395 } 396 397 402 public String getDefaultValue() 403 { 404 return defaultValue; 405 } 406 407 412 public String getVariable() 413 { 414 String firstChar = getName().substring(0,1).toLowerCase(); 415 return firstChar + getName().substring(1); 416 } 417 418 public String getPPMethod() 419 { 420 String result = null; 421 if ( convertHash.containsKey(getType())) 422 { 423 result = (String )convertHash.get(getType()); 424 } 425 return result; 426 } 427 428 public String getArrayConvert() 429 { 430 String result = null; 431 if ( convertArrayHash.containsKey(getType())) 432 { 433 result = (String )convertArrayHash.get(getType()); 434 } 435 return result; 436 } 437 438 441 public void setGroup(XmlGroup parent) 442 { 443 this.parent = parent; 444 if ( mapToObject != null && mapToObject.length() != 0 ) 445 { 446 mapToObject = parent.getAppData().getBasePackage() + mapToObject; 447 } 448 } 449 450 453 public XmlGroup getGroup() 454 { 455 return parent; 456 } 457 458 462 public String getIfRequiredMessage() 463 { 464 return ifRequiredMessage; 465 } 466 467 471 public void setIfRequiredMessage(String v) 472 { 473 this.ifRequiredMessage = v; 474 } 475 476 480 public Rule addRule(Attributes attrib) 481 { 482 Rule rule = new Rule(); 483 rule.loadFromXML(attrib); 484 addRule(rule); 485 486 return rule; 487 } 488 489 493 public void addRule(Rule rule) 494 { 495 rule.setField(this); 496 rules.add(rule); 497 ruleMap.put(rule.getName(), rule); 498 } 499 500 505 public List getRules() 506 { 507 return rules; 508 } 509 510 516 public Map getRuleMap() 517 { 518 return ruleMap; 519 } 520 521 525 public String toString() 526 { 527 StringBuffer result = new StringBuffer (); 528 result.append(" <field name=\""+name+"\""); 529 result.append(" key=\""+key+"\""); 530 result.append(" type=\""+type+"\""); 531 532 if (displayName != null) 533 { 534 result.append(" displayName=\""+displayName+"\""); 535 } 536 if (onError != null) 537 { 538 result.append(" onError=\""+onError+"\""); 539 } 540 if (mapToObject != null) 541 { 542 result.append(" mapToObject=\""+mapToObject+"\""); 543 } 544 if (mapToProperty != null) 545 { 546 result.append(" mapToProperty=\""+mapToProperty+"\""); 547 } 548 if (validator != null) 549 { 550 result.append(" validator=\""+validator+"\""); 551 } 552 if (defaultValue != null) 553 { 554 result.append(" defaultValue=\""+defaultValue+"\""); 555 } 556 557 558 if ( rules.size() == 0 ) 559 { 560 result.append(" />\n"); 561 } 562 else 563 { 564 result.append(">\n"); 565 for (Iterator i = rules.iterator() ; i.hasNext() ;) 566 { 567 result.append(i.next()); 568 } 569 result.append("</field>\n"); 570 } 571 572 return result.toString(); 573 } 574 575 private void writeObject(java.io.ObjectOutputStream stream) 577 throws java.io.IOException 578 { 579 stream.defaultWriteObject(); 580 } 581 582 private void readObject(java.io.ObjectInputStream stream) 583 throws java.io.IOException , ClassNotFoundException 584 { 585 stream.defaultReadObject(); 586 populateDefaults(); 587 } 588 } 589 | Popular Tags |