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 import org.apache.commons.lang.StringUtils;
20
21 import org.apache.turbine.services.intake.IntakeException;
22 import org.apache.turbine.services.intake.validator.IntegerValidator;
23 import org.apache.turbine.services.intake.xmlmodel.XmlField;
24
25 /***
26 * Processor for int fields.
27 *
28 * @author <a href="mailto:jmcnally@collab.net">John McNally</a>
29 * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
30 * @author <a href="mailto:quintonm@bellsouth.net">Quinton McCombs</a>
31 * @version $Id: IntegerField.java 264148 2005-08-29 14:21:04Z henning $
32 */
33 public class IntegerField
34 extends Field
35 {
36
37 /***
38 * Constructor.
39 *
40 * @param field xml field definition object
41 * @param group xml group definition object
42 * @throws IntakeException thrown by superclass
43 */
44 public IntegerField(XmlField field, Group group)
45 throws IntakeException
46 {
47 super(field, group);
48 }
49
50 /***
51 * Sets the default value for an Integer Field
52 *
53 * @param prop Parameter for the default values
54 */
55 public void setDefaultValue(String prop)
56 {
57 defaultValue = null;
58
59 if (prop == null)
60 {
61 return;
62 }
63
64 defaultValue = new Integer(prop);
65 }
66
67 /***
68 * Set the empty Value. This value is used if Intake
69 * maps a field to a parameter returned by the user and
70 * the corresponding field is either empty (empty string)
71 * or non-existant.
72 *
73 * @param prop The value to use if the field is empty.
74 */
75 public void setEmptyValue(String prop)
76 {
77 emptyValue = null;
78
79 if (prop == null)
80 {
81 return;
82 }
83
84 emptyValue = new Integer(prop);
85 }
86
87 /***
88 * Provides access to emptyValue such that the value returned will be
89 * acceptable as an argument parameter to Method.invoke. Subclasses
90 * that deal with primitive types should ensure that they return an
91 * appropriate value wrapped in the object wrapper class for the
92 * primitive type.
93 *
94 * @return the value to use when the field is empty or an Object that
95 * wraps the empty value for primitive types.
96 */
97 protected Object getSafeEmptyValue()
98 {
99 if (isMultiValued)
100 {
101 return new int[0];
102 }
103 else
104 {
105 return (null == getEmptyValue())
106 ? new Integer(0) : getEmptyValue();
107 }
108 }
109
110 /***
111 * A suitable validator.
112 *
113 * @return A suitable validator
114 */
115 protected String getDefaultValidator()
116 {
117 return IntegerValidator.class.getName();
118 }
119
120 /***
121 * Sets the value of the field from data in the parser.
122 */
123 protected void doSetValue()
124 {
125 if (isMultiValued)
126 {
127 String[] inputs = parser.getStrings(getKey());
128 int[] values = new int[inputs.length];
129 for (int i = 0; i < inputs.length; i++)
130 {
131 values[i] = StringUtils.isNotEmpty(inputs[i])
132 ? new Integer(inputs[i]).intValue()
133 : ((Integer) getEmptyValue()).intValue();
134 }
135 setTestValue(values);
136 }
137 else
138 {
139 String val = parser.getString(getKey());
140 setTestValue(StringUtils.isNotEmpty(val) ? new Integer(val) : (Integer) getEmptyValue());
141 }
142 }
143
144 }