001    package org.apache.fulcrum.intake.xmlmodel;
002    
003    /*
004     * Licensed to the Apache Software Foundation (ASF) under one
005     * or more contributor license agreements.  See the NOTICE file
006     * distributed with this work for additional information
007     * regarding copyright ownership.  The ASF licenses this file
008     * to you under the Apache License, Version 2.0 (the
009     * "License"); you may not use this file except in compliance
010     * with the License.  You may obtain a copy of the License at
011     *
012     *   http://www.apache.org/licenses/LICENSE-2.0
013     *
014     * Unless required by applicable law or agreed to in writing,
015     * software distributed under the License is distributed on an
016     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017     * KIND, either express or implied.  See the License for the
018     * specific language governing permissions and limitations
019     * under the License.
020     */
021    
022    import java.io.IOException;
023    import java.io.ObjectInputStream;
024    import java.io.ObjectOutputStream;
025    import java.io.Serializable;
026    
027    import java.util.ArrayList;
028    import java.util.HashMap;
029    import java.util.Iterator;
030    import java.util.List;
031    import java.util.Map;
032    
033    import org.apache.commons.lang.StringUtils;
034    
035    import org.xml.sax.Attributes;
036    
037    /**
038     * A Class for holding data about a property used in an Application.
039     *
040     * @author <a href="mailto:jmcnally@collab.net">John McNally</a>
041     * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
042     * @author <a href="mailto:quintonm@bellsouth.net">Quinton McCombs</a>
043     * @author <a href="mailto:tv@apache.org">Thomas Vandahl</a>
044     * @version $Id: XmlField.java 535465 2007-05-05 06:58:06Z tv $
045     */
046    public class XmlField
047            implements Serializable
048    {
049        /**
050         * Serial version id
051         */
052        private static final long serialVersionUID = -734309157828058007L;
053    
054        private String name;
055        private String key;
056        private String type;
057        private String displayName;
058        private String multiValued;
059        private XmlGroup parent;
060        private List rules;
061        private Map ruleMap;
062        private String ifRequiredMessage;
063        private String mapToObject;
064        private String mapToProperty;
065        private String validator;
066        private String defaultValue;
067        private String emptyValue;
068        private String displaySize;
069        private String fieldClass;
070    
071        /**
072         * Default Constructor
073         */
074        public XmlField()
075        {
076            rules = new ArrayList();
077            ruleMap = new HashMap();
078        }
079    
080        /**
081         * Creates a new column and set the name
082         */
083        public XmlField(String name)
084        {
085            this.name = name;
086            rules = new ArrayList();
087            ruleMap = new HashMap();
088        }
089    
090        /**
091         * Imports a column from an XML specification
092         */
093        public void loadFromXML(Attributes attrib)
094        {
095            setName(attrib.getValue("name"));
096            setKey(attrib.getValue("key"));
097            setType(attrib.getValue("type"));
098            setDisplayName(attrib.getValue("displayName"));
099            setDisplaySize(attrib.getValue("displaySize"));
100            setMultiValued(attrib.getValue("multiValued"));
101    
102            String mapObj = attrib.getValue("mapToObject");
103            if (mapObj != null && mapObj.length() != 0)
104            {
105                setMapToObject(mapObj);
106            }
107    
108            setMapToProperty(attrib.getValue("mapToProperty"));
109            setFieldClass(attrib.getValue("fieldClass"));
110            setValidator(attrib.getValue("validator"));
111            setDefaultValue(attrib.getValue("defaultValue"));
112            setEmptyValue(attrib.getValue("emptyValue"));
113        }
114    
115        /**
116         * Get the name of the property
117         */
118        public String getRawName()
119        {
120            return name;
121        }
122    
123        /**
124         * Get the name of the property
125         */
126        public String getName()
127        {
128            return StringUtils.replace(name, "_", "");
129        }
130    
131        /**
132         * Set the name of the property
133         */
134        public void setName(String newName)
135        {
136            name = newName;
137        }
138    
139        /**
140         * Get the display name of the property
141         */
142        public String getDisplayName()
143        {
144            return displayName;
145        }
146    
147        /**
148         * Set the display name of the property
149         */
150        public void setDisplayName(String newDisplayName)
151        {
152            displayName = newDisplayName;
153        }
154    
155        /**
156         * Sets the display size of the field.
157         */
158        private void setDisplaySize(String size)
159        {
160            this.displaySize = size;
161        }
162    
163        /**
164         * Gets the display size of the field.  This is
165         * useful for constructing the HTML input tag.
166         */
167        public String getDisplaySize()
168        {
169            return this.displaySize;
170        }
171    
172        /**
173         * Set the parameter key of the property
174         */
175        public void setKey(String newKey)
176        {
177            key = newKey;
178        }
179    
180        /**
181         * Get the parameter key of the property
182         */
183        public String getKey()
184        {
185            return key;
186        }
187    
188        /**
189         * Set the type of the property
190         */
191        public void setType(String newType)
192        {
193            type = newType;
194        }
195    
196        /**
197         * Get the type of the property
198         */
199        public String getType()
200        {
201            return type;
202        }
203    
204        /**
205         * Set whether this class can have multiple values
206         */
207        public void setMultiValued(String newMultiValued)
208        {
209            multiValued = newMultiValued;
210        }
211    
212        /**
213         * can this field have several values?
214         */
215        public boolean isMultiValued()
216        {
217            if (multiValued != null && multiValued.equals("true"))
218            {
219                return true;
220            }
221            return false;
222        }
223    
224        /**
225         * Set the name of the object that takes this input
226         *
227         * @param objectName name of the class.
228         */
229        public void setMapToObject(String objectName)
230        {
231            mapToObject = objectName;
232        }
233    
234        /**
235         * Get the name of the object that takes this input
236         */
237        public String getMapToObject()
238        {
239            return mapToObject;
240        }
241    
242        /**
243         * Set the property method that takes this input
244         *
245         * @param prop Name of the property to which the field will be mapped.
246         */
247        public void setMapToProperty(String prop)
248        {
249            mapToProperty = prop;
250        }
251    
252        /**
253         * Get the property method that takes this input
254         */
255        public String getMapToProperty()
256        {
257            if (mapToProperty == null)
258            {
259                return getName();
260            }
261            else
262            {
263                return mapToProperty;
264            }
265        }
266    
267        /**
268         * Set the class name of the validator
269         */
270        public void setValidator(String prop)
271        {
272            validator = prop;
273        }
274    
275        /**
276         * Get the className of the validator
277         */
278        public String getValidator()
279        {
280            return validator;
281        }
282    
283        /**
284         * Set the default Value.
285         *
286         * @param prop The parameter to use as default value.
287         */
288        public void setDefaultValue(String prop)
289        {
290            defaultValue = prop;
291        }
292    
293        /**
294         * Get the default Value.
295         *
296         * @return The default value for this field.
297         */
298        public String getDefaultValue()
299        {
300            return defaultValue;
301        }
302    
303        /**
304         * Set the empty Value.
305         *
306         * @param prop The parameter to use as empty value.
307         */
308        public void setEmptyValue(String prop)
309        {
310            emptyValue = prop;
311        }
312    
313        /**
314         * Get the empty Value.
315         *
316         * @return The empty value for this field.
317         */
318        public String getEmptyValue()
319        {
320            return emptyValue;
321        }
322    
323        /**
324         * The name of the field making sure the first letter is lowercase.
325         *
326         * @return a <code>String</code> value
327         * @deprecated No replacement
328         */
329        public String getVariable()
330        {
331            String firstChar = getName().substring(0, 1).toLowerCase();
332            return firstChar + getName().substring(1);
333        }
334    
335        /**
336         * Set the parent XmlGroup of the property
337         */
338        public void setGroup(XmlGroup parent)
339        {
340            this.parent = parent;
341            if (mapToObject != null && mapToObject.length() != 0)
342            {
343                mapToObject = parent.getAppData().getBasePackage() + mapToObject;
344            }
345        }
346    
347        /**
348         * Get the parent XmlGroup of the property
349         */
350        public XmlGroup getGroup()
351        {
352            return parent;
353        }
354    
355        /**
356         * Get the value of ifRequiredMessage.
357         *
358         * @return value of ifRequiredMessage.
359         */
360        public String getIfRequiredMessage()
361        {
362            return ifRequiredMessage;
363        }
364    
365        /**
366         * Set the value of ifRequiredMessage.
367         *
368         * @param v  Value to assign to ifRequiredMessage.
369         */
370        public void setIfRequiredMessage(String v)
371        {
372            this.ifRequiredMessage = v;
373        }
374    
375        /**
376         * Get the value of fieldClass.
377         *
378         * @return value of fieldClass.
379         */
380        public String getFieldClass()
381        {
382            return fieldClass;
383        }
384    
385        /**
386         * Set the value of fieldClass.
387         *
388         * @param v  Value to assign to fieldClass.
389         */
390        public void setFieldClass(String v)
391        {
392            this.fieldClass = v;
393        }
394    
395        /**
396         * A utility function to create a new input parameter
397         * from attrib and add it to this property.
398         */
399        public Rule addRule(Attributes attrib)
400        {
401            Rule rule = new Rule();
402            rule.loadFromXML(attrib);
403            addRule(rule);
404    
405            return rule;
406        }
407    
408        /**
409         * Adds a new rule to the parameter Map and set the
410         * parent property of the Rule to this property
411         */
412        public void addRule(Rule rule)
413        {
414            rule.setField(this);
415            rules.add(rule);
416            ruleMap.put(rule.getName(), rule);
417        }
418    
419        /**
420         * The collection of rules for this field.
421         *
422         * @return a <code>List</code> value
423         */
424        public List getRules()
425        {
426            return rules;
427        }
428    
429        /**
430         * The collection of rules for this field keyed by
431         * parameter name.
432         *
433         * @return a <code>Map</code> value
434         */
435        public Map getRuleMap()
436        {
437            return ruleMap;
438        }
439    
440        /**
441         * String representation of the column. This
442         * is an xml representation.
443         */
444        public String toString()
445        {
446            StringBuffer result = new StringBuffer();
447            result.append(" <field name=\"" + name + "\"");
448            result.append(" key=\"" + key + "\"");
449            result.append(" type=\"" + type + "\"");
450    
451            if (displayName != null)
452            {
453                result.append(" displayName=\"" + displayName + "\"");
454            }
455            if (mapToObject != null)
456            {
457                result.append(" mapToObject=\"" + mapToObject + "\"");
458            }
459            if (mapToProperty != null)
460            {
461                result.append(" mapToProperty=\"" + mapToProperty + "\"");
462            }
463            if (validator != null)
464            {
465                result.append(" validator=\"" + validator + "\"");
466            }
467            if (defaultValue != null)
468            {
469                result.append(" defaultValue=\"" + defaultValue + "\"");
470            }
471    
472            if (emptyValue != null)
473            {
474                result.append(" emptyValue=\"" + emptyValue + "\"");
475            }
476    
477            if (rules.size() == 0)
478            {
479                result.append(" />\n");
480            }
481            else
482            {
483                result.append(">\n");
484                for (Iterator i = rules.iterator(); i.hasNext();)
485                {
486                    result.append(i.next());
487                }
488                result.append("</field>\n");
489            }
490    
491            return result.toString();
492        }
493    
494        // this methods are called during serialization
495        private void writeObject(ObjectOutputStream stream)
496                throws IOException
497        {
498            stream.defaultWriteObject();
499        }
500    
501        private void readObject(ObjectInputStream stream)
502                throws IOException, ClassNotFoundException
503        {
504            stream.defaultReadObject();
505        }
506    }