View Javadoc
1   package org.apache.fulcrum.intake.model;
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  import java.util.ArrayList;
27  import java.util.HashMap;
28  import java.util.List;
29  import java.util.Map;
30  
31  import javax.xml.bind.Unmarshaller;
32  import javax.xml.bind.annotation.XmlAccessType;
33  import javax.xml.bind.annotation.XmlAccessorType;
34  import javax.xml.bind.annotation.XmlAttribute;
35  import javax.xml.bind.annotation.XmlElement;
36  import javax.xml.bind.annotation.XmlType;
37  
38  import org.apache.avalon.framework.logger.LogEnabled;
39  import org.apache.avalon.framework.logger.Logger;
40  import org.apache.commons.lang3.StringUtils;
41  
42  /**
43   * A Class for holding data about a property used in an Application.
44   *
45   * @author <a href="mailto:jmcnally@collab.net">John McNally</a>
46   * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
47   * @author <a href="mailto:quintonm@bellsouth.net">Quinton McCombs</a>
48   * @author <a href="mailto:tv@apache.org">Thomas Vandahl</a>
49   * @version $Id$
50   */
51  @XmlType(name="field")
52  @XmlAccessorType(XmlAccessType.NONE)
53  public class XmlField
54          implements Serializable, LogEnabled
55  {
56      /**
57       * Serial version id
58       */
59      private static final long serialVersionUID = -734309157828058007L;
60  
61      @XmlAttribute(required=true)
62      private String key;
63  
64      @XmlAttribute(required=true)
65      private String name;
66  
67      @XmlAttribute
68      private String displayName;
69  
70      @XmlAttribute
71      private String displaySize;
72  
73      @XmlAttribute
74      private FieldType type = FieldType.FIELD_STRING;
75  
76      @XmlAttribute
77      private boolean multiValued = false;
78  
79      @XmlAttribute
80      private String fieldClass;
81  
82      @XmlAttribute
83      private String mapToObject;
84  
85      @XmlAttribute
86      private String mapToProperty;
87  
88      @XmlAttribute
89      private String validator;
90  
91      @XmlAttribute
92      private String defaultValue;
93  
94      @XmlAttribute
95      private String emptyValue;
96  
97      private List<Rule> rules;
98      private Map<String, Rule> ruleMap;
99  
100     private Group parent;
101 
102     private Logger log;
103 
104     /**
105      * Default Constructor
106      */
107     public XmlField()
108     {
109         rules = new ArrayList<Rule>();
110         ruleMap = new HashMap<String, Rule>();
111     }
112 
113     /**
114 	 * Enable Avalon Logging
115 	 */
116 	@Override
117 	public void enableLogging(Logger logger)
118 	{
119 		this.log = logger;
120 	}
121 
122 	/**
123 	 * Return Avalon logger
124 	 *
125 	 * @return the logger
126 	 */
127 	public Logger getLogger()
128 	{
129 		return log;
130 	}
131 
132     /**
133      * Get the name of the property
134      *
135      * @return the raw name of the property
136      */
137     public String getRawName()
138     {
139         return name;
140     }
141 
142     /**
143      * Get the name of the property
144      *
145      * @return the name of the property with underscores removed
146      */
147     public String getName()
148     {
149         return StringUtils.replace(name, "_", "");
150     }
151 
152     /**
153      * Get the display name of the property
154      *
155      * @return the display name of the property
156      */
157     public String getDisplayName()
158     {
159         return displayName;
160     }
161 
162     /**
163      * Gets the display size of the field.  This is
164      * useful for constructing the HTML input tag.
165      *
166      * @return the display size for the field
167      */
168     public String getDisplaySize()
169     {
170         return this.displaySize;
171     }
172 
173     /**
174      * Get the parameter key of the property
175      *
176      * @return the key of the property
177      */
178     public String getKey()
179     {
180         return key;
181     }
182 
183     /**
184      * Get the type of the property
185      *
186      * @return the type of the field
187      */
188     public FieldType getType()
189     {
190         return type;
191     }
192 
193     /**
194      * Can this field have several values?
195      *
196      * @return true if the field can have multiple values
197      */
198     public boolean isMultiValued()
199     {
200         return multiValued;
201     }
202 
203     /**
204      * Get the name of the object that takes this input
205      *
206      * @return the name of the mapped object
207      */
208     public String getMapToObject()
209     {
210         return mapToObject;
211     }
212 
213     /**
214      * Get the property method that takes this input
215      *
216      * @return the property this field is mapped to
217      */
218     public String getMapToProperty()
219     {
220         if (mapToProperty == null)
221         {
222             return getName();
223         }
224         else
225         {
226             return mapToProperty;
227         }
228     }
229 
230     /**
231      * Get the className of the validator
232      *
233      * @return the validator class name
234      */
235     public String getValidator()
236     {
237         return validator;
238     }
239 
240     /**
241      * Get the default Value.
242      *
243      * @return The default value for this field.
244      */
245     public String getDefaultValue()
246     {
247         return defaultValue;
248     }
249 
250     /**
251      * Get the empty Value.
252      *
253      * @return The empty value for this field.
254      */
255     public String getEmptyValue()
256     {
257         return emptyValue;
258     }
259 
260     /**
261      * Get the parent XmlGroup of the field
262      *
263      * @return the group this field belongs to
264      */
265     public Group getGroup()
266     {
267         return this.parent;
268     }
269 
270     /**
271      * Get the value of fieldClass.
272      *
273      * @return value of fieldClass.
274      */
275     public String getFieldClass()
276     {
277         return fieldClass;
278     }
279 
280     /**
281      * The collection of rules for this field.
282      *
283      * @return a <code>List</code> value
284      */
285     public List<Rule> getRules()
286     {
287         return rules;
288     }
289 
290     /**
291      * Set the collection of rules for this field
292      *
293      * @param rules the rules to set
294      */
295     @XmlElement(name="rule")
296     public void setRules(List<Rule> rules)
297     {
298         this.rules = rules;
299     }
300 
301     /**
302      * The collection of rules for this field keyed by
303      * parameter name.
304      *
305      * @return a <code>Map</code> value
306      */
307     public Map<String, Rule> getRuleMap()
308     {
309         return ruleMap;
310     }
311 
312     /**
313      * JAXB callback to set the parent object
314      *
315      * @param um the Unmarshaller
316      * @param parent the parent object (an XmlGroup)
317      */
318     public void afterUnmarshal(Unmarshaller um, Object parent)
319     {
320         this.parent = (Group)parent;
321 
322         // Build map
323         this.ruleMap.clear();
324         for (Rule rule : rules)
325         {
326             ruleMap.put(rule.getName(), rule);
327         }
328 
329         // if a mapToProperty exists, set the object to this group's default
330         if (mapToObject == null && 
331         		mapToProperty != null &&
332         		StringUtils.isNotEmpty(mapToProperty) &&
333         		this.parent.getDefaultMapToObject() != null)
334         {
335         	mapToObject = this.parent.getDefaultMapToObject();
336         }
337     }
338 
339     /**
340      * String representation of the column. This
341      * is an xml representation.
342      *
343      * @return the value of this field as an XML representation
344      */
345     @Override
346     public String toString()
347     {
348         StringBuilder result = new StringBuilder();
349         result.append(" <field name=\"").append(name).append("\"")
350             .append(" key=\"").append(key).append("\"")
351             .append(" type=\"").append(type.value()).append("\"");
352 
353         if (displayName != null)
354         {
355             result.append(" displayName=\"").append(displayName).append("\"");
356         }
357         if (mapToObject != null)
358         {
359             result.append(" mapToObject=\"").append(mapToObject).append("\"");
360         }
361         if (mapToProperty != null)
362         {
363             result.append(" mapToProperty=\"").append(mapToProperty).append("\"");
364         }
365         if (validator != null)
366         {
367             result.append(" validator=\"").append(validator).append("\"");
368         }
369         if (defaultValue != null)
370         {
371             result.append(" defaultValue=\"").append(defaultValue).append("\"");
372         }
373 
374         if (emptyValue != null)
375         {
376             result.append(" emptyValue=\"").append(emptyValue).append("\"");
377         }
378 
379         if (rules.size() == 0)
380         {
381             result.append(" />\n");
382         }
383         else
384         {
385             result.append(">\n");
386             for (Rule rule : rules)
387             {
388                 result.append(rule);
389             }
390             result.append("</field>\n");
391         }
392 
393         return result.toString();
394     }
395 
396     // this methods are called during serialization
397     private void writeObject(ObjectOutputStream stream)
398             throws IOException
399     {
400         stream.defaultWriteObject();
401     }
402 
403     private void readObject(ObjectInputStream stream)
404             throws IOException, ClassNotFoundException
405     {
406         stream.defaultReadObject();
407     }
408 }