Mercurial > hg > digilib-old
annotate servlet/src/digilib/servlet/Parameter.java @ 170:d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
- more generic class for request parameters
- like already done for DiglibConfig
- changes in JSPs for new request stuff
- changes in ImageSize class so size=0 is "wildcard"
- missing dw and dh parameters now treated as wildcards
- changed package name in imageinfo class
author | robcast |
---|---|
date | Wed, 29 Oct 2003 22:47:15 +0100 |
parents | 4980c969be4c |
children | afe7ff98bb71 |
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 //System.out.println("ERROR: trytoGetParam(int) failed on param "+name); |
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 |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
189 return fa; |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
190 } |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
191 |
153 | 192 /** Set the value. |
193 * | |
194 * @param value | |
195 */ | |
196 public void setValue(Object value) { | |
197 this.value = value; | |
198 } | |
199 | |
170
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
200 /** Set the value. |
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 * @param value |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
203 */ |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
204 public void setValue(int value) { |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
205 this.value = new Integer(value); |
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 |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
208 /** Set the value. |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
209 * |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
210 * @param value |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
211 */ |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
212 public void setValue(float value) { |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
213 this.value = new Float(value); |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
214 } |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
215 |
153 | 216 /** |
217 * @return | |
218 */ | |
219 public String getName() { | |
220 return name; | |
221 } | |
222 | |
223 /** | |
224 * @param name | |
225 */ | |
226 public void setName(String name) { | |
227 this.name = name; | |
228 } | |
229 | |
230 /** | |
231 * @return | |
232 */ | |
233 public int getType() { | |
234 return type; | |
235 } | |
236 | |
237 /** | |
238 * @param type | |
239 */ | |
240 public void setType(int type) { | |
241 this.type = type; | |
242 } | |
243 | |
244 } |