1 package org.apache.turbine.services.intake.xmlmodel;
2
3 /* ====================================================================
4 * The Apache Software License, Version 1.1
5 *
6 * Copyright (c) 2001 The Apache Software Foundation. All rights
7 * reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 *
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
19 * distribution.
20 *
21 * 3. The end-user documentation included with the redistribution,
22 * if any, must include the following acknowledgment:
23 * "This product includes software developed by the
24 * Apache Software Foundation (http://www.apache.org/)."
25 * Alternately, this acknowledgment may appear in the software itself,
26 * if and wherever such third-party acknowledgments normally appear.
27 *
28 * 4. The names "Apache" and "Apache Software Foundation" and
29 * "Apache Turbine" must not be used to endorse or promote products
30 * derived from this software without prior written permission. For
31 * written permission, please contact apache@apache.org.
32 *
33 * 5. Products derived from this software may not be called "Apache",
34 * "Apache Turbine", nor may "Apache" appear in their name, without
35 * prior written permission of the Apache Software Foundation.
36 *
37 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
41 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48 * SUCH DAMAGE.
49 * ====================================================================
50 *
51 * This software consists of voluntary contributions made by many
52 * individuals on behalf of the Apache Software Foundation. For more
53 * information on the Apache Software Foundation, please see
54 * <http://www.apache.org/>.
55 */
56
57 import java.util.HashMap;
58 import java.util.Map;
59 import java.util.List;
60 import java.util.ArrayList;
61 import java.util.Iterator;
62 import org.apache.turbine.util.StringUtils;
63 import org.xml.sax.Attributes;
64
65 /***
66 * A Class for holding data about a property used in an Application.
67 *
68 * @author <a href="mailto:jmcnally@collab.net>John McNally</a>
69 * @version $Id: XmlField.java,v 1.2 2001/10/09 14:44:39 henning Exp $
70 */
71 public class XmlField
72 implements java.io.Serializable
73 {
74 private static final String DEFAULT_VALIDATOR =
75 "org.apache.turbine.services.intake.validator.DefaultValidator";
76
77 private String baseClass;
78 private String name;
79 private String key;
80 private String type;
81 private String displayName;
82 private String onError;
83 private String multiValued;
84 private XmlGroup parent;
85 private List rules;
86 private Map ruleMap;
87 private String ifRequiredMessage;
88 private String mapToObject;
89 private String mapToProperty;
90 private String validator;
91 private String defaultValue;
92
93 private static HashMap defaultOnErrors;
94 private static HashMap convertHash;
95 private static HashMap convertArrayHash;
96
97 // static
98 {
99 populateDefaults();
100 }
101
102 private static void populateDefaults()
103 {
104 defaultOnErrors = new HashMap(15);
105 convertHash = new HashMap(15);
106 convertArrayHash = new HashMap(15);
107
108 defaultOnErrors.put("boolean", "false");
109 defaultOnErrors.put("byte", "-1");
110 defaultOnErrors.put("short", "-1");
111 defaultOnErrors.put("int", "-1");
112 defaultOnErrors.put("long", "-1");
113 defaultOnErrors.put("float", "-1.0f");
114 defaultOnErrors.put("double", "-1.0");
115 defaultOnErrors.put("BigDecimal", "new BigDecimal(\"-1.0\")");
116 // defaultOnErrors.put("BigInteger", "new BigInteger(\"-1\")");
117
118 convertHash.put("boolean", "getBoolean");
119 convertHash.put("byte", "getByte");
120 convertHash.put("short", "getShort");
121 convertHash.put("int", "getInt");
122 convertHash.put("long", "getLong");
123 convertHash.put("float", "getFloat");
124 convertHash.put("double", "getDouble");
125 convertHash.put("Date", "getDate");
126 convertHash.put("BigDecimal", "getBigDecimal");
127 // convertHash.put("BigInteger", "getBigInteger");
128
129 convertHash.put("boolean[]",
130 "Boolean.valueOf(stringValue[i]).booleanValue()");
131 convertArrayHash.put("byte[]",
132 "Byte.valueOf(stringValue[i]).byteValue()");
133 convertArrayHash.put("short[]",
134 "Short.valueOf(stringValue[i]).shortValue()");
135 convertArrayHash.put("int[]", "Integer.parseInt(stringValue[i])");
136 convertArrayHash.put("long[]", "Long.parseLong(stringValue[i])");
137 convertArrayHash.put("float[]",
138 "Float.valueOf(stringValue[i]).floatValue()");
139 convertArrayHash.put("double[]",
140 "Double.valueOf(stringValue[i]).doubleValue()");
141 convertArrayHash.put("Date[]", "FIXME!!");
142 convertArrayHash.put("BigDecimal[]", "new BigDecimal(stringValue[i])");
143 // convertHash.put("BigInteger", "new BigInteger(stringValue)");
144 }
145
146 /***
147 * Default Constructor
148 */
149 public XmlField()
150 {
151 rules = new ArrayList();
152 ruleMap = new HashMap();
153 }
154
155
156 /***
157 * Creates a new column and set the name
158 */
159 public XmlField(String name)
160 {
161 this.name = name;
162 rules = new ArrayList();
163 ruleMap = new HashMap();
164 }
165
166 /***
167 * Imports a column from an XML specification
168 */
169 public void loadFromXML (Attributes attrib)
170 {
171 setBaseClass(attrib.getValue("baseClass"));
172 setName(attrib.getValue("name"));
173 key = attrib.getValue("key");
174 type = attrib.getValue("type");
175 displayName = attrib.getValue("displayName");
176 //setOnError(attrib.getValue("onError"));
177 setMultiValued(attrib.getValue("multiValued"));
178
179 String mapObj = attrib.getValue("mapToObject");
180 if ( mapObj != null && mapObj.length() != 0 )
181 {
182 setMapToObject(mapObj);
183 }
184
185 String mapProp = attrib.getValue("mapToProperty");
186 if ( mapProp != null )
187 {
188 setMapToProperty(mapProp);
189 }
190 setValidator(attrib.getValue("validator"));
191 setDefaultValue(attrib.getValue("defaultValue"));
192 }
193
194
195 /***
196 * Get the name of the property
197 */
198 public String getRawName()
199 {
200 return name;
201 }
202
203 /***
204 * Get the name of the property
205 */
206 public String getName()
207 {
208 return StringUtils.removeUnderScores(name);
209 }
210
211 /***
212 * Set the name of the property
213 */
214 public void setName(String newName)
215 {
216 name = newName;
217 }
218
219 /***
220 * Get the display name of the property
221 */
222 public String getDisplayName()
223 {
224 return displayName;
225 }
226
227 /***
228 * Set the display name of the property
229 */
230 public void setDisplayName(String newDisplayName)
231 {
232 displayName = newDisplayName;
233 }
234
235 /***
236 * Set the parameter key of the property
237 */
238 public void setKey(String newKey)
239 {
240 key = newKey;
241 }
242 /***
243 * Get the parameter key of the property
244 */
245 public String getKey()
246 {
247 return key;
248 }
249
250 /***
251 * Set the type of the property
252 */
253 public void setType(String newType)
254 {
255 type = newType;
256 }
257
258 /***
259 * Get the type of the property
260 */
261 public String getType()
262 {
263 /*
264 if ( isMultiValued() )
265 {
266 return type + "[]";
267 }
268 */
269 return type;
270 }
271
272 /***
273 * Set the base class of the field
274 */
275 public void setBaseClass(String newBaseClass)
276 {
277 baseClass = newBaseClass;
278 }
279
280 /***
281 * Get the base class of the field
282 */
283 public String getBaseClass()
284 {
285 return baseClass;
286 }
287
288 /* *
289 * Set the value of the property, if a conversion error occurs.
290 * /
291 public void setOnError(String newOnError)
292 {
293 onError = newOnError;
294 }
295
296 /**
297 * Get the value of the property, if a conversion error occurs.
298 * /
299 public String getOnError()
300 {
301 if ( onError == null && defaultOnErrors.containsKey(getType()) )
302 {
303 onError = (String)defaultOnErrors.get(getType());
304 }
305 return onError;
306 }
307 */
308
309 /***
310 * Set whether this class can have multiple values
311 */
312 public void setMultiValued(String newMultiValued)
313 {
314 multiValued = newMultiValued;
315 }
316
317 /***
318 * can this field have several values?
319 */
320 public boolean isMultiValued()
321 {
322 if ( multiValued != null && multiValued.equals("true") )
323 {
324 return true;
325 }
326 return false;
327 }
328
329 /***
330 * Set the name of the object that takes this input
331 */
332 public void setMapToObject(String obj)
333 {
334 mapToObject = obj;
335 }
336
337 /***
338 * Get the name of the object that takes this input
339 */
340 public String getMapToObject()
341 {
342 return mapToObject;
343 }
344
345 /***
346 * Set the property method that takes this input
347 */
348 public void setMapToProperty(String prop)
349 {
350 mapToProperty = prop;
351 }
352
353 /***
354 * Get the property method that takes this input
355 */
356 public String getMapToProperty()
357 {
358 if ( mapToProperty == null )
359 {
360 return getName();
361 }
362 else
363 {
364 return mapToProperty;
365 }
366 }
367
368 /***
369 * Set the class name of the validator
370 */
371 public void setValidator(String prop)
372 {
373 validator = prop;
374 }
375
376 /***
377 * Get the className of the validator
378 */
379 public String getValidator()
380 {
381 return validator;
382 }
383
384 /***
385 * Set the default Value
386 */
387 public void setDefaultValue(String prop)
388 {
389 defaultValue = prop;
390 }
391
392 /***
393 * Get the default Value
394 */
395 public String getDefaultValue()
396 {
397 return defaultValue;
398 }
399
400 /***
401 * The name of the field making sure the first letter is lowercase.
402 *
403 * @return a <code>String</code> value
404 */
405 public String getVariable()
406 {
407 String firstChar = getName().substring(0,1).toLowerCase();
408 return firstChar + getName().substring(1);
409 }
410
411 public String getPPMethod()
412 {
413 String result = null;
414 if ( convertHash.containsKey(getType()))
415 {
416 result = (String)convertHash.get(getType());
417 }
418 return result;
419 }
420
421 public String getArrayConvert()
422 {
423 String result = null;
424 if ( convertArrayHash.containsKey(getType()))
425 {
426 result = (String)convertArrayHash.get(getType());
427 }
428 return result;
429 }
430
431 /***
432 * Set the parent XmlGroup of the property
433 */
434 public void setGroup(XmlGroup parent)
435 {
436 this.parent = parent;
437 if ( mapToObject != null && mapToObject.length() != 0 )
438 {
439 mapToObject = parent.getAppData().getBasePackage() + mapToObject;
440 }
441 }
442
443 /***
444 * Get the parent XmlGroup of the property
445 */
446 public XmlGroup getGroup()
447 {
448 return parent;
449 }
450
451 /***
452 * Get the value of ifRequiredMessage.
453 * @return value of ifRequiredMessage.
454 */
455 public String getIfRequiredMessage()
456 {
457 return ifRequiredMessage;
458 }
459
460 /***
461 * Set the value of ifRequiredMessage.
462 * @param v Value to assign to ifRequiredMessage.
463 */
464 public void setIfRequiredMessage(String v)
465 {
466 this.ifRequiredMessage = v;
467 }
468
469 /***
470 * A utility function to create a new input parameter
471 * from attrib and add it to this property.
472 */
473 public Rule addRule(Attributes attrib)
474 {
475 Rule rule = new Rule();
476 rule.loadFromXML(attrib);
477 addRule(rule);
478
479 return rule;
480 }
481
482 /***
483 * Adds a new rule to the parameter Map and set the
484 * parent property of the Rule to this property
485 */
486 public void addRule(Rule rule)
487 {
488 rule.setField(this);
489 rules.add(rule);
490 ruleMap.put(rule.getName(), rule);
491 }
492
493 /***
494 * The collection of rules for this field.
495 *
496 * @return a <code>List</code> value
497 */
498 public List getRules()
499 {
500 return rules;
501 }
502
503 /***
504 * The collection of rules for this field keyed by
505 * parameter name.
506 *
507 * @return a <code>Map</code> value
508 */
509 public Map getRuleMap()
510 {
511 return ruleMap;
512 }
513
514 /***
515 * String representation of the column. This
516 * is an xml representation.
517 */
518 public String toString()
519 {
520 StringBuffer result = new StringBuffer();
521 result.append(" <field name=\""+name+"\"");
522 result.append(" key=\""+key+"\"");
523 result.append(" type=\""+type+"\"");
524
525 if (displayName != null)
526 {
527 result.append(" displayName=\""+displayName+"\"");
528 }
529 if (onError != null)
530 {
531 result.append(" onError=\""+onError+"\"");
532 }
533 if (mapToObject != null)
534 {
535 result.append(" mapToObject=\""+mapToObject+"\"");
536 }
537 if (mapToProperty != null)
538 {
539 result.append(" mapToProperty=\""+mapToProperty+"\"");
540 }
541 if (validator != null)
542 {
543 result.append(" validator=\""+validator+"\"");
544 }
545 if (defaultValue != null)
546 {
547 result.append(" defaultValue=\""+defaultValue+"\"");
548 }
549
550
551 if ( rules.size() == 0 )
552 {
553 result.append(" />\n");
554 }
555 else
556 {
557 result.append(">\n");
558 for (Iterator i = rules.iterator() ; i.hasNext() ;)
559 {
560 result.append(i.next());
561 }
562 result.append("</field>\n");
563 }
564
565 return result.toString();
566 }
567
568 // this methods are called during serialization
569 private void writeObject(java.io.ObjectOutputStream stream)
570 throws java.io.IOException
571 {
572 stream.defaultWriteObject();
573 }
574
575 private void readObject(java.io.ObjectInputStream stream)
576 throws java.io.IOException, ClassNotFoundException
577 {
578 stream.defaultReadObject();
579 populateDefaults();
580 }
581
582
583 }
This page was automatically generated by Maven