Mercurial > hg > digilib-old
comparison common/src/main/java/digilib/util/Parameter.java @ 903:7779b37d1d05
refactored into maven modules per servlet type.
can build servlet-api 2.3 and 3.0 via profile now!
author | robcast |
---|---|
date | Tue, 26 Apr 2011 20:24:31 +0200 |
parents | servlet/src/main/java/digilib/util/Parameter.java@ba1eb2d821a2 |
children |
comparison
equal
deleted
inserted
replaced
902:89ba3ffcf552 | 903:7779b37d1d05 |
---|---|
1 /* Parameter -- General digilib parameter class. | |
2 | |
3 Digital Image Library servlet components | |
4 | |
5 Copyright (C) 2003 Robert Casties (robcast@mail.berlios.de) | |
6 | |
7 This program is free software; you can redistribute it and/or modify it | |
8 under the terms of the GNU General Public License as published by the | |
9 Free Software Foundation; either version 2 of the License, or (at your | |
10 option) any later version. | |
11 | |
12 Please read license.txt for the full details. A copy of the GPL | |
13 may be found at http://www.gnu.org/copyleft/lgpl.html | |
14 | |
15 You should have received a copy of the GNU General Public License | |
16 along with this program; if not, write to the Free Software | |
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
18 | |
19 * | |
20 * Created on 02.09.2003 by casties | |
21 * | |
22 */ | |
23 package digilib.util; | |
24 | |
25 import java.io.File; | |
26 import java.io.IOException; | |
27 | |
28 | |
29 /** | |
30 * General digilib parameter class. | |
31 * | |
32 * @author casties | |
33 * | |
34 */ | |
35 public class Parameter { | |
36 /** real value */ | |
37 protected Object value = null; | |
38 | |
39 /** default value */ | |
40 protected Object defval = null; | |
41 | |
42 /** parameter name (e.g. in config file) */ | |
43 protected String name = null; | |
44 | |
45 /** parameter type */ | |
46 protected int type = 0; | |
47 | |
48 /** | |
49 * Default constructor. | |
50 * | |
51 */ | |
52 public Parameter() { | |
53 super(); | |
54 } | |
55 | |
56 /** | |
57 * Constructor with name, default, and value. | |
58 * | |
59 * @param value | |
60 * @param defval | |
61 */ | |
62 public Parameter(String name, Object defval, Object value) { | |
63 this.name = name; | |
64 this.value = value; | |
65 this.defval = defval; | |
66 } | |
67 | |
68 /** | |
69 * Constructor with name, default, value, and type. | |
70 * | |
71 * @param value | |
72 * @param defval | |
73 */ | |
74 public Parameter(String name, Object defval, Object value, int type) { | |
75 this.name = name; | |
76 this.value = value; | |
77 this.defval = defval; | |
78 this.type = type; | |
79 } | |
80 | |
81 /** | |
82 * Is the value valid. | |
83 * | |
84 * @return | |
85 */ | |
86 public boolean hasValue() { | |
87 return (value != null); | |
88 } | |
89 | |
90 /** | |
91 * Try to set the value from a String. | |
92 * | |
93 * Tries to convert the String to the same type as the default value. Sets | |
94 * the value anyway if the default is null. Returns if the value could be | |
95 * set. | |
96 * | |
97 * @param val | |
98 * @return | |
99 */ | |
100 public boolean setValueFromString(String val) { | |
101 if (val == null) { | |
102 val = ""; | |
103 } | |
104 // no default matches all | |
105 if (defval == null) { | |
106 this.value = val; | |
107 return true; | |
108 } | |
109 Class<? extends Object> c = defval.getClass(); | |
110 // take String as is | |
111 if (c == String.class) { | |
112 this.value = val; | |
113 return true; | |
114 } | |
115 // set File | |
116 if (c == File.class) { | |
117 this.value = new File(val); | |
118 return true; | |
119 } | |
120 // set Options | |
121 if (c == OptionsSet.class) { | |
122 this.value = new OptionsSet(val); | |
123 return true; | |
124 } | |
125 // set Boolean if string == "true" | |
126 if (c == Boolean.class) { | |
127 this.value = new Boolean(val.compareToIgnoreCase("true") == 0); | |
128 return true; | |
129 } | |
130 try { | |
131 // set Integer | |
132 if (c == Integer.class) { | |
133 this.value = new Integer(Integer.parseInt(val)); | |
134 return true; | |
135 } | |
136 // set Float | |
137 if (c == Float.class) { | |
138 this.value = new Float(Float.parseFloat(val)); | |
139 return true; | |
140 } | |
141 } catch (NumberFormatException e) { | |
142 } | |
143 // then it's unknown | |
144 return false; | |
145 } | |
146 | |
147 /** | |
148 * Get the default as Object. | |
149 * | |
150 * @return | |
151 */ | |
152 public Object getDefault() { | |
153 return defval; | |
154 } | |
155 | |
156 /** | |
157 * Set the default. | |
158 * | |
159 * @param defval | |
160 */ | |
161 public void setDefault(Object defval) { | |
162 this.defval = defval; | |
163 } | |
164 | |
165 /** | |
166 * Get the value as Object. | |
167 * | |
168 * Returns the default if the value is not set. | |
169 * | |
170 * @return | |
171 */ | |
172 public Object getValue() { | |
173 return (value != null) ? value : defval; | |
174 } | |
175 | |
176 public int getAsInt() { | |
177 Integer i = (Integer) getValue(); | |
178 return (i != null) ? i.intValue() : 0; | |
179 } | |
180 | |
181 public float getAsFloat() { | |
182 Float f = (Float) getValue(); | |
183 return (f != null) ? f.floatValue() : 0f; | |
184 } | |
185 | |
186 public String getAsString() { | |
187 Object s = getValue(); | |
188 if (s == null) { | |
189 return ""; | |
190 } | |
191 if (s.getClass() == File.class) { | |
192 try { | |
193 return ((File) s).getCanonicalPath(); | |
194 } catch (IOException e) { | |
195 return "ERR: " + s.toString(); | |
196 } | |
197 } | |
198 return s.toString(); | |
199 } | |
200 | |
201 public boolean getAsBoolean() { | |
202 Boolean b = (Boolean) getValue(); | |
203 return (b != null) ? b.booleanValue() : false; | |
204 } | |
205 | |
206 public String[] parseAsArray(String separator) { | |
207 String s = getAsString(); | |
208 String[] sa = s.split(separator); | |
209 return sa; | |
210 } | |
211 | |
212 public float[] parseAsFloatArray(String separator) { | |
213 String s = getAsString(); | |
214 String[] sa = s.split(separator); | |
215 float[] fa = null; | |
216 try { | |
217 int n = sa.length; | |
218 fa = new float[n]; | |
219 for (int i = 0; i < n; i++) { | |
220 float f = Float.parseFloat(sa[i]); | |
221 fa[i] = f; | |
222 } | |
223 } catch (Exception e) { | |
224 } | |
225 | |
226 return fa; | |
227 } | |
228 | |
229 /** | |
230 * Set the value. | |
231 * | |
232 * @param value | |
233 */ | |
234 public void setValue(Object value) { | |
235 this.value = value; | |
236 } | |
237 | |
238 /** | |
239 * Set the value. | |
240 * | |
241 * @param value | |
242 */ | |
243 public void setValue(int value) { | |
244 this.value = new Integer(value); | |
245 } | |
246 | |
247 /** | |
248 * Set the value. | |
249 * | |
250 * @param value | |
251 */ | |
252 public void setValue(float value) { | |
253 this.value = new Float(value); | |
254 } | |
255 | |
256 /** | |
257 * @return | |
258 */ | |
259 public String getName() { | |
260 return name; | |
261 } | |
262 | |
263 /** | |
264 * @param name | |
265 */ | |
266 public void setName(String name) { | |
267 this.name = name; | |
268 } | |
269 | |
270 /** | |
271 * @return | |
272 */ | |
273 public int getType() { | |
274 return type; | |
275 } | |
276 | |
277 /** | |
278 * @param type | |
279 */ | |
280 public void setType(int type) { | |
281 this.type = type; | |
282 } | |
283 | |
284 } |