1 2 3 package Jt.xml; 4 5 import Jt.*; 6 import java.util.*; 7 import java.lang.reflect.*; 8 import java.beans.*; 9 import java.io.*; 10 import org.xml.sax.*; 11 import org.xml.sax.helpers.*; 12 13 16 17 public class JtXMLHelper extends JtObject implements ContentHandler { 18 19 private XMLReader reader = null; 20 protected static final String DEFAULT_PARSER_NAME = "org.apache.xerces.parsers.SAXParser"; 21 protected static final String XML_HEADER = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"; 22 private StringBuffer buffer; 23 HashMap map; 24 private Object object = null; 25 private String parserName = DEFAULT_PARSER_NAME; 26 private int level = 0; 27 Vector mapTable = new Vector (); 28 Vector objTable = new Vector (); 29 30 public JtXMLHelper() { 31 } 32 33 37 38 39 void setParserName (String parserName) { 40 this.parserName = parserName; 41 42 } 43 44 45 48 49 String getParserName () { 50 return (parserName); 51 } 52 53 54 57 public void startElement(String uri, String local, String raw, 58 Attributes attrs) throws SAXException { 59 60 if ("".equals (uri)) { 61 63 if ("Object".equals (raw)) { 64 level++; 65 66 map = new HashMap (); 67 mapTable.addElement (map); 68 return; 69 } 70 71 buffer = null; 74 75 } else { 76 } 78 } 79 80 81 Object setResources (Object obj) { 82 Object args[]; 83 PropertyDescriptor[] prop; 84 int i; 85 Class p; 86 Method m; 87 BeanInfo info = null; 88 String tval; 89 90 91 map = getAttributeMap (level); 92 if (obj == null || map == null) 93 return (null); 94 95 try { 96 info = Introspector.getBeanInfo( 97 obj.getClass (), obj.getClass ().getSuperclass()); 98 } catch(Exception e) { 99 handleException (e); 100 return (null); } 102 103 prop = info.getPropertyDescriptors(); 104 105 106 for(i = 0; i < prop.length; i++) { 107 108 tval = (String ) map.get (prop[i].getName()); 109 110 if (tval != null) setValue (obj, prop[i].getName(), tval); 112 113 } 114 115 return (null); } 117 118 119 private Hashtable getAttributes (Object obj) { 120 121 Object args[]; 122 PropertyDescriptor[] prop; 123 int i; 124 Class p; 125 Method m; 126 BeanInfo info = null; 127 Object value; 128 Hashtable attr; 129 130 131 if (obj == null) 132 return (null); 133 134 attr = new Hashtable (); 135 136 137 try { 138 info = Introspector.getBeanInfo( 139 obj.getClass (), java.lang.Object .class); 140 } catch(Exception e) { 141 handleException (e); 142 return (null); 143 } 144 145 prop = info.getPropertyDescriptors(); 146 for(i = 0; i < prop.length; i++) { 147 148 p = prop[i].getPropertyType(); 149 150 try { 151 m = prop[i].getReadMethod (); 152 if (m == null) { 153 handleError 154 ("getAttributes: getReadMethod returned null"); 155 return (null); 156 } 157 158 value = m.invoke (obj, null); 159 if (value == null) { 160 continue; 161 } 162 163 if (value instanceof String ) { 164 attr.put (prop[i].getName(), value); 165 continue; 167 } 168 169 if (value instanceof Integer || 170 value instanceof Long || 171 value instanceof Float || 172 value instanceof Byte || 173 value instanceof Boolean || 174 value instanceof Short || 175 value instanceof Double ) { 176 attr.put (prop[i].getName(), value.toString () ); 177 178 continue; 179 } else { 180 handleWarning 181 ("JtXMLHelper.getAttributes: unable to convert attribute type to String:" + prop[i].getName()); 182 183 } 184 185 } catch (Exception e) { 186 handleException(e); 187 return (null); 188 } 189 } 190 191 return (attr); 192 } 193 194 195 private Object createInstance () { 196 String classname; 197 Class jtclass; 198 Object obj; 199 String value; 200 String stmp; 201 JtRemoteException jex; 202 203 if (map == null) 204 return (null); 206 207 classname = (String ) map.get ("classname"); 208 209 if (classname == null) 210 return (null); 212 try { 213 jtclass = Class.forName ((String ) classname); 214 } catch (Exception e) { 215 handleException (e); 216 return (null); 217 } 218 219 220 228 229 230 if (classname.equals ("Jt.JtRemoteException")) { 231 value = (String ) map.get ("value"); 232 stmp = (String ) map.get ("trace"); 233 234 236 if (value == null) { 237 handleError ("createInstance: invalid value:" + 238 value); 239 240 return (null); 241 } 242 jex = new JtRemoteException (value); 243 jex.setTrace (stmp); 244 return (jex); 245 } 246 247 if (classname.startsWith ("Jt.")) { 248 try { 249 obj = jtclass.newInstance (); 250 setResources (obj); 251 return (obj); } catch (Exception e) { 253 handleException (e); 254 return (null); 255 } 256 } else if (classname.startsWith ("java.lang")) { 257 258 value = (String ) map.get ("value"); 259 } else { 260 handleError ("createInstance: unable to handle class type:" + 261 classname); 262 return (null); 263 } 264 265 266 if (value == null) { 267 handleError ("createInstance: invalid value:" + 268 value); 269 270 return (null); 271 } 272 273 if (classname.equals ("java.lang.Byte")) { 274 275 try { 276 obj = new Byte (value); 277 return (obj); 278 } catch (Exception e) { 279 handleException (e); 280 return (null); 281 } 282 283 } 284 285 286 if (classname.equals ("java.lang.Short")) { 287 288 try { 289 obj = new Short (value); 290 return (obj); 291 } catch (Exception e) { 292 handleException (e); 293 return (null); 294 } 295 296 } 297 298 299 if (classname.equals ("java.lang.Float")) { 300 301 try { 302 obj = new Float (value); 303 return (obj); 304 } catch (Exception e) { 305 handleException (e); 306 return (null); 307 } 308 309 } 310 311 if (classname.equals ("java.lang.Double")) { 312 313 try { 314 obj = new Double (value); 315 return (obj); 316 } catch (Exception e) { 317 handleException (e); 318 return (null); 319 } 320 321 } 322 323 324 if (classname.equals ("java.lang.String")) { 325 326 try { 327 obj = new String (value); 328 return (obj); 329 } catch (Exception e) { 330 handleException (e); 331 return (null); 332 } 333 334 } 335 336 if (classname.equals ("java.lang.Integer")) { 337 338 try { 339 obj = new Integer (value); 340 return (obj); 341 } catch (Exception e) { 342 handleException (e); 343 return (null); 344 } 345 346 } 347 348 if (classname.equals ("java.lang.Long")) { 349 350 try { 351 obj = new Long (value); 352 return (obj); 353 } catch (Exception e) { 354 handleException (e); 355 return (null); 356 } 357 358 } 359 360 if (classname.equals ("java.lang.Boolean")) { 361 362 try { 363 obj = new Boolean (value); 364 return (obj); 365 } catch (Exception e) { 366 handleException (e); 367 return (null); 368 } 369 370 } 371 372 handleError ("createInstance: unable to handle class type:" + 373 classname); 374 return (null); } 376 377 HashMap getAttributeMap (int level) { 378 if (level < 1) 379 return (null); 380 381 if (level > mapTable.size ()) 382 return (null); 383 384 385 return ((HashMap) mapTable.elementAt (level - 1)); 386 387 } 388 389 392 393 public void endElement(String uri, 394 String name, 395 String qName) 396 throws SAXException { 397 JtMessage msg; 398 int i; 399 400 map = getAttributeMap (level); 401 if (qName == null || map == null) 402 return; 404 if ("".equals (uri)) { 405 407 if ("Object".equals (qName)) { 410 if (map == null) 411 return; object = createInstance (); 413 map = null; 414 buffer = null; 415 level--; 416 417 if (level > 0) 418 objTable.addElement (object); 419 else { 420 421 if (!(object instanceof JtCollection)) 422 return; 423 for (i = 0; i < objTable.size (); i++) { 424 msg = new JtMessage ("JtADD"); 425 msg.setMsgContent (objTable.elementAt (i)); 426 ((JtObject) object).processMessage (msg); 427 } 429 430 } 431 return; 432 } else { 433 434 437 438 440 if (buffer == null) 441 return; 443 map.put (qName, buffer.toString ()); 444 445 buffer = null; 446 } 447 448 } else { 449 } 451 } 452 453 454 457 public void startDocument() throws SAXException { 458 459 } 460 461 464 465 public void endDocument() 466 throws SAXException { 467 468 } 469 470 473 474 public void characters(char[] ch, 475 int start, 476 int length) 477 throws SAXException { 478 String tmp; 479 480 if (buffer == null) 482 buffer = new StringBuffer (); 483 484 for (int i = start; i < start + length; i++) { 485 buffer.append (ch[i]); 486 487 509 } 510 511 tmp = buffer.toString().trim (); 514 515 if (!tmp.equals ("")) { 516 } 519 520 } 521 522 525 public void endPrefixMapping(String prefix) 526 throws SAXException { 527 528 } 529 530 533 public void ignorableWhitespace(char[] ch, 534 int start, 535 int length) 536 throws SAXException { 537 538 } 539 540 543 public void processingInstruction(String target, 544 String data) 545 throws SAXException { 546 547 } 548 549 552 553 public void setDocumentLocator(Locator locator) { 554 555 } 556 557 560 561 public void skippedEntity(java.lang.String name) 562 throws SAXException { 563 564 } 565 566 567 570 571 public void startPrefixMapping(String prefix, 572 String uri) 573 throws SAXException 574 { 575 576 } 577 578 void realize () { 580 581 if (reader != null) 582 return; 583 584 try{ 585 reader = XMLReaderFactory.createXMLReader (parserName); 586 587 reader.setContentHandler (this); 588 } catch (Exception ex) { 590 591 handleException (ex); 592 } 593 594 } 595 596 private Object convertXMLToObject (String string) { 597 598 ByteArrayInputStream bstream; 599 600 object = null; 602 if (string == null) 603 return (null); 604 605 if (reader == null) 606 realize (); 607 608 609 try { 610 611 bstream = new ByteArrayInputStream (string.getBytes()); 612 reader.parse (new InputSource ((InputStream) bstream)); 613 614 } catch (Exception ex) { 615 handleException (ex); 616 } 617 return object; 618 } 619 620 private String convertJtObjectToXML (Object obj) { 621 622 StringBuffer buf = new StringBuffer (); 623 624 Hashtable tbl; 625 String key; 627 Enumeration keys; 628 629 if (obj == null) 630 return (null); 631 632 tbl = getAttributes (obj); 633 634 637 639 640 buf.append ("<Object>\n"); 642 buf.append ("<classname>" + obj.getClass().getName() + "</classname>\n"); 643 644 645 if (tbl != null) { 646 keys = tbl.keys (); 647 while (keys.hasMoreElements()) { 648 649 key = (String ) keys.nextElement (); 650 651 buf.append ("<" + key + ">"); 652 buf.append ((String ) tbl.get (key)); 653 buf.append ("</" + key + ">\n"); 654 655 656 } 657 } 658 buf.append ("</Object>\n"); 659 660 return (buf.toString ()); 662 663 } 664 665 private String convertJtCollectionToXML (Object obj) { 666 667 StringBuffer buf = new StringBuffer (); 668 669 Hashtable tbl; 670 String key; 672 JtIterator jit; 673 Object tmp; 674 String tmp1; 675 676 if (obj == null) 677 return (null); 678 679 buf.append ("<Object>\n"); 681 buf.append ("<classname>" + obj.getClass().getName() + "</classname>\n"); 682 683 jit = (JtIterator) getValue (obj, "iterator"); 684 685 if (jit == null) 686 return (null); 688 for (;;) { 689 tmp = jit.processMessage (new JtMessage ("JtNEXT")); 691 if (tmp == null) 692 break; 693 tmp1 = convertToXML (tmp); 694 695 if (tmp1 == null) 696 return (null); 698 buf.append (tmp1); 699 } 700 buf.append ("</Object>\n"); 701 702 return (buf.toString ()); 703 704 } 705 706 707 private String stackTrace (Exception ex ) { 708 ByteArrayOutputStream bstream; 709 710 711 if (ex == null) 712 return (null); 713 714 bstream = new ByteArrayOutputStream (); 715 716 717 719 720 722 ex.printStackTrace (new PrintWriter (bstream, true)); 723 724 726 return (bstream.toString ()); 727 728 729 } 730 731 private String convertToXML (Object obj) { 732 733 buffer = new StringBuffer (); 734 735 if (obj == null) { 736 buffer.append ("<Object>\n"); 737 buffer.append ("</Object>\n"); 738 return (buffer.toString ()); 739 740 } 741 742 744 745 if (obj instanceof Byte || 746 obj instanceof Short || 747 obj instanceof Integer || 748 obj instanceof Long || 749 obj instanceof Boolean || 750 obj instanceof Float || 751 obj instanceof Double || 752 obj instanceof String ) { 753 buffer.append ("<Object>\n"); 755 buffer.append ("<classname>" + obj.getClass().getName() + "</classname>\n"); 756 buffer.append ("<value>" + obj + "</value>\n"); 757 buffer.append ("</Object>\n"); 758 return (buffer.toString ()); 759 760 } else if (obj instanceof JtCollection) { 761 762 return (convertJtCollectionToXML (obj)); 763 764 } else if (obj instanceof JtObject) { 765 766 return (convertJtObjectToXML (obj)); 767 } else if (obj instanceof Exception ) { 768 buffer.append ("<Object>\n"); 769 buffer.append ("<classname>" + "Jt.JtRemoteException" + "</classname>\n"); 771 772 buffer.append ("<value>" + ((Exception ) obj).getMessage () + "</value>\n"); 773 buffer.append ("<trace>" + stackTrace ((Exception ) obj) + "</trace>\n"); 774 buffer.append ("</Object>\n"); 775 return (buffer.toString ()); 776 } 777 778 handleError ("convertToXML:unable to convert object class" + obj.getClass().getName()); 779 return (null); 780 781 } 782 783 private void initialize () { 784 buffer = null; 785 map = null; 786 object = null; 787 level = 0; 788 mapTable = new Vector (); 789 objTable = new Vector (); 790 } 791 792 801 802 public Object processMessage (Object event) { 803 804 String msgid = null; 805 JtMessage e = (JtMessage) event; 806 Object content; 807 808 if (e == null) 809 return null; 810 811 msgid = (String ) e.getMsgId (); 812 813 if (msgid == null) 814 return null; 815 816 content = e.getMsgContent(); 817 818 820 if (msgid.equals ("JtCONVERT_OBJECT_TO_XML")) { 821 822 823 return (XML_HEADER + convertToXML (content)); } 825 826 827 if (msgid.equals ("JtCONVERT_XML_TO_OBJECT")) { 828 829 initialize (); 830 return (convertXMLToObject ((String ) content)); 831 } 832 833 if (msgid.equals ("JtREMOVE")) { 834 835 return (null); 836 } 837 838 839 840 handleError ("JtXMLHelper.processMessage: invalid message id:" + msgid); 841 return (null); 842 843 } 844 845 846 849 850 public static void main(String [] args) { 851 852 JtObject main = new JtObject (); 853 JtMessage msg1, msg2; 854 Integer count; 855 String tmp; 856 Integer i; 857 JtXMLHelper helper; 858 JtCommand command; 859 JtList col; 860 861 862 865 msg1 = new JtMessage (); 866 msg2 = new JtMessage (); 867 868 869 msg1.setMsgId ("JtCONVERT_OBJECT_TO_XML"); 870 871 msg1.setMsgContent (new Integer (2)); 872 873 System.out.println ("JtCONVERT_OBJECT_TO_XML: converting Integer (2) to XML .."); 874 875 helper = (JtXMLHelper) main.createObject ("Jt.xml.JtXMLHelper", "helper"); 876 877 878 tmp = (String ) main.sendMessage (helper, msg1); 879 System.out.println (tmp); 880 881 882 msg2.setMsgId ("JtCONVERT_XML_TO_OBJECT"); 883 System.out.print ("JtCONVERT_XML_TO_OBJECT: converting XML to object (Integer) .. "); 884 885 msg2.setMsgContent (tmp); 886 i = (Integer ) main.sendMessage (helper, msg2); 887 888 System.out.println (i); 889 890 if (i != null && i.intValue() == 2) { 891 System.out.println ("JtCONVERT_OBJECT_TO_XML (INTEGER): PASS"); 892 System.out.println ("JtCONVERT_XML_TO_OBJECT (INTEGER): PASS"); 893 } else { 894 System.out.println ("JtCONVERT_OBJECT_TO_XML (INTEGER): FAIL"); 895 System.out.println ("JtCONVERT_XML_TO_OBJECT (INTEGER): FAIL"); 896 897 } 898 899 msg1.setMsgId ("JtCONVERT_OBJECT_TO_XML"); 900 901 msg1.setMsgContent ("My string"); 902 903 System.out.println ("JtCONVERT_OBJECT_TO_XML: converting String to XML .."); 904 tmp = (String ) main.sendMessage (helper, msg1); 905 System.out.println (tmp); 906 907 908 msg2.setMsgId ("JtCONVERT_XML_TO_OBJECT"); 909 msg2.setMsgContent (tmp); 910 tmp = (String ) main.sendMessage (helper, msg2); 911 912 System.out.print ("JtCONVERT_XML_TO_OBJECT: converting XML to object (String) .. "); 913 914 System.out.println (tmp); 915 916 917 if (tmp != null && tmp.equals("My string")) { 918 System.out.println ("JtCONVERT_OBJECT_TO_XML (String): PASS"); 919 System.out.println ("JtCONVERT_XML_TO_OBJECT (String): PASS"); 920 } else { 921 System.out.println ("JtCONVERT_OBJECT_TO_XML (String): FAIL"); 922 System.out.println ("JtCONVERT_XML_TO_OBJECT (String): FAIL"); 923 924 } 925 926 927 command = (JtCommand) main.createObject ("Jt.JtCommand", "cmd"); 928 main.setValue ("cmd", "command", "notepad"); 929 930 msg1.setMsgId ("JtCONVERT_OBJECT_TO_XML"); 931 932 msg1.setMsgContent (command); 933 934 tmp = (String ) main.sendMessage (helper, msg1); 935 System.out.println ("Output =" + tmp); 936 937 msg1.setMsgId ("JtCONVERT_XML_TO_OBJECT"); 938 939 msg1.setMsgContent (tmp); 940 941 command = (JtCommand) main.sendMessage (helper, msg1); 942 943 msg1.setMsgId ("JtEXECUTE"); 944 main.sendMessage (command, msg1); 945 946 947 col = (JtList) main.createObject ("Jt.JtList", "col"); 948 949 msg1 = new JtMessage ("JtADD"); 950 msg1.setMsgContent (new Integer (2)); 951 952 953 main.sendMessage (col, msg1); 954 955 msg1.setMsgContent ("String"); 956 957 main.sendMessage (col, msg1); 958 959 msg1.setMsgContent (new JtException ("JtException")); 960 961 main.sendMessage (col, msg1); 962 963 msg2.setMsgId ("JtCONVERT_OBJECT_TO_XML"); 964 msg2.setMsgContent (col); 965 tmp = (String ) main.sendMessage (helper, msg2); 966 967 System.out.println ("XtCollection=" + tmp); 968 969 msg1.setMsgId ("JtCONVERT_XML_TO_OBJECT"); 970 971 msg1.setMsgContent (tmp); 972 973 col = (JtList) main.sendMessage (helper, msg1); 974 975 976 msg1 = new JtMessage ("JtADD"); 977 msg1.setMsgContent (new JtException ("JtException")); 978 979 981 msg2.setMsgId ("JtCONVERT_OBJECT_TO_XML"); 982 msg2.setMsgContent (col); 983 tmp = (String ) main.sendMessage (helper, msg2); 984 985 System.out.println ("XtCollection=" + tmp); 986 987 main.removeObject (helper); 988 989 } 990 991 } 992 993 994 | Popular Tags |