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