1 package org.apache.fulcrum.intake.model;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.IOException;
23 import java.io.ObjectInputStream;
24 import java.io.ObjectOutputStream;
25 import java.io.Serializable;
26 import java.util.ArrayList;
27 import java.util.HashMap;
28 import java.util.List;
29 import java.util.Map;
30
31 import javax.xml.bind.Unmarshaller;
32 import javax.xml.bind.annotation.XmlAccessType;
33 import javax.xml.bind.annotation.XmlAccessorType;
34 import javax.xml.bind.annotation.XmlAttribute;
35 import javax.xml.bind.annotation.XmlElement;
36 import javax.xml.bind.annotation.XmlType;
37
38 import org.apache.avalon.framework.logger.LogEnabled;
39 import org.apache.avalon.framework.logger.Logger;
40 import org.apache.commons.lang3.StringUtils;
41
42
43
44
45
46
47
48
49
50
51 @XmlType(name="field")
52 @XmlAccessorType(XmlAccessType.NONE)
53 public class XmlField
54 implements Serializable, LogEnabled
55 {
56
57
58
59 private static final long serialVersionUID = -734309157828058007L;
60
61 @XmlAttribute(required=true)
62 private String key;
63
64 @XmlAttribute(required=true)
65 private String name;
66
67 @XmlAttribute
68 private String displayName;
69
70 @XmlAttribute
71 private String displaySize;
72
73 @XmlAttribute
74 private FieldType type = FieldType.FIELD_STRING;
75
76 @XmlAttribute
77 private boolean multiValued = false;
78
79 @XmlAttribute
80 private String fieldClass;
81
82 @XmlAttribute
83 private String mapToObject;
84
85 @XmlAttribute
86 private String mapToProperty;
87
88 @XmlAttribute
89 private String validator;
90
91 @XmlAttribute
92 private String defaultValue;
93
94 @XmlAttribute
95 private String emptyValue;
96
97 private List<Rule> rules;
98 private Map<String, Rule> ruleMap;
99
100 private Group parent;
101
102 private Logger log;
103
104
105
106
107 public XmlField()
108 {
109 rules = new ArrayList<Rule>();
110 ruleMap = new HashMap<String, Rule>();
111 }
112
113
114
115
116 @Override
117 public void enableLogging(Logger logger)
118 {
119 this.log = logger;
120 }
121
122
123
124
125
126
127 public Logger getLogger()
128 {
129 return log;
130 }
131
132
133
134
135
136
137 public String getRawName()
138 {
139 return name;
140 }
141
142
143
144
145
146
147 public String getName()
148 {
149 return StringUtils.replace(name, "_", "");
150 }
151
152
153
154
155
156
157 public String getDisplayName()
158 {
159 return displayName;
160 }
161
162
163
164
165
166
167
168 public String getDisplaySize()
169 {
170 return this.displaySize;
171 }
172
173
174
175
176
177
178 public String getKey()
179 {
180 return key;
181 }
182
183
184
185
186
187
188 public FieldType getType()
189 {
190 return type;
191 }
192
193
194
195
196
197
198 public boolean isMultiValued()
199 {
200 return multiValued;
201 }
202
203
204
205
206
207
208 public String getMapToObject()
209 {
210 return mapToObject;
211 }
212
213
214
215
216
217
218 public String getMapToProperty()
219 {
220 if (mapToProperty == null)
221 {
222 return getName();
223 }
224 else
225 {
226 return mapToProperty;
227 }
228 }
229
230
231
232
233
234
235 public String getValidator()
236 {
237 return validator;
238 }
239
240
241
242
243
244
245 public String getDefaultValue()
246 {
247 return defaultValue;
248 }
249
250
251
252
253
254
255 public String getEmptyValue()
256 {
257 return emptyValue;
258 }
259
260
261
262
263
264
265 public Group getGroup()
266 {
267 return this.parent;
268 }
269
270
271
272
273
274
275 public String getFieldClass()
276 {
277 return fieldClass;
278 }
279
280
281
282
283
284
285 public List<Rule> getRules()
286 {
287 return rules;
288 }
289
290
291
292
293
294
295 @XmlElement(name="rule")
296 public void setRules(List<Rule> rules)
297 {
298 this.rules = rules;
299 }
300
301
302
303
304
305
306
307 public Map<String, Rule> getRuleMap()
308 {
309 return ruleMap;
310 }
311
312
313
314
315
316
317
318 public void afterUnmarshal(Unmarshaller um, Object parent)
319 {
320 this.parent = (Group)parent;
321
322
323 this.ruleMap.clear();
324 for (Rule rule : rules)
325 {
326 ruleMap.put(rule.getName(), rule);
327 }
328
329
330 if (mapToObject == null &&
331 mapToProperty != null &&
332 StringUtils.isNotEmpty(mapToProperty) &&
333 this.parent.getDefaultMapToObject() != null)
334 {
335 mapToObject = this.parent.getDefaultMapToObject();
336 }
337 }
338
339
340
341
342
343
344
345 @Override
346 public String toString()
347 {
348 StringBuilder result = new StringBuilder();
349 result.append(" <field name=\"").append(name).append("\"")
350 .append(" key=\"").append(key).append("\"")
351 .append(" type=\"").append(type.value()).append("\"");
352
353 if (displayName != null)
354 {
355 result.append(" displayName=\"").append(displayName).append("\"");
356 }
357 if (mapToObject != null)
358 {
359 result.append(" mapToObject=\"").append(mapToObject).append("\"");
360 }
361 if (mapToProperty != null)
362 {
363 result.append(" mapToProperty=\"").append(mapToProperty).append("\"");
364 }
365 if (validator != null)
366 {
367 result.append(" validator=\"").append(validator).append("\"");
368 }
369 if (defaultValue != null)
370 {
371 result.append(" defaultValue=\"").append(defaultValue).append("\"");
372 }
373
374 if (emptyValue != null)
375 {
376 result.append(" emptyValue=\"").append(emptyValue).append("\"");
377 }
378
379 if (rules.size() == 0)
380 {
381 result.append(" />\n");
382 }
383 else
384 {
385 result.append(">\n");
386 for (Rule rule : rules)
387 {
388 result.append(rule);
389 }
390 result.append("</field>\n");
391 }
392
393 return result.toString();
394 }
395
396
397 private void writeObject(ObjectOutputStream stream)
398 throws IOException
399 {
400 stream.defaultWriteObject();
401 }
402
403 private void readObject(ObjectInputStream stream)
404 throws IOException, ClassNotFoundException
405 {
406 stream.defaultReadObject();
407 }
408 }