KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > turbine > services > intake > model > IntegerField


1 package org.apache.turbine.services.intake.model;
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 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,v 1.10.2.6 2004/05/20 03:16:39 seade Exp $
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 JavaDoc prop)
56     {
57         defaultValue = null;
58
59         if (prop == null)
60         {
61             return;
62         }
63
64         defaultValue = new Integer JavaDoc(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 JavaDoc prop)
76     {
77         emptyValue = null;
78
79         if (prop == null)
80         {
81             return;
82         }
83
84         emptyValue = new Integer JavaDoc(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 JavaDoc getSafeEmptyValue()
98     {
99         if (isMultiValued)
100         {
101             return new int[0];
102         }
103         else
104         {
105             return (null == getEmptyValue())
106                     ? new Integer JavaDoc(0) : getEmptyValue();
107         }
108     }
109
110     /**
111      * A suitable validator.
112      *
113      * @return A suitable validator
114      */

115     protected String JavaDoc 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 JavaDoc[] 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 JavaDoc(inputs[i]).intValue()
133                         : ((Integer JavaDoc) getEmptyValue()).intValue();
134             }
135             setTestValue(values);
136         }
137         else
138         {
139             String JavaDoc val = parser.getString(getKey());
140             setTestValue(StringUtils.isNotEmpty(val) ? new Integer JavaDoc(val) : (Integer JavaDoc) getEmptyValue());
141         }
142     }
143     
144 }
145
Popular Tags