KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fulcrum > intake > xmlmodel > XmlField


1 package org.apache.fulcrum.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.io.Serializable JavaDoc;
58 import java.util.HashMap JavaDoc;
59 import java.util.Map JavaDoc;
60 import java.util.List JavaDoc;
61 import java.util.ArrayList JavaDoc;
62 import java.util.Iterator JavaDoc;
63 import org.apache.commons.lang.StringUtils;
64 import org.apache.fulcrum.intake.validator.DefaultValidator;
65 import org.xml.sax.Attributes JavaDoc;
66
67 /**
68  * A Class for holding data about a property used in an Application.
69  *
70  * @author <a HREF="mailto:jmcnally@collab.net>John McNally</a>
71  * @author <a HREF="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
72  * @version $Id: XmlField.java,v 1.1 2004/11/12 10:25:47 epugh Exp $
73  */

74 public class XmlField
75     implements Serializable JavaDoc
76 {
77     private static final String JavaDoc DEFAULT_VALIDATOR =
78         DefaultValidator.class.getName();
79
80     private String JavaDoc baseClass;
81     private String JavaDoc name;
82     private String JavaDoc key;
83     private String JavaDoc type;
84     private String JavaDoc displayName;
85     private String JavaDoc onError;
86     private String JavaDoc multiValued;
87     private XmlGroup parent;
88     private List JavaDoc rules;
89     private Map JavaDoc ruleMap;
90     private String JavaDoc ifRequiredMessage;
91     private String JavaDoc mapToObject;
92     private String JavaDoc mapToProperty;
93     private String JavaDoc validator;
94     private String JavaDoc defaultValue;
95
96     private static HashMap JavaDoc defaultOnErrors;
97     private static HashMap JavaDoc convertHash;
98     private static HashMap JavaDoc convertArrayHash;
99
100     // static
101
{
102         populateDefaults();
103     }
104
105     private static void populateDefaults()
106     {
107         defaultOnErrors = new HashMap JavaDoc(15);
108         convertHash = new HashMap JavaDoc(15);
109         convertArrayHash = new HashMap JavaDoc(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         // defaultOnErrors.put("BigInteger", "new BigInteger(\"-1\")");
120

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         // convertHash.put("BigInteger", "getBigInteger");
131

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         // convertHash.put("BigInteger", "new BigInteger(stringValue)");
147
}
148
149     /**
150      * Default Constructor
151      */

152     public XmlField()
153     {
154         rules = new ArrayList JavaDoc();
155         ruleMap = new HashMap JavaDoc();
156     }
157
158
159     /**
160      * Creates a new column and set the name
161      */

162     public XmlField(String JavaDoc name)
163     {
164         this.name = name;
165         rules = new ArrayList JavaDoc();
166         ruleMap = new HashMap JavaDoc();
167     }
168
169     /**
170      * Imports a column from an XML specification
171      */

172     public void loadFromXML (Attributes JavaDoc 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         //setOnError(attrib.getValue("onError"));
180
setMultiValued(attrib.getValue("multiValued"));
181
182         String JavaDoc mapObj = attrib.getValue("mapToObject");
183         if ( mapObj != null && mapObj.length() != 0 )
184         {
185             setMapToObject(mapObj);
186         }
187
188         String JavaDoc 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     /**
199      * Get the name of the property
200      */

201     public String JavaDoc getRawName()
202     {
203         return name;
204     }
205
206     /**
207      * Get the name of the property
208      */

209     public String JavaDoc getName()
210     {
211         return StringUtils.replace(name, "_", "");
212     }
213
214     /**
215      * Set the name of the property
216      */

217     public void setName(String JavaDoc newName)
218     {
219         name = newName;
220     }
221
222     /**
223      * Get the display name of the property
224      */

225     public String JavaDoc getDisplayName()
226     {
227         return displayName;
228     }
229
230     /**
231      * Set the display name of the property
232      */

233     public void setDisplayName(String JavaDoc newDisplayName)
234     {
235         displayName = newDisplayName;
236     }
237
238     /**
239      * Set the parameter key of the property
240      */

241     public void setKey(String JavaDoc newKey)
242     {
243         key = newKey;
244     }
245     /**
246      * Get the parameter key of the property
247      */

248     public String JavaDoc getKey()
249     {
250         return key;
251     }
252
253     /**
254      * Set the type of the property
255      */

256     public void setType(String JavaDoc newType)
257     {
258         type = newType;
259     }
260
261     /**
262      * Get the type of the property
263      */

264     public String JavaDoc getType()
265     {
266         /*
267         if ( isMultiValued() )
268         {
269             return type + "[]";
270         }
271         */

272         return type;
273     }
274
275     /**
276      * Set the base class of the field
277      */

278     public void setBaseClass(String JavaDoc newBaseClass)
279     {
280         baseClass = newBaseClass;
281     }
282
283     /**
284      * Get the base class of the field
285      */

286     public String JavaDoc getBaseClass()
287     {
288         return baseClass;
289     }
290
291     /* *
292      * Set the value of the property, if a conversion error occurs.
293      * /
294     public void setOnError(String newOnError)
295     {
296         onError = newOnError;
297     }
298
299     /* *
300      * Get the value of the property, if a conversion error occurs.
301      * /
302     public String getOnError()
303     {
304         if ( onError == null && defaultOnErrors.containsKey(getType()) )
305         {
306             onError = (String)defaultOnErrors.get(getType());
307         }
308         return onError;
309     }
310     */

311
312     /**
313      * Set whether this class can have multiple values
314      */

315     public void setMultiValued(String JavaDoc newMultiValued)
316     {
317         multiValued = newMultiValued;
318     }
319
320     /**
321      * can this field have several values?
322      */

323     public boolean isMultiValued()
324     {
325         if ( multiValued != null && multiValued.equals("true") )
326         {
327             return true;
328         }
329         return false;
330     }
331
332     /**
333      * Set the name of the object that takes this input
334      */

335     public void setMapToObject(String JavaDoc obj)
336     {
337         mapToObject = obj;
338     }
339
340     /**
341      * Get the name of the object that takes this input
342      */

343     public String JavaDoc getMapToObject()
344     {
345         return mapToObject;
346     }
347
348     /**
349      * Set the property method that takes this input
350      */

351     public void setMapToProperty(String JavaDoc prop)
352     {
353         mapToProperty = prop;
354     }
355
356     /**
357      * Get the property method that takes this input
358      */

359     public String JavaDoc getMapToProperty()
360     {
361         if ( mapToProperty == null )
362         {
363             return getName();
364         }
365         else
366         {
367             return mapToProperty;
368         }
369     }
370
371     /**
372      * Set the class name of the validator
373      */

374     public void setValidator(String JavaDoc prop)
375     {
376         validator = prop;
377     }
378
379     /**
380      * Get the className of the validator
381      */

382     public String JavaDoc getValidator()
383     {
384         return validator;
385     }
386
387     /**
388      * Set the default Value.
389      *
390      * @param prop The parameter to use as default value.
391      */

392     public void setDefaultValue(String JavaDoc prop)
393     {
394         defaultValue = prop;
395     }
396     
397     /**
398      * Get the default Value.
399      *
400      * @return The default value for this field.
401      */

402     public String JavaDoc getDefaultValue()
403     {
404         return defaultValue;
405     }
406     
407     /**
408      * The name of the field making sure the first letter is lowercase.
409      *
410      * @return a <code>String</code> value
411      */

412     public String JavaDoc getVariable()
413     {
414         String JavaDoc firstChar = getName().substring(0,1).toLowerCase();
415         return firstChar + getName().substring(1);
416     }
417
418     public String JavaDoc getPPMethod()
419     {
420         String JavaDoc result = null;
421         if ( convertHash.containsKey(getType()))
422         {
423             result = (String JavaDoc)convertHash.get(getType());
424         }
425         return result;
426     }
427
428     public String JavaDoc getArrayConvert()
429     {
430         String JavaDoc result = null;
431         if ( convertArrayHash.containsKey(getType()))
432         {
433             result = (String JavaDoc)convertArrayHash.get(getType());
434         }
435         return result;
436     }
437
438     /**
439      * Set the parent XmlGroup of the property
440      */

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     /**
451      * Get the parent XmlGroup of the property
452      */

453     public XmlGroup getGroup()
454     {
455         return parent;
456     }
457
458     /**
459      * Get the value of ifRequiredMessage.
460      * @return value of ifRequiredMessage.
461      */

462     public String JavaDoc getIfRequiredMessage()
463     {
464         return ifRequiredMessage;
465     }
466
467     /**
468      * Set the value of ifRequiredMessage.
469      * @param v Value to assign to ifRequiredMessage.
470      */

471     public void setIfRequiredMessage(String JavaDoc v)
472     {
473         this.ifRequiredMessage = v;
474     }
475
476     /**
477      * A utility function to create a new input parameter
478      * from attrib and add it to this property.
479      */

480     public Rule addRule(Attributes JavaDoc attrib)
481     {
482         Rule rule = new Rule();
483         rule.loadFromXML(attrib);
484         addRule(rule);
485
486         return rule;
487     }
488
489     /**
490      * Adds a new rule to the parameter Map and set the
491      * parent property of the Rule to this property
492      */

493     public void addRule(Rule rule)
494     {
495         rule.setField(this);
496         rules.add(rule);
497         ruleMap.put(rule.getName(), rule);
498     }
499
500     /**
501      * The collection of rules for this field.
502      *
503      * @return a <code>List</code> value
504      */

505     public List JavaDoc getRules()
506     {
507         return rules;
508     }
509
510     /**
511      * The collection of rules for this field keyed by
512      * parameter name.
513      *
514      * @return a <code>Map</code> value
515      */

516     public Map JavaDoc getRuleMap()
517     {
518         return ruleMap;
519     }
520
521     /**
522      * String representation of the column. This
523      * is an xml representation.
524      */

525     public String JavaDoc toString()
526     {
527         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
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 JavaDoc 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     // this methods are called during serialization
576
private void writeObject(java.io.ObjectOutputStream JavaDoc stream)
577         throws java.io.IOException JavaDoc
578     {
579         stream.defaultWriteObject();
580     }
581
582     private void readObject(java.io.ObjectInputStream JavaDoc stream)
583         throws java.io.IOException JavaDoc, ClassNotFoundException JavaDoc
584     {
585         stream.defaultReadObject();
586         populateDefaults();
587     }
588 }
589
Popular Tags