%line | %branch | |||||||||
---|---|---|---|---|---|---|---|---|---|---|
org.apache.turbine.services.intake.xmlmodel.XmlField |
|
|
1 | package org.apache.turbine.services.intake.xmlmodel; |
|
2 | ||
3 | /* |
|
4 | * Copyright 2001-2004 The Apache Software Foundation. |
|
5 | * |
|
6 | * Licensed under the Apache License, Version 2.0 (the "License") |
|
7 | * you may not use this file except in compliance with the License. |
|
8 | * You may obtain a copy of the License at |
|
9 | * |
|
10 | * http://www.apache.org/licenses/LICENSE-2.0 |
|
11 | * |
|
12 | * Unless required by applicable law or agreed to in writing, software |
|
13 | * distributed under the License is distributed on an "AS IS" BASIS, |
|
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
15 | * See the License for the specific language governing permissions and |
|
16 | * limitations under the License. |
|
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 | 0 | { |
66 | 0 | rules = new ArrayList(); |
67 | 0 | ruleMap = new HashMap(); |
68 | 0 | } |
69 | ||
70 | /** |
|
71 | * Creates a new column and set the name |
|
72 | */ |
|
73 | public XmlField(String name) |
|
74 | 0 | { |
75 | 0 | this.name = name; |
76 | 0 | rules = new ArrayList(); |
77 | 0 | ruleMap = new HashMap(); |
78 | 0 | } |
79 | ||
80 | /** |
|
81 | * Imports a column from an XML specification |
|
82 | */ |
|
83 | public void loadFromXML(Attributes attrib) |
|
84 | { |
|
85 | 0 | setName(attrib.getValue("name")); |
86 | 0 | setKey(attrib.getValue("key")); |
87 | 0 | setType(attrib.getValue("type")); |
88 | 0 | setDisplayName(attrib.getValue("displayName")); |
89 | 0 | setDisplaySize(attrib.getValue("displaySize")); |
90 | 0 | setMultiValued(attrib.getValue("multiValued")); |
91 | ||
92 | 0 | String mapObj = attrib.getValue("mapToObject"); |
93 | 0 | if (mapObj != null && mapObj.length() != 0) |
94 | { |
|
95 | 0 | setMapToObject(mapObj); |
96 | } |
|
97 | ||
98 | 0 | setMapToProperty(attrib.getValue("mapToProperty")); |
99 | 0 | setValidator(attrib.getValue("validator")); |
100 | 0 | setDefaultValue(attrib.getValue("defaultValue")); |
101 | 0 | setEmptyValue(attrib.getValue("emptyValue")); |
102 | 0 | } |
103 | ||
104 | /** |
|
105 | * Get the name of the property |
|
106 | */ |
|
107 | public String getRawName() |
|
108 | { |
|
109 | 0 | return name; |
110 | } |
|
111 | ||
112 | /** |
|
113 | * Get the name of the property |
|
114 | */ |
|
115 | public String getName() |
|
116 | { |
|
117 | 0 | return StringUtils.replace(name, "_", ""); |
118 | } |
|
119 | ||
120 | /** |
|
121 | * Set the name of the property |
|
122 | */ |
|
123 | public void setName(String newName) |
|
124 | { |
|
125 | 0 | name = newName; |
126 | 0 | } |
127 | ||
128 | /** |
|
129 | * Get the display name of the property |
|
130 | */ |
|
131 | public String getDisplayName() |
|
132 | { |
|
133 | 0 | return displayName; |
134 | } |
|
135 | ||
136 | /** |
|
137 | * Set the display name of the property |
|
138 | */ |
|
139 | public void setDisplayName(String newDisplayName) |
|
140 | { |
|
141 | 0 | displayName = newDisplayName; |
142 | 0 | } |
143 | ||
144 | /** |
|
145 | * Sets the display size of the field. |
|
146 | */ |
|
147 | private void setDisplaySize(String size) |
|
148 | { |
|
149 | 0 | this.displaySize = size; |
150 | 0 | } |
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 | 0 | return this.displaySize; |
159 | } |
|
160 | ||
161 | /** |
|
162 | * Set the parameter key of the property |
|
163 | */ |
|
164 | public void setKey(String newKey) |
|
165 | { |
|
166 | 0 | key = newKey; |
167 | 0 | } |
168 | ||
169 | /** |
|
170 | * Get the parameter key of the property |
|
171 | */ |
|
172 | public String getKey() |
|
173 | { |
|
174 | 0 | return key; |
175 | } |
|
176 | ||
177 | /** |
|
178 | * Set the type of the property |
|
179 | */ |
|
180 | public void setType(String newType) |
|
181 | { |
|
182 | 0 | type = newType; |
183 | 0 | } |
184 | ||
185 | /** |
|
186 | * Get the type of the property |
|
187 | */ |
|
188 | public String getType() |
|
189 | { |
|
190 | 0 | return type; |
191 | } |
|
192 | ||
193 | /** |
|
194 | * Set whether this class can have multiple values |
|
195 | */ |
|
196 | public void setMultiValued(String newMultiValued) |
|
197 | { |
|
198 | 0 | multiValued = newMultiValued; |
199 | 0 | } |
200 | ||
201 | /** |
|
202 | * can this field have several values? |
|
203 | */ |
|
204 | public boolean isMultiValued() |
|
205 | { |
|
206 | 0 | if (multiValued != null && multiValued.equals("true")) |
207 | { |
|
208 | 0 | return true; |
209 | } |
|
210 | 0 | 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 | 0 | mapToObject = objectName; |
221 | 0 | } |
222 | ||
223 | /** |
|
224 | * Get the name of the object that takes this input |
|
225 | */ |
|
226 | public String getMapToObject() |
|
227 | { |
|
228 | 0 | 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 | 0 | mapToProperty = prop; |
239 | 0 | } |
240 | ||
241 | /** |
|
242 | * Get the property method that takes this input |
|
243 | */ |
|
244 | public String getMapToProperty() |
|
245 | { |
|
246 | 0 | if (mapToProperty == null) |
247 | { |
|
248 | 0 | return getName(); |
249 | } |
|
250 | else |
|
251 | { |
|
252 | 0 | return mapToProperty; |
253 | } |
|
254 | } |
|
255 | ||
256 | /** |
|
257 | * Set the class name of the validator |
|
258 | */ |
|
259 | public void setValidator(String prop) |
|
260 | { |
|
261 | 0 | validator = prop; |
262 | 0 | } |
263 | ||
264 | /** |
|
265 | * Get the className of the validator |
|
266 | */ |
|
267 | public String getValidator() |
|
268 | { |
|
269 | 0 | 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 | 0 | defaultValue = prop; |
280 | 0 | } |
281 | ||
282 | /** |
|
283 | * Get the default Value. |
|
284 | * |
|
285 | * @return The default value for this field. |
|
286 | */ |
|
287 | public String getDefaultValue() |
|
288 | { |
|
289 | 0 | 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 | 0 | emptyValue = prop; |
300 | 0 | } |
301 | ||
302 | /** |
|
303 | * Get the empty Value. |
|
304 | * |
|
305 | * @return The empty value for this field. |
|
306 | */ |
|
307 | public String getEmptyValue() |
|
308 | { |
|
309 | 0 | 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 | 0 | String firstChar = getName().substring(0, 1).toLowerCase(); |
321 | 0 | 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 | 0 | this.parent = parent; |
330 | 0 | if (mapToObject != null && mapToObject.length() != 0) |
331 | { |
|
332 | 0 | mapToObject = parent.getAppData().getBasePackage() + mapToObject; |
333 | } |
|
334 | 0 | } |
335 | ||
336 | /** |
|
337 | * Get the parent XmlGroup of the property |
|
338 | */ |
|
339 | public XmlGroup getGroup() |
|
340 | { |
|
341 | 0 | return parent; |
342 | } |
|
343 | ||
344 | /** |
|
345 | * Get the value of ifRequiredMessage. |
|
346 | * |
|
347 | * @return value of ifRequiredMessage. |
|
348 | */ |
|
349 | public String getIfRequiredMessage() |
|
350 | { |
|
351 | 0 | 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 | 0 | this.ifRequiredMessage = v; |
362 | 0 | } |
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 | 0 | Rule rule = new Rule(); |
371 | 0 | rule.loadFromXML(attrib); |
372 | 0 | addRule(rule); |
373 | ||
374 | 0 | 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 | 0 | rule.setField(this); |
384 | 0 | rules.add(rule); |
385 | 0 | ruleMap.put(rule.getName(), rule); |
386 | 0 | } |
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 | 0 | 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 | 0 | return ruleMap; |
407 | } |
|
408 | ||
409 | /** |
|
410 | * String representation of the column. This |
|
411 | * is an xml representation. |
|
412 | */ |
|
413 | public String toString() |
|
414 | { |
|
415 | 0 | StringBuffer result = new StringBuffer(); |
416 | 0 | result.append(" <field name=\"" + name + "\""); |
417 | 0 | result.append(" key=\"" + key + "\""); |
418 | 0 | result.append(" type=\"" + type + "\""); |
419 | ||
420 | 0 | if (displayName != null) |
421 | { |
|
422 | 0 | result.append(" displayName=\"" + displayName + "\""); |
423 | } |
|
424 | 0 | if (mapToObject != null) |
425 | { |
|
426 | 0 | result.append(" mapToObject=\"" + mapToObject + "\""); |
427 | } |
|
428 | 0 | if (mapToProperty != null) |
429 | { |
|
430 | 0 | result.append(" mapToProperty=\"" + mapToProperty + "\""); |
431 | } |
|
432 | 0 | if (validator != null) |
433 | { |
|
434 | 0 | result.append(" validator=\"" + validator + "\""); |
435 | } |
|
436 | 0 | if (defaultValue != null) |
437 | { |
|
438 | 0 | result.append(" defaultValue=\"" + defaultValue + "\""); |
439 | } |
|
440 | ||
441 | 0 | if (emptyValue != null) |
442 | { |
|
443 | 0 | result.append(" emptyValue=\"" + emptyValue + "\""); |
444 | } |
|
445 | ||
446 | 0 | if (rules.size() == 0) |
447 | { |
|
448 | 0 | result.append(" />\n"); |
449 | } |
|
450 | else |
|
451 | { |
|
452 | 0 | result.append(">\n"); |
453 | 0 | for (Iterator i = rules.iterator(); i.hasNext();) |
454 | { |
|
455 | 0 | result.append(i.next()); |
456 | } |
|
457 | 0 | result.append("</field>\n"); |
458 | } |
|
459 | ||
460 | 0 | return result.toString(); |
461 | } |
|
462 | ||
463 | // this methods are called during serialization |
|
464 | private void writeObject(ObjectOutputStream stream) |
|
465 | throws IOException |
|
466 | { |
|
467 | 0 | stream.defaultWriteObject(); |
468 | 0 | } |
469 | ||
470 | private void readObject(ObjectInputStream stream) |
|
471 | throws IOException, ClassNotFoundException |
|
472 | { |
|
473 | 0 | stream.defaultReadObject(); |
474 | 0 | } |
475 | } |
This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |