KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > turbine > services > intake > xmlmodel > XmlField


1 package org.apache.turbine.services.intake.xmlmodel;
2
3 /*
4  * Copyright 2001-2004 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License")
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 import java.io.IOException JavaDoc;
20 import java.io.ObjectInputStream JavaDoc;
21 import java.io.ObjectOutputStream JavaDoc;
22 import java.io.Serializable JavaDoc;
23
24 import java.util.ArrayList JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.Map JavaDoc;
29
30 import org.apache.commons.lang.StringUtils;
31
32 import org.xml.sax.Attributes JavaDoc;
33
34 /**
35  * A Class for holding data about a property used in an Application.
36  *
37  * @author <a HREF="mailto:jmcnally@collab.net">John McNally</a>
38  * @author <a HREF="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
39  * @author <a HREF="mailto:quintonm@bellsouth.net">Quinton McCombs</a>
40  * @version $Id: XmlField.java,v 1.10.2.2 2004/05/20 03:06:49 seade Exp $
41  */

42 public class XmlField
43         implements Serializable JavaDoc
44 {
45     private String JavaDoc name;
46     private String JavaDoc key;
47     private String JavaDoc type;
48     private String JavaDoc displayName;
49     private String JavaDoc multiValued;
50     private XmlGroup parent;
51     private List JavaDoc rules;
52     private Map JavaDoc ruleMap;
53     private String JavaDoc ifRequiredMessage;
54     private String JavaDoc mapToObject;
55     private String JavaDoc mapToProperty;
56     private String JavaDoc validator;
57     private String JavaDoc defaultValue;
58     private String JavaDoc emptyValue;
59     private String JavaDoc displaySize;
60
61     /**
62      * Default Constructor
63      */

64     public XmlField()
65     {
66         rules = new ArrayList JavaDoc();
67         ruleMap = new HashMap JavaDoc();
68     }
69
70     /**
71      * Creates a new column and set the name
72      */

73     public XmlField(String JavaDoc name)
74     {
75         this.name = name;
76         rules = new ArrayList JavaDoc();
77         ruleMap = new HashMap JavaDoc();
78     }
79
80     /**
81      * Imports a column from an XML specification
82      */

83     public void loadFromXML(Attributes JavaDoc 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 JavaDoc 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     /**
105      * Get the name of the property
106      */

107     public String JavaDoc getRawName()
108     {
109         return name;
110     }
111
112     /**
113      * Get the name of the property
114      */

115     public String JavaDoc getName()
116     {
117         return StringUtils.replace(name, "_", "");
118     }
119
120     /**
121      * Set the name of the property
122      */

123     public void setName(String JavaDoc newName)
124     {
125         name = newName;
126     }
127
128     /**
129      * Get the display name of the property
130      */

131     public String JavaDoc getDisplayName()
132     {
133         return displayName;
134     }
135
136     /**
137      * Set the display name of the property
138      */

139     public void setDisplayName(String JavaDoc newDisplayName)
140     {
141         displayName = newDisplayName;
142     }
143
144     /**
145      * Sets the display size of the field.
146      */

147     private void setDisplaySize(String JavaDoc size)
148     {
149         this.displaySize = size;
150     }
151
152     /**
153      * Gets the display size of the field. This is
154      * useful for constructing the HTML input tag.
155      */

156     public String JavaDoc getDisplaySize()
157     {
158         return this.displaySize;
159     }
160
161     /**
162      * Set the parameter key of the property
163      */

164     public void setKey(String JavaDoc newKey)
165     {
166         key = newKey;
167     }
168
169     /**
170      * Get the parameter key of the property
171      */

172     public String JavaDoc getKey()
173     {
174         return key;
175     }
176
177     /**
178      * Set the type of the property
179      */

180     public void setType(String JavaDoc newType)
181     {
182         type = newType;
183     }
184
185     /**
186      * Get the type of the property
187      */

188     public String JavaDoc getType()
189     {
190         return type;
191     }
192
193     /**
194      * Set whether this class can have multiple values
195      */

196     public void setMultiValued(String JavaDoc newMultiValued)
197     {
198         multiValued = newMultiValued;
199     }
200
201     /**
202      * can this field have several values?
203      */

204     public boolean isMultiValued()
205     {
206         if (multiValued != null && multiValued.equals("true"))
207         {
208             return true;
209         }
210         return false;
211     }
212
213     /**
214      * Set the name of the object that takes this input
215      *
216      * @param objectName name of the class.
217      */

218     public void setMapToObject(String JavaDoc objectName)
219     {
220         mapToObject = objectName;
221     }
222
223     /**
224      * Get the name of the object that takes this input
225      */

226     public String JavaDoc getMapToObject()
227     {
228         return mapToObject;
229     }
230
231     /**
232      * Set the property method that takes this input
233      *
234      * @param prop Name of the property to which the field will be mapped.
235      */

236     public void setMapToProperty(String JavaDoc prop)
237     {
238         mapToProperty = prop;
239     }
240
241     /**
242      * Get the property method that takes this input
243      */

244     public String JavaDoc getMapToProperty()
245     {
246         if (mapToProperty == null)
247         {
248             return getName();
249         }
250         else
251         {
252             return mapToProperty;
253         }
254     }
255
256     /**
257      * Set the class name of the validator
258      */

259     public void setValidator(String JavaDoc prop)
260     {
261         validator = prop;
262     }
263
264     /**
265      * Get the className of the validator
266      */

267     public String JavaDoc getValidator()
268     {
269         return validator;
270     }
271
272     /**
273      * Set the default Value.
274      *
275      * @param prop The parameter to use as default value.
276      */

277     public void setDefaultValue(String JavaDoc prop)
278     {
279         defaultValue = prop;
280     }
281
282     /**
283      * Get the default Value.
284      *
285      * @return The default value for this field.
286      */

287     public String JavaDoc getDefaultValue()
288     {
289         return defaultValue;
290     }
291
292     /**
293      * Set the empty Value.
294      *
295      * @param prop The parameter to use as empty value.
296      */

297     public void setEmptyValue(String JavaDoc prop)
298     {
299         emptyValue = prop;
300     }
301
302     /**
303      * Get the empty Value.
304      *
305      * @return The empty value for this field.
306      */

307     public String JavaDoc getEmptyValue()
308     {
309         return emptyValue;
310     }
311
312     /**
313      * The name of the field making sure the first letter is lowercase.
314      *
315      * @return a <code>String</code> value
316      * @deprecated No replacement
317      */

318     public String JavaDoc getVariable()
319     {
320         String JavaDoc firstChar = getName().substring(0, 1).toLowerCase();
321         return firstChar + getName().substring(1);
322     }
323
324     /**
325      * Set the parent XmlGroup of the property
326      */

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

339     public XmlGroup getGroup()
340     {
341         return parent;
342     }
343
344     /**
345      * Get the value of ifRequiredMessage.
346      *
347      * @return value of ifRequiredMessage.
348      */

349     public String JavaDoc getIfRequiredMessage()
350     {
351         return ifRequiredMessage;
352     }
353
354     /**
355      * Set the value of ifRequiredMessage.
356      *
357      * @param v Value to assign to ifRequiredMessage.
358      */

359     public void setIfRequiredMessage(String JavaDoc v)
360     {
361         this.ifRequiredMessage = v;
362     }
363
364     /**
365      * A utility function to create a new input parameter
366      * from attrib and add it to this property.
367      */

368     public Rule addRule(Attributes JavaDoc attrib)
369     {
370         Rule rule = new Rule();
371         rule.loadFromXML(attrib);
372         addRule(rule);
373
374         return rule;
375     }
376
377     /**
378      * Adds a new rule to the parameter Map and set the
379      * parent property of the Rule to this property
380      */

381     public void addRule(Rule rule)
382     {
383         rule.setField(this);
384         rules.add(rule);
385         ruleMap.put(rule.getName(), rule);
386     }
387
388     /**
389      * The collection of rules for this field.
390      *
391      * @return a <code>List</code> value
392      */

393     public List JavaDoc getRules()
394     {
395         return rules;
396     }
397
398     /**
399      * The collection of rules for this field keyed by
400      * parameter name.
401      *
402      * @return a <code>Map</code> value
403      */

404     public Map JavaDoc getRuleMap()
405     {
406         return ruleMap;
407     }
408
409     /**
410      * String representation of the column. This
411      * is an xml representation.
412      */

413     public String JavaDoc toString()
414     {
415         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
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 JavaDoc 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     // this methods are called during serialization
464
private void writeObject(ObjectOutputStream JavaDoc stream)
465             throws IOException JavaDoc
466     {
467         stream.defaultWriteObject();
468     }
469
470     private void readObject(ObjectInputStream JavaDoc stream)
471             throws IOException JavaDoc, ClassNotFoundException JavaDoc
472     {
473         stream.defaultReadObject();
474     }
475 }
476
Popular Tags