001: package org.apache.turbine.services.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 534527 2007-05-02 16:10:59Z tv $
045: */
046: public class XmlField implements Serializable {
047: /** Serial Version UID */
048: private static final long serialVersionUID = 6403078189106766569L;
049:
050: private String name;
051: private String key;
052: private String type;
053: private String displayName;
054: private String multiValued;
055: private XmlGroup parent;
056: private List rules;
057: private Map ruleMap;
058: private String ifRequiredMessage;
059: private String mapToObject;
060: private String mapToProperty;
061: private String validator;
062: private String defaultValue;
063: private String emptyValue;
064: private String displaySize;
065: private String fieldClass;
066:
067: /**
068: * Default Constructor
069: */
070: public XmlField() {
071: rules = new ArrayList();
072: ruleMap = new HashMap();
073: }
074:
075: /**
076: * Creates a new column and set the name
077: */
078: public XmlField(String name) {
079: this .name = name;
080: rules = new ArrayList();
081: ruleMap = new HashMap();
082: }
083:
084: /**
085: * Imports a column from an XML specification
086: */
087: public void loadFromXML(Attributes attrib) {
088: setName(attrib.getValue("name"));
089: setKey(attrib.getValue("key"));
090: setType(attrib.getValue("type"));
091: setDisplayName(attrib.getValue("displayName"));
092: setDisplaySize(attrib.getValue("displaySize"));
093: setMultiValued(attrib.getValue("multiValued"));
094:
095: String mapObj = attrib.getValue("mapToObject");
096: if (mapObj != null && mapObj.length() != 0) {
097: setMapToObject(mapObj);
098: }
099:
100: setMapToProperty(attrib.getValue("mapToProperty"));
101: setFieldClass(attrib.getValue("fieldClass"));
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: return name;
112: }
113:
114: /**
115: * Get the name of the property
116: */
117: public String getName() {
118: return StringUtils.replace(name, "_", "");
119: }
120:
121: /**
122: * Set the name of the property
123: */
124: public void setName(String newName) {
125: name = newName;
126: }
127:
128: /**
129: * Get the display name of the property
130: */
131: public String getDisplayName() {
132: return displayName;
133: }
134:
135: /**
136: * Set the display name of the property
137: */
138: public void setDisplayName(String newDisplayName) {
139: displayName = newDisplayName;
140: }
141:
142: /**
143: * Sets the display size of the field.
144: */
145: private void setDisplaySize(String size) {
146: this .displaySize = size;
147: }
148:
149: /**
150: * Gets the display size of the field. This is
151: * useful for constructing the HTML input tag.
152: */
153: public String getDisplaySize() {
154: return this .displaySize;
155: }
156:
157: /**
158: * Set the parameter key of the property
159: */
160: public void setKey(String newKey) {
161: key = newKey;
162: }
163:
164: /**
165: * Get the parameter key of the property
166: */
167: public String getKey() {
168: return key;
169: }
170:
171: /**
172: * Set the type of the property
173: */
174: public void setType(String newType) {
175: type = newType;
176: }
177:
178: /**
179: * Get the type of the property
180: */
181: public String getType() {
182: return type;
183: }
184:
185: /**
186: * Set whether this class can have multiple values
187: */
188: public void setMultiValued(String newMultiValued) {
189: multiValued = newMultiValued;
190: }
191:
192: /**
193: * can this field have several values?
194: */
195: public boolean isMultiValued() {
196: if (multiValued != null && multiValued.equals("true")) {
197: return true;
198: }
199: return false;
200: }
201:
202: /**
203: * Set the name of the object that takes this input
204: *
205: * @param objectName name of the class.
206: */
207: public void setMapToObject(String objectName) {
208: mapToObject = objectName;
209: }
210:
211: /**
212: * Get the name of the object that takes this input
213: */
214: public String getMapToObject() {
215: return mapToObject;
216: }
217:
218: /**
219: * Set the property method that takes this input
220: *
221: * @param prop Name of the property to which the field will be mapped.
222: */
223: public void setMapToProperty(String prop) {
224: mapToProperty = prop;
225: }
226:
227: /**
228: * Get the property method that takes this input
229: */
230: public String getMapToProperty() {
231: if (mapToProperty == null) {
232: return getName();
233: } else {
234: return mapToProperty;
235: }
236: }
237:
238: /**
239: * Set the class name of the validator
240: */
241: public void setValidator(String prop) {
242: validator = prop;
243: }
244:
245: /**
246: * Get the className of the validator
247: */
248: public String getValidator() {
249: return validator;
250: }
251:
252: /**
253: * Set the default Value.
254: *
255: * @param prop The parameter to use as default value.
256: */
257: public void setDefaultValue(String prop) {
258: defaultValue = prop;
259: }
260:
261: /**
262: * Get the default Value.
263: *
264: * @return The default value for this field.
265: */
266: public String getDefaultValue() {
267: return defaultValue;
268: }
269:
270: /**
271: * Set the empty Value.
272: *
273: * @param prop The parameter to use as empty value.
274: */
275: public void setEmptyValue(String prop) {
276: emptyValue = prop;
277: }
278:
279: /**
280: * Get the empty Value.
281: *
282: * @return The empty value for this field.
283: */
284: public String getEmptyValue() {
285: return emptyValue;
286: }
287:
288: /**
289: * The name of the field making sure the first letter is lowercase.
290: *
291: * @return a <code>String</code> value
292: * @deprecated No replacement
293: */
294: public String getVariable() {
295: String firstChar = getName().substring(0, 1).toLowerCase();
296: return firstChar + getName().substring(1);
297: }
298:
299: /**
300: * Set the parent XmlGroup of the property
301: */
302: public void setGroup(XmlGroup parent) {
303: this .parent = parent;
304: if (mapToObject != null && mapToObject.length() != 0) {
305: mapToObject = parent.getAppData().getBasePackage()
306: + mapToObject;
307: }
308: }
309:
310: /**
311: * Get the parent XmlGroup of the property
312: */
313: public XmlGroup getGroup() {
314: return parent;
315: }
316:
317: /**
318: * Get the value of ifRequiredMessage.
319: *
320: * @return value of ifRequiredMessage.
321: */
322: public String getIfRequiredMessage() {
323: return ifRequiredMessage;
324: }
325:
326: /**
327: * Set the value of ifRequiredMessage.
328: *
329: * @param v Value to assign to ifRequiredMessage.
330: */
331: public void setIfRequiredMessage(String v) {
332: this .ifRequiredMessage = v;
333: }
334:
335: /**
336: * Get the value of fieldClass.
337: *
338: * @return value of fieldClass.
339: */
340: public String getFieldClass() {
341: return fieldClass;
342: }
343:
344: /**
345: * Set the value of fieldClass.
346: *
347: * @param v Value to assign to fieldClass.
348: */
349: public void setFieldClass(String v) {
350: this .fieldClass = v;
351: }
352:
353: /**
354: * A utility function to create a new input parameter
355: * from attrib and add it to this property.
356: */
357: public Rule addRule(Attributes attrib) {
358: Rule rule = new Rule();
359: rule.loadFromXML(attrib);
360: addRule(rule);
361:
362: return rule;
363: }
364:
365: /**
366: * Adds a new rule to the parameter Map and set the
367: * parent property of the Rule to this property
368: */
369: public void addRule(Rule rule) {
370: rule.setField(this );
371: rules.add(rule);
372: ruleMap.put(rule.getName(), rule);
373: }
374:
375: /**
376: * The collection of rules for this field.
377: *
378: * @return a <code>List</code> value
379: */
380: public List getRules() {
381: return rules;
382: }
383:
384: /**
385: * The collection of rules for this field keyed by
386: * parameter name.
387: *
388: * @return a <code>Map</code> value
389: */
390: public Map getRuleMap() {
391: return ruleMap;
392: }
393:
394: /**
395: * String representation of the column. This
396: * is an xml representation.
397: */
398: public String toString() {
399: StringBuffer result = new StringBuffer();
400: result.append(" <field name=\"" + name + "\"");
401: result.append(" key=\"" + key + "\"");
402: result.append(" type=\"" + type + "\"");
403:
404: if (displayName != null) {
405: result.append(" displayName=\"" + displayName + "\"");
406: }
407: if (mapToObject != null) {
408: result.append(" mapToObject=\"" + mapToObject + "\"");
409: }
410: if (mapToProperty != null) {
411: result.append(" mapToProperty=\"" + mapToProperty + "\"");
412: }
413: if (validator != null) {
414: result.append(" validator=\"" + validator + "\"");
415: }
416: if (defaultValue != null) {
417: result.append(" defaultValue=\"" + defaultValue + "\"");
418: }
419:
420: if (emptyValue != null) {
421: result.append(" emptyValue=\"" + emptyValue + "\"");
422: }
423:
424: if (rules.size() == 0) {
425: result.append(" />\n");
426: } else {
427: result.append(">\n");
428: for (Iterator i = rules.iterator(); i.hasNext();) {
429: result.append(i.next());
430: }
431: result.append("</field>\n");
432: }
433:
434: return result.toString();
435: }
436:
437: // this methods are called during serialization
438: private void writeObject(ObjectOutputStream stream)
439: throws IOException {
440: stream.defaultWriteObject();
441: }
442:
443: private void readObject(ObjectInputStream stream)
444: throws IOException, ClassNotFoundException {
445: stream.defaultReadObject();
446: }
447: }
|