1
/*___INFO__MARK_BEGIN__*/
2
/*************************************************************************
4
* The Contents of this file are made available subject to the terms of
5
* the Sun Industry Standards Source License Version 1.2
7
* Sun Microsystems Inc., March, 2001
10
* Sun Industry Standards Source License Version 1.2
11
* =================================================
12
* The contents of this file are subject to the Sun Industry Standards
13
* Source License Version 1.2 (the "License"); You may not use this file
14
* except in compliance with the License. You may obtain a copy of the
15
* License at http://gridengine.sunsource.net/Gridengine_SISSL_license.html
17
* Software provided under this License is provided on an "AS IS" basis,
18
* WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
19
* WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
20
* MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
21
* See the License for the specific provisions governing your rights and
22
* obligations concerning the Software.
24
* The Initial Developer of the Original Code is: Sun Microsystems, Inc.
26
* Copyright: 2001 by Sun Microsystems, Inc.
28
* All Rights Reserved.
30
************************************************************************/
31
/*___INFO__MARK_END__*/
32
package com.sun.grid.jgdi.configuration.xml;
34
import com.sun.grid.jgdi.configuration.GEObject;
35
import com.sun.grid.jgdi.configuration.Util;
36
import com.sun.grid.jgdi.configuration.reflect.ClassDescriptor;
37
import com.sun.grid.jgdi.configuration.reflect.ListPropertyDescriptor;
38
import com.sun.grid.jgdi.configuration.reflect.MapListPropertyDescriptor;
39
import com.sun.grid.jgdi.configuration.reflect.MapPropertyDescriptor;
40
import com.sun.grid.jgdi.configuration.reflect.PropertyDescriptor;
41
import com.sun.grid.jgdi.configuration.reflect.SimplePropertyDescriptor;
43
import java.io.IOException;
44
import java.io.InputStream;
45
import java.io.OutputStream;
46
import java.io.Reader;
47
import java.io.Writer;
48
import java.util.Iterator;
51
import java.util.Stack;
52
import java.util.logging.Level;
53
import java.util.logging.Logger;
54
import javax.xml.parsers.ParserConfigurationException;
55
import javax.xml.parsers.SAXParser;
56
import javax.xml.parsers.SAXParserFactory;
57
import org.xml.sax.InputSource;
58
import org.xml.sax.Locator;
59
import org.xml.sax.SAXException;
60
import org.xml.sax.SAXParseException;
61
import org.xml.sax.helpers.DefaultHandler;
65
* This class implements the serialisation/deserialion of cull object
68
* <p>Implement missing primitive handlers</p>
70
public class XMLUtil {
72
private static Logger logger = Logger.getLogger(XMLUtil.class.getName());
74
public static final String HEADER = "<?xml version='1.0' encoding='UTF-8'?>";
76
private static final String NONE = "NONE";
79
public static class Context {
81
private final IndentedPrintWriter p;
82
private boolean hideReadOnly;
83
private boolean hideConfigurable;
84
private boolean hideBrowseable;
86
public Context(IndentedPrintWriter p) {
88
setHideReadOnly(true);
89
setHideConfigurable(false);
90
setHideBrowseable(false);
93
public Context(Writer wr) {
94
this(new IndentedPrintWriter(wr));
97
public Context(OutputStream out) {
98
this(new IndentedPrintWriter(out));
101
public Context(File file) throws IOException {
102
this(new IndentedPrintWriter(file));
105
public boolean include(PropertyDescriptor pd) {
106
if(pd.isReadOnly() && hideReadOnly) {
108
} else if (pd.isConfigurable() && hideConfigurable) {
110
} else if (pd.isBrowsable() && hideBrowseable) {
116
public boolean isHideReadOnly() {
120
public void setHideReadOnly(boolean hideReadOnly) {
121
this.hideReadOnly = hideReadOnly;
124
public boolean isHideConfigurable() {
125
return hideConfigurable;
128
public void setHideConfigurable(boolean hideConfigurable) {
129
this.hideConfigurable = hideConfigurable;
132
public boolean isHideBrowseable() {
133
return hideBrowseable;
136
public void setHideBrowseable(boolean hideBrowseable) {
137
this.hideBrowseable = hideBrowseable;
142
public static boolean write(GEObject obj, Context ctx) {
143
ctx.p.println(HEADER);
144
write(obj, obj.getClass(), ctx);
146
return ctx.p.checkError();
149
public static boolean write(GEObject obj, Writer wr) throws IOException {
150
Context ctx = new Context(wr);
151
return write(obj, ctx);
154
public static boolean write(GEObject obj, OutputStream out) throws IOException {
155
Context ctx = new Context(out);
156
return write(obj, ctx);
159
public static boolean write(GEObject obj, File file) throws IOException {
160
Context ctx = new Context(file);
161
return write(obj, ctx);
164
private static void write(GEObject obj, Class clazz, Context ctx) {
166
ClassDescriptor cd = Util.getDescriptor(clazz);
168
ctx.p.print(cd.getCullName());
175
for(int i = 0; i < cd.getPropertyCount(); i++) {
176
PropertyDescriptor pd = cd.getProperty(i);
178
if (!ctx.include(pd)) {
181
if (pd instanceof SimplePropertyDescriptor) {
182
write(obj, (SimplePropertyDescriptor)pd, ctx);
183
} else if (pd instanceof ListPropertyDescriptor) {
184
write(obj, (ListPropertyDescriptor)pd, ctx);
185
} else if (pd instanceof MapPropertyDescriptor) {
186
write(obj, (MapPropertyDescriptor)pd, ctx);
187
} else if (pd instanceof MapListPropertyDescriptor) {
188
write(obj, (MapListPropertyDescriptor)pd, ctx);
190
throw new IllegalStateException("Unknown property type " + pd.getClass());
197
ctx.p.print(cd.getCullName());
202
private static void write(GEObject obj, SimplePropertyDescriptor pd, Context ctx) {
204
Object value = pd.getValue(obj);
207
ctx.p.print(pd.getPropertyName());
209
if (GEObject.class.isAssignableFrom(pd.getPropertyType())) {
211
write((GEObject)value, pd.getPropertyType(), ctx);
213
write((GEObject)value, value.getClass(), ctx);
216
ctx.p.print(quoteCharacters(value.toString()));
219
ctx.p.print(pd.getPropertyName());
224
private static void write(GEObject obj, ListPropertyDescriptor pd, Context ctx) {
226
int count = pd.getCount(obj);
229
ctx.p.print(pd.getPropertyName());
236
for(int i = 0; i < count; i++) {
237
Object value = pd.get(obj,i);
238
if (GEObject.class.isAssignableFrom(pd.getPropertyType())) {
240
write((GEObject)value, pd.getPropertyType(), ctx);
242
write((GEObject)value, value.getClass(), ctx);
245
writePrimitive(value, pd.getPropertyType(), ctx);
251
ctx.p.print(pd.getPropertyName());
255
private static void write(GEObject obj, MapPropertyDescriptor pd, Context ctx) {
257
Set keys = pd.getKeys(obj);
259
Iterator keyIter = keys.iterator();
260
while(keyIter.hasNext()) {
261
Object key = keyIter.next();
262
Object value = pd.get(obj,key);
264
ctx.p.print(pd.getPropertyName());
265
ctx.p.print(" key='");
266
ctx.p.print(quoteCharacters(key.toString()));
268
if (GEObject.class.isAssignableFrom(pd.getPropertyType())) {
272
write((GEObject)value, pd.getPropertyType(), ctx);
274
write((GEObject)value, value.getClass(), ctx);
281
ctx.p.print(quoteCharacters(value.toString()));
285
ctx.p.print(pd.getPropertyName());
290
public static final String STRING_TAG = "string";
291
public static final String BOOLEAN_TAG = "boolean";
292
public static final String INT_TAG = "int";
293
public static final String SHORT_TAG = "short";
294
public static final String LONG_TAG = "long";
295
public static final String DOUBLE_TAG = "double";
296
public static final String FLOAT_TAG = "float";
298
private static void writePrimitive(Object value, Class clazz, Context ctx) {
301
if (String.class.isAssignableFrom(clazz)) {
303
} else if (Boolean.TYPE.isAssignableFrom(clazz) ||
304
Boolean.class.isAssignableFrom(clazz)) {
306
} else if (Integer.TYPE.isAssignableFrom(clazz) ||
307
Integer.class.isAssignableFrom(clazz)) {
309
} else if (Short.TYPE.isAssignableFrom(clazz) ||
310
Short.class.isAssignableFrom(clazz)) {
312
} else if (Long.TYPE.isAssignableFrom(clazz) ||
313
Long.class.isAssignableFrom(clazz)) {
315
} else if (Double.TYPE.isAssignableFrom(clazz) ||
316
Double.class.isAssignableFrom(clazz)) {
318
} else if (Float.TYPE.isAssignableFrom(clazz) ||
319
Float.class.isAssignableFrom(clazz)) {
322
throw new IllegalArgumentException("Unknown primitive type " + clazz.getName());
328
if (String.class.isAssignableFrom(clazz)) {
331
throw new IllegalArgumentException("Don't know howto handle null value for type " + clazz.getName());
334
ctx.p.print(quoteCharacters(value.toString()));
342
private static void write(GEObject obj, MapListPropertyDescriptor pd, Context ctx) {
344
Set keys = pd.getKeys(obj);
346
Iterator keyIter = keys.iterator();
347
while(keyIter.hasNext()) {
348
Object key = keyIter.next();
350
ctx.p.print(pd.getPropertyName());
351
ctx.p.print(" key='");
352
ctx.p.print(quoteCharacters(key.toString()));
354
int count = pd.getCount(obj, key);
358
if (GEObject.class.isAssignableFrom(pd.getPropertyType())) {
359
write((GEObject)null, pd.getPropertyType(), ctx);
361
writePrimitive(null, pd.getPropertyType(), ctx);
367
for(int i = 0; i < count; i++) {
368
Object value = pd.get(obj, key, i);
369
if (GEObject.class.isAssignableFrom(pd.getPropertyType())) {
371
write((GEObject)value, pd.getPropertyType(), ctx);
373
write((GEObject)value, value.getClass(), ctx);
376
writePrimitive(value, pd.getPropertyType(), ctx);
382
ctx.p.print(pd.getPropertyName());
387
private static String quoteCharacters(String s) {
388
StringBuilder result = null;
389
for(int i = 0, max = s.length(), delta = 0; i < max; i++) {
390
char c = s.charAt(i);
391
String replacement = null;
394
replacement = "&";
395
} else if (c == '<') {
396
replacement = "<";
397
} else if (c == '\r') {
398
replacement = " ";
399
} else if (c == '>') {
400
replacement = ">";
401
} else if (c == '"') {
402
replacement = """;
403
} else if (c == '\'') {
404
replacement = "'";
407
if (replacement != null) {
408
if (result == null) {
409
result = new StringBuilder(s);
411
result.replace(i + delta, i + delta + 1, replacement);
412
delta += (replacement.length() - 1);
415
if (result == null) {
418
return result.toString();
423
* Read a XML definition of a gridengine object from an <code>InputStream</code>.
425
* @param in the InputStream
426
* @param properties All ${key} expressions in the xml file will be replaced be
427
* the corresponding value from the properties
428
* @throws java.io.IOException on any I/O Error
429
* @throws javax.xml.parsers.ParserConfigurationException if a SAX parser has an invalid configuration
430
* @throws org.xml.sax.SAXException on any parse error
431
* @return the gridengine object
433
public static Object read(InputStream in, Map properties) throws IOException, ParserConfigurationException, SAXException {
434
// Use an instance of ourselves as the SAX event handler
435
RootHandler handler = new RootHandler(properties);
437
// Use the default (non-validating) parser
438
SAXParserFactory factory = SAXParserFactory.newInstance();
441
SAXParser saxParser = factory.newSAXParser();
443
saxParser.parse(in, handler);
445
return handler.getObject();
449
* Read a XML definition of a gridengine object from an <code>InputStream</code>.
451
* @param in the InputStream
452
* @throws java.io.IOException on any I/O Error
453
* @throws javax.xml.parsers.ParserConfigurationException if a SAX parser has an invalid configuration
454
* @throws org.xml.sax.SAXException on any parse error
455
* @return the gridengine object
457
public static Object read(InputStream in) throws IOException, ParserConfigurationException, SAXException {
458
return read(in, null);
462
* Read a XML definition of a gridengine object from a <code>File</code>.
464
* @param file the file
465
* @param properties All ${key} expressions in the xml file will be replaced be
466
* the corresponding value from the properties
467
* @throws java.io.IOException on any I/O Error
468
* @throws javax.xml.parsers.ParserConfigurationException if a SAX parser has an invalid configuration
469
* @throws org.xml.sax.SAXException on any parse error
470
* @return the gridengine object
472
public static Object read(File file, Map properties) throws IOException, ParserConfigurationException, SAXException {
473
// Use an instance of ourselves as the SAX event handler
474
RootHandler handler = new RootHandler(properties);
476
// Use the default (non-validating) parser
477
SAXParserFactory factory = SAXParserFactory.newInstance();
480
SAXParser saxParser = factory.newSAXParser();
482
saxParser.parse(file, handler);
484
return handler.getObject();
488
* Read a XML definition of a gridengine object from a <code>File</code>.
490
* @param file the file
491
* @throws java.io.IOException on any I/O Error
492
* @throws javax.xml.parsers.ParserConfigurationException if a SAX parser has an invalid configuration
493
* @throws org.xml.sax.SAXException on any parse error
494
* @return the gridengine object
496
public static Object read(File file) throws IOException, ParserConfigurationException, SAXException {
497
return read(file, null);
501
* Read a XML definition of a gridengine object from a <code>Reader</code>.
503
* @param rd the reader
504
* @throws java.io.IOException on any I/O Error
505
* @throws javax.xml.parsers.ParserConfigurationException if a SAX parser has an invalid configuration
506
* @throws org.xml.sax.SAXException on any parse error
507
* @return the gridengine object
509
public static Object read(Reader rd, Map properties) throws IOException, ParserConfigurationException, SAXException {
511
// Use an instance of ourselves as the SAX event handler
512
RootHandler handler = new RootHandler(properties);
514
// Use the default (non-validating) parser
515
SAXParserFactory factory = SAXParserFactory.newInstance();
518
SAXParser saxParser = factory.newSAXParser();
519
InputSource source = new InputSource(rd);
520
saxParser.parse(source, handler);
522
return handler.getObject();
526
* Read a XML definition of a gridengine object from a <code>Reader</code>.
528
* @param rd the reader
529
* @throws java.io.IOException on any I/O Error
530
* @throws javax.xml.parsers.ParserConfigurationException if a SAX parser has an invalid configuration
531
* @throws org.xml.sax.SAXException on any parse error
532
* @return the gridengine object
534
public static Object read(Reader rd) throws IOException, ParserConfigurationException, SAXException {
535
return read(rd, null);
539
static class RootHandler extends DefaultHandler {
541
private Map properties;
543
private Stack stack = new Stack();
545
private Object rootObject;
547
private Locator locator;
549
public RootHandler() {
553
public RootHandler(Map properties) {
554
this.properties = properties;
557
public Object getObject() {
561
public void setDocumentLocator(Locator locator) {
562
this.locator = locator;
565
public void startElement(String uri, String localName, String qName, org.xml.sax.Attributes attributes) throws org.xml.sax.SAXException {
567
if (logger.isLoggable(Level.FINEST)) {
568
logger.finest("startElement: uri = " + uri + ", localName = " + localName +
569
" qName = " + qName);
571
if (stack.isEmpty()) {
572
GEObjectHandler handler = new GEObjectHandler(qName);
573
rootObject = handler.getObject();
576
CullHandler handler = (CullHandler)stack.peek();
578
handler = handler.getHandler(qName, attributes);
583
public void endElement(String uri, String localName, String qName) throws SAXException {
585
if (logger.isLoggable(Level.FINEST)) {
586
logger.finest("endElement: uri = " + uri + ", localName = " + localName +
587
" qName = " + qName);
589
CullHandler handler = (CullHandler)stack.pop();
590
handler.endElement(uri, localName, qName);
593
public void characters(char[] ch, int start, int length) throws org.xml.sax.SAXException {
595
if (logger.isLoggable(Level.FINEST)) {
596
logger.finest("characters: '" + new String(ch, start, length) + "'");
599
if (properties != null) {
600
ch = resolveResources(ch, start, length);
604
CullHandler handler = (CullHandler)stack.peek();
605
handler.characters(ch, start, length);
608
char[] resolveResources(char[] ch, int start, int length) throws SAXException {
609
int end = start + length;
611
StringBuilder ret = new StringBuilder();
621
// we found the beginning of a property
624
throw new SAXException("Unclosed property in " + new String(ch, start, length));
636
throw new SAXException("Unclosed property in " + new String(ch, start, length));
638
String property = new String(ch, startIndex, endIndex - startIndex);
640
Object value = properties.get(property);
642
logger.fine("Replace property " + property + " with value " + value);
656
return ret.toString().toCharArray();
659
abstract class CullHandler extends DefaultHandler {
661
protected CullHandler parent;
663
public CullHandler(CullHandler parent) {
664
this.parent = parent;
667
public CullHandler getParent() {
671
public abstract CullHandler getHandler(String name, org.xml.sax.Attributes attributes) throws SAXException;
674
abstract class AbstractObjectHandler extends CullHandler {
678
public AbstractObjectHandler(CullPropertyHandler parent, String name) {
683
public String getName() {
687
public abstract Object getObject();
689
public void endElement(String uri, String localName, String qName) throws SAXException {
690
if (parent != null) {
691
((CullPropertyHandler)parent).addObject(getObject());
697
class GEObjectHandler extends AbstractObjectHandler {
699
private ClassDescriptor cd;
702
public GEObjectHandler(String name) throws SAXException {
706
public Object getObject() {
710
public GEObjectHandler(CullPropertyHandler parent, String name) throws SAXException {
713
cd = Util.getDescriptorForCullType(name);
714
} catch(IllegalArgumentException ilae) {
715
throw new SAXParseException("No descriptor for cull type " + name + " found", locator, ilae);
717
obj = cd.newInstance();
720
public CullHandler getHandler(String name, org.xml.sax.Attributes attributes) throws SAXException {
721
PropertyDescriptor pd = cd.getProperty(name);
723
throw new SAXParseException("cull type " + cd.getCullName() + " has no property " + name, locator);
724
} if (pd instanceof SimplePropertyDescriptor) {
725
if (pd.getPropertyType().isPrimitive() || pd.getPropertyType().equals(String.class)) {
726
return new SimplePropertyHandler(this, (SimplePropertyDescriptor)pd);
728
return new ObjectPropertyHandler(this, (SimplePropertyDescriptor)pd);
730
} else if (pd instanceof ListPropertyDescriptor) {
731
return new ListPropertyHandler(this, (ListPropertyDescriptor)pd);
732
} else if (pd instanceof MapPropertyDescriptor) {
733
return new MapPropertyHandler(this, (MapPropertyDescriptor)pd, attributes);
734
} else if (pd instanceof MapListPropertyDescriptor) {
735
return new MapListPropertyHandler(this, (MapListPropertyDescriptor)pd, attributes);
737
throw new SAXParseException("Unknown property type " + pd.getClass(), locator);
741
private StringBuilder value;
743
public void characters(char[] ch, int start, int length) throws SAXException {
746
value = new StringBuilder();
748
value.append(ch,start,length);
752
public void endElement(String uri, String localName, String qName) throws SAXException {
754
String str = value.toString().trim();
755
if (str.length()>0) {
756
if (NONE.equalsIgnoreCase(str)) {
757
if (parent instanceof MapListPropertyHandler) {
758
((MapListPropertyHandler)parent).addEmptyObject();
759
} else if (parent instanceof ListPropertyHandler) {
760
// empty list and null has the same meaning
762
throw new SAXException("object can only have content if parent is a map list or a list (2)");
765
super.endElement(uri, localName, qName);
768
super.endElement(uri, localName, qName);
774
class StringHandler extends AbstractObjectHandler {
776
private StringBuilder buffer;
778
public StringHandler(CullPropertyHandler parent, String name) {
782
public CullHandler getHandler(String name, org.xml.sax.Attributes attributes) throws SAXException {
783
throw new SAXException("String handler " + getName() + " does not support sub element " + name);
786
public void characters(char[] ch, int start, int length) throws SAXException {
787
if (buffer == null) {
788
buffer = new StringBuilder();
790
buffer.append(ch,start, length);
793
public void endElement(String uri, String localName, String qName) throws SAXException {
794
if (buffer == null) {
795
throw new SAXException("A string tag must not be empty");
797
super.endElement(uri, localName, qName);
801
public Object getObject() {
802
if (buffer != null) {
803
String ret = buffer.toString();
804
if (NONE.equalsIgnoreCase(ret)) {
810
throw new IllegalStateException("A string tag must not be empty");
815
class IntHandler extends StringHandler {
816
public IntHandler(CullPropertyHandler parent, String name) {
820
public Object getObject() {
821
return new Integer((String)super.getObject());
825
class LongHandler extends StringHandler {
826
public LongHandler(CullPropertyHandler parent, String name) {
830
public Object getObject() {
831
return new Long((String)super.getObject());
835
class ShortHandler extends StringHandler {
836
public ShortHandler(CullPropertyHandler parent, String name) {
840
public Object getObject() {
841
return new Short((String)super.getObject());
845
class DoubleHandler extends StringHandler {
846
public DoubleHandler(CullPropertyHandler parent, String name) {
850
public Object getObject() {
851
return new Double((String)super.getObject());
855
class FloatHandler extends StringHandler {
856
public FloatHandler(CullPropertyHandler parent, String name) {
860
public Object getObject() {
861
return new Float((String)super.getObject());
865
class BooleanHandler extends StringHandler {
866
public BooleanHandler(CullPropertyHandler parent, String name) {
870
public Object getObject() {
871
return new Boolean((String)super.getObject());
876
abstract class CullPropertyHandler extends CullHandler {
879
public CullPropertyHandler(GEObjectHandler parent) {
881
if (parent == null) {
882
throw new NullPointerException("parent is null");
886
public abstract void addObject(Object obj) throws SAXException;
888
public CullHandler getHandler(String name, org.xml.sax.Attributes attributes) throws SAXException {
889
if (STRING_TAG.equals(name)) {
890
return new StringHandler(this, name);
891
} else if (DOUBLE_TAG.equals(name)) {
892
return new DoubleHandler(this, name);
893
} else if (BOOLEAN_TAG.equals(name)) {
894
return new BooleanHandler(this, name);
895
} else if (INT_TAG.equals(name)) {
896
return new IntHandler(this, name);
897
} else if (LONG_TAG.equals(name)) {
898
return new LongHandler(this, name);
899
} else if (SHORT_TAG.equals(name)) {
900
return new ShortHandler(this,name);
901
} else if (FLOAT_TAG.equals(name)) {
902
return new FloatHandler(this, name);
904
return new GEObjectHandler(this, name);
909
class SimplePropertyHandler extends CullPropertyHandler {
910
private SimplePropertyDescriptor pd;
911
private StringBuilder value;
913
public SimplePropertyHandler(GEObjectHandler parent, SimplePropertyDescriptor pd) {
916
throw new NullPointerException("pd is null");
921
public void addObject(Object obj) throws SAXException {
922
if (!pd.isReadOnly()) {
923
if (((GEObjectHandler)parent).getObject() == null) {
924
throw new SAXException("parent " + ((GEObjectHandler)parent).getName() + " has no value");
926
pd.setValue(((GEObjectHandler)parent).getObject(), obj);
930
public void characters(char[] ch, int start, int length) throws SAXException {
932
value = new StringBuilder();
934
value.append(ch,start,length);
937
public void endElement(String uri, String localName, String qName) throws SAXException {
940
// We have a simple element
941
String str = value.toString();
942
addObject(parse(str, pd));
947
class ObjectPropertyHandler extends CullPropertyHandler {
948
private SimplePropertyDescriptor pd;
950
public ObjectPropertyHandler(GEObjectHandler parent, SimplePropertyDescriptor pd) {
955
public void addObject(Object obj) throws SAXException {
956
if (((GEObjectHandler)parent).getObject() == null) {
957
throw new SAXException("parent " + ((GEObjectHandler)parent).getName() + " has not object");
959
pd.setValue(((GEObjectHandler)parent).getObject(), obj);
963
class ListPropertyHandler extends CullPropertyHandler {
964
private ListPropertyDescriptor pd;
966
public ListPropertyHandler(GEObjectHandler parent, ListPropertyDescriptor pd) {
971
public void addObject(Object obj) throws SAXException {
972
if (((GEObjectHandler)parent).getObject() == null) {
973
throw new SAXException("parent " + ((GEObjectHandler)parent).getName() + " has not object");
975
pd.add(((GEObjectHandler)parent).getObject(), obj);
979
class MapPropertyHandler extends CullPropertyHandler {
980
private MapPropertyDescriptor pd;
981
private StringBuilder value;
984
public MapPropertyHandler(GEObjectHandler parent, MapPropertyDescriptor pd, org.xml.sax.Attributes attributes) {
987
this.key = attributes.getValue("key");
990
public CullHandler getHandler(String name, org.xml.sax.Attributes attributes) throws SAXException {
991
if (!pd.getCullType().equals(name)) {
992
throw new SAXParseException("This handler can only handle object of type " + pd.getCullType(), locator);
994
return super.getHandler(name, attributes);
997
public void addObject(Object obj) throws SAXException {
998
if (((GEObjectHandler)parent).getObject() == null) {
999
throw new SAXException("parent " + ((GEObjectHandler)parent).getName() + " has no object");
1003
pd.put(((GEObjectHandler)parent).getObject(), key, "NONE");
1005
pd.put(((GEObjectHandler)parent).getObject(), key, obj);
1009
public void characters(char[] ch, int start, int length) throws SAXException {
1010
if (value == null) {
1011
value = new StringBuilder();
1013
value.append(ch,start,length);
1016
public void endElement(String uri, String localName, String qName) throws SAXException {
1017
if (value != null) {
1018
// We have a simple element
1019
String str = value.toString();
1020
if (NONE.equalsIgnoreCase(str)) {
1023
addObject(parse(value.toString(), pd));
1029
class MapListPropertyHandler extends CullPropertyHandler {
1030
private MapListPropertyDescriptor pd;
1032
private StringBuilder value;
1034
public MapListPropertyHandler(GEObjectHandler parent, MapListPropertyDescriptor pd, org.xml.sax.Attributes attributes) {
1037
this.key = attributes.getValue("key");
1040
public CullHandler getHandler(String name, org.xml.sax.Attributes attributes) throws SAXException {
1041
return super.getHandler(name, attributes);
1044
public void addObject(Object obj) throws SAXException {
1046
throw new NullPointerException("pd is null");
1048
if (parent == null) {
1049
throw new NullPointerException("parent is null");
1051
if (((GEObjectHandler)parent).getObject() == null) {
1052
throw new SAXException("parent " + ((GEObjectHandler)parent).getName() + " has not object");
1055
pd.add(((GEObjectHandler)parent).getObject(), key, obj);
1062
public void addEmptyObject() throws SAXException {
1064
throw new NullPointerException("pd is null");
1066
if (parent == null) {
1067
throw new NullPointerException("parent is null");
1069
if (((GEObjectHandler)parent).getObject() == null) {
1070
throw new SAXException("parent " + ((GEObjectHandler)parent).getName() + " has no object");
1072
pd.addEmpty(((GEObjectHandler)parent).getObject(), key);
1079
* parse methode for primitive and string elements
1080
* @param value the value which should be parsed
1081
* @param PropertyDescriptor descriptor of the property
1082
* @throws org.xml.sax.SAXException the value could not be parsed
1083
* @return the parsed value
1085
private Object parse(String value, PropertyDescriptor pd) throws SAXException {
1086
Class clazz = pd.getPropertyType();
1087
if (pd.hasCullWrapper()) {
1089
ClassDescriptor realClassDescriptor = Util.getDescriptor(pd.getPropertyType());
1091
Object obj = realClassDescriptor.newInstance();
1093
PropertyDescriptor rpd = realClassDescriptor.getPropertyByCullFieldName(pd.getCullContentField());
1095
if (rpd instanceof SimplePropertyDescriptor) {
1096
((SimplePropertyDescriptor)rpd).setValue(obj, parse(value, rpd));
1099
throw new SAXParseException("Can only handle simple wrapped properties", locator);
1101
} else if (Boolean.TYPE.isAssignableFrom(clazz)) {
1102
return new Boolean(value);
1103
} else if (Integer.TYPE.isAssignableFrom(clazz)) {
1105
return new Integer(value);
1106
} catch(NumberFormatException nfe) {
1107
throw new SAXParseException("'" + value + "' is not a valid int value", locator, nfe);
1109
} else if (Long.TYPE.isAssignableFrom(clazz)) {
1111
return new Long(value);
1112
} catch(NumberFormatException nfe) {
1113
throw new SAXParseException("'" + value + "' is not a valid long value", locator, nfe);
1115
} else if (Float.TYPE.isAssignableFrom(clazz)) {
1117
return new Float(value);
1118
} catch(NumberFormatException nfe) {
1119
throw new SAXParseException("'" + value + "' is not a valid float value", locator, nfe);
1121
} else if (Double.TYPE.isAssignableFrom(clazz)) {
1123
return new Double(value);
1124
} catch(NumberFormatException nfe) {
1125
throw new SAXParseException("'" + value + "' is not a valid double value", locator, nfe);
1127
} else if (String.class.isAssignableFrom(clazz)) {
1130
throw new SAXParseException("Can not parse object of type " + clazz, locator);