KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > dbschema > migration > archiver > deserializer > XMLGraphDeserializer


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.dbschema.migration.archiver.deserializer;
21
22 import java.lang.*;
23 import java.util.*;
24
25 import java.lang.reflect.Field JavaDoc;
26 import java.lang.reflect.Array JavaDoc;
27
28 import org.xml.sax.*;
29
30 public class XMLGraphDeserializer extends BaseSpecificXMLDeserializer implements java.io.Serializable JavaDoc
31 {
32
33     // Fields
34

35     // The following static fields control the various states that this
36
// state machine expects and handles. Some states are currently no-op's
37

38     public static final int XGD_END = 99;
39     public static final int XGD_NEED_ATTRIBUTE = 1;
40     public static final int XGD_NEED_END_ATTR = 2;
41     public static final int XGD_NEED_END_NULLVALUE = 4;
42     public static final int XGD_NEED_END_OBJECT = 5;
43     public static final int XGD_NEED_END_PARAM = 10;
44     public static final int XGD_NEED_END_ROW = 3;
45     public static final int XGD_NEED_END_ROW_ELEMENT = 14;
46     public static final int XGD_NEED_OBJECT = 7;
47     public static final int XGD_NEED_PARAM = 9;
48     public static final int XGD_NEED_ROW = 6;
49     public static final int XGD_NEED_ROW_ELEMENT = 13;
50     public static final int XGD_NEED_ROW_TAG = 12;
51     public static final int XGD_NEED_STRING = 11;
52     public static final int XGD_NEED_VALUE = 8;
53
54     // The following fields maintain various states
55
//@olsen+MBO: used unsynchronized HashMap and ArrayListStack
56
private java.util.HashMap JavaDoc ObjectHash;
57     private ArrayListStack AttrNameStack;
58     public ArrayListStack RowTypeStack;
59     private ArrayListStack RowCountStack;
60 /*
61     private java.util.Hashtable ObjectHash;
62     private java.util.Stack AttrNameStack;
63     public java.util.Stack RowTypeStack;
64     private java.util.Stack RowCountStack;
65 */

66
67     //@olsen+MBO: added field for reflection lookup
68
private HashMap hashedClasses = new HashMap();
69
70
71     // Constructors
72

73     //@lars: added classloader-constructor
74
public XMLGraphDeserializer(ClassLoader JavaDoc cl)
75     {
76         super(cl);
77 //@olsen+MBO: used unsynchronized HashMap and ArrayListStack
78
this.AttrNameStack = new ArrayListStack();
79         this.ObjectHash = new java.util.HashMap JavaDoc(20);
80         this.RowTypeStack = new ArrayListStack();
81         this.RowCountStack = new ArrayListStack();
82 /*
83         this.AttrNameStack = new java.util.Stack();
84         this.ObjectHash = new java.util.Hashtable(20, 0.75F);
85         this.RowTypeStack = new java.util.Stack();
86         this.RowCountStack = new java.util.Stack();
87 */

88     } /*Method-End*/
89
90     public XMLGraphDeserializer()
91     {
92         this (null);
93     }
94
95     //@olsen+MBO: added method for reflection lookup
96
private Field JavaDoc findField(Object JavaDoc lCurrentObj,
97                             final String JavaDoc lCurrentField)
98                             //String lCurrentField)
99
{
100         Field JavaDoc lField = null;
101         Class JavaDoc lClass = lCurrentObj.getClass();
102         HashMap hashedFields = (HashMap)hashedClasses.get(lClass);
103         if (hashedFields == null) {
104             hashedFields = new HashMap();
105             hashedClasses.put(lClass, hashedFields);
106         } else {
107             lField = (Field JavaDoc)hashedFields.get(lCurrentField);
108         }
109
110         for (;
111              lClass != null && lField == null;
112              lClass = lClass.getSuperclass())
113         {
114 /*
115             try {
116                 lField = lClass.getDeclaredField(lCurrentField);
117                 if (lField != null) {
118                     hashedFields.put(lCurrentField, lField);
119                     lField.setAccessible(true);
120                     break;
121                 }
122             } catch (java.lang.NoSuchFieldException ex) {
123             }
124 */

125
126             final Class JavaDoc tmpClass = lClass;
127             lField = (Field JavaDoc)java.security.AccessController.doPrivileged(
128                 new java.security.PrivilegedAction JavaDoc() {
129                     public Object JavaDoc run() {
130                         try {
131                         Field JavaDoc tmpField = tmpClass.getDeclaredField(lCurrentField);
132                         if (tmpField != null) {
133                                 tmpField.setAccessible(true);
134                             }
135                             return tmpField;
136                         } catch (java.lang.NoSuchFieldException JavaDoc ex) {
137                             return null;
138                         }
139                     }
140                 }
141                 );
142                 if (lField != null) {
143                     hashedFields.put(lCurrentField, lField);
144                     break;
145                 }
146
147         }
148         return lField;
149     }
150
151     public void endDocument() throws org.xml.sax.SAXException JavaDoc
152     {
153         super.endDocument();
154
155         this.State = new Integer JavaDoc(this.XGD_END);
156     } /*Method-End*/
157
158     public void endElement(java.lang.String JavaDoc name) throws org.xml.sax.SAXException JavaDoc
159     {
160         // Debug information
161
int lInitialState = this.State.intValue();
162
163         try {
164             if ( name.equals("ARRAY") )
165                 this.popRowCount();
166
167             if (!name.equals("_.ALIAS"))
168             {
169                 switch ( this.State.intValue() )
170                 {
171                 case XGD_NEED_ATTRIBUTE:
172                     this.validateTag(name, "OBJECT", true);
173                     this.popState();
174
175                     break;
176                 case XGD_NEED_END_ATTR:
177                     java.lang.String JavaDoc lFieldName = this.topAttrName();
178                     this.validateTag(this.unescapeName(name), lFieldName, true);
179                     java.lang.Object JavaDoc lObj = this.popObject();
180                     this.setCurrentAttribute(lObj);
181                     this.popAttrName();
182                     this.popState();
183
184                     break;
185                 case XGD_NEED_END_PARAM:
186                     this.validateTag(name, "PARAM", true);
187
188                     break;
189                 case XGD_NEED_END_ROW:
190                     this.validateTag(name, "ROW", true);
191                     this.popState();
192                     java.lang.Object JavaDoc lRow = this.popObject();
193
194                     Object JavaDoc lArrayRef = this.topObject();
195                     if ( lArrayRef instanceof java.util.Collection JavaDoc )
196                     {
197                         java.util.Collection JavaDoc lArray = (java.util.Collection JavaDoc)(lArrayRef);
198                         lArray.add(lRow);
199                     }
200                     else if ( lArrayRef.getClass().isArray() )
201                     {
202                         int lRowNo = this.currentRowCount();
203                         Array.set(lArrayRef, lRowNo, lRow);
204                         this.incrementRowCount();
205                     }
206                     break;
207                 case XGD_NEED_END_NULLVALUE:
208                     this.validateTag(name, "NULLVALUE", true);
209                     this.popState();
210
211                     break;
212                 case XGD_NEED_END_OBJECT:
213                     this.validateTag(name, "OBJECT", true);
214                     this.popState();
215
216                     break;
217
218                 case XGD_NEED_PARAM:
219 // System.out.println("In endElement and this.XGD_NEED_PARAM currently a no op ");
220
this.popState();
221                     break;
222                 case XGD_NEED_ROW:
223                     this.validateTag(name, "ARRAY", true);
224                     this.popState();
225                     this.popRowType();
226
227                     break;
228
229                 case XGD_NEED_VALUE:
230                     this.popState();
231
232                     java.lang.String JavaDoc lValue = null;
233                     java.lang.String JavaDoc lSource = this.getCharacters();
234
235                     int lLength = lSource.length();
236
237                     if (lLength != 0 ||
238                         lLength != 1)
239                     {
240                         lValue = lSource;
241                     }
242
243                     switch ( this.State.intValue() )
244                     {
245                     case XGD_NEED_END_ROW:
246                         this.validateTag(name, "ROW", true);
247
248                         java.util.Collection JavaDoc lArray2 = (java.util.Collection JavaDoc)(this.topObject());
249
250                         java.lang.Object JavaDoc lRow2 = null;
251                         java.lang.Class JavaDoc lRowType;
252
253                         lRowType = this.topRowType();
254
255                         if (lRowType == java.lang.String JavaDoc.class)
256                         {
257                             //java.lang.String lNewString = new String(lValue);
258
java.lang.String JavaDoc lNewString = (lValue);
259                             lArray2.add(lNewString);
260                         }
261                         else
262                         {
263                             lArray2.add(lRow2);
264                         }// end if
265
this.popState();
266                         break;
267                     case XGD_NEED_END_ATTR:
268                         this.validateTag(this.unescapeName(name), this.topAttrName(), true);
269                         this.setCurrentAttribute(lValue);
270                         this.popAttrName();
271                         this.popState();
272                         break;
273                     case XGD_NEED_END_PARAM:
274 // System.out.println("In endElement and this.XGD_NEED_VALUE,XGD_NEED_END_PARAM currently a no op ");
275
this.popState();
276                         this.pushState(this.XGD_NEED_PARAM);
277                         break;
278                         //AddParameter(value, ParameterClass, ParameterSetMethod);
279
}// end case;
280

281                     break;
282                 case XGD_NEED_STRING:
283 // System.out.println("In endElement and this.XGD_NEED_STRING currently a no op ");
284
this.popState();
285                     break;
286                 case XGD_NEED_END_ROW_ELEMENT:
287 // System.out.println("In endElement and this.XGD_NEED_END_ROW_ELEMENT currently a no op ");
288

289                     this.State = new Integer JavaDoc(this.XGD_NEED_ROW_ELEMENT);
290                     break;
291                 case XGD_NEED_ROW_ELEMENT:
292 // System.out.println("In endElement and this.XGD_NEED_ROW_ELEMENT currently a no op ");
293
this.State = new Integer JavaDoc(this.XGD_NEED_ROW_TAG);
294                     break;
295                 case XGD_NEED_ROW_TAG:
296 // System.out.println("In endElement and this.XGD_NEED_ROW_TAG currently a no op ");
297
this.popState();
298
299                     break;
300                 case XGD_NEED_OBJECT:
301                 // Cases we should never see
302
this.unexpectedTag(name, "OBJECT", false);
303
304                     break;
305                 }// end case
306
}// end if
307

308             super.endElement(name);
309         }// end try
310
catch (SAXException lError)
311         {
312             // Dump so debug information if ew get an exception at this point
313
lError.printStackTrace();
314             System.out.println("Exception cause in XMLGraphDeserializer.endElement");
315             System.out.println("Tag being process is " + name + " initial state was " + lInitialState);
316             this.DumpStatus();
317             // Now rethrow the exception
318
throw lError;
319
320         }// end catch
321
catch (RuntimeException JavaDoc lError)
322         {
323             // Dump so debug information if ew get an exception at this point
324
lError.printStackTrace();
325             System.out.println("Exception cause in XMLGraphDeserializer.endElement");
326             System.out.println("Tag being process is " + name + " initial state was " + lInitialState);
327             this.DumpStatus();
328             // Now rethrow the exception
329
throw lError;
330
331         }// end catch
332

333     } /*Method-End*/
334
335     public void startDocument() throws org.xml.sax.SAXException JavaDoc
336     {
337         super.startDocument();
338
339         State = new Integer JavaDoc(this.XGD_END);
340         this.pushState(this.XGD_NEED_OBJECT);
341     } /*Method-End*/
342
343     public void startElement(java.lang.String JavaDoc name, org.xml.sax.AttributeList JavaDoc atts) throws org.xml.sax.SAXException JavaDoc
344     {
345
346         // Debug information
347
int lInitialState = this.State.intValue();
348
349 /* System.out.println("startElement : Current State is " + this.State);
350         System.out.println("startElement : State stack is ");
351         System.out.println(this.StateStack);
352         System.out.println("startElement : State stack finished ");
353         System.out.println("Name is " + name);
354 */

355         try {
356
357             if ( name.equals("ARRAY") )
358                 this.pushRowCount();
359
360             if ( name.equals( "_.ALIAS" ) )
361             {
362                 java.lang.String JavaDoc lName = atts.getValue("NAME");
363                 java.lang.String JavaDoc lAlias = atts.getValue("ALIAS");
364                 this.addActiveAlias(lName, lAlias);
365             }
366             else
367             {
368                 switch ( this.State.intValue() )
369                 {
370                 case XGD_NEED_ATTRIBUTE:
371                     this.readAttributeHeader(name, atts);
372                     break;
373                 case XGD_NEED_ROW_TAG:
374 // System.out.println("In startElement and this.XGD_NEED_ROW_TAG currently a no op ");
375
this.State = new Integer JavaDoc(this.XGD_NEED_ROW_ELEMENT);
376                     break;
377                 // row : XMLRow = new;
378
// row.Elements = new;
379
// row.RowTag = name;
380
// state = XGD_NEED_ROW_ELEMENT;
381
// PushObject(row);
382
case XGD_NEED_ROW_ELEMENT:
383 // System.out.println("In startElement and this.XGD_NEED_ROW_ELEMENT currently a no op ");
384
this.State = new Integer JavaDoc(this.XGD_NEED_END_ROW_ELEMENT);
385                     break;
386                 // elm : XMLRowElement = new;
387
// elm.ElementNm = name;
388
// elm.ClassNm = atts.GetValue('CLASS');
389
// XMLRow(TopObject()).Elements.Add(elm);
390
// PushObject(elm);
391
// state = XGD_NEED_END_ROW_ELEMENT;
392
case XGD_NEED_ROW:
393                     this.validateTag(name, "ROW", false);
394
395                     if ( this.checkSimpleRow(name, atts) )
396                     {
397                         this.pushState(XGD_NEED_END_ROW);
398                     }
399                     else
400                     {
401                         this.pushState(XGD_NEED_END_ROW);
402                         this.pushState(XGD_NEED_VALUE);
403                     }
404                     break;
405                 case XGD_NEED_OBJECT:
406                     this.validateTag(name, "OBJECT" , false);
407                     this.readObjectHeader(name, atts, false);
408                     break;
409                 case XGD_NEED_PARAM:
410 // System.out.println("In startElement and this.XGD_NEED_PARAM currently a no op ");
411
//this.validateTag(name, "PARAM", false);
412
this.popState();
413                     this.pushState(this.XGD_NEED_END_PARAM);
414                     this.pushState(this.XGD_NEED_VALUE);
415                     //className : string = atts.GetValue('CLASS');
416
//if className = NIL then
417
// ParameterClass = NIL;
418
//else
419
// ParameterClass = FindClass(className);
420
//end if;
421
//ParameterSetMethod = atts.GetValue('SETMETHOD');
422
//PopState();
423
//PushState(XGD_NEED_END_PARAM);
424
//PushState(XGD_NEED_VALUE);
425
break;
426                 case XGD_NEED_VALUE:
427                     if (!name.equals("OBJECT") &&
428                         !name.equals("ARRAY") &&
429                         !name.equals("NULLVALUE"))
430                     {
431                         this.unexpectedTag(name, "OBJECT, ARRAY, NULLVALUE", false);
432                     }// end if;
433
this.readValue(name, atts);
434
435                     break;
436
437                 case XGD_NEED_END_ATTR:
438                 // Cases we should never see
439
this.unexpectedTag(name, this.topAttrName(), true);
440                     break;
441                 case XGD_NEED_END_PARAM:
442                     this.unexpectedTag(name, "/PARAM", false);
443                     break;
444                 case XGD_NEED_END_ROW:
445
446                     this.unexpectedTag(name, "/ROW", false);
447                     break;
448                 case XGD_NEED_END_NULLVALUE:
449                     this.unexpectedTag(name, "/NULLVALUE", false);
450                     break;
451                 case XGD_NEED_END_OBJECT:
452                     this.unexpectedTag(name, "/OBJECT", false);
453                     break;
454                 }// end case
455
}// end if
456

457
458             super.startElement(name, atts);
459
460         }// end try
461
catch (SAXException lError)
462         {
463             // Dump so debug information if ew get an exception at this point
464
lError.printStackTrace();
465             System.out.println("Exception cause in XMLGraphDeserializer.startElement");
466             System.out.println("Tag being process is " + name + " initial state was " + lInitialState);
467             System.out.println("Attribute list is :");
468             for ( int i = 0; i < atts.getLength(); i++)
469             {
470                 System.out.println("Attribute " + atts.getName(i) + " Type " + atts.getType(i) + " value " + atts.getValue(i));
471             }
472             this.DumpStatus();
473             // Now rethrow the exception
474
throw lError;
475
476         }// end catch
477
catch (RuntimeException JavaDoc lError)
478         {
479             // Dump so debug information if ew get an exception at this point
480
lError.printStackTrace();
481             System.out.println("Exception cause in XMLGraphDeserializer.startElement");
482             System.out.println("Tag being process is " + name + " initial state was " + lInitialState);
483             System.out.println("Attribute list is :");
484             for ( int i = 0; i < atts.getLength(); i++)
485             {
486                 System.out.println("Attribute " + atts.getName(i) + " Type " + atts.getType(i) + " value " + atts.getValue(i));
487             }
488             this.DumpStatus();
489             // Now rethrow the exception
490
throw lError;
491
492         }// end catch
493
} /*Method-End*/
494
495     public java.lang.Class JavaDoc findClass(java.lang.String JavaDoc name) throws java.lang.ClassNotFoundException JavaDoc
496     {
497         return super.findClass(this.lookupAlias(name));
498     } /*Method-End*/
499
500     public java.lang.String JavaDoc popAttrName()
501     {
502         return (java.lang.String JavaDoc)(AttrNameStack.pop());
503     } /*Method-End*/
504
505     public void processObjectReference(java.lang.String JavaDoc refName) throws org.xml.sax.SAXException JavaDoc
506     {
507         //@olsen+MBO: assign directly
508
java.lang.Object JavaDoc lObj = this.ObjectHash.get(refName);
509
510         if (lObj == null)
511         {
512             //String message = new String("Object " + refName + " could not be found");
513
String JavaDoc message = ("Object " + refName + " could not be found");
514
515             SAXException objError = new SAXException(message);
516
517             throw objError;
518         }
519         this.pushObject(lObj);
520     } /*Method-End*/
521
522     public void pushAttrName(java.lang.String JavaDoc name)
523     {
524         this.AttrNameStack.push(name);
525     } /*Method-End*/
526
527     public void readAttributeHeader(java.lang.String JavaDoc name, org.xml.sax.AttributeList JavaDoc atts)
528     {
529         //@olsen+MBO: intended to compare strings by '==' instead of equals()?
530
if (name == "_.METHOD" || name == "_.CALLBACK")
531         {
532             this.pushAttrName(atts.getValue("NAME"));
533             this.pushAttrName(name);
534             this.pushState(this.XGD_NEED_PARAM);
535             this.ParameterArray.clear();
536         }
537         else
538         {
539             this.pushAttrName(this.unescapeName(name));
540             this.pushState(this.XGD_NEED_END_ATTR);
541             this.pushState(this.XGD_NEED_VALUE);
542         }
543     } /*Method-End*/
544
545     public void readObjectHeader(java.lang.String JavaDoc name, org.xml.sax.AttributeList JavaDoc atts, boolean refOK) throws org.xml.sax.SAXException JavaDoc
546     {
547         boolean lFirstObj = (this.ObjectStack.size() == 0);
548         Object JavaDoc lObj = null;
549         String JavaDoc lIDName = null;
550
551         if (refOK)
552         {
553             java.lang.String JavaDoc lRefName = atts.getValue("REFERENCE");
554             if (lRefName != null)
555             {
556                 this.processObjectReference(lRefName);
557                 State = new Integer JavaDoc(this.XGD_NEED_END_OBJECT);
558                 return;
559             }// end if
560
}// end if;
561

562         // MBO: need to get the object name here
563
lIDName = atts.getValue("ID");
564
565         if (lFirstObj ||
566             !this.useExistingAttribute(atts, topAttrName(), lObj)
567            )
568         {
569             java.lang.String JavaDoc lClassName = atts.getValue("CLASS");
570             if (lClassName == null)
571             {
572                 lObj = null;
573             }
574             else
575             {
576                 java.lang.Class JavaDoc lClass = null;
577                 try
578                 {
579                     lClass = this.findClass(lClassName);
580                     // MBO: special handling for wrapper classes.
581
// We need to postpone the creation of the wrapper
582
// class instance to the point in time where the value
583
// is available, because the value field might be
584
// final. Store all the information that is available
585
// here (class object and object id) into a
586
// WrapperClassHelper and push this on the object
587
// stack. The helper will be replaced later by the
588
// wrapper class instance.
589
if (WrapperClassHelper.isWrapperClass(lClass))
590                         lObj = new WrapperClassHelper(lClass, lIDName);
591                     else
592                         lObj = lClass.newInstance();
593                 }
594                 catch (IllegalAccessException JavaDoc e1)
595                 {
596                     e1.printStackTrace();
597                     //java.lang.String message = new String("Illegal Access to class " + lClass.getName());
598
java.lang.String JavaDoc message = ("Illegal Access to class " + lClass.getName());
599                     SAXException useError = new SAXException(message);
600                     throw useError;
601                 }
602                 catch (InstantiationException JavaDoc e2)
603                 {
604
605                     lObj = NewInstanceHelper.newInstance(lClassName, this.topObject());
606                     if ( lObj == null )
607                     {
608                         e2.printStackTrace();
609
610                         //java.lang.String message = new String("Instantiation exception of class " + lClass.getName());
611
java.lang.String JavaDoc message = ("Instantiation exception of class " + lClass.getName());
612                         SAXException useError = new SAXException(message);
613                         throw useError;
614                     }
615                 }
616                 catch (ClassNotFoundException JavaDoc e3)
617                 {
618                     e3.printStackTrace();
619
620                     //java.lang.String message = new String("Class " + lClass.getName() + " could not be found");
621
java.lang.String JavaDoc message = ("Class " + lClass.getName() + " could not be found");
622                     SAXException useError = new SAXException(message);
623                     throw useError;
624                 }
625             }// end if
626
}// end if
627

628
629         this.pushObject(lObj);
630
631         // handle id
632
if (lIDName != null)
633         {
634             this.ObjectHash.put(lIDName, lObj);
635         }// end if
636

637         if (lFirstObj)
638         {
639             this.MasterDeserializer.setInitialObject(lObj);
640         }// end if
641

642         if (lObj == null)
643         {
644                State = new Integer JavaDoc(this.XGD_NEED_END_OBJECT);
645         }
646         //else if (lobj.IsA(Stringizeable) then
647
// State = XGD_NEED_STRING;
648
//else if obj.IsA(UseXMLRows) then
649
// UseXMLRows(obj).BeginXMLRows(FALSE);
650
// State = XGD_NEED_ROW_TAG;
651
else
652         {
653             State = new Integer JavaDoc(this.XGD_NEED_ATTRIBUTE);
654         }
655     } /*Method-End*/
656
657     public java.lang.String JavaDoc topAttrName()
658     {
659
660         return (java.lang.String JavaDoc)(this.AttrNameStack.peek());
661     } /*Method-End*/
662
663     public void readValue(java.lang.String JavaDoc name, org.xml.sax.AttributeList JavaDoc atts) throws org.xml.sax.SAXException JavaDoc
664     {
665         boolean lFirstObj = (this.ObjectStack.size() == 0);
666
667         if (name.equals("OBJECT"))
668         {
669             this.readObjectHeader(name, atts, true);
670         }
671         else if (name.equals("ARRAY"))
672         {
673             java.lang.Object JavaDoc lObj = null;
674             java.lang.Class JavaDoc lRowClassType = null;
675             java.lang.Object JavaDoc lArray = null;
676
677             // really need a way of working out what type
678
// of array this is but this will do for now
679

680             String JavaDoc lArrayType = atts.getValue("CLASS");
681             int lArraySize = Integer.parseInt(atts.getValue("SIZE"));
682             java.lang.String JavaDoc lRowTypeName = atts.getValue("ROWCLASS");
683
684 // if ( lArrayType.equals("PRIMITIVE") )
685
// lArray = new ArrayList();
686
// else
687
if ( !lArrayType.equals("PRIMITIVE") )
688             {
689                 try
690                 {
691                     Class JavaDoc lArrayTypeClass = this.findClass(lArrayType);
692                     lArray = lArrayTypeClass.newInstance();
693                 }
694                 catch (ClassNotFoundException JavaDoc e1)
695                 {
696                     e1.printStackTrace();
697                     //java.lang.String message = new String("Class " + lArrayType + " could not be found, " +
698
// "either it has the wrong name or cannot be found in lookup table");
699
java.lang.String JavaDoc message = ("Class " + lArrayType + " could not be found, " +
700                                                           "either it has the wrong name or cannot be found in lookup table");
701                     SAXException classError = new SAXException(message);
702                     throw classError;
703                 }
704                 catch (IllegalAccessException JavaDoc e2)
705                 {
706                     e2.printStackTrace();
707                     //java.lang.String message = new String("Illegal Access exception whilst trying to init new instance of " + lArrayType);
708
java.lang.String JavaDoc message = ("Illegal Access exception whilst trying to init new instance of " + lArrayType);
709                     SAXException accessError = new SAXException(message);
710                     throw accessError;
711                 }
712                 catch (InstantiationException JavaDoc e3)
713                 {
714                     e3.printStackTrace();
715                     //java.lang.String message = new String("Instantiation exception whilst trying to init new instance of " + lArrayType);
716
java.lang.String JavaDoc message = ("Instantiation exception whilst trying to init new instance of " + lArrayType);
717                     SAXException initError = new SAXException(message);
718                     throw initError;
719                 }
720
721             }
722
723             if (!lFirstObj &&
724                 this.useExistingAttribute(atts, this.topAttrName(), lObj))
725             {
726 // lArray = (java.util.ArrayList)(lObj);
727
lArray = lObj;
728             }// end if;
729

730             // I can't simply check the array for the row type
731
// as array list does not have an default class
732
// also I am assuming for the minutes that TOOL array map
733
// to ArrayList's RESOLVE later
734

735             if (lRowTypeName != null &&
736                 !lRowTypeName.equals("")
737                )
738             {
739                 try
740                 {
741                     lRowClassType = this.findClass(lRowTypeName);
742                 }
743                 catch (ClassNotFoundException JavaDoc e1)
744                 {
745                     e1.printStackTrace();
746                     //java.lang.String message = new String("Class " + lRowTypeName + " could not be found, " +
747
// "either it has the wrong name or cannot be found in lookup table");
748
java.lang.String JavaDoc message = ("Class " + lRowTypeName + " could not be found, " +
749                                                 "either it has the wrong name or cannot be found in lookup table");
750                     SAXException classError = new SAXException(message);
751                     throw classError;
752                 }
753             }
754             else
755             {
756                 lRowClassType = java.lang.Object JavaDoc.class;
757             }// end if;
758

759             if ( lArrayType.equals("PRIMITIVE") )
760             {
761                 lArray = Array.newInstance(lRowClassType, lArraySize);
762             }
763
764             this.pushRowType(lRowClassType);
765             this.pushObject(lArray);
766             this.State = new Integer JavaDoc(this.XGD_NEED_ROW);
767         }
768         else if (name.equals("NULLVALUE"))
769         {
770             java.lang.Object JavaDoc lCurrentObj = null;
771             Field JavaDoc lField = null;
772             try
773             {
774                 lCurrentObj = this.topObject();
775                 // due to the fact that this method is currently
776
// designed to handle tool to java conversion
777
// we must get all of the declared fields and do
778
// a caseless search as tool is not case sensitive
779
// if we have someway of working out the destination
780
// language then we should do a check here
781
//lField = lCurrentObj.getClass().getDeclaredField(this.topAttrName());
782

783 /* @olsen+MBO: perf. optimization: replace reflect:field lookup by method findField that caches Field objects
784                 Field[] lFields = null;
785                 lFields = lCurrentObj.getClass().getDeclaredFields();
786
787
788                 if (lFields == null ||
789                     lFields.length == 0)
790                 {
791                     java.lang.String message = new String("No fields on class " + lCurrentObj.getClass().getName());
792                     java.lang.NoSuchFieldException noFieldsError = new NoSuchFieldException(message);
793                     //SAXException noFieldsError = new SAXException(message);
794
795                     throw noFieldsError;
796
797                 }
798
799                 FORLOOP:
800                 for (int i = 0; i < lFields.length; i++)
801                 {
802                     if (lFields[i].getName().equalsIgnoreCase(this.topAttrName()))
803                     {
804                         lField = lFields[i];
805                         break FORLOOP;
806                     }
807                 }
808 */

809                 // MBO: This code is suspicious!
810
// - I could not find the place in the XMLGraphSerializer
811
// that creates an element called NULLVALUE, so maybe this
812
// code is never executed.
813
// - It creates an instance of the type of the current
814
// field, which fails if the field type is an interface!
815
// - It pushes an instance on the object statck where it is
816
// not clear to me where the instance is removed.
817

818                 //@olsen+MBO: perf. optimization
819
lField = findField(lCurrentObj, this.topAttrName());
820
821                 if (lField == null)
822                     // MBO: field not found. The current version of the class
823
// does not declare the field anymore => ignore.
824
this.pushObject(new Object JavaDoc());
825                 else
826                     this.pushObject(lField.getType().newInstance());
827
828                 this.State = new Integer JavaDoc(this.XGD_NEED_END_NULLVALUE);
829             }
830             catch (InstantiationException JavaDoc e1)
831             {
832                 e1.printStackTrace();
833                 //java.lang.String message = new String("Could not init instance of " + lField.getType().getName());
834
java.lang.String JavaDoc message = ("Could not init instance of " + lField.getType().getName());
835                 SAXException initError = new SAXException(message);
836                 throw initError;
837             }
838             catch (IllegalAccessException JavaDoc e2)
839             {
840                 e2.printStackTrace();
841                 //java.lang.String message = new String("Illegal access of field " + this.topAttrName());
842
java.lang.String JavaDoc message = ("Illegal access of field " + this.topAttrName());
843                 SAXException illegalError = new SAXException(message);
844                 throw illegalError;
845             }
846         }// end if;
847
} /*Method-End*/
848
849     public void pushRowType(java.lang.Class JavaDoc type)
850     {
851         this.RowTypeStack.push(type);
852     } /*Method-End*/
853
854     public java.lang.Class JavaDoc popRowType()
855     {
856         return (java.lang.Class JavaDoc)(this.RowTypeStack.pop());
857     } /*Method-End*/
858
859
860     public void setCurrentAttribute(java.lang.Object JavaDoc value) throws org.xml.sax.SAXException JavaDoc
861     {
862         java.lang.Object JavaDoc lCurrentObj = this.topObject();
863         java.lang.String JavaDoc lCurrentField = this.topAttrName();
864
865         // MBO: special handling for wrapper class instances.
866
// A WrapperClassHelper instance on top of the object stack
867
// indicates the creation of the wrapper class instance was
868
// postponed. Now we have the value, so we can create the wrapper
869
// class instance.
870
if (lCurrentObj instanceof WrapperClassHelper) {
871             WrapperClassHelper helper = (WrapperClassHelper)lCurrentObj;
872             // Represent the value as an instance of the wrapper class.
873
lCurrentObj = helper.valueOf((String JavaDoc)value);
874             // The top of the ObjectStack is a WrapperClassHelper, which
875
// needs to be replaced by the wrapper class instance itself.
876
popObject();
877             pushObject(lCurrentObj);
878             // In case of a named object (having an ID) we need to replace
879
// the WrapperClassHelper instance in the ObjectMap by the
880
// wrapper class instance itself.
881
String JavaDoc id = helper.getId();
882             if (id != null)
883                 this.ObjectHash.put(id, lCurrentObj);
884
885             // The wrapper class object is created with its value, so we
886
// are done with this instace => return.
887
return;
888         }
889
890         Field JavaDoc lField = null;
891
892         try
893         {
894             // due to the fact that this method is currently
895
// designed to handle tool to java conversion
896
// we must get all of the declared fields and do
897
// a caseless search as tool is not case sensitive
898
// if we have someway of working out the destination
899
// language then we should do a check here
900
//lField = lCurrentObj.getClass().getDeclaredField(lCurrentField);
901

902             // also to handle a particular tool convension that is not
903
// used much in java we will strip leading '_' characters of
904
// field name and check both types. This is a fudge that we may wish
905
// to remove
906

907 /* @olsen+MBO: perf. optimization: replace reflect:field lookup by method findField that caches Field objects
908             java.lang.StringBuffer lSBuff = new StringBuffer(lCurrentField);
909             java.lang.String lAltFieldName = null;
910             if (lSBuff.charAt(0) == '_')
911             {
912                 // chop the '_' character off
913                 lSBuff.deleteCharAt(0);
914                 lAltFieldName = lSBuff.toString();
915             }
916
917             // this is a tmp store of the fields
918             Field[] lTFields = null;
919             // this is were all of the fields including the
920             // super classes fields will end up
921             java.util.ArrayList lFields = new ArrayList();
922
923             // place holder for a class reference which initially
924             // points to the class of the current object
925             // but later will point to the currect objects's super clases
926             java.lang.Class lClass = lCurrentObj.getClass();
927
928             //lTFields = lCurrentObj.getClass().getDeclaredFields();
929
930             while (lClass != null)
931             {
932                 lTFields = lClass.getDeclaredFields();
933                 lFields.ensureCapacity(lTFields.length);
934
935                 for (int i = 0; i < lTFields.length; i++)
936                 {
937                     lFields.add(lTFields[i]);
938                 }
939
940                 lClass = lClass.getSuperclass();
941             }
942
943
944         // if (lFields == null ||
945         // lFields.length == 0)
946             if (lFields.isEmpty())
947             {
948                 java.lang.String message = new String("No fields on class " + lCurrentObj.getClass().getName());
949                 java.lang.NoSuchFieldException noFieldsError = new NoSuchFieldException(message);
950                 //SAXException noFieldsError = new SAXException(message);
951
952                 throw noFieldsError;
953
954             }
955
956         // for (int i = 0; i < lFields.length; i++)
957             int lSize = lFields.size();
958             FORLOOP:
959             for (int i = 0; i < lSize; i++)
960             {
961                 Field tField = (Field)(lFields.get(i));
962                 if (tField.getName().equalsIgnoreCase(lCurrentField))
963                 {
964                     lField = tField;
965                     break FORLOOP;
966                 }// end if
967
968                 if (lAltFieldName != null)
969                 {
970                     if (tField.getName().equalsIgnoreCase(lAltFieldName))
971                    {
972                        lField = tField;
973                        break FORLOOP;
974                    }// end if
975                 }// end if
976             }// end for
977 */

978
979             //@olsen+MBO: perf. optimization
980
lField = findField(lCurrentObj, lCurrentField);
981
982             if (lField == null)
983                 // MBO: field not found. The current version of the class
984
// does not declare the field anymore => ignore.
985
return;
986
987 // System.out.println(lField.toString());
988
// System.out.println(lField.getType().getName());
989
// System.out.println("Value is :" + value + ":");
990

991             // first thing we are going to do it set the accessibility
992
// flag such that we are able to access the field just in
993
// case it is private/package/protected visibility
994

995             //@olsen+MBO: moved to findField()
996
//lField.setAccessible(true);
997

998             java.lang.Class JavaDoc lFieldClass = lField.getType();
999             if (lFieldClass.isPrimitive() &&
1000                value instanceof java.lang.String JavaDoc)
1001            {
1002                //@olsen+MBO: use .class instead of .TYPE
1003
if (lFieldClass == int.class)
1004                    lField.setInt(lCurrentObj, java.lang.Integer.parseInt((java.lang.String JavaDoc)(value)));
1005                else if (lFieldClass == short.class)
1006                    lField.setShort(lCurrentObj, java.lang.Short.parseShort((java.lang.String JavaDoc)(value)));
1007                else if (lFieldClass == long.class)
1008                    lField.setLong(lCurrentObj, java.lang.Long.parseLong((java.lang.String JavaDoc)(value)));
1009                //@olsen+MBO: changed for Byte
1010
else if (lFieldClass == byte.class)
1011                    lField.setByte(lCurrentObj, java.lang.Byte.parseByte((java.lang.String JavaDoc)(value)));
1012                else if (lFieldClass == double.class)
1013                    lField.setDouble(lCurrentObj, java.lang.Double.parseDouble((java.lang.String JavaDoc)(value)));
1014                else if (lFieldClass == float.class)
1015                    lField.setFloat(lCurrentObj, java.lang.Float.parseFloat((java.lang.String JavaDoc)(value)));
1016                else if (lFieldClass == boolean.class)
1017                    lField.setBoolean(lCurrentObj, (java.lang.Boolean.valueOf((java.lang.String JavaDoc)(value))).booleanValue());
1018                else if (lFieldClass == char.class)
1019                    lField.setChar(lCurrentObj, ((java.lang.String JavaDoc)(value)).charAt(0));
1020            }
1021            else if (lFieldClass.isArray() &&
1022                     value instanceof ArrayList)
1023            {
1024                // OK we are actually dealing with a primitive array here
1025
// but we have got an ArrayList so we need to do some transformation
1026

1027                Class JavaDoc lArrayType = lFieldClass.getComponentType();
1028                ArrayList lList = (ArrayList)(value);
1029
1030                Object JavaDoc lArray = Array.newInstance(lArrayType, lList.size());
1031                for ( int i = 0; i < lList.size(); i++)
1032                {
1033                    Array.set(lArray, i, lList.get(i));
1034                }
1035
1036                lField.set(lCurrentObj, lArray);
1037            }
1038// else if (lFieldClass.isArray())
1039
// {
1040
// lField.set(lCurrentObj, value);
1041
// }
1042
else {
1043                lField.set(lCurrentObj, value);
1044            }
1045        }
1046        catch (IllegalAccessException JavaDoc e1)
1047        {
1048            e1.printStackTrace();
1049            //java.lang.String message = new String("Illegal access of field " + lCurrentField);
1050
java.lang.String JavaDoc message = ("Illegal access of field " + lCurrentField);
1051            SAXException accessError = new SAXException(message);
1052
1053            throw accessError;
1054        }
1055
1056        catch (SecurityException JavaDoc e3)
1057        {
1058            e3.printStackTrace();
1059            //java.lang.String message = new String("Security Exception accessing fields of" + lCurrentObj.getClass().getName());
1060
java.lang.String JavaDoc message = ("Security Exception accessing fields of" + lCurrentObj.getClass().getName());
1061            SAXException accessError = new SAXException(message);
1062
1063            throw accessError;
1064        }
1065        catch (RuntimeException JavaDoc e4)
1066        {
1067            e4.printStackTrace();
1068            throw e4;
1069        }
1070    } /*Method-End*/
1071
1072    public java.lang.Class JavaDoc topRowType()
1073    {
1074        return (java.lang.Class JavaDoc)(this.RowTypeStack.peek());
1075    } /*Method-End*/
1076
1077
1078    private boolean checkSimpleRow(java.lang.String JavaDoc name, org.xml.sax.AttributeList JavaDoc atts) throws org.xml.sax.SAXException JavaDoc
1079    {
1080        boolean isSimple = false;
1081
1082        String JavaDoc lRowType = atts.getValue("ROWCLASS");
1083        String JavaDoc lRowValue = atts.getValue("VALUE");
1084
1085        if ( lRowType != null &&
1086             !lRowType.equals("")
1087           )
1088        {
1089            isSimple = true;
1090
1091            if ( lRowType.equals("java.lang.String") )
1092                this.pushObject(lRowValue);
1093            else if ( lRowType.equals("java.lang.StringBuffer") )
1094                this.pushObject(new StringBuffer JavaDoc(lRowValue));
1095            else if (lRowType.equals("int") || lRowType.equals("java.lang.Integer"))
1096                this.pushObject(Integer.valueOf(lRowValue));
1097            else if (lRowType.equals("short") || lRowType.equals("java.lang.Short"))
1098                this.pushObject(Short.valueOf(lRowValue));
1099            else if (lRowType.equals("long") || lRowType.equals("java.lang.Long"))
1100                this.pushObject(Long.valueOf(lRowValue));
1101            else if (lRowType.equals("float") || lRowType.equals("java.lang.Float"))
1102                this.pushObject(Float.valueOf(lRowValue));
1103            else if (lRowType.equals("double") || lRowType.equals("java.lang.Double"))
1104                this.pushObject(Double.valueOf(lRowValue));
1105            else if (lRowType.equals("boolean") || lRowType.equals("java.lang.Boolean"))
1106                this.pushObject(Boolean.valueOf(lRowValue));
1107            else if (lRowType.equals("char") || lRowType.equals("java.lang.Character"))
1108                this.pushObject(new Character JavaDoc(lRowValue.charAt(0)));
1109            else
1110            {
1111                System.out.println("Found and unknow type in a row");
1112                this.pushObject(null);
1113            }
1114
1115        }
1116
1117        return isSimple;
1118    }
1119
1120    public void DumpStatus()
1121    {
1122        // This method is a debug method to dump status information about this object
1123
super.DumpStatus();
1124
1125        System.out.println("Dump Status from class XMLGraphSerializer");
1126        System.out.println("Row Type stack " + this.RowTypeStack);
1127        System.out.println("Row Counter stack " + this.RowCountStack);
1128        System.out.println("Object hash table " + this.ObjectHash);
1129        System.out.println("Dump Status from class XMLGraphSerializer - END");
1130
1131    }
1132
1133    private void pushRowCount()
1134    {
1135        Integer JavaDoc lInt = new Integer JavaDoc(0);
1136        this.RowCountStack.push(lInt);
1137    }
1138
1139    private void popRowCount()
1140    {
1141        this.RowCountStack.pop();
1142    }
1143
1144    private int currentRowCount()
1145    {
1146        Integer JavaDoc lInt = (Integer JavaDoc)(this.RowCountStack.peek());
1147        return lInt.intValue();
1148    }
1149
1150    private void incrementRowCount()
1151    {
1152        Integer JavaDoc lInt = (Integer JavaDoc)(this.RowCountStack.pop());
1153        int lRowCount = lInt.intValue();
1154        lRowCount++;
1155        this.RowCountStack.push(new Integer JavaDoc(lRowCount));
1156    }
1157
1158    // MBO: added new class WrapperClassHelper.
1159

1160    /**
1161     * The WrapperClassHelper stores all necessary information (the wrapper
1162     * class object and an ID) in case the creation of a wrapper class
1163     * instance is postponed to when the value is available. Use method
1164     * valueOf(String s) to convert a String represenation into a value of
1165     * the wrapper class stored in the helper.
1166     */

1167    private static class WrapperClassHelper
1168    {
1169        /**
1170         * Interface defining a valueOf method that converts the specified
1171         * string into an instance of a wrapper class.
1172         */

1173        private static interface Converter { public Object JavaDoc valueOf(String JavaDoc s); }
1174
1175        /** Map of wrapper class to Converter instances. */
1176        private static final Map converters;
1177
1178        static
1179        {
1180            // Initialize wrapper class to converter map
1181
converters = new HashMap(8);
1182            converters.put(Boolean JavaDoc.class,
1183                           new Converter() { public Object JavaDoc valueOf(String JavaDoc s)
1184                               { return Boolean.valueOf(s); } });
1185            converters.put(Byte JavaDoc.class,
1186                           new Converter() { public Object JavaDoc valueOf(String JavaDoc s)
1187                               { return Byte.valueOf(s); } });
1188            converters.put(Short JavaDoc.class,
1189                           new Converter() { public Object JavaDoc valueOf(String JavaDoc s)
1190                               { return Short.valueOf(s); } });
1191            converters.put(Character JavaDoc.class,
1192                           new Converter() { public Object JavaDoc valueOf(String JavaDoc s)
1193                               { return new Character JavaDoc(s.charAt(0)); } });
1194            converters.put(Integer JavaDoc.class,
1195                           new Converter() { public Object JavaDoc valueOf(String JavaDoc s)
1196                               { return Integer.valueOf(s); } });
1197            converters.put(Long JavaDoc.class,
1198                           new Converter() { public Object JavaDoc valueOf(String JavaDoc s)
1199                               { return Long.valueOf(s); } });
1200            converters.put(Float JavaDoc.class,
1201                           new Converter() { public Object JavaDoc valueOf(String JavaDoc s)
1202                               { return Float.valueOf(s); } });
1203            converters.put(Double JavaDoc.class,
1204                           new Converter() { public Object JavaDoc valueOf(String JavaDoc s)
1205                               { return Double.valueOf(s); } });
1206        }
1207
1208        /** Class instance of the wrapper class. */
1209        private Class JavaDoc wrapperClass;
1210
1211        /** Key in the ObjectHash map. */
1212        private String JavaDoc id;
1213
1214        /**
1215         * Constructor.
1216         * @param wrapperClasses the Class instance of the wrapper class.
1217         * @param id the key in the the ObjectHash map.
1218         */

1219        public WrapperClassHelper(Class JavaDoc wrapperClass, String JavaDoc id)
1220        {
1221            this.wrapperClass = wrapperClass;
1222            this.id = id;
1223        }
1224
1225        /** Returns the key in the ObjectHash map. */
1226        public String JavaDoc getId()
1227        {
1228            return id;
1229        }
1230
1231        /**
1232         * Returns a wrapper Class object holding the value given by the
1233         * specified String. This method converts the specified string into
1234         * an instance of the wrapper class stored in this
1235         * WrapperClassHelper.
1236         * @param s the string to be parsed.
1237         * @return a wrapper Class object holding the value represented by
1238         * the string argument.
1239         */

1240        public Object JavaDoc valueOf(String JavaDoc s)
1241        {
1242            Converter conv = (Converter) converters.get(wrapperClass);
1243            return conv != null ? conv.valueOf(s) : null;
1244        }
1245
1246        /**
1247         * Determines if the specified argument represents a Java wrapper
1248         * class.
1249         * @param class the Class to be tested
1250         * @return true if the specified argument represents a Java wrapper
1251         * class.
1252         */

1253        public static boolean isWrapperClass(Class JavaDoc clazz)
1254        {
1255            return converters.containsKey(clazz);
1256        }
1257
1258    }
1259
1260} // end of class
1261
Popular Tags