1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434
| package Configuration;
import java.lang.reflect.Field;
import java.util.LinkedList;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
/**
* Parse configuration XML file and modify default values of object o
*
* @author Patrick Mast
* @since 2007/04/13
*
*/
public class SaxParser extends DefaultHandler {
/**
* Configuration object, which will be modified
*/
private Object o = null;
/**
* To store the name of the current property
*/
private String name = null;
/**
* To store the values of the current property
*/
private LinkedList<String> value = new LinkedList<String>();
/**
* When true, the current value must be read
*/
private boolean readValue = false;
/**
* Class of object o
*/
Class<?> c = null;
/**
* Fields of the class above
*/
Field[] f = null;
Class[] classes = { boolean.class, Boolean.class,
byte.class, Byte.class,
short.class, Short.class,
int.class, Integer.class,
long.class, Long.class,
float.class, Float.class,
double.class, Double.class,
String.class,
boolean[].class, Boolean[].class,
byte[].class, Byte[].class,
short[].class, Short[].class,
int[].class, Integer[].class,
long[].class, Long[].class,
float[].class, Float[].class,
double[].class, Double[].class,
String[].class};
/**
* boolean type
*/
final byte BOOLEAN_PRIMITIVE = 0;
/**
* Boolean type
*/
final byte BOOLEAN = 1;
/**
* byte type
*/
final byte BYTE_PRIMITIVE = 2;
/**
* Byte type
*/
final byte BYTE = 3;
/**
* short type
*/
final byte SHORT_PRIMITIVE = 4;
/**
* Short type
*/
final byte SHORT = 5;
/**
* int type
*/
final byte INTEGER_PRIMITIVE = 6;
/**
* Integer type
*/
final byte INTEGER = 7;
/**
* long type
*/
final byte LONG_PRIMITIVE = 8;
/**
* Long type
*/
final byte LONG = 9;
/**
* float type
*/
final byte FLOAT_PRIMITIVE = 10;
/**
* Float type
*/
final byte FLOAT = 11;
/**
* double type
*/
final byte DOUBLE_PRIMITIVE = 12;
/**
* Double type
*/
final byte DOUBLE = 13;
/**
* String type
*/
final byte STRING = 14;
/**
* boolean array type
*/
final byte BOOLEAN_P_ARRAY = 15;
/**
* Boolean array type
*/
final byte BOOLEAN_ARRAY = 16;
/**
* byte array type
*/
final byte BYTE_P_ARRAY = 17;
/**
* Bate array type
*/
final byte BYTE_ARRAY = 18;
/**
* short array type
*/
final byte SHORT_P_ARRAY = 19;
/**
* Short array type
*/
final byte SHORT_ARRAY = 20;
/**
* int array type
*/
final byte INTEGER_P_ARRAY = 21;
/**
* Integer array type
*/
final byte INTEGER_ARRAY = 22;
/**
* long array type
*/
final byte LONG_P_ARRAY = 23;
/**
* Long array type
*/
final byte LONG_ARRAY = 24;
/**
* float array type
*/
final byte FLOAT_P_ARRAY = 25;
/**
* Float array type
*/
final byte FLOAT_ARRAY = 26;
/**
* double array type
*/
final byte DOUBLE_P_ARRAY = 27;
/**
* Double array type
*/
final byte DOUBLE_ARRAY = 28;
/**
* String array type
*/
final byte STRING_ARRAY = 29;
/**
* Field's names of the configuration file
*/
private LinkedList<String> names = new LinkedList<String>();
/**
* Field's type of the configuration file
*/
private LinkedList<Class> types = new LinkedList<Class>();
/**
* Constructor
*
* @param o Configuration object, which will be modified
*/
public SaxParser(Object o) {
this.o=o;
}
/* (non-Javadoc)
* @see org.xml.sax.helpers.DefaultHandler#startDocument()
*/
@Override
public void startDocument() throws SAXException, ConfigException {
// Class of configuration file
c=this.o.getClass();
// Fields of configuration file
f=c.getFields();
// Catch type and name for each field
for(int i=0;i<f.length;i++){
try {
types.add(f[i].getType());
names.add(f[i].getName());
} catch (IllegalArgumentException e) {
throw new ConfigException(ConfigException.ILLEGAL_ARGUMENT);
}
}
}
/* (non-Javadoc)
* @see org.xml.sax.helpers.DefaultHandler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)
*/
@Override
public void startElement(String arg0, String arg1, String arg2, Attributes arg3) throws SAXException {
// The tag <property> is the current tag
if(arg2=="property"){
// Set property's name
this.name=new String(arg3.getValue(0));
}
// The tag <value> is the current tag
if(arg2=="value"){
// Value must be read
readValue = true;
}
}
/* (non-Javadoc)
* @see org.xml.sax.helpers.DefaultHandler#characters(char[], int, int)
*/
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
// Get the current value
String value = new String(ch, start, length);
// If the current values must be read
if(readValue){
// Add the value to the list
this.value.add(value);
}
}
/**
* Search an object in an object's array
*
* @param array Array
* @param search Object to search
* @return Index of fouded object in the array, negative value if no object found
*/
private int searchArray (Object[] array, Object search){
for(int i=0;i<array.length;i++){
if(array[i].equals(search)){
// Found, return index
return i;
}
}
// Not found, return negative value
return -1;
}
/* (non-Javadoc)
* @see org.xml.sax.helpers.DefaultHandler#endElement(java.lang.String, java.lang.String, java.lang.String)
*/
@Override
public void endElement(String arg0, String arg1, String arg2) throws SAXException {
// The tag </property> is the current tag
if(arg2=="property"){
//System.out.println(this.name+"="+this.value); // to delete
// Find "this.name" in the names LinkedList
int index = names.indexOf(this.name);
if(index>=0){
try {
// Type of the current field
Class type = f[index].getType();
// Value which will be store in the current field
Object val = null;
// Type of current field
int intType = searchArray(classes, type);
// Traitement differes for different types
val=typeTraitement(val, intType, index);
// Normal
if(val!=null){
f[index].set(this.o, val);
}
// An error occured, the type was not recognised
else{
throw new ConfigException(ConfigException.BAD_TYPE_FOUND);
}
} catch (IllegalArgumentException e) {
throw new ConfigException(ConfigException.ILLEGAL_ARGUMENT);
} catch (IllegalAccessException e) {
throw new ConfigException(ConfigException.ILLEGAL_ACCESS);
}
}
// Clear the list for next element
this.value.clear();
}
// Maybe the next value is not important
readValue=false;
}
private Object typeTraitement(Object val, int intType, int index){
switch(intType){
case BOOLEAN_PRIMITIVE:
case BOOLEAN: val = Boolean.parseBoolean(this.value.get(0)); break;
case BYTE_PRIMITIVE:
case BYTE: val = Byte.parseByte(this.value.get(0)); break;
case SHORT_PRIMITIVE:
case SHORT: val = Short.parseShort(this.value.get(0)); break;
case INTEGER_PRIMITIVE:
case INTEGER: val = Integer.parseInt(this.value.get(0)); break;
case LONG_PRIMITIVE:
case LONG: val = Long.parseLong(this.value.get(0)); break;
case FLOAT_PRIMITIVE:
case FLOAT: val = Float.parseFloat(this.value.get(0)); break;
case DOUBLE_PRIMITIVE:
case DOUBLE: val = Double.parseDouble(this.value.get(0)); break;
case STRING: val = this.value.get(0); break;
case BOOLEAN_P_ARRAY:
boolean[] arrayBooleanP = new boolean[this.value.size()];
for(int i=0;i<this.value.size();i++)
arrayBooleanP[i]=Boolean.parseBoolean(this.value.get(i));
val=arrayBooleanP;
break;
case BOOLEAN_ARRAY:
Boolean[] arrayBoolean = new Boolean[this.value.size()];
for(int i=0;i<this.value.size();i++)
arrayBoolean[i]=Boolean.parseBoolean(this.value.get(i));
val=arrayBoolean;
break;
case BYTE_P_ARRAY:
byte[] arrayByteP = new byte[this.value.size()];
for(int i=0;i<this.value.size();i++)
arrayByteP[i]=Byte.parseByte(this.value.get(i));
val=arrayByteP;
break;
case BYTE_ARRAY:
Byte[] arrayByte = new Byte[this.value.size()];
for(int i=0;i<this.value.size();i++)
arrayByte[i]=Byte.parseByte(this.value.get(i));
val=arrayByte;
break;
case SHORT_P_ARRAY:
short[] arrayShortP = new short[this.value.size()];
for(int i=0;i<this.value.size();i++)
arrayShortP[i]=Short.parseShort(this.value.get(i));
val=arrayShortP;
break;
case SHORT_ARRAY:
Short[] arrayShort = new Short[this.value.size()];
for(int i=0;i<this.value.size();i++)
arrayShort[i]=Short.parseShort(this.value.get(i));
val=arrayShort;
break;
case INTEGER_P_ARRAY:
int[] arrayIntP = new int[this.value.size()];
for(int i=0;i<this.value.size();i++)
arrayIntP[i]=Integer.parseInt(this.value.get(i));
val=arrayIntP;
break;
case INTEGER_ARRAY:
Integer[] arrayInt = new Integer[this.value.size()];
for(int i=0;i<this.value.size();i++)
arrayInt[i]=Integer.parseInt(this.value.get(i));
val=arrayInt;
break;
case LONG_P_ARRAY:
long[] arrayLongP = new long[this.value.size()];
for(int i=0;i<this.value.size();i++)
arrayLongP[i]=Long.parseLong(this.value.get(i));
val=arrayLongP;
break;
case LONG_ARRAY:
Long[] arrayLong = new Long[this.value.size()];
for(int i=0;i<this.value.size();i++)
arrayLong[i]=Long.parseLong(this.value.get(i));
val=arrayLong;
break;
case FLOAT_P_ARRAY:
float[] arrayFloatP = new float[this.value.size()];
for(int i=0;i<this.value.size();i++)
arrayFloatP[i]=Float.parseFloat(this.value.get(i));
val=arrayFloatP;
break;
case FLOAT_ARRAY:
Float[] arrayFloat = new Float[this.value.size()];
for(int i=0;i<this.value.size();i++)
arrayFloat[i]=Float.parseFloat(this.value.get(i));
val=arrayFloat;
break;
case DOUBLE_P_ARRAY:
double[] arrayDoubleP = new double[this.value.size()];
for(int i=0;i<this.value.size();i++)
arrayDoubleP[i]=Double.parseDouble(this.value.get(i));
val=arrayDoubleP;
break;
case DOUBLE_ARRAY:
Double[] arrayDouble = new Double[this.value.size()];
for(int i=0;i<this.value.size();i++)
arrayDouble[i]=Double.parseDouble(this.value.get(i));
val=arrayDouble;
break;
case STRING_ARRAY:
String[] arrayString = new String[this.value.size()];
for(int i=0;i<this.value.size();i++)
arrayString[i]=this.value.get(i);
val=arrayString;
break;
}
return val;
}
} |