1 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 ; 16 17 import java.lang.reflect.*; 18 import java.util.Hashtable ; 19 20 21 27 28 29 public class Object2XML implements Consts { 30 31 35 38 private Hashtable objCache = new Hashtable (); 39 40 43 private boolean serializeTransientFlag = false; 44 45 48 private ContentHandler ch; 49 50 53 private ObjAttsFactory oaf; 54 55 56 60 61 64 public Object2XML( ContentHandler contHandler ) { 65 ch = contHandler; 66 oaf = new OzoneObjAttsFactory(); 67 } 68 69 70 74 public Object2XML( ContentHandler contHandler, boolean serializeTransientFlag ) { 75 this( contHandler ); 76 this.serializeTransientFlag = serializeTransientFlag; 77 } 78 79 83 84 89 public void toXML( Object obj ) throws SAXException { 90 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 } 99 100 101 108 protected void getMember( Object obj, Class objClass ) throws SAXException { 109 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 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 memberName = fd.getName(); 132 133 try { 134 135 Object 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 iae) { 146 System.err.println( "(getMember) EXCEPTION: " + iae ); 147 } 148 } 149 150 superClass( obj, objClass ); 151 } 152 153 154 160 private void superClass( Object obj, Class objClass ) throws SAXException { 161 162 Class 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 179 protected void getValue( Object value, Class 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 || value instanceof Byte || value instanceof Character 199 || value instanceof Double || value instanceof Float || value instanceof Long 200 || value instanceof Integer || value instanceof Short || value instanceof String ) { 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 subType = value.getClass().getComponentType(); 212 writeArrayStartTag( valueType.getName(), getID( value ) ); 213 for (int l = 0; l < length; l++) { 214 215 Object 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 241 protected void handleOzoneProxyMember( String memberName, OzoneProxy proxy) throws SAXException { 242 String proxyType = proxy.getClass().getName(); 243 String objectID; 244 245 try { 246 247 Field remoteID = proxy.getClass().getField( REMOTE_ID ); 248 Object valueID = remoteID.get( proxy ); 249 objectID = valueID.toString(); 250 251 writeMemberTagForOzoneProxy( memberName, proxyType, objectID ); 252 253 } catch (NoSuchFieldException nsfe) { 254 System.err.println( "(handleOzoneProxyMember) EXCEPTION: " + nsfe ); 255 } catch (IllegalAccessException iae) { 256 System.err.println( "(handleOzoneProxyMember) EXCEPTION: " + iae ); 257 } 258 } 259 260 266 protected Integer getID( Object obj ) { 267 Integer id = new Integer ( System.identityHashCode( obj ) ); 268 return id; 269 } 270 271 272 protected void writeObjStartTag( String classname, Integer id, Attributes additionallyAtts ) throws SAXException { 277 278 AttributesImpl atts; 279 if (additionallyAtts != null) { 280 atts = new AttributesImpl ( additionallyAtts ); 281 } else { 282 atts = new AttributesImpl (); 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 classname ) throws SAXException { 298 AttributesImpl atts = new AttributesImpl (); 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 name ) throws SAXException { 311 AttributesImpl atts = new AttributesImpl (); 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 name, String proxyType, String objectID ) throws SAXException { 322 AttributesImpl atts = new AttributesImpl (); 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 value ) throws SAXException { 334 ch.characters( value.toCharArray(), 0, value.length() ); 335 } 336 337 338 protected void writeValueStartTag( String type, Integer id ) throws SAXException { 339 AttributesImpl atts = new AttributesImpl (); 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 type, Integer id ) throws SAXException { 353 AttributesImpl atts = new AttributesImpl (); 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 type, Integer id ) throws SAXException { 367 AttributesImpl atts = new AttributesImpl (); 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 sourceId ) throws SAXException { 381 AttributesImpl atts = new AttributesImpl (); 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 |