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