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.LongValidator;
24  import org.apache.turbine.services.intake.xmlmodel.XmlField;
25  
26  /***
27   * Processor for long 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: LongField.java 646752 2008-04-10 10:56:15Z tv $
32   */
33  public class LongField
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 LongField(XmlField field, Group group)
45              throws IntakeException
46      {
47          super(field, group);
48      }
49  
50      /***
51       * Sets the default value for an Long 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 Long(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 Long(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 long[0];
102         }
103         else
104         {
105             return (null == getEmptyValue()) ? new Long(0l) : getEmptyValue();
106         }
107     }
108 
109     /***
110      * A suitable validator.
111      *
112      * @return A suitable validator
113      */
114     protected String getDefaultValidator()
115     {
116         return LongValidator.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             Long[] inputs = parser.getLongObjects(getKey());
127             long[] values = new long[inputs.length];
128 
129             for (int i = 0; i < inputs.length; i++)
130             {
131                 values[i] = inputs[i] == null 
132                         ? ((Long) getEmptyValue()).longValue() 
133                         : inputs[i].longValue();
134             }
135 
136             setTestValue(values);
137         }
138         else
139         {
140             setTestValue(parser.getLongObject(getKey(), (Long)getEmptyValue()));
141         }
142     }
143 }