View Javadoc

1   package org.apache.turbine.services.intake.model;
2   
3   /*
4    * Copyright 2001-2005 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.StringValidator;
23  import org.apache.turbine.services.intake.xmlmodel.XmlField;
24  
25  /***
26   * Text field.
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: StringField.java 264148 2005-08-29 14:21:04Z henning $
32   */
33  public class StringField
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 StringField(XmlField field, Group group)
45              throws IntakeException
46      {
47          super(field, group);
48      }
49  
50      /***
51       * Produces the fully qualified class name of the default validator.
52       *
53       * @return class name of the default validator
54       */
55      protected String getDefaultValidator()
56      {
57          return StringValidator.class.getName();
58      }
59  
60      /***
61       * Sets the default value for a String field
62       *
63       * @param prop Parameter for the default values
64       */
65      public void setDefaultValue(String prop)
66      {
67          defaultValue = prop;
68      }
69  
70      /***
71       * Set the empty Value. This value is used if Intake
72       * maps a field to a parameter returned by the user and
73       * the corresponding field is either empty (empty string)
74       * or non-existant.
75       *
76       * @param prop The value to use if the field is empty.
77       */
78      public void setEmptyValue(String prop)
79      {
80          emptyValue = prop;
81      }
82  
83      /***
84       * Sets the value of the field from data in the parser.
85       */
86      protected void doSetValue()
87      {
88          if (isMultiValued)
89          {
90              String[] ss = parser.getStrings(getKey());
91              String[] sval = new String[ss.length];
92              for (int i = 0; i < ss.length; i++)
93              {
94                  sval[i] = (StringUtils.isNotEmpty(ss[i])) ? ss[i] : (String) getEmptyValue();
95              }
96              setTestValue(sval);
97          }
98          else
99          {
100             String val = parser.getString(getKey());
101             setTestValue(StringUtils.isNotEmpty(val) ? val : (String) getEmptyValue());
102         }
103     }
104 
105     /***
106      * Set the value of required.
107      *
108      * @param v  Value to assign to required.
109      * @param message an error message
110      */
111     public void setRequired(boolean v, String message)
112     {
113         this.required = v;
114         if (v)
115         {
116             if (isMultiValued)
117             {
118                 String[] ss = (String[]) getTestValue();
119                 if (ss == null || ss.length == 0)
120                 {
121                     validFlag = false;
122                     this.message = message;
123                 }
124                 else
125                 {
126                     boolean set = false;
127                     for (int i = 0; i < ss.length; i++)
128                     {
129                         set |= StringUtils.isNotEmpty(ss[i]);
130                         if (set)
131                         {
132                             break;
133                         }
134                     }
135                     if (!set)
136                     {
137                         validFlag = false;
138                         this.message = message;
139                     }
140                 }
141             }
142             else
143             {
144                 if (!setFlag || StringUtils.isEmpty((String) getTestValue()))
145                 {
146                     validFlag = false;
147                     this.message = message;
148                 }
149             }
150         }
151     }
152 }