Mercurial > hg > digilib-old
annotate servlet/src/digilib/util/Parameter.java @ 632:9a054ba5b365 digilibPDF
close digilibPDF branch
| author | robcast |
|---|---|
| date | Tue, 18 Jan 2011 13:01:18 +0100 |
| parents | 0885f5ca5b24 |
| children |
| 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 */ | |
| 557 | 23 package digilib.util; |
| 153 | 24 |
| 261 | 25 import java.io.File; |
| 26 import java.io.IOException; | |
| 27 | |
| 557 | 28 |
| 261 | 29 /** |
| 30 * General digilib parameter class. | |
| 153 | 31 * |
| 32 * @author casties | |
| 261 | 33 * |
| 153 | 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 | |
| 261 | 48 /** |
| 49 * Default constructor. | |
| 50 * | |
| 153 | 51 */ |
| 52 public Parameter() { | |
| 53 super(); | |
| 54 } | |
| 55 | |
| 261 | 56 /** |
| 57 * Constructor with name, default, and value. | |
| 153 | 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 | |
| 261 | 68 /** |
| 69 * Constructor with name, default, value, and type. | |
| 70 * | |
| 153 | 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 | |
| 261 | 81 /** |
| 82 * Is the value valid. | |
| 153 | 83 * |
| 84 * @return | |
| 85 */ | |
| 86 public boolean hasValue() { | |
| 87 return (value != null); | |
| 88 } | |
| 89 | |
| 261 | 90 /** |
| 91 * Try to set the value from a String. | |
| 153 | 92 * |
| 261 | 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. | |
| 153 | 96 * |
| 97 * @param val | |
| 98 * @return | |
| 99 */ | |
| 100 public boolean setValueFromString(String val) { | |
| 554 | 101 if (val == null) { |
| 102 val = ""; | |
| 103 } | |
|
170
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
104 // no default matches all |
| 153 | 105 if (defval == null) { |
| 106 this.value = val; | |
| 107 return true; | |
| 108 } | |
| 531 | 109 Class<? extends Object> c = defval.getClass(); |
| 153 | 110 // take String as is |
| 111 if (c == String.class) { | |
| 112 this.value = val; | |
| 113 return true; | |
| 114 } | |
| 261 | 115 // set File |
| 116 if (c == File.class) { | |
| 117 this.value = new File(val); | |
| 118 return true; | |
| 119 } | |
| 547 | 120 // set Options |
| 121 if (c == OptionsSet.class) { | |
| 122 this.value = new OptionsSet(val); | |
| 123 return true; | |
| 124 } | |
| 153 | 125 // set Boolean if string == "true" |
| 126 if (c == Boolean.class) { | |
| 127 this.value = new Boolean(val.compareToIgnoreCase("true") == 0); | |
| 128 return true; | |
| 129 } | |
|
170
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
130 try { |
|
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
131 // set Integer |
|
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
132 if (c == Integer.class) { |
|
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
133 this.value = new Integer(Integer.parseInt(val)); |
|
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
134 return true; |
|
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
135 } |
|
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
136 // set Float |
|
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
137 if (c == Float.class) { |
|
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
138 this.value = new Float(Float.parseFloat(val)); |
|
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
139 return true; |
|
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
140 } |
|
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
141 } catch (NumberFormatException e) { |
| 153 | 142 } |
| 261 | 143 // then it's unknown |
| 153 | 144 return false; |
| 145 } | |
| 146 | |
| 261 | 147 /** |
| 148 * Get the default as Object. | |
| 149 * | |
| 153 | 150 * @return |
| 151 */ | |
| 152 public Object getDefault() { | |
| 153 return defval; | |
| 154 } | |
| 155 | |
| 261 | 156 /** |
| 157 * Set the default. | |
| 158 * | |
| 153 | 159 * @param defval |
| 160 */ | |
| 161 public void setDefault(Object defval) { | |
| 162 this.defval = defval; | |
| 163 } | |
| 164 | |
| 261 | 165 /** |
| 166 * Get the value as Object. | |
| 153 | 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() { | |
|
170
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
187 Object s = getValue(); |
| 261 | 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(); | |
| 153 | 199 } |
| 200 | |
| 201 public boolean getAsBoolean() { | |
| 202 Boolean b = (Boolean) getValue(); | |
| 203 return (b != null) ? b.booleanValue() : false; | |
| 204 } | |
| 205 | |
|
170
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
206 public String[] parseAsArray(String separator) { |
|
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
207 String s = getAsString(); |
|
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
208 String[] sa = s.split(separator); |
|
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
209 return sa; |
|
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 |
|
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
212 public float[] parseAsFloatArray(String separator) { |
|
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
213 String s = getAsString(); |
|
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
214 String[] sa = s.split(separator); |
|
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
215 float[] fa = null; |
|
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
216 try { |
|
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
217 int n = sa.length; |
|
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
218 fa = new float[n]; |
|
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
219 for (int i = 0; i < n; i++) { |
|
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
220 float f = Float.parseFloat(sa[i]); |
|
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
221 fa[i] = f; |
|
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
222 } |
|
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
223 } catch (Exception e) { |
|
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
224 } |
| 261 | 225 |
|
170
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
226 return fa; |
|
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
227 } |
|
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
228 |
| 261 | 229 /** |
| 230 * Set the value. | |
| 153 | 231 * |
| 232 * @param value | |
| 233 */ | |
| 234 public void setValue(Object value) { | |
| 235 this.value = value; | |
| 236 } | |
| 237 | |
| 261 | 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(int value) { |
|
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
244 this.value = new Integer(value); |
|
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
245 } |
| 261 | 246 |
| 247 /** | |
| 248 * Set the value. | |
|
170
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
249 * |
|
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
250 * @param value |
|
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
251 */ |
|
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
252 public void setValue(float value) { |
|
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
253 this.value = new Float(value); |
|
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
254 } |
| 261 | 255 |
| 153 | 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 } |
