1 57 58 package clients.zipcode; 59 60 61 import java.io.Serializable ; 62 63 import java.lang.reflect.Array ; 64 65 import java.util.Arrays ; 66 import java.util.ArrayList ; 67 import java.util.Date ; 68 import java.util.GregorianCalendar ; 69 import java.util.HashMap ; 70 import java.util.Hashtable ; 71 import java.util.Iterator ; 72 import java.util.List ; 73 import java.util.ListIterator ; 74 import java.util.Map ; 75 import java.util.Vector ; 76 77 import java.text.ParseException ; 78 import java.text.DateFormat ; 79 import java.text.SimpleDateFormat ; 80 81 import org.apache.xerces.dom.DocumentImpl; 82 83 import org.w3c.dom.Attr ; 84 import org.w3c.dom.Document ; 85 import org.w3c.dom.Element ; 86 import org.w3c.dom.NamedNodeMap ; 87 import org.w3c.dom.Node ; 88 import org.w3c.dom.Text ; 89 90 91 94 public abstract class AnyType implements Serializable 95 { 96 99 protected String namespaceURI; 100 101 104 protected String localName = "default"; 105 106 109 protected AnyType parent; 110 111 114 protected Map uriAttributeMap = new HashMap (); 115 116 119 protected Map uriElementMap = new HashMap (); 120 121 124 protected Map uriTextMap = new HashMap (); 125 126 129 protected Map uriClassMap = new HashMap (); 130 131 134 protected List uriList = new ArrayList (); 135 136 139 public AnyType() 140 { 141 this.localName = "default"; 142 } 143 144 147 protected void addElement(String uri, Class aClass) 148 { 149 List list = new ArrayList (); 150 uriElementMap.put(uri, list); 151 uriClassMap.put(uri, aClass); 152 uriList.add(uri); 153 } 154 155 158 public Map elements() 159 { 160 return uriElementMap; 161 } 162 163 166 protected void addAttribute(String uri, Class aClass) 167 { 168 uriAttributeMap.put(uri, null); 169 uriClassMap.put(uri, aClass); 170 uriList.add(uri); 171 } 172 173 176 public Map attributes() 177 { 178 return uriAttributeMap; 179 } 180 181 184 protected void addText(String uri, Class aClass) 185 { 186 uriTextMap.put(uri, null); 187 uriClassMap.put(uri, aClass); 188 uriList.add(uri); 189 } 190 191 194 public Map text() 195 { 196 return uriTextMap; 197 } 198 199 202 public Map uriToClassMap() 203 { 204 return uriClassMap; 205 } 206 207 210 public List properties() 211 { 212 return uriList; 213 } 214 215 218 protected AnyType parent() 219 { 220 return parent; 221 } 222 223 226 protected void changeParent(AnyType parent) 227 { 228 this.parent = parent; 229 } 230 231 234 public String namespaceURI() 235 { 236 return namespaceURI; 237 } 238 239 242 public void changeNamespaceURI(String namespaceURI) 243 { 244 this.namespaceURI = namespaceURI; 245 } 246 247 250 public String localName() 251 { 252 return localName; 253 } 254 255 258 public void changeLocalName(String localName) 259 { 260 this.localName = localName; 261 } 262 263 266 public Element createElement() 267 { 268 Document document = createDocument(); 269 Element result = createElement(document); 270 document.appendChild(result); 271 return result; 272 } 273 274 277 public Element createElement(String namespaceURI, String localName) 278 { 279 Document document = createDocument(); 280 Element result = createElement(document, namespaceURI, localName); 281 document.appendChild(result); 282 return result; 283 } 284 285 289 public Element createElement(Document document) 290 { 291 Element result = createElement(document, namespaceURI, localName); 292 return result; 293 } 294 295 299 public Element createElement(Document document, String namespaceURI, String localName) 300 { 301 List namespaces = new ArrayList (); 302 Element result; 303 if (namespaceURI == null) 304 { 305 result = document.createElementNS(null, localName); 306 } 307 else 308 { 309 namespaces.add(namespaceURI); 310 result = document.createElementNS(namespaceURI, "Q1:" + localName); 311 } 312 populateTo(result, namespaces); 313 for (ListIterator i = namespaces.listIterator(); i.hasNext(); ) 314 { 315 String namespace = (String )i.next(); 316 result.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:Q" + i.nextIndex(), namespace); 317 } 318 return result; 319 } 320 321 324 protected Element createElement(Document document, String uri, List namespaces) 325 { 326 int index = uri.lastIndexOf("#"); 327 if (index == -1) 328 { 329 return document.createElementNS(null, uri); 330 } 331 else 332 { 333 String namespaceURI = uri.substring(0, index); 334 String localName = uri.substring(index + 1); 335 int namespaceIndex = namespaces.indexOf(namespaceURI); 336 if (namespaceIndex == -1) 337 { 338 namespaces.add(namespaceURI); 339 namespaceIndex = namespaces.size() - 1; 340 } 341 return document.createElementNS(namespaceURI, "Q" + (namespaceIndex + 1) + ":" + localName); 342 } 343 } 344 345 348 protected void createAttribute(Element element, String uri, String value, List namespaces) 349 { 350 int index = uri.lastIndexOf("#"); 351 if (index == -1) 352 { 353 createAttribute(element, null, uri, value, namespaces); 354 } 355 else 356 { 357 createAttribute(element, uri.substring(0, index), uri.substring(index + 1), value, namespaces); 358 } 359 } 360 361 364 protected void createAttribute(Element element, String namespaceURI, String localName, String value, List namespaces) 365 { 366 if (value == null) 367 { 368 element.removeAttributeNS(namespaceURI, localName); 369 } 370 else 371 { 372 if (namespaceURI == null) 373 { 374 element.setAttributeNS(null, localName, value); 375 } 376 else 377 { 378 int namespaceIndex = namespaces.indexOf(namespaceURI); 379 if (namespaceIndex == -1) 380 { 381 namespaces.add(namespaceURI); 382 namespaceIndex = namespaces.size() - 1; 383 } 384 element.setAttributeNS(namespaceURI, "Q" + (namespaceIndex + 1) + ":" + localName, value); 385 } 386 } 387 } 388 389 392 protected Document createDocument() 393 { 394 return new DocumentImpl(); 395 } 396 397 400 protected Text createText(Document document, String stringValue) 401 { 402 return document.createTextNode(stringValue); 403 } 404 405 408 protected List basicGet(String uri) 409 { 410 List result = (List )uriElementMap.get(uri); 411 return result; 412 } 413 414 417 protected void basicSet(String uri, List values) 418 { 419 List oldList = (List )uriElementMap.get(uri); 420 if (oldList != null) 421 { 422 for (Iterator i = oldList.iterator(); i.hasNext(); ) 423 { 424 orphan(uri, i.next()); 425 } 426 } 427 428 uriElementMap.put(uri, values); 429 for (Iterator i = values.iterator(); i.hasNext(); ) 430 { 431 adopt(uri, i.next()); 432 } 433 } 434 435 438 protected Object basicGet(String uri, int index) 439 { 440 List list = (List )uriElementMap.get(uri); 441 if (list != null) 442 { 443 if (index < list.size() ) 444 { 445 return list.get(index); 446 } 447 } 448 else if (index == 0) 449 { 450 Object result = uriAttributeMap.get(uri); 451 if (result == null) 452 { 453 result = uriTextMap.get(uri); 454 } 455 return result; 456 } 457 458 return null; 459 } 460 461 464 protected void basicSet(String uri, int index, Object value) 465 { 466 Object oldValue = basicGet(uri, index); 467 orphan(uri, oldValue); 468 469 List list = (List )uriElementMap.get(uri); 470 if (list != null) 471 { 472 if (index < list.size()) 473 { 474 list.set(index, value); 475 } 476 else 477 { 478 list.add(value); 479 } 480 } 481 else if (uriAttributeMap.containsKey(uri)) 482 { 483 uriAttributeMap.put(uri, value); 484 } 485 else if (uriTextMap.containsKey(uri)) 486 { 487 uriTextMap.put(uri, value); 488 } 489 adopt(uri, value); 490 } 491 492 495 protected void orphan(String uri, Object value) 496 { 497 if (value instanceof AnyType) 498 { 499 ((AnyType)value).changeParent(null); 500 } 501 } 502 503 506 protected void adopt(String uri, Object value) 507 { 508 if (value instanceof AnyType) 509 { 510 AnyType anyType = (AnyType)value; 511 anyType.changeParent(this); 512 int i = uri.lastIndexOf("#"); 513 if (i == -1) 514 { 515 anyType.changeNamespaceURI(null); 516 anyType.changeLocalName(uri); 517 } 518 else 519 { 520 anyType.changeNamespaceURI(uri.substring(0, i)); 521 anyType.changeLocalName(uri.substring(i + 1)); 522 } 523 ((AnyType)value).changeParent(this); 524 } 525 } 526 527 530 public void basicSetText(String value) 531 { 532 uriTextMap.put(uriTextMap.keySet().iterator().next(), value); 533 } 534 535 538 protected void populateTo(Element element, List namespaces) 539 { 540 Document document = element.getOwnerDocument(); 541 542 for (Iterator attributes = uriAttributeMap.entrySet().iterator(); attributes.hasNext(); ) 545 { 546 Map.Entry entry = (Map.Entry )attributes.next(); 547 String attrURI = (String )entry.getKey(); 548 Object value = entry.getValue(); 549 createAttribute(element, attrURI, value == null ? null : convertValueToString(value), namespaces); 550 } 551 552 for (Iterator properties = properties().iterator(); properties.hasNext(); ) 555 { 556 String propertyURI = (String )properties.next(); 557 List list = (List )uriElementMap.get(propertyURI); 558 if (list != null) 559 { 560 for (Iterator values = list.iterator(); values.hasNext(); ) 561 { 562 Object value = values.next(); 563 populateValueTo(value, (Class )uriClassMap.get(propertyURI), propertyURI, element, namespaces); 564 } 565 } 566 else 567 { 568 Object value = uriTextMap.get(propertyURI); 569 if (value != null) 570 { 571 element.appendChild(createText(document, convertValueToString(value))); 572 } 573 } 574 } 575 } 576 577 580 protected void populateValueTo(Object value, Class aClass, String propertyURI, Element element, List namespaces) 581 { 582 if (AnyType.class.isAssignableFrom(aClass)) 583 { 584 Element childElement = ((AnyType)value).createElement(element.getOwnerDocument(), propertyURI, namespaces); 585 element.appendChild(childElement); 586 ((AnyType)value).populateTo(childElement, namespaces); 587 } 588 else 589 { 590 Element childElement = createElement(element.getOwnerDocument(), propertyURI, namespaces); 591 if (value != null) 592 { 593 if (Element .class.isAssignableFrom(aClass)) 594 { 595 Element actualElement = (Element )element.getOwnerDocument().importNode((Element )value, true); 596 childElement.appendChild(actualElement); 597 } 598 else if (aClass.isArray()) 599 { 600 for (int i = 0, length = Array.getLength(value); i < length; ++i) 601 { 602 populateValueTo(Array.get(value, i), childElement, namespaces); 603 } 604 } 605 else if (Hashtable .class.isAssignableFrom(aClass)) 606 { 607 for (Iterator entries = ((Map )value).entrySet().iterator(); entries.hasNext(); ) 608 { 609 Map.Entry entry = (Map.Entry )entries.next(); 610 populateValueTo(entry.getKey(), childElement, namespaces); 611 populateValueTo(entry.getValue(), childElement, namespaces); 612 } 613 } 614 else if (Vector .class.isAssignableFrom(aClass)) 615 { 616 for (Iterator objects = ((Vector )value).iterator(); objects.hasNext(); ) 617 { 618 Object object = objects.next(); 619 populateValueTo(object, childElement, namespaces); 620 } 621 } 622 else 623 { 624 childElement.appendChild(createText(element.getOwnerDocument(), convertValueToString(value))); 625 } 626 } 627 628 element.appendChild(childElement); 629 } 630 } 631 632 635 protected void populateValueTo(Object value, Element element, List namespaces) 636 { 637 if (value == null) 638 { 639 populateValueTo(value, Object .class, "null", element, namespaces); 640 } 641 else if (value instanceof Element ) 642 { 643 Element actualElement = (Element )element.getOwnerDocument().importNode((Element )value, true); 644 element.appendChild(actualElement); 645 } 646 else 647 { 648 populateValueTo(value, value.getClass(), value.getClass().getName(), element, namespaces); 649 } 650 } 651 652 655 public void populateFrom(Element element) 656 { 657 NamedNodeMap attributes = element.getAttributes(); 658 for (int i = 0, size = attributes.getLength(); i < size; ++i) 659 { 660 Attr attr = (Attr )attributes.item(i); 661 String attrNamespaceURI = attr.getNamespaceURI(); 662 String attrLocalName = attr.getLocalName(); 663 String attrURI = (attrNamespaceURI == null ? "" : attrNamespaceURI + "#") + attrLocalName; 664 if (uriAttributeMap.containsKey(attrURI)) 665 { 666 basicSet(attrURI, 0, convertStringToValue((Class )uriClassMap.get(attrURI), attr.getNodeValue())); 667 } 668 } 669 670 for (Node child = element.getFirstChild(); child != null; child = child.getNextSibling()) 671 { 672 if (child.getNodeType() == Node.ELEMENT_NODE) 673 { 674 String elementNamespaceURI = child.getNamespaceURI(); 675 String elementLocalName = child.getLocalName(); 676 String elementURI = (elementNamespaceURI == null ? "" : elementNamespaceURI + "#") + elementLocalName; 677 List list = (List )uriElementMap.get(elementURI); 678 if (list != null) 679 { 680 Object object = populateValueFrom((Class )uriClassMap.get(elementURI), (Element )child); 681 if (object != null) 682 { 683 basicSet(elementURI, list.size(), object); 684 } 685 } 686 } 687 else if (child.getNodeType() == Node.TEXT_NODE) 688 { 689 if (!uriTextMap.isEmpty()) 690 { 691 basicSetText(((Text )child).getData()); 692 } 693 } 694 } 695 } 696 697 700 protected String elementText(Element element) 701 { 702 for (Node child = element.getFirstChild(); child != null; child = child.getNextSibling()) 703 { 704 if (child.getNodeType() == Node.TEXT_NODE) 705 { 706 return ((Text )child).getData(); 707 } 708 } 709 710 return ""; 711 } 712 713 716 protected List elementChildren(Element element) 717 { 718 List result = new ArrayList (); 719 for (Node child = element.getFirstChild(); child != null; child = child.getNextSibling()) 720 { 721 if (child.getNodeType() == Node.ELEMENT_NODE) 722 { 723 Element elementChild = (Element )child; 724 try 725 { 726 Class aClass = Class.forName(elementChild.getTagName()); 727 result.add(populateValueFrom(aClass, elementChild)); 728 } 729 catch (ClassNotFoundException exception) 730 { 731 result.add(elementChild); 732 } 733 } 734 } 735 736 return result; 737 } 738 739 742 protected Object populateValueFrom(Class aClass, Element element) 743 { 744 try 745 { 746 if (Element .class.isAssignableFrom(aClass)) 747 { 748 for (Node child = element.getFirstChild(); child != null; child = child.getNextSibling()) 749 { 750 if (child.getNodeType() == Node.ELEMENT_NODE) 751 { 752 return child; 753 } 754 } 755 } 756 else if (AnyType.class.isAssignableFrom(aClass)) 757 { 758 AnyType object = (AnyType)aClass.newInstance(); 759 object.populateFrom(element); 760 return object; 761 } 762 else if (GregorianCalendar .class.isAssignableFrom(aClass)) 763 { 764 GregorianCalendar result = new GregorianCalendar (); 765 try 766 { 767 result.setTime(gregorianCalandarDateFormat.parse(elementText(element))); 768 } 769 catch (Exception exception) 770 { 771 exception.printStackTrace(); 772 } 773 return result; 774 } 775 else if (Date .class.isAssignableFrom(aClass)) 776 { 777 Date result = new Date (); 778 try 779 { 780 result = standardDateFormat.parse(elementText(element)); 781 } 782 catch (ParseException standardException) 783 { 784 try 785 { 786 result = preciseDateFormat.parse(elementText(element)); 787 } 788 catch (ParseException preciseException) 789 { 790 } 791 } 792 return result; 793 } 794 else if (Hashtable .class.isAssignableFrom(aClass)) 795 { 796 Hashtable hashtable = new Hashtable (); 797 for (Iterator iterator = elementChildren(element).iterator(); iterator.hasNext(); ) 798 { 799 Object key = iterator.next(); 800 Object value = iterator.next(); 801 hashtable.put(key, value); 802 } 803 return hashtable; 804 } 805 else if (Vector .class.isAssignableFrom(aClass)) 806 { 807 Vector vector = new Vector (); 808 for (Iterator iterator = elementChildren(element).iterator(); iterator.hasNext(); ) 809 { 810 Object value = iterator.next(); 811 vector.add(value); 812 } 813 return vector; 814 } 815 else if (aClass == String .class) 816 { 817 return elementText(element); 818 } 819 else if (aClass.isArray()) 820 { 821 List children = elementChildren(element); 822 Object result = Array.newInstance(aClass.getComponentType(), children.size()); 823 int index = 0; 824 for (Iterator iterator = elementChildren(element).iterator(); iterator.hasNext(); ++index) 825 { 826 Object value = iterator.next(); 827 Array.set(result, index, value); 828 } 829 return result; 830 } 831 else 832 { 833 try 834 { 835 return aClass.getConstructor(stringConstructor).newInstance(new Object [] { elementText(element) }); 836 } 837 catch (Exception exception) 838 { 839 exception.printStackTrace(); 840 841 return elementText(element); 842 } 843 } 844 } 845 catch (Exception exception) 846 { 847 exception.printStackTrace(); 848 } 849 850 return null; 851 } 852 853 protected static Class [] stringConstructor = { String .class }; 854 protected static DateFormat gregorianCalandarDateFormat = new SimpleDateFormat ("yyyy-MM-dd"); 855 protected static DateFormat standardDateFormat = new SimpleDateFormat ("yyyy-MM-dd'T'HH:mm:ss'Z'"); 856 protected static DateFormat preciseDateFormat = new SimpleDateFormat ("yyyy-MM-dd'T'HH:mm:ss'.'SSS'Z'"); 857 858 861 protected Object convertStringToValue(Class aClass, String string) 862 { 863 if (aClass == String .class) 864 { 865 return string; 866 } 867 else 868 { 869 if (GregorianCalendar .class.isAssignableFrom(aClass)) 870 { 871 GregorianCalendar result = new GregorianCalendar (); 872 try 873 { 874 result.setTime(gregorianCalandarDateFormat.parse(string)); 875 } 876 catch (Exception exception) 877 { 878 exception.printStackTrace(); 879 } 880 return result; 881 } 882 else if (Date .class.isAssignableFrom(aClass)) 883 { 884 Date result = new Date (); 885 try 886 { 887 result = standardDateFormat.parse(string); 888 } 889 catch (ParseException standardException) 890 { 891 try 892 { 893 result = preciseDateFormat.parse(string); 894 } 895 catch (ParseException preciseException) 896 { 897 } 898 } 899 return result; 900 } 901 else 902 { 903 try 904 { 905 return aClass.getConstructor(stringConstructor).newInstance(new Object [] { string }); 906 } 907 catch (Exception exception) 908 { 909 exception.printStackTrace(); 910 911 return string; 912 } 913 } 914 } 915 } 916 917 920 protected String convertValueToString(Object value) 921 { 922 if (value == null) 923 { 924 return ""; 925 } 926 else if (value instanceof GregorianCalendar ) 927 { 928 String result = gregorianCalandarDateFormat.format(((GregorianCalendar )value).getTime()); 929 return result; 930 } 931 else if (value instanceof Date ) 932 { 933 String result = standardDateFormat.format((Date )value); 934 return result; 935 } 936 else 937 { 938 return value.toString(); 939 } 940 } 941 942 945 protected Object basicToArray(List list, Object array) 946 { 947 int length = Array.getLength(array); 948 for (int i = 0; i < length; ++i) 949 { 950 Array.set(array, i, list.get(i)); 951 } 952 return array; 953 } 954 955 958 protected List basicToList(Object array) 959 { 960 int length = Array.getLength(array); 961 ArrayList result = new ArrayList (length); 962 for (int i = 0; i < length; ++i) 963 { 964 result.add(Array.get(array, i)); 965 } 966 return result; 967 } 968 } 969 | Popular Tags |