Mercurial > hg > digilib-old
annotate servlet/src/digilib/servlet/Parameter.java @ 181:afe7ff98bb71
Servlet version 1.18b1
- new transfer mode "rawfile" with mime-type application/octet-stream
- finally proper logging with Log4J!
- therefore a lot of debugging-prints changed
- the Util class is now useless
- ServletOps and FileOps are now purely static
author | robcast |
---|---|
date | Fri, 21 Nov 2003 00:17:31 +0100 |
parents | d40922628e4a |
children | 3bce5fe0e78d |
rev | line source |
---|---|
153 | 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.servlet; | |
24 | |
25 /** General digilib parameter class. | |
26 * | |
27 * @author casties | |
28 * | |
29 */ | |
30 public class Parameter { | |
31 /** real value */ | |
32 protected Object value = null; | |
33 | |
34 /** default value */ | |
35 protected Object defval = null; | |
36 | |
37 /** parameter name (e.g. in config file) */ | |
38 protected String name = null; | |
39 | |
40 /** parameter type */ | |
41 protected int type = 0; | |
42 | |
43 /** Default constructor. | |
44 * | |
45 */ | |
46 public Parameter() { | |
47 super(); | |
48 } | |
49 | |
50 /** Constructor with name, default, and value. | |
51 * | |
52 * @param value | |
53 * @param defval | |
54 */ | |
55 public Parameter(String name, Object defval, Object value) { | |
56 this.name = name; | |
57 this.value = value; | |
58 this.defval = defval; | |
59 } | |
60 | |
61 /** Constructor with name, default, value, and type. | |
62 * @param value | |
63 * @param defval | |
64 */ | |
65 public Parameter(String name, Object defval, Object value, int type) { | |
66 this.name = name; | |
67 this.value = value; | |
68 this.defval = defval; | |
69 this.type = type; | |
70 } | |
71 | |
72 /** Is the value valid. | |
73 * | |
74 * @return | |
75 */ | |
76 public boolean hasValue() { | |
77 return (value != null); | |
78 } | |
79 | |
80 /** Try to set the value from a String. | |
81 * | |
82 * Tries to convert the String to the same type as the default value. | |
83 * Sets the value anyway if the default is null. | |
84 * Returns if the value could be set. | |
85 * | |
86 * @param val | |
87 * @return | |
88 */ | |
89 public boolean setValueFromString(String val) { | |
170
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
90 // no default matches all |
153 | 91 if (defval == null) { |
92 this.value = val; | |
93 return true; | |
94 } | |
95 Class c = defval.getClass(); | |
96 // take String as is | |
97 if (c == String.class) { | |
98 this.value = val; | |
99 return true; | |
100 } | |
101 // set Boolean if string == "true" | |
102 if (c == Boolean.class) { | |
103 this.value = new Boolean(val.compareToIgnoreCase("true") == 0); | |
104 return true; | |
105 } | |
170
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
106 try { |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
107 // set Integer |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
108 if (c == Integer.class) { |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
109 this.value = new Integer(Integer.parseInt(val)); |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
110 return true; |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
111 } |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
112 // set Float |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
113 if (c == Float.class) { |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
114 this.value = new Float(Float.parseFloat(val)); |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
115 return true; |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
116 } |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
117 } catch (NumberFormatException e) { |
153 | 118 } |
119 // then it's unknown | |
120 return false; | |
121 } | |
122 | |
123 /** Get the default as Object. | |
124 * | |
125 * @return | |
126 */ | |
127 public Object getDefault() { | |
128 return defval; | |
129 } | |
130 | |
131 /** Set the default. | |
132 * @param defval | |
133 */ | |
134 public void setDefault(Object defval) { | |
135 this.defval = defval; | |
136 } | |
137 | |
138 /** Get the value as Object. | |
139 * | |
140 * Returns the default if the value is not set. | |
141 * | |
142 * @return | |
143 */ | |
144 public Object getValue() { | |
145 return (value != null) ? value : defval; | |
146 } | |
147 | |
148 public int getAsInt() { | |
149 Integer i = (Integer) getValue(); | |
150 return (i != null) ? i.intValue() : 0; | |
151 } | |
152 | |
153 public float getAsFloat() { | |
154 Float f = (Float) getValue(); | |
155 return (f != null) ? f.floatValue() : 0f; | |
156 } | |
157 | |
158 public String getAsString() { | |
170
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
159 Object s = getValue(); |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
160 return (s != null) ? s.toString() : ""; |
153 | 161 } |
162 | |
163 public boolean getAsBoolean() { | |
164 Boolean b = (Boolean) getValue(); | |
165 return (b != null) ? b.booleanValue() : false; | |
166 } | |
167 | |
170
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
168 public String[] parseAsArray(String separator) { |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
169 String s = getAsString(); |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
170 String[] sa = s.split(separator); |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
171 return sa; |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
172 } |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
173 |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
174 public float[] parseAsFloatArray(String separator) { |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
175 String s = getAsString(); |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
176 String[] sa = s.split(separator); |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
177 float[] fa = null; |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
178 try { |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
179 int n = sa.length; |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
180 fa = new float[n]; |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
181 for (int i = 0; i < n; i++) { |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
182 float f = Float.parseFloat(sa[i]); |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
183 fa[i] = f; |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
184 } |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
185 } catch (Exception e) { |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
186 } |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
187 |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
188 return fa; |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
189 } |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
190 |
153 | 191 /** Set the value. |
192 * | |
193 * @param value | |
194 */ | |
195 public void setValue(Object value) { | |
196 this.value = value; | |
197 } | |
198 | |
170
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
199 /** Set the value. |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
200 * |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
201 * @param value |
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 void setValue(int value) { |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
204 this.value = new Integer(value); |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
205 } |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
206 |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
207 /** Set the value. |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
208 * |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
209 * @param value |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
210 */ |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
211 public void setValue(float value) { |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
212 this.value = new Float(value); |
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 |
153 | 215 /** |
216 * @return | |
217 */ | |
218 public String getName() { | |
219 return name; | |
220 } | |
221 | |
222 /** | |
223 * @param name | |
224 */ | |
225 public void setName(String name) { | |
226 this.name = name; | |
227 } | |
228 | |
229 /** | |
230 * @return | |
231 */ | |
232 public int getType() { | |
233 return type; | |
234 } | |
235 | |
236 /** | |
237 * @param type | |
238 */ | |
239 public void setType(int type) { | |
240 this.type = type; | |
241 } | |
242 | |
243 } |