KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ozoneDB > core > xml > XML2ObjectContentHandler


1 // You can redistribute this software and/or modify it under the terms of
2
// the Ozone Core License version 1 published by ozone-db.org.
3
//
4
// The original code and portions created by SMB are
5
// Copyright (C) 1997-@year@ by SMB GmbH. All rights reserved.
6
//
7
// $Id: XML2ObjectContentHandler.java,v 1.4 2002/12/29 11:15:57 per_nyfelt Exp $
8

9 package org.ozoneDB.core.xml;
10
11 import org.ozoneDB.OzoneProxy;
12 import org.ozoneDB.OzoneClassNotFoundException;
13 import org.ozoneDB.core.ObjectID;
14 import org.ozoneDB.core.Env;
15 import org.xml.sax.*;
16
17 import java.lang.reflect.Array JavaDoc;
18 import java.lang.reflect.Field JavaDoc;
19 import java.util.*;
20 /**
21  * This class handles the XML and transform it into an Object.
22  *
23  * @version $Revision: 1.4 $
24  * @author <a HREF="http://www.softwarebuero.de">SMB</a>
25  */

26 public class XML2ObjectContentHandler implements ContentHandler, Consts {
27
28     public static final boolean debug = false;
29
30     //
31
// member
32
//
33

34     /**
35      */

36     protected Locator locator;
37
38     /**
39      * Cache for the ref-elements.
40      */

41     protected static Hashtable objCache = new Hashtable();
42
43     /**
44      * All objs, members, values etc are saved in this stack.
45      */

46     protected Stack stack;
47
48     /**
49      * CH (ContentHandler) is for handling a special part of the XML
50      * in a special ContentHandler. (e.g. HashtableContentHandler)
51      */

52     protected XML2ObjectContentHandler CH = null;
53
54     /**
55      */

56     protected XML2ObjectDelegate delegate;
57
58     //
59
// construcor
60
//
61

62     /**
63      */

64     public XML2ObjectContentHandler() {
65         CH = this;
66     }
67
68     /**
69      */

70     public XML2ObjectContentHandler(XML2ObjectDelegate delegate) {
71         this();
72         this.delegate = delegate;
73     }
74
75     //
76
// methods
77
//
78

79     /**
80      * The method setDocumentLocator sets the locator.
81      *
82      * @param locator
83      */

84     public void setDocumentLocator(Locator locator) {
85         this.locator = locator;
86     }
87
88     /**
89      * Start Document parsing.
90      */

91     public void startDocument() {
92         if (debug) {
93             System.out.println("Start parsing ..");
94         }
95     }
96
97     /**
98      */

99     public void endDocument() {
100         if (debug) {
101             System.out.println("End parsing ..");
102         }
103     }
104
105     /**
106      * The method startElement handels all startElements.
107      * It refers to the methods, which process the individual startElement
108      * in detail.
109      *
110      * @param namespaceURI
111      * @param localName
112      * @param rawName (the tagname)
113      * @param atts (the attributes of the tag)
114      */

115     public void startElement (String JavaDoc namespaceURI, String JavaDoc localName,
116                               String JavaDoc rawName, Attributes atts) {
117
118         if (rawName.equals(TAG_OBJ)) {
119             CH.objStartElement(atts);
120         } else if (rawName.equals(TAG_MEMBER)) {
121             CH.memberStartElement(atts);
122         } else if (rawName.equals(TAG_VALUE)) {
123             CH.valueStartElement(atts);
124         } else if (rawName.equals(TAG_VALUEOBJ)) {
125             CH.valueObjStartElement(atts);
126         } else if (rawName.equals(TAG_VALUEARRAY)) {
127             CH.valueArrayStartElement(atts);
128         } else if (rawName.equals(TAG_SUPERCLASS)) {
129             CH.superclassStartElement(atts);
130         }
131     }
132
133     /**
134      * The method endElement handles all endElements.
135      * It refers to the methods, which process the individual endElement
136      * in detail.
137      *
138      * @param namespaceURI
139      * @param localName
140      * @param rawName (the tagname)
141      */

142     public void endElement (String JavaDoc namespaceURI, String JavaDoc localName,
143                             String JavaDoc rawName) {
144
145         if (CH.stack.size() <= 1 && rawName.equals(TAG_VALUEOBJ)) {
146
147             Hashtable hash = (Hashtable)CH.stack.pop();
148
149             CH = this;
150             stack.push(hash);
151         }
152
153         if (rawName.equals(TAG_OBJ)) {
154             CH.objEndElement();
155         } else if (rawName.equals(TAG_MEMBER)) {
156             CH.memberEndElement();
157         } else if (rawName.equals(TAG_VALUE)) {
158             CH.valueEndElement();
159         } else if (rawName.equals(TAG_VALUEOBJ)) {
160             CH.valueObjEndElement();
161         } else if (rawName.equals(TAG_VALUEARRAY)) {
162             CH.valueArrayEndElement();
163         } else if (rawName.equals(TAG_SUPERCLASS)) {
164             CH.superclassEndElement();
165         }
166     }
167
168     /**
169      * The method characters handles the text-elements.
170      *
171      * @param ch (char-array)
172      * @param start (start of the array)
173      * @param end (end of the array)
174      */

175     public void characters (char[] ch, int start, int end) {
176         CH.values(ch, start, end);
177     }
178
179     /**
180      */

181     public void processingInstruction(String JavaDoc target, String JavaDoc data) {
182         if (debug) {
183             System.out.println("Target: " + target + " Data: " + data);
184         }
185     }
186
187     /**
188      */

189     public void startPrefixMapping (String JavaDoc prefix, String JavaDoc uri) {
190         if (debug) {
191             System.out.println("Prefix: " + prefix + " Uri: " + uri);
192         }
193     }
194
195     /**
196      */

197     public void endPrefixMapping (String JavaDoc prefix) {
198         if (debug) {
199             System.out.println("EndPrefix: " + prefix);
200         }
201     }
202
203     /**
204      */

205     public void ignorableWhitespace (char[] ch, int start, int end) {
206         if (debug) {
207             String JavaDoc s = new String JavaDoc(ch,start,end);
208             System.out.println("Ignor chars: "+s);
209         }
210     }
211
212     /**
213      */

214     public void skippedEntity (String JavaDoc name) {
215         if (debug) {
216             System.out.println("Skip entity "+name);
217         }
218     }
219
220
221     //
222
// handle elements
223
//
224

225     /**
226      * The method objStartElement refers to handleObjStartElement.
227      * @param atts (the attributes)
228      */

229     protected void objStartElement(Attributes atts) {
230         handleObjStartElement(atts);
231     }
232
233     /**
234      * The method memberStartElement refers to handleMemberStartElement.
235      * @param atts (the attributes)
236      */

237     protected void memberStartElement(Attributes atts) {
238         handleMemberStartElement(atts);
239     }
240
241     /**
242      * The method valueStartElement refers to handleValueStartElement.
243      * @param atts (the attributes)
244      */

245     protected void valueStartElement(Attributes atts) {
246         handleValueStartElement(atts);
247     }
248
249     /**
250      * The method valueObjStartElement refers to handleValueObjStartElement.
251      * @param atts (the attributes)
252      */

253     protected void valueObjStartElement(Attributes atts) {
254         handleValueObjStartElement(atts);
255     }
256
257     /**
258      * The method valueArrayStartElement refers to handleValueArrayStartElement.
259      * @param atts (the attributes)
260      */

261     protected void valueArrayStartElement(Attributes atts) {
262         handleValueArrayStartElement(atts);
263     }
264
265     /**
266      * The method superclassStartElement refers to handleSuperclassStartElement.
267      * @param atts (the attributes)
268      */

269     protected void superclassStartElement(Attributes atts) {
270         handleSuperclassStartElement(atts);
271     }
272
273     /**
274      * The method values refers to handleValues.
275      *
276      * @param ch (char-array)
277      * @param start (start of the array)
278      * @param end (end of the array)
279      */

280     protected void values(char[] ch, int start, int end) {
281         handleValues(ch, start, end);
282     }
283
284     /**
285      * The method objEndElement refers to handleObjEndElement.
286      */

287     protected void objEndElement() {
288         handleObjEndElement();
289     }
290
291     /**
292      * The method memberEndElement refers to handleMemberEndElement.
293      */

294     protected void memberEndElement() {
295         handleMemberEndElement ();
296     }
297
298     /**
299      * The method valueEndElement refers to handleValueEndElement.
300      */

301     protected void valueEndElement() {
302         handleValueEndElement();
303     }
304
305     /**
306      * The method valueObjEndElement refers to handleValueObjEndElement.
307      */

308     protected void valueObjEndElement() {
309         handleValueObjEndElement();
310     }
311
312     /**
313      * The method valueArrayEndElement refers to handleValueArrayEndElement.
314      */

315     protected void valueArrayEndElement() {
316         handleValueArrayEndElement();
317     }
318
319     /**
320      * The method superclassEndElement refers to handleSuperclassEndElement.
321      */

322     protected void superclassEndElement() {
323         handleSuperclassEndElement();
324     }
325
326
327     /**
328      * The method handleObjStartElement creates a new object
329      * and put it in the stack.
330      *
331      * @param atts (the attributes)
332      */

333     protected void handleObjStartElement(Attributes atts) {
334         ObjElement oe;
335
336         try {
337
338             stack = new Stack();
339             oe = new ObjElement(atts);
340
341             stack.push(oe);
342             objCache.put(oe.getId(), oe.getObject());
343
344         } catch (ClassNotFoundException JavaDoc cnfe) {
345             System.err.println("handleObjStartElement: " + cnfe);
346         } catch (InstantiationException JavaDoc ie) {
347             System.err.println("handleObjStartElement: " + ie);
348         } catch (IllegalAccessException JavaDoc iae) {
349             System.err.println("handleObjStartElement: " + iae);
350         }
351     }
352
353     /**
354      * The method handleObjEndElement gets the finished Object from the stack.
355      */

356     protected void handleObjEndElement() {
357         ObjElement oe = (ObjElement)stack.pop();
358         delegate.handleObject(oe);
359     }
360
361     /**
362      * This methode handles an OzoneProxy member.
363      *
364      * @param atts
365      */

366     protected void handleOzoneProxyMember(Attributes atts) {
367         String JavaDoc proxyType = atts.getValue(ATTR_PROXY_TYPE);
368         String JavaDoc href = atts.getValue(ATTR_XLINK_HREF_RAW);
369
370         ObjectID objID = new ObjectID((new Long JavaDoc(href)).longValue());
371
372         try {
373
374             Class JavaDoc proxyClass = Thread.currentThread().getContextClassLoader().loadClass(proxyType);
375             OzoneProxy proxy = (OzoneProxy)proxyClass.newInstance();
376
377             Field JavaDoc remoteID = proxyClass.getField( REMOTE_ID );
378             remoteID.set(proxy, objID);
379
380             ValueObjElement voe = new ValueObjElement(proxy);
381             stack.push(voe);
382
383         } catch (ClassNotFoundException JavaDoc cnfe) {
384             System.err.println("handleOzoneProxyMember: " + cnfe);
385         } catch (InstantiationException JavaDoc ie) {
386             System.err.println("handleOzoneProxyMember: " + ie);
387         } catch (IllegalAccessException JavaDoc iae) {
388             System.err.println("handleOzoneProxyMember: " + iae);
389         } catch (NoSuchFieldException JavaDoc nsfe) {
390             System.err.println("handleOzoneProxyMember: " + nsfe);
391         }
392     }
393
394     /**
395      * The method handleMemberStartElement creates a MemberElement
396      * and put it the stack.
397      *
398      * @param atts (the attributes)
399      */

400     protected void handleMemberStartElement(Attributes atts) {
401         MemberElement me = new MemberElement(atts);
402         stack.push(me);
403
404         if (atts.getValue(ATTR_PROXY_TYPE) != null) //member is an OzoneProxy
405
handleOzoneProxyMember(atts);
406     }
407
408     /**
409      * The method handleMemberEndElement gets the finished MemberElement and the value
410      * from the stack and put it in the object.
411      */

412     protected void handleMemberEndElement() {
413         Object JavaDoc value = null;
414
415         if (stack.peek() instanceof ValueObjElement)
416             value = ((ValueObjElement)stack.pop()).getObject();
417         else if (stack.peek() instanceof MemberElement)
418             value = null;
419         else
420             value = stack.pop();
421
422         if (stack.peek() instanceof MemberElement) {
423
424             MemberElement me = (MemberElement)stack.pop();
425
426             Object JavaDoc obj = stack.peek();
427             Class JavaDoc objClass = obj.getClass();
428
429             if (stack.peek() instanceof ObjElement) {
430                 obj = ((ObjElement)stack.peek()).getObject();
431                 objClass = obj.getClass();
432             } else if (stack.peek() instanceof ValueObjElement) {
433                 obj = ((ValueObjElement)stack.peek()).getObject();
434                 objClass = obj.getClass();
435             } else if (stack.peek() instanceof SuperclassElement) {
436                 obj = ((SuperclassElement)stack.peek()).getObject();
437                 objClass = ((SuperclassElement)stack.peek()).getSuperclass();
438             }
439
440             try {
441
442                 Field JavaDoc fd = objClass.getDeclaredField(me.getName());
443                 fd.setAccessible(true);
444
445                 fd.set(obj, value);
446
447             } catch (NoSuchFieldException JavaDoc nsfe) { // wrong Class
448
System.err.println("handleMemberEndElement: " + nsfe);
449             } catch (IllegalAccessException JavaDoc iae) { //no access for this member
450
System.err.println("handleMemberEndElement: " + iae);
451             }
452         }
453     }
454
455     /**
456      * The method handleValueStartElement creates a ValueElement
457      * and put it the stack.
458      *
459      * @param atts (the attributes)
460      */

461     protected void handleValueStartElement(Attributes atts) {
462         String JavaDoc ref = atts.getValue(ATTR_REF);
463         if (ref != null) {
464             Object JavaDoc refObj = objCache.get(ref);
465             stack.push(new RefElement(refObj));
466             return;
467         }
468
469         ValueElement ve = new ValueElement(atts);
470
471         stack.push(ve);
472     }
473
474     /**
475      * The method handleValues gets the ValueElement from the stack.
476      * All values in the ValueElement are saved as String.
477      *
478      * @param ch (char-array)
479      * @param start (start of the array)
480      * @param end (end of the array)
481      */

482     public void handleValues (char[] ch, int start, int end) {
483         if (stack == null || stack.empty())
484             return;
485
486         if ((stack.peek() instanceof ValueElement)) {
487             String JavaDoc value = new String JavaDoc(ch,start,end);
488             ValueElement ve = (ValueElement)stack.pop();
489
490             if (ve.getStrValue() != null) // if ValueElement.value has already a value,
491
value = ve.getStrValue() + value; // then append (new)value to (old)value
492

493             ve.setStrValue(value);
494
495             stack.push(ve);
496         }
497     }
498
499     /**
500      * The method handleValueEndElement gets the ValueElement from the stack.
501      * It casts the value (as String) in the special type and put the real
502      * value in the stack back.
503      */

504     protected void handleValueEndElement() {
505         Object JavaDoc value;
506
507         if (stack.peek() instanceof RefElement) {
508             value = ((RefElement)(stack.pop())).getRefObj();
509
510         } else {
511             ValueElement ve = (ValueElement) stack.pop();
512
513             if (ve.getStrValue() == null) value = null;
514             else {
515                 value = castValue (ve.getType(), ve.getStrValue());
516                 objCache.put(ve.getId(), value);
517             }
518         }
519
520         stack.push(value);
521     }
522
523     /**
524      * The method castValues casts the valueString into the real type.
525      *
526      * @param type (the type)
527      * @param valueString (start of the array)
528      */

529     protected Object JavaDoc castValue(String JavaDoc type, String JavaDoc valueString) {
530
531         if ((type.equals("java.lang.Boolean")) ||
532             (type.equals("boolean"))) {
533             Boolean JavaDoc value = new Boolean JavaDoc(valueString);
534             return value;
535         }
536
537         if ((type.equals("java.lang.Byte")) ||
538             (type.equals("byte"))) {
539             Byte JavaDoc value = new Byte JavaDoc(valueString);
540             return value;
541         }
542
543         if ((type.equals("java.lang.Character")) ||
544             (type.equals("char"))) {
545             Character JavaDoc value = new Character JavaDoc(valueString.charAt(0));
546             return value;
547         }
548
549         if ((type.equals("java.lang.Short")) ||
550             (type.equals("short"))) {
551             Short JavaDoc value = new Short JavaDoc(valueString);
552             return value;
553         }
554
555         if ((type.equals("java.lang.Integer")) ||
556             (type.equals("int"))) {
557             Integer JavaDoc value = new Integer JavaDoc(valueString);
558             return value;
559         }
560
561         if ((type.equals("java.lang.Long")) ||
562             (type.equals("long"))) {
563             Long JavaDoc value = new Long JavaDoc(valueString);
564             return value;
565         }
566
567         if ((type.equals("java.lang.Float")) ||
568             (type.equals("float"))) {
569             Float JavaDoc value = new Float JavaDoc(valueString);
570             return value;
571         }
572
573         if ((type.equals("java.lang.Double")) ||
574             (type.equals("double"))) {
575             Double JavaDoc value = new Double JavaDoc(valueString);
576             return value;
577         }
578
579         if (type.equals("java.lang.String")) {
580             String JavaDoc value = new String JavaDoc(valueString);
581             return value;
582         }
583
584         return null;
585     }
586
587     /**
588      * The method handleValueObjStartElement creates a ValueObjElement
589      * and put it in the stack.
590      * If the valueObj is from type java.util.Hashtable:
591      * -> new ContentHandler which creates a Hashtable
592      * (It is because of can not creating innerClasses!!)
593      *
594      * @param atts (the attributes)
595      */

596     protected void handleValueObjStartElement(Attributes atts) {
597         String JavaDoc ref = atts.getValue("ref");
598         if (ref != null) {
599             Object JavaDoc refObj = objCache.get(ref);
600             stack.push(refObj);
601             return;
602         }
603
604         try {
605
606             ValueObjElement voe = new ValueObjElement(atts);
607
608             if (voe.getType().equals("java.util.Hashtable")) {
609                 System.out.println("new HashContentHandler");
610                 CH = new HashtableContentHandler(locator, (Hashtable)voe.getObject());
611             } else {
612                 stack.push(voe);
613                 objCache.put(voe.getId(), voe.getObject());
614             }
615
616         } catch (ClassNotFoundException JavaDoc cnfe) {
617             System.err.println("handleValueObjStartElement: " + cnfe);
618         } catch (InstantiationException JavaDoc ie) {
619             System.err.println("handleValueObjStartElement: " + ie);
620         } catch (IllegalAccessException JavaDoc iae) {
621             System.err.println("handleValueObjStartElement: " + iae);
622         }
623     }
624
625     /**
626      */

627     protected void handleValueObjEndElement() {
628     }
629
630     /**
631      * The method handleValueArrayStartElement creates a ValueArrayElement
632      * and put it in the stack.
633      *
634      * @param atts (the attributes)
635      */

636     protected void handleValueArrayStartElement(Attributes atts) {
637         String JavaDoc ref = atts.getValue("ref");
638         if (ref != null) {
639             Object JavaDoc refObj = objCache.get(ref);
640             stack.push(new RefElement(refObj));
641             return;
642         }
643
644         ValueArrayElement vae = new ValueArrayElement(atts);
645         stack.push(vae);
646     }
647
648     /**
649      * The method handleValueArrayEndElement joins the values to an array.
650      */

651     protected void handleValueArrayEndElement() {
652         Object JavaDoc array;
653         if (stack.peek() instanceof RefElement) {
654             array = ((RefElement)(stack.pop())).getRefObj();
655             stack.push(array);
656             return;
657         }
658
659         try {
660             Vector vaValues = new Vector();
661             while (!(stack.peek() instanceof ValueArrayElement)) {
662                 vaValues.addElement(stack.pop());
663             }
664
665             ValueArrayElement vae = (ValueArrayElement) stack.pop();
666
667             // todo: uncommented until verified that it works
668
//Class vaClass = Class.forName(vae.getType()).getComponentType();
669
Class JavaDoc vaClass = Env.currentEnv().classManager.classForName(vae.getType()).getComponentType();
670
671             array = Array.newInstance(vaClass, vaValues.size());
672
673             int k = vaValues.size()-1; // k-- otherwise -> wrong sequence
674
for (int j = 0; j < vaValues.size(); j++, k--) {
675                 Array.set(array, j, vaValues.elementAt(k));
676             }
677
678             objCache.put(vae.getId(), array);
679             stack.push(array);
680
681         } catch (OzoneClassNotFoundException cnfe) {
682             System.err.println("handleValueArrayStartElement: " + cnfe);
683         }
684     }
685
686     /**
687      * The method handleSuperclassStartElement creates a SuperclassElement
688      * and put into the stack.
689      *
690      * @param atts (the attributes)
691      */

692     protected void handleSuperclassStartElement(Attributes atts) {
693         try {
694             Object JavaDoc obj = stack.peek();
695
696             if (stack.peek() instanceof ObjElement) {
697                 obj = ((ObjElement)stack.peek()).getObject();
698             } else if (stack.peek() instanceof ValueObjElement) {
699                 obj = ((ValueObjElement)stack.peek()).getObject();
700             } else if (stack.peek() instanceof SuperclassElement) {
701                 obj = ((SuperclassElement)stack.peek()).getObject();
702             }
703
704             SuperclassElement sce = new SuperclassElement(obj, atts);
705             stack.push(sce);
706         } catch (ClassNotFoundException JavaDoc cnfe) {
707             System.err.println("handleSuperclassStartElement: " + cnfe);
708         }
709     }
710
711     /**
712      * The method handleSuperclassEndElement pop the superclass in the stack.
713      */

714     protected void handleSuperclassEndElement() {
715         stack.pop();
716     }
717
718
719 // ------------ TEST -------------
720
// -- System.out.println(STACK) --
721
// -------------------------------
722
protected void showStack(Stack s) {
723         try {
724             System.out.println("------------------------------ (start)");
725             for (int i=s.size()-1; i>=0; i--) {
726                 System.out.print(" " + i+". " + s.elementAt(i));
727                 System.out.println();
728             }
729             System.out.println("------------------------------ (end)");
730         } catch (Exception JavaDoc e) {
731                 System.err.println("showStack: " + e);
732         }
733     }
734
735 }
736
Popular Tags