Mercurial > hg > digilib-old
annotate servlet/src/digilib/servlet/Parameter.java @ 261:3bce5fe0e78d
Servlet version 1.21b1
- directory indexing got faster but less safe
(configurable by "safe-dir-index")
- mo=rawfile supplies filename
- DigilibConfig takes File parameters
- some SerialVersionUIDs (suggested by Eclipse)
author | robcast |
---|---|
date | Mon, 11 Oct 2004 21:23:00 +0200 |
parents | afe7ff98bb71 |
children | e758a49258e8 |
rev | line source |
---|---|
153 | 1 /* Parameter -- General digilib parameter class. |
2 | |
261 | 3 Digital Image Library servlet components |
153 | 4 |
261 | 5 Copyright (C) 2003 Robert Casties (robcast@mail.berlios.de) |
153 | 6 |
261 | 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 | |
153 | 14 |
261 | 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 | |
153 | 19 * |
20 * Created on 02.09.2003 by casties | |
21 * | |
22 */ | |
23 package digilib.servlet; | |
24 | |
261 | 25 import java.io.File; |
26 import java.io.IOException; | |
27 | |
28 /** | |
29 * General digilib parameter class. | |
153 | 30 * |
31 * @author casties | |
261 | 32 * |
153 | 33 */ |
34 public class Parameter { | |
35 /** real value */ | |
36 protected Object value = null; | |
37 | |
38 /** default value */ | |
39 protected Object defval = null; | |
40 | |
41 /** parameter name (e.g. in config file) */ | |
42 protected String name = null; | |
43 | |
44 /** parameter type */ | |
45 protected int type = 0; | |
46 | |
261 | 47 /** |
48 * Default constructor. | |
49 * | |
153 | 50 */ |
51 public Parameter() { | |
52 super(); | |
53 } | |
54 | |
261 | 55 /** |
56 * Constructor with name, default, and value. | |
153 | 57 * |
58 * @param value | |
59 * @param defval | |
60 */ | |
61 public Parameter(String name, Object defval, Object value) { | |
62 this.name = name; | |
63 this.value = value; | |
64 this.defval = defval; | |
65 } | |
66 | |
261 | 67 /** |
68 * Constructor with name, default, value, and type. | |
69 * | |
153 | 70 * @param value |
71 * @param defval | |
72 */ | |
73 public Parameter(String name, Object defval, Object value, int type) { | |
74 this.name = name; | |
75 this.value = value; | |
76 this.defval = defval; | |
77 this.type = type; | |
78 } | |
79 | |
261 | 80 /** |
81 * Is the value valid. | |
153 | 82 * |
83 * @return | |
84 */ | |
85 public boolean hasValue() { | |
86 return (value != null); | |
87 } | |
88 | |
261 | 89 /** |
90 * Try to set the value from a String. | |
153 | 91 * |
261 | 92 * Tries to convert the String to the same type as the default value. Sets |
93 * the value anyway if the default is null. Returns if the value could be | |
94 * set. | |
153 | 95 * |
96 * @param val | |
97 * @return | |
98 */ | |
99 public boolean setValueFromString(String val) { | |
170
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
100 // no default matches all |
153 | 101 if (defval == null) { |
102 this.value = val; | |
103 return true; | |
104 } | |
105 Class c = defval.getClass(); | |
106 // take String as is | |
107 if (c == String.class) { | |
108 this.value = val; | |
109 return true; | |
110 } | |
261 | 111 // set File |
112 if (c == File.class) { | |
113 this.value = new File(val); | |
114 return true; | |
115 } | |
153 | 116 // set Boolean if string == "true" |
117 if (c == Boolean.class) { | |
118 this.value = new Boolean(val.compareToIgnoreCase("true") == 0); | |
119 return true; | |
120 } | |
170
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
121 try { |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
122 // set Integer |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
123 if (c == Integer.class) { |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
124 this.value = new Integer(Integer.parseInt(val)); |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
125 return true; |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
126 } |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
127 // set Float |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
128 if (c == Float.class) { |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
129 this.value = new Float(Float.parseFloat(val)); |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
130 return true; |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
131 } |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
132 } catch (NumberFormatException e) { |
153 | 133 } |
261 | 134 // then it's unknown |
153 | 135 return false; |
136 } | |
137 | |
261 | 138 /** |
139 * Get the default as Object. | |
140 * | |
153 | 141 * @return |
142 */ | |
143 public Object getDefault() { | |
144 return defval; | |
145 } | |
146 | |
261 | 147 /** |
148 * Set the default. | |
149 * | |
153 | 150 * @param defval |
151 */ | |
152 public void setDefault(Object defval) { | |
153 this.defval = defval; | |
154 } | |
155 | |
261 | 156 /** |
157 * Get the value as Object. | |
153 | 158 * |
159 * Returns the default if the value is not set. | |
160 * | |
161 * @return | |
162 */ | |
163 public Object getValue() { | |
164 return (value != null) ? value : defval; | |
165 } | |
166 | |
167 public int getAsInt() { | |
168 Integer i = (Integer) getValue(); | |
169 return (i != null) ? i.intValue() : 0; | |
170 } | |
171 | |
172 public float getAsFloat() { | |
173 Float f = (Float) getValue(); | |
174 return (f != null) ? f.floatValue() : 0f; | |
175 } | |
176 | |
177 public String getAsString() { | |
170
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
178 Object s = getValue(); |
261 | 179 if (s == null) { |
180 return ""; | |
181 } | |
182 if (s.getClass() == File.class) { | |
183 try { | |
184 return ((File) s).getCanonicalPath(); | |
185 } catch (IOException e) { | |
186 return "ERR: " + s.toString(); | |
187 } | |
188 } | |
189 return s.toString(); | |
153 | 190 } |
191 | |
192 public boolean getAsBoolean() { | |
193 Boolean b = (Boolean) getValue(); | |
194 return (b != null) ? b.booleanValue() : false; | |
195 } | |
196 | |
170
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
197 public String[] parseAsArray(String separator) { |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
198 String s = getAsString(); |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
199 String[] sa = s.split(separator); |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
200 return sa; |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
201 } |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
202 |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
203 public float[] parseAsFloatArray(String separator) { |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
204 String s = getAsString(); |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
205 String[] sa = s.split(separator); |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
206 float[] fa = null; |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
207 try { |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
208 int n = sa.length; |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
209 fa = new float[n]; |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
210 for (int i = 0; i < n; i++) { |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
211 float f = Float.parseFloat(sa[i]); |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
212 fa[i] = f; |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
213 } |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
214 } catch (Exception e) { |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
215 } |
261 | 216 |
170
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
217 return fa; |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
218 } |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
219 |
261 | 220 /** |
221 * Set the value. | |
153 | 222 * |
223 * @param value | |
224 */ | |
225 public void setValue(Object value) { | |
226 this.value = value; | |
227 } | |
228 | |
261 | 229 /** |
230 * Set the value. | |
170
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
231 * |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
232 * @param value |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
233 */ |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
234 public void setValue(int value) { |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
235 this.value = new Integer(value); |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
236 } |
261 | 237 |
238 /** | |
239 * Set the value. | |
170
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
240 * |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
241 * @param value |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
242 */ |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
243 public void setValue(float value) { |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
244 this.value = new Float(value); |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
245 } |
261 | 246 |
153 | 247 /** |
248 * @return | |
249 */ | |
250 public String getName() { | |
251 return name; | |
252 } | |
253 | |
254 /** | |
255 * @param name | |
256 */ | |
257 public void setName(String name) { | |
258 this.name = name; | |
259 } | |
260 | |
261 /** | |
262 * @return | |
263 */ | |
264 public int getType() { | |
265 return type; | |
266 } | |
267 | |
268 /** | |
269 * @param type | |
270 */ | |
271 public void setType(int type) { | |
272 this.type = type; | |
273 } | |
274 | |
275 } |