1 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 ; 18 import java.lang.reflect.Field ; 19 import java.util.*; 20 26 public class XML2ObjectContentHandler implements ContentHandler, Consts { 27 28 public static final boolean debug = false; 29 30 34 36 protected Locator locator; 37 38 41 protected static Hashtable objCache = new Hashtable(); 42 43 46 protected Stack stack; 47 48 52 protected XML2ObjectContentHandler CH = null; 53 54 56 protected XML2ObjectDelegate delegate; 57 58 62 64 public XML2ObjectContentHandler() { 65 CH = this; 66 } 67 68 70 public XML2ObjectContentHandler(XML2ObjectDelegate delegate) { 71 this(); 72 this.delegate = delegate; 73 } 74 75 79 84 public void setDocumentLocator(Locator locator) { 85 this.locator = locator; 86 } 87 88 91 public void startDocument() { 92 if (debug) { 93 System.out.println("Start parsing .."); 94 } 95 } 96 97 99 public void endDocument() { 100 if (debug) { 101 System.out.println("End parsing .."); 102 } 103 } 104 105 115 public void startElement (String namespaceURI, String localName, 116 String 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 142 public void endElement (String namespaceURI, String localName, 143 String 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 175 public void characters (char[] ch, int start, int end) { 176 CH.values(ch, start, end); 177 } 178 179 181 public void processingInstruction(String target, String data) { 182 if (debug) { 183 System.out.println("Target: " + target + " Data: " + data); 184 } 185 } 186 187 189 public void startPrefixMapping (String prefix, String uri) { 190 if (debug) { 191 System.out.println("Prefix: " + prefix + " Uri: " + uri); 192 } 193 } 194 195 197 public void endPrefixMapping (String prefix) { 198 if (debug) { 199 System.out.println("EndPrefix: " + prefix); 200 } 201 } 202 203 205 public void ignorableWhitespace (char[] ch, int start, int end) { 206 if (debug) { 207 String s = new String (ch,start,end); 208 System.out.println("Ignor chars: "+s); 209 } 210 } 211 212 214 public void skippedEntity (String name) { 215 if (debug) { 216 System.out.println("Skip entity "+name); 217 } 218 } 219 220 221 225 229 protected void objStartElement(Attributes atts) { 230 handleObjStartElement(atts); 231 } 232 233 237 protected void memberStartElement(Attributes atts) { 238 handleMemberStartElement(atts); 239 } 240 241 245 protected void valueStartElement(Attributes atts) { 246 handleValueStartElement(atts); 247 } 248 249 253 protected void valueObjStartElement(Attributes atts) { 254 handleValueObjStartElement(atts); 255 } 256 257 261 protected void valueArrayStartElement(Attributes atts) { 262 handleValueArrayStartElement(atts); 263 } 264 265 269 protected void superclassStartElement(Attributes atts) { 270 handleSuperclassStartElement(atts); 271 } 272 273 280 protected void values(char[] ch, int start, int end) { 281 handleValues(ch, start, end); 282 } 283 284 287 protected void objEndElement() { 288 handleObjEndElement(); 289 } 290 291 294 protected void memberEndElement() { 295 handleMemberEndElement (); 296 } 297 298 301 protected void valueEndElement() { 302 handleValueEndElement(); 303 } 304 305 308 protected void valueObjEndElement() { 309 handleValueObjEndElement(); 310 } 311 312 315 protected void valueArrayEndElement() { 316 handleValueArrayEndElement(); 317 } 318 319 322 protected void superclassEndElement() { 323 handleSuperclassEndElement(); 324 } 325 326 327 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 cnfe) { 345 System.err.println("handleObjStartElement: " + cnfe); 346 } catch (InstantiationException ie) { 347 System.err.println("handleObjStartElement: " + ie); 348 } catch (IllegalAccessException iae) { 349 System.err.println("handleObjStartElement: " + iae); 350 } 351 } 352 353 356 protected void handleObjEndElement() { 357 ObjElement oe = (ObjElement)stack.pop(); 358 delegate.handleObject(oe); 359 } 360 361 366 protected void handleOzoneProxyMember(Attributes atts) { 367 String proxyType = atts.getValue(ATTR_PROXY_TYPE); 368 String href = atts.getValue(ATTR_XLINK_HREF_RAW); 369 370 ObjectID objID = new ObjectID((new Long (href)).longValue()); 371 372 try { 373 374 Class proxyClass = Thread.currentThread().getContextClassLoader().loadClass(proxyType); 375 OzoneProxy proxy = (OzoneProxy)proxyClass.newInstance(); 376 377 Field 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 cnfe) { 384 System.err.println("handleOzoneProxyMember: " + cnfe); 385 } catch (InstantiationException ie) { 386 System.err.println("handleOzoneProxyMember: " + ie); 387 } catch (IllegalAccessException iae) { 388 System.err.println("handleOzoneProxyMember: " + iae); 389 } catch (NoSuchFieldException nsfe) { 390 System.err.println("handleOzoneProxyMember: " + nsfe); 391 } 392 } 393 394 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) handleOzoneProxyMember(atts); 406 } 407 408 412 protected void handleMemberEndElement() { 413 Object 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 obj = stack.peek(); 427 Class 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 fd = objClass.getDeclaredField(me.getName()); 443 fd.setAccessible(true); 444 445 fd.set(obj, value); 446 447 } catch (NoSuchFieldException nsfe) { System.err.println("handleMemberEndElement: " + nsfe); 449 } catch (IllegalAccessException iae) { System.err.println("handleMemberEndElement: " + iae); 451 } 452 } 453 } 454 455 461 protected void handleValueStartElement(Attributes atts) { 462 String ref = atts.getValue(ATTR_REF); 463 if (ref != null) { 464 Object 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 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 value = new String (ch,start,end); 488 ValueElement ve = (ValueElement)stack.pop(); 489 490 if (ve.getStrValue() != null) value = ve.getStrValue() + value; 493 ve.setStrValue(value); 494 495 stack.push(ve); 496 } 497 } 498 499 504 protected void handleValueEndElement() { 505 Object 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 529 protected Object castValue(String type, String valueString) { 530 531 if ((type.equals("java.lang.Boolean")) || 532 (type.equals("boolean"))) { 533 Boolean value = new Boolean (valueString); 534 return value; 535 } 536 537 if ((type.equals("java.lang.Byte")) || 538 (type.equals("byte"))) { 539 Byte value = new Byte (valueString); 540 return value; 541 } 542 543 if ((type.equals("java.lang.Character")) || 544 (type.equals("char"))) { 545 Character value = new Character (valueString.charAt(0)); 546 return value; 547 } 548 549 if ((type.equals("java.lang.Short")) || 550 (type.equals("short"))) { 551 Short value = new Short (valueString); 552 return value; 553 } 554 555 if ((type.equals("java.lang.Integer")) || 556 (type.equals("int"))) { 557 Integer value = new Integer (valueString); 558 return value; 559 } 560 561 if ((type.equals("java.lang.Long")) || 562 (type.equals("long"))) { 563 Long value = new Long (valueString); 564 return value; 565 } 566 567 if ((type.equals("java.lang.Float")) || 568 (type.equals("float"))) { 569 Float value = new Float (valueString); 570 return value; 571 } 572 573 if ((type.equals("java.lang.Double")) || 574 (type.equals("double"))) { 575 Double value = new Double (valueString); 576 return value; 577 } 578 579 if (type.equals("java.lang.String")) { 580 String value = new String (valueString); 581 return value; 582 } 583 584 return null; 585 } 586 587 596 protected void handleValueObjStartElement(Attributes atts) { 597 String ref = atts.getValue("ref"); 598 if (ref != null) { 599 Object 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 cnfe) { 617 System.err.println("handleValueObjStartElement: " + cnfe); 618 } catch (InstantiationException ie) { 619 System.err.println("handleValueObjStartElement: " + ie); 620 } catch (IllegalAccessException iae) { 621 System.err.println("handleValueObjStartElement: " + iae); 622 } 623 } 624 625 627 protected void handleValueObjEndElement() { 628 } 629 630 636 protected void handleValueArrayStartElement(Attributes atts) { 637 String ref = atts.getValue("ref"); 638 if (ref != null) { 639 Object 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 651 protected void handleValueArrayEndElement() { 652 Object 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 Class vaClass = Env.currentEnv().classManager.classForName(vae.getType()).getComponentType(); 670 671 array = Array.newInstance(vaClass, vaValues.size()); 672 673 int k = vaValues.size()-1; 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 692 protected void handleSuperclassStartElement(Attributes atts) { 693 try { 694 Object 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 cnfe) { 707 System.err.println("handleSuperclassStartElement: " + cnfe); 708 } 709 } 710 711 714 protected void handleSuperclassEndElement() { 715 stack.pop(); 716 } 717 718 719 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 e) { 731 System.err.println("showStack: " + e); 732 } 733 } 734 735 } 736 | Popular Tags |