1 package org.apache.fulcrum.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.commons.lang3.StringUtils;
23 import org.apache.fulcrum.intake.IntakeException;
24 import org.apache.fulcrum.intake.validator.StringValidator;
25
26 /**
27 * Text field.
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$
33 */
34 public class StringField
35 extends Field<String>
36 {
37 /** Serial version */
38 private static final long serialVersionUID = -7218385325093690333L;
39
40 /**
41 * Constructor.
42 *
43 * @param field xml field definition object
44 * @param group xml group definition object
45 * @throws IntakeException thrown by superclass
46 */
47 public StringField(XmlField field, Group group)
48 throws IntakeException
49 {
50 super(field, group);
51 }
52
53 /**
54 * Produces the fully qualified class name of the default validator.
55 *
56 * @return class name of the default validator
57 */
58 @Override
59 protected String getDefaultValidator()
60 {
61 return StringValidator.class.getName();
62 }
63
64 /**
65 * Sets the default value for a String field
66 *
67 * @param prop Parameter for the default values
68 */
69 @Override
70 public void setDefaultValue(String prop)
71 {
72 defaultValue = prop;
73 }
74
75 /**
76 * Set the empty Value. This value is used if Intake
77 * maps a field to a parameter returned by the user and
78 * the corresponding field is either empty (empty string)
79 * or non-existant.
80 *
81 * @param prop The value to use if the field is empty.
82 */
83 @Override
84 public void setEmptyValue(String prop)
85 {
86 emptyValue = prop;
87 }
88
89 /**
90 * Sets the value of the field from data in the parser.
91 */
92 @Override
93 protected void doSetValue()
94 {
95 if (isMultiValued())
96 {
97 String[] ss = parser.getStrings(getKey());
98 String[] sval = new String[ss.length];
99 for (int i = 0; i < ss.length; i++)
100 {
101 sval[i] = StringUtils.isNotEmpty(ss[i]) ? ss[i] : (String) getEmptyValue();
102 }
103 setTestValue(sval);
104 }
105 else
106 {
107 String val = parser.getString(getKey());
108 setTestValue(StringUtils.isNotEmpty(val) ? val : (String) getEmptyValue());
109 }
110 }
111
112 /**
113 * Set the value of required.
114 *
115 * @param v Value to assign to required.
116 * @param message an error message
117 */
118 @Override
119 public void setRequired(boolean v, String message)
120 {
121 super.setRequired(v, message);
122 if (v)
123 {
124 if (isMultiValued())
125 {
126 String[] ss = (String[]) getTestValue();
127 if (ss == null || ss.length == 0)
128 {
129 setValid(false);
130 setMessage(message);
131 }
132 else
133 {
134 boolean set = false;
135 for (int i = 0; i < ss.length; i++)
136 {
137 set |= StringUtils.isNotEmpty(ss[i]);
138 if (set)
139 {
140 break;
141 }
142 }
143 if (!set)
144 {
145 setValid(false);
146 setMessage(message);
147 }
148 }
149 }
150 else
151 {
152 if (!isSet() || StringUtils.isEmpty((String)getTestValue()))
153 {
154 setValid(false);
155 setMessage(message);
156 }
157 }
158 }
159 }
160 }