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