View Javadoc

1   package org.apache.turbine.services.intake.model;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.turbine.services.intake.IntakeException;
23  import org.apache.turbine.services.intake.validator.ShortValidator;
24  import org.apache.turbine.services.intake.xmlmodel.XmlField;
25  
26  /***
27   * Processor for short fields.
28   *
29   * @author <a href="mailto:jmcnally@collab.net>John McNally</a>
30   * @author <a href="mailto:Colin.Chalmers@maxware.nl">Colin Chalmers</a>
31   * @version $Id: ShortField.java 646752 2008-04-10 10:56:15Z tv $
32   */
33  public class ShortField
34          extends Field
35  {
36      /***
37       * Constructor.
38       *
39       * @param field xml field definition object
40       * @param group xml group definition object
41       * @throws IntakeException thrown by superclass
42       */
43      public ShortField(XmlField field, Group group)
44              throws IntakeException
45      {
46          super(field, group);
47      }
48  
49      /***
50       * Sets the default value for an Short Field
51       *
52       * @param prop Parameter for the default values
53       */
54      public void setDefaultValue(String prop)
55      {
56          defaultValue = null;
57  
58          if (prop == null)
59          {
60              return;
61          }
62  
63          defaultValue = new Short(prop);
64      }
65  
66      /***
67       * Set the empty Value. This value is used if Intake
68       * maps a field to a parameter returned by the user and
69       * the corresponding field is either empty (empty string)
70       * or non-existant.
71       *
72       * @param prop The value to use if the field is empty.
73       */
74      public void setEmptyValue(String prop)
75      {
76          emptyValue = null;
77  
78          if (prop == null)
79          {
80              return;
81          }
82  
83          emptyValue = new Short(prop);
84      }
85  
86      /***
87       * Provides access to emptyValue such that the value returned will be
88       * acceptable as an argument parameter to Method.invoke.  Subclasses
89       * that deal with primitive types should ensure that they return an
90       * appropriate value wrapped in the object wrapper class for the
91       * primitive type.
92       *
93       * @return the value to use when the field is empty or an Object that
94       * wraps the empty value for primitive types.
95       */
96      protected Object getSafeEmptyValue()
97      {
98          if (isMultiValued)
99          {
100             return new short[0];
101         }
102         else
103         {
104             return (null == getEmptyValue())
105                     ? new Short((short) 0) : getEmptyValue();
106         }
107     }
108 
109     /***
110      * A suitable validator.
111      *
112      * @return A suitable validator
113      */
114     protected String getDefaultValidator()
115     {
116         return ShortValidator.class.getName();
117     }
118 
119     /***
120      * Sets the value of the field from data in the parser.
121      */
122     protected void doSetValue()
123     {
124         if (isMultiValued)
125         {
126             Integer[] inputs = parser.getIntObjects(getKey());
127             short[] values = new short[inputs.length];
128 
129             for (int i = 0; i < inputs.length; i++)
130             {
131                 values[i] = inputs[i] == null 
132                         ? ((Short) getEmptyValue()).shortValue() 
133                         : inputs[i].shortValue();
134             }
135 
136             setTestValue(values);
137         }
138         else
139         {
140             Integer value = parser.getIntObject(getKey());
141             
142             if (value == null)
143             {
144                 setTestValue((Short)getEmptyValue());
145             }
146             else
147             {
148                 setTestValue(new Short(value.shortValue()));
149             }
150         }
151     }
152 }