1 package org.apache.turbine.services.intake.model;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.commons.lang.StringUtils;
23
24 import org.apache.turbine.services.intake.IntakeException;
25 import org.apache.turbine.services.intake.validator.StringValidator;
26 import org.apache.turbine.services.intake.xmlmodel.XmlField;
27
28 /***
29 * Text field.
30 *
31 * @author <a href="mailto:jmcnally@collab.net">John McNally</a>
32 * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
33 * @author <a href="mailto:quintonm@bellsouth.net">Quinton McCombs</a>
34 * @version $Id: StringField.java 534527 2007-05-02 16:10:59Z tv $
35 */
36 public class StringField
37 extends Field
38 {
39
40 /***
41 * Constructor.
42 *
43 * @param field xml field definition object
44 * @param group xml group definition object
45 * @throws IntakeException thrown by superclass
46 */
47 public StringField(XmlField field, Group group)
48 throws IntakeException
49 {
50 super(field, group);
51 }
52
53 /***
54 * Produces the fully qualified class name of the default validator.
55 *
56 * @return class name of the default validator
57 */
58 protected String getDefaultValidator()
59 {
60 return StringValidator.class.getName();
61 }
62
63 /***
64 * Sets the default value for a String field
65 *
66 * @param prop Parameter for the default values
67 */
68 public void setDefaultValue(String prop)
69 {
70 defaultValue = prop;
71 }
72
73 /***
74 * Set the empty Value. This value is used if Intake
75 * maps a field to a parameter returned by the user and
76 * the corresponding field is either empty (empty string)
77 * or non-existant.
78 *
79 * @param prop The value to use if the field is empty.
80 */
81 public void setEmptyValue(String prop)
82 {
83 emptyValue = prop;
84 }
85
86 /***
87 * Sets the value of the field from data in the parser.
88 */
89 protected void doSetValue()
90 {
91 if (isMultiValued)
92 {
93 String[] ss = parser.getStrings(getKey());
94 String[] sval = new String[ss.length];
95 for (int i = 0; i < ss.length; i++)
96 {
97 sval[i] = (StringUtils.isNotEmpty(ss[i])) ? ss[i] : (String) getEmptyValue();
98 }
99 setTestValue(sval);
100 }
101 else
102 {
103 String val = parser.getString(getKey());
104 setTestValue(StringUtils.isNotEmpty(val) ? val : (String) getEmptyValue());
105 }
106 }
107
108 /***
109 * Set the value of required.
110 *
111 * @param v Value to assign to required.
112 * @param message an error message
113 */
114 public void setRequired(boolean v, String message)
115 {
116 this.required = v;
117 if (v)
118 {
119 if (isMultiValued)
120 {
121 String[] ss = (String[]) getTestValue();
122 if (ss == null || ss.length == 0)
123 {
124 validFlag = false;
125 this.message = message;
126 }
127 else
128 {
129 boolean set = false;
130 for (int i = 0; i < ss.length; i++)
131 {
132 set |= StringUtils.isNotEmpty(ss[i]);
133 if (set)
134 {
135 break;
136 }
137 }
138 if (!set)
139 {
140 validFlag = false;
141 this.message = message;
142 }
143 }
144 }
145 else
146 {
147 if (!setFlag || StringUtils.isEmpty((String) getTestValue()))
148 {
149 validFlag = false;
150 this.message = message;
151 }
152 }
153 }
154 }
155 }