KickJava   Java API By Example, From Geeks To Geeks.

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


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: Object2XML.java,v 1.3 2002/09/18 06:54:17 per_nyfelt Exp $
8

9 package org.ozoneDB.core.xml;
10
11 import org.ozoneDB.OzoneInterface;
12 import org.ozoneDB.OzoneProxy;
13 import org.ozoneDB.core.ObjectContainer;
14 import org.xml.sax.*;
15 import org.xml.sax.helpers.AttributesImpl JavaDoc;
16
17 import java.lang.reflect.*;
18 import java.util.Hashtable JavaDoc;
19
20
21 /**
22  * This class transform an Object into XML.
23  *
24  * @version $Revision: 1.3 $
25  * @author <a HREF="http://www.softwarebuero.de">SMB</a>
26  */

27
28
29 public class Object2XML implements Consts {
30
31     //
32
// member
33
//
34

35     /**
36      * ObjCache saves the references of the processed objects.
37      */

38     private Hashtable JavaDoc objCache = new Hashtable JavaDoc();
39
40     /**
41      * This flag decides if transient member will be serialized or not.
42      */

43     private boolean serializeTransientFlag = false;
44
45     /**
46      * The SAX handler for the XML content.
47      */

48     private ContentHandler ch;
49
50     /**
51      * Factory for the attributes of the first obj-Tag.
52      */

53     private ObjAttsFactory oaf;
54
55
56     //
57
// construcor
58
//
59

60
61     /**
62      * @param contHandler
63      */

64     public Object2XML( ContentHandler contHandler ) {
65         ch = contHandler;
66         oaf = new OzoneObjAttsFactory();
67     }
68
69
70     /**
71      * @param contHandler
72      * @param serializeTransientFlag
73      */

74     public Object2XML( ContentHandler contHandler, boolean serializeTransientFlag ) {
75         this( contHandler );
76         this.serializeTransientFlag = serializeTransientFlag;
77     }
78
79     //
80
// methods
81
//
82

83
84     /**
85      * ToXML(obj) gets an object and serialize this object into XML.
86      *
87      * @param obj (the Object)
88      */

89     public void toXML( Object JavaDoc obj ) throws SAXException {
90        // ch.startDocument();
91
writeObjStartTag( obj.getClass().getName(), getID( obj ), oaf.additionallyAtts( obj ) );
92         objCache.put( getID( obj ), getID( obj ) );
93
94         getMember( obj, obj.getClass() );
95
96         writeObjEndTag();
97        // ch.endDocument();
98
}
99
100
101     /**
102      * GetMember gets the informations (name/type/value)
103      * of all members (private and protected too) !!
104      *
105      * @param obj (the Object)
106      * @param objClass (the Class of the Object)
107      */

108     protected void getMember( Object JavaDoc obj, Class JavaDoc objClass ) throws SAXException {
109        // System.out.println( "getMember(): " + objClass.getName() );
110

111         Field[] fds = objClass.getDeclaredFields();
112
113         AccessibleObject.setAccessible( fds, true );
114         for (int i = 0; i < fds.length; i++) {
115             Field fd = fds[i];
116
117             // avoid converting the container of a database object
118
if (ObjectContainer.class.isAssignableFrom( fd.getType() )) {
119                 continue;
120             }
121             if (OzoneInterface.class.isAssignableFrom( fd.getType() )) {
122                 continue;
123             }
124             if (Modifier.isStatic( fd.getModifiers() )) {
125                 continue;
126             }
127             if (!serializeTransientFlag && Modifier.isTransient( fd.getModifiers() )) {
128                 continue;
129             }
130
131             String JavaDoc memberName = fd.getName();
132
133             try {
134
135                 Object JavaDoc value = fd.get( obj );
136                 if (value instanceof org.ozoneDB.OzoneProxy)
137                     handleOzoneProxyMember( memberName, (OzoneProxy)value);
138                 else {
139                     writeMemberStartTag( memberName );
140                     getValue( value , fd.getType() );
141                 }
142
143                 writeMemberEndTag();
144
145             } catch (IllegalAccessException JavaDoc iae) {
146                 System.err.println( "(getMember) EXCEPTION: " + iae );
147             }
148         }
149
150         superClass( obj, objClass );
151     }
152
153
154     /**
155      * SuperClass gets the informations about the Superclass of the Object.
156      *
157      * @param obj (the Object)
158      * @param objClass (the Class of the Object)
159      */

160     private void superClass( Object JavaDoc obj, Class JavaDoc objClass ) throws SAXException {
161
162         Class JavaDoc superClass = objClass.getSuperclass();
163         if (superClass == null) {
164             return;
165         }
166
167         writeSuperClStartTag( superClass.getName() );
168
169         getMember( obj, superClass );
170         writeSuperClEndTag();
171     }
172
173     /**
174      * GetValue gets the value of the certain member.
175      *
176      * @param value (value of the member)
177      * @param valueType (the Class of the certain member)
178      */

179     protected void getValue( Object JavaDoc value, Class JavaDoc valueType ) throws SAXException {
180         if (value == null) {
181             return;
182         }
183
184         if (objCache.containsKey( getID( value ) )) {
185             writeObjRefElement( getID( value ) );
186             return;
187         }
188
189         objCache.put( getID( value ), getID( value ) );
190
191         if (valueType.isPrimitive()) {
192             writeValueStartTag( valueType.getName(), getID( value ) );
193             writeValue( value.toString() );
194             writeValueEndTag();
195             return;
196         }
197
198         if ( value instanceof Boolean JavaDoc || value instanceof Byte JavaDoc || value instanceof Character JavaDoc
199             || value instanceof Double JavaDoc || value instanceof Float JavaDoc || value instanceof Long JavaDoc
200             || value instanceof Integer JavaDoc || value instanceof Short JavaDoc || value instanceof String JavaDoc) {
201
202             writeValueStartTag( value.getClass().getName(), getID( value ) );
203             writeValue( value.toString() );
204             writeValueEndTag();
205             return;
206         }
207
208
209         if (valueType.isArray()) {
210             int length = Array.getLength( value );
211             Class JavaDoc subType = value.getClass().getComponentType();
212             writeArrayStartTag( valueType.getName(), getID( value ) );
213             for (int l = 0; l < length; l++) {
214
215                 Object JavaDoc subValue = Array.get( value, l );
216                 if(subValue == null)
217                     continue;
218
219                 if (subType.isPrimitive()) {
220                     getValue( subValue , subType );
221                 } else {
222                     getValue( subValue , subValue.getClass());
223                 }
224             }
225             writeArrayEndTag();
226
227         } else {
228             writeValueObjStartTag( value.getClass().getName(), getID( value ) );
229
230             getMember( value, value.getClass() );
231             writeValueObjEndTag();
232         }
233     }
234
235     /**
236      * This methode handles an OzoneProxy member.
237      *
238      * @param memberName (name of the member)
239      * @param proxy (the OzoneProxy object)
240      */

241     protected void handleOzoneProxyMember( String JavaDoc memberName, OzoneProxy proxy) throws SAXException {
242         String JavaDoc proxyType = proxy.getClass().getName();
243         String JavaDoc objectID;
244
245         try {
246
247             Field remoteID = proxy.getClass().getField( REMOTE_ID );
248             Object JavaDoc valueID = remoteID.get( proxy );
249             objectID = valueID.toString();
250
251             writeMemberTagForOzoneProxy( memberName, proxyType, objectID );
252
253         } catch (NoSuchFieldException JavaDoc nsfe) {
254             System.err.println( "(handleOzoneProxyMember) EXCEPTION: " + nsfe );
255         } catch (IllegalAccessException JavaDoc iae) {
256             System.err.println( "(handleOzoneProxyMember) EXCEPTION: " + iae );
257         }
258     }
259
260     /**
261      * GetId gets the reference/address of the Object.
262      *
263      * @param obj (the Object)
264      * @return id
265      */

266     protected Integer JavaDoc getID( Object JavaDoc obj ) {
267         Integer JavaDoc id = new Integer JavaDoc( System.identityHashCode( obj ) );
268         return id;
269     }
270
271
272     //
273
// writeMethods
274
// The writeMethods helps to write XML/SAX.
275
//
276
protected void writeObjStartTag( String JavaDoc classname, Integer JavaDoc id, Attributes additionallyAtts ) throws SAXException {
277
278         AttributesImpl JavaDoc atts;
279         if (additionallyAtts != null) {
280             atts = new AttributesImpl JavaDoc( additionallyAtts );
281         } else {
282             atts = new AttributesImpl JavaDoc();
283         }
284
285         atts.addAttribute( "", ATTR_TYPE, ATTR_TYPE, "String", classname );
286         atts.addAttribute( "", ATTR_ID, ATTR_ID, "Integer", id + "" );
287
288         ch.startElement( "", TAG_OBJ, TAG_OBJ, atts );
289     }
290
291
292     protected void writeObjEndTag() throws SAXException {
293         ch.endElement( "", TAG_OBJ, TAG_OBJ );
294     }
295
296
297     protected void writeSuperClStartTag( String JavaDoc classname ) throws SAXException {
298         AttributesImpl JavaDoc atts = new AttributesImpl JavaDoc();
299         atts.addAttribute( "", ATTR_TYPE, ATTR_TYPE, "String", classname );
300
301         ch.startElement( "", TAG_SUPERCLASS, TAG_SUPERCLASS, atts );
302     }
303
304
305     protected void writeSuperClEndTag() throws SAXException {
306         ch.endElement( "", TAG_SUPERCLASS, TAG_SUPERCLASS );
307     }
308
309
310     protected void writeMemberStartTag( String JavaDoc name ) throws SAXException {
311         AttributesImpl JavaDoc atts = new AttributesImpl JavaDoc();
312         atts.addAttribute( "", ATTR_NAME, ATTR_NAME, "String", name );
313
314         ch.startElement( "", TAG_MEMBER, TAG_MEMBER, atts );
315     }
316
317     protected void writeMemberEndTag() throws SAXException {
318         ch.endElement( "", TAG_MEMBER, TAG_MEMBER );
319     }
320
321     protected void writeMemberTagForOzoneProxy( String JavaDoc name, String JavaDoc proxyType, String JavaDoc objectID ) throws SAXException {
322         AttributesImpl JavaDoc atts = new AttributesImpl JavaDoc();
323         atts.addAttribute( "", ATTR_NAME, ATTR_NAME, "String", name );
324         atts.addAttribute( "", ATTR_PROXY_TYPE, ATTR_PROXY_TYPE, "java.lang.Class", proxyType );
325         atts.addAttribute( ATTR_XLINK_NAMESPACE, ATTR_XLINK_TYPE_LOCAL, ATTR_XLINK_TYPE_RAW,
326                            "String", ATTR_XLINK_TYPE_VALUE );
327         atts.addAttribute( ATTR_XLINK_NAMESPACE, ATTR_XLINK_HREF_LOCAL, ATTR_XLINK_HREF_RAW,
328                            "long", objectID );
329
330         ch.startElement( "", TAG_MEMBER, TAG_MEMBER, atts );
331     }
332
333     protected void writeValue( String JavaDoc value ) throws SAXException {
334         ch.characters( value.toCharArray(), 0, value.length() );
335     }
336
337
338     protected void writeValueStartTag( String JavaDoc type, Integer JavaDoc id ) throws SAXException {
339         AttributesImpl JavaDoc atts = new AttributesImpl JavaDoc();
340         atts.addAttribute( "", ATTR_TYPE, ATTR_TYPE, "String", type );
341         atts.addAttribute( "", ATTR_ID, ATTR_ID, "Integer", id + "" );
342
343         ch.startElement( "", TAG_VALUE, TAG_VALUE, atts );
344     }
345
346
347     protected void writeValueEndTag() throws SAXException {
348         ch.endElement( "", TAG_VALUE, TAG_VALUE );
349     }
350
351
352     protected void writeValueObjStartTag( String JavaDoc type, Integer JavaDoc id ) throws SAXException {
353         AttributesImpl JavaDoc atts = new AttributesImpl JavaDoc();
354         atts.addAttribute( "", ATTR_TYPE, ATTR_TYPE, "String", type );
355         atts.addAttribute( "", ATTR_ID, ATTR_ID, "Integer", id + "" );
356
357         ch.startElement( "", TAG_VALUEOBJ, TAG_VALUEOBJ, atts );
358     }
359
360
361     protected void writeValueObjEndTag() throws SAXException {
362         ch.endElement( "", TAG_VALUEOBJ, TAG_VALUEOBJ );
363     }
364
365
366     protected void writeArrayStartTag( String JavaDoc type, Integer JavaDoc id ) throws SAXException {
367         AttributesImpl JavaDoc atts = new AttributesImpl JavaDoc();
368         atts.addAttribute( "", ATTR_TYPE, ATTR_TYPE, "String", type );
369         atts.addAttribute( "", ATTR_ID, ATTR_ID, "Integer", id + "" );
370
371         ch.startElement( "", TAG_VALUEARRAY, TAG_VALUEARRAY, atts );
372     }
373
374
375     protected void writeArrayEndTag() throws SAXException {
376         ch.endElement( "", TAG_VALUEARRAY, TAG_VALUEARRAY );
377     }
378
379
380     protected void writeObjRefElement( Integer JavaDoc sourceId ) throws SAXException {
381         AttributesImpl JavaDoc atts = new AttributesImpl JavaDoc();
382         atts.addAttribute( "", ATTR_REF, ATTR_REF, "Integer", sourceId + "" );
383
384         ch.startElement( "", TAG_VALUE, TAG_VALUE, atts );
385         ch.endElement( "", TAG_VALUE, TAG_VALUE );
386     }
387 }
388
Popular Tags