View Javadoc

1   package org.apache.turbine.services.intake.xmlmodel;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.IOException;
23  import java.io.ObjectInputStream;
24  import java.io.ObjectOutputStream;
25  import java.io.Serializable;
26  
27  import java.util.ArrayList;
28  import java.util.HashMap;
29  import java.util.Iterator;
30  import java.util.List;
31  import java.util.Map;
32  
33  import org.apache.commons.lang.StringUtils;
34  
35  import org.xml.sax.Attributes;
36  
37  /***
38   * A Class for holding data about a property used in an Application.
39   *
40   * @author <a href="mailto:jmcnally@collab.net">John McNally</a>
41   * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
42   * @author <a href="mailto:quintonm@bellsouth.net">Quinton McCombs</a>
43   * @author <a href="mailto:tv@apache.org">Thomas Vandahl</a>
44   * @version $Id: XmlField.java 534527 2007-05-02 16:10:59Z tv $
45   */
46  public class XmlField
47          implements Serializable
48  {
49      /*** Serial Version UID */
50      private static final long serialVersionUID = 6403078189106766569L;
51  
52      private String name;
53      private String key;
54      private String type;
55      private String displayName;
56      private String multiValued;
57      private XmlGroup parent;
58      private List rules;
59      private Map ruleMap;
60      private String ifRequiredMessage;
61      private String mapToObject;
62      private String mapToProperty;
63      private String validator;
64      private String defaultValue;
65      private String emptyValue;
66      private String displaySize;
67      private String fieldClass;
68  
69      /***
70       * Default Constructor
71       */
72      public XmlField()
73      {
74          rules = new ArrayList();
75          ruleMap = new HashMap();
76      }
77  
78      /***
79       * Creates a new column and set the name
80       */
81      public XmlField(String name)
82      {
83          this.name = name;
84          rules = new ArrayList();
85          ruleMap = new HashMap();
86      }
87  
88      /***
89       * Imports a column from an XML specification
90       */
91      public void loadFromXML(Attributes attrib)
92      {
93          setName(attrib.getValue("name"));
94          setKey(attrib.getValue("key"));
95          setType(attrib.getValue("type"));
96          setDisplayName(attrib.getValue("displayName"));
97          setDisplaySize(attrib.getValue("displaySize"));
98          setMultiValued(attrib.getValue("multiValued"));
99  
100         String mapObj = attrib.getValue("mapToObject");
101         if (mapObj != null && mapObj.length() != 0)
102         {
103             setMapToObject(mapObj);
104         }
105 
106         setMapToProperty(attrib.getValue("mapToProperty"));
107         setFieldClass(attrib.getValue("fieldClass"));
108         setValidator(attrib.getValue("validator"));
109         setDefaultValue(attrib.getValue("defaultValue"));
110         setEmptyValue(attrib.getValue("emptyValue"));
111     }
112 
113     /***
114      * Get the name of the property
115      */
116     public String getRawName()
117     {
118         return name;
119     }
120 
121     /***
122      * Get the name of the property
123      */
124     public String getName()
125     {
126         return StringUtils.replace(name, "_", "");
127     }
128 
129     /***
130      * Set the name of the property
131      */
132     public void setName(String newName)
133     {
134         name = newName;
135     }
136 
137     /***
138      * Get the display name of the property
139      */
140     public String getDisplayName()
141     {
142         return displayName;
143     }
144 
145     /***
146      * Set the display name of the property
147      */
148     public void setDisplayName(String newDisplayName)
149     {
150         displayName = newDisplayName;
151     }
152 
153     /***
154      * Sets the display size of the field.
155      */
156     private void setDisplaySize(String size)
157     {
158         this.displaySize = size;
159     }
160 
161     /***
162      * Gets the display size of the field.  This is
163      * useful for constructing the HTML input tag.
164      */
165     public String getDisplaySize()
166     {
167         return this.displaySize;
168     }
169 
170     /***
171      * Set the parameter key of the property
172      */
173     public void setKey(String newKey)
174     {
175         key = newKey;
176     }
177 
178     /***
179      * Get the parameter key of the property
180      */
181     public String getKey()
182     {
183         return key;
184     }
185 
186     /***
187      * Set the type of the property
188      */
189     public void setType(String newType)
190     {
191         type = newType;
192     }
193 
194     /***
195      * Get the type of the property
196      */
197     public String getType()
198     {
199         return type;
200     }
201 
202     /***
203      * Set whether this class can have multiple values
204      */
205     public void setMultiValued(String newMultiValued)
206     {
207         multiValued = newMultiValued;
208     }
209 
210     /***
211      * can this field have several values?
212      */
213     public boolean isMultiValued()
214     {
215         if (multiValued != null && multiValued.equals("true"))
216         {
217             return true;
218         }
219         return false;
220     }
221 
222     /***
223      * Set the name of the object that takes this input
224      *
225      * @param objectName name of the class.
226      */
227     public void setMapToObject(String objectName)
228     {
229         mapToObject = objectName;
230     }
231 
232     /***
233      * Get the name of the object that takes this input
234      */
235     public String getMapToObject()
236     {
237         return mapToObject;
238     }
239 
240     /***
241      * Set the property method that takes this input
242      *
243      * @param prop Name of the property to which the field will be mapped.
244      */
245     public void setMapToProperty(String prop)
246     {
247         mapToProperty = prop;
248     }
249 
250     /***
251      * Get the property method that takes this input
252      */
253     public String getMapToProperty()
254     {
255         if (mapToProperty == null)
256         {
257             return getName();
258         }
259         else
260         {
261             return mapToProperty;
262         }
263     }
264 
265     /***
266      * Set the class name of the validator
267      */
268     public void setValidator(String prop)
269     {
270         validator = prop;
271     }
272 
273     /***
274      * Get the className of the validator
275      */
276     public String getValidator()
277     {
278         return validator;
279     }
280 
281     /***
282      * Set the default Value.
283      *
284      * @param prop The parameter to use as default value.
285      */
286     public void setDefaultValue(String prop)
287     {
288         defaultValue = prop;
289     }
290 
291     /***
292      * Get the default Value.
293      *
294      * @return The default value for this field.
295      */
296     public String getDefaultValue()
297     {
298         return defaultValue;
299     }
300 
301     /***
302      * Set the empty Value.
303      *
304      * @param prop The parameter to use as empty value.
305      */
306     public void setEmptyValue(String prop)
307     {
308         emptyValue = prop;
309     }
310 
311     /***
312      * Get the empty Value.
313      *
314      * @return The empty value for this field.
315      */
316     public String getEmptyValue()
317     {
318         return emptyValue;
319     }
320 
321     /***
322      * The name of the field making sure the first letter is lowercase.
323      *
324      * @return a <code>String</code> value
325      * @deprecated No replacement
326      */
327     public String getVariable()
328     {
329         String firstChar = getName().substring(0, 1).toLowerCase();
330         return firstChar + getName().substring(1);
331     }
332 
333     /***
334      * Set the parent XmlGroup of the property
335      */
336     public void setGroup(XmlGroup parent)
337     {
338         this.parent = parent;
339         if (mapToObject != null && mapToObject.length() != 0)
340         {
341             mapToObject = parent.getAppData().getBasePackage() + mapToObject;
342         }
343     }
344 
345     /***
346      * Get the parent XmlGroup of the property
347      */
348     public XmlGroup getGroup()
349     {
350         return parent;
351     }
352 
353     /***
354      * Get the value of ifRequiredMessage.
355      *
356      * @return value of ifRequiredMessage.
357      */
358     public String getIfRequiredMessage()
359     {
360         return ifRequiredMessage;
361     }
362 
363     /***
364      * Set the value of ifRequiredMessage.
365      *
366      * @param v  Value to assign to ifRequiredMessage.
367      */
368     public void setIfRequiredMessage(String v)
369     {
370         this.ifRequiredMessage = v;
371     }
372 
373     /***
374      * Get the value of fieldClass.
375      *
376      * @return value of fieldClass.
377      */
378     public String getFieldClass()
379     {
380         return fieldClass;
381     }
382 
383     /***
384      * Set the value of fieldClass.
385      *
386      * @param v  Value to assign to fieldClass.
387      */
388     public void setFieldClass(String v)
389     {
390         this.fieldClass = v;
391     }
392 
393     /***
394      * A utility function to create a new input parameter
395      * from attrib and add it to this property.
396      */
397     public Rule addRule(Attributes attrib)
398     {
399         Rule rule = new Rule();
400         rule.loadFromXML(attrib);
401         addRule(rule);
402 
403         return rule;
404     }
405 
406     /***
407      * Adds a new rule to the parameter Map and set the
408      * parent property of the Rule to this property
409      */
410     public void addRule(Rule rule)
411     {
412         rule.setField(this);
413         rules.add(rule);
414         ruleMap.put(rule.getName(), rule);
415     }
416 
417     /***
418      * The collection of rules for this field.
419      *
420      * @return a <code>List</code> value
421      */
422     public List getRules()
423     {
424         return rules;
425     }
426 
427     /***
428      * The collection of rules for this field keyed by
429      * parameter name.
430      *
431      * @return a <code>Map</code> value
432      */
433     public Map getRuleMap()
434     {
435         return ruleMap;
436     }
437 
438     /***
439      * String representation of the column. This
440      * is an xml representation.
441      */
442     public String toString()
443     {
444         StringBuffer result = new StringBuffer();
445         result.append(" <field name=\"" + name + "\"");
446         result.append(" key=\"" + key + "\"");
447         result.append(" type=\"" + type + "\"");
448 
449         if (displayName != null)
450         {
451             result.append(" displayName=\"" + displayName + "\"");
452         }
453         if (mapToObject != null)
454         {
455             result.append(" mapToObject=\"" + mapToObject + "\"");
456         }
457         if (mapToProperty != null)
458         {
459             result.append(" mapToProperty=\"" + mapToProperty + "\"");
460         }
461         if (validator != null)
462         {
463             result.append(" validator=\"" + validator + "\"");
464         }
465         if (defaultValue != null)
466         {
467             result.append(" defaultValue=\"" + defaultValue + "\"");
468         }
469 
470         if (emptyValue != null)
471         {
472             result.append(" emptyValue=\"" + emptyValue + "\"");
473         }
474 
475         if (rules.size() == 0)
476         {
477             result.append(" />\n");
478         }
479         else
480         {
481             result.append(">\n");
482             for (Iterator i = rules.iterator(); i.hasNext();)
483             {
484                 result.append(i.next());
485             }
486             result.append("</field>\n");
487         }
488 
489         return result.toString();
490     }
491 
492     // this methods are called during serialization
493     private void writeObject(ObjectOutputStream stream)
494             throws IOException
495     {
496         stream.defaultWriteObject();
497     }
498 
499     private void readObject(ObjectInputStream stream)
500             throws IOException, ClassNotFoundException
501     {
502         stream.defaultReadObject();
503     }
504 }