View Javadoc

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