1 16 package org.apache.juddi.handler; 17 18 import java.util.Vector ; 19 20 import org.apache.juddi.datatype.Address; 21 import org.apache.juddi.datatype.AddressLine; 22 import org.apache.juddi.datatype.Description; 23 import org.apache.juddi.datatype.Email; 24 import org.apache.juddi.datatype.Phone; 25 import org.apache.juddi.datatype.RegistryObject; 26 import org.apache.juddi.datatype.business.Contact; 27 import org.apache.juddi.datatype.business.Contacts; 28 import org.apache.juddi.util.xml.XMLUtils; 29 import org.w3c.dom.Element ; 30 31 39 public class ContactsHandler extends AbstractHandler 40 { 41 public static final String TAG_NAME = "contacts"; 42 43 private HandlerMaker maker = null; 44 45 protected ContactsHandler(HandlerMaker maker) 46 { 47 this.maker = maker; 48 } 49 50 public RegistryObject unmarshal(Element element) 51 { 52 Contacts obj = new Contacts(); 53 Vector nodeList = null; 54 AbstractHandler handler = null; 55 56 59 62 nodeList = XMLUtils.getChildElementsByTagName(element,ContactHandler.TAG_NAME); 64 for (int i=0; i<nodeList.size(); i++) 65 { 66 handler = maker.lookup(ContactHandler.TAG_NAME); 67 obj.addContact((Contact)handler.unmarshal((Element )nodeList.elementAt(i))); 68 } 69 70 return obj; 71 } 72 73 public void marshal(RegistryObject object,Element parent) 74 { 75 Contacts contacts = (Contacts)object; 76 Element element = parent.getOwnerDocument().createElementNS(null,TAG_NAME); 77 AbstractHandler handler = null; 78 79 Vector contactVector = contacts.getContactVector(); 80 if ((contactVector!=null) && (contactVector.size() > 0)) 81 { 82 handler = maker.lookup(ContactHandler.TAG_NAME); 83 for (int i=0; i < contactVector.size(); i++) 84 handler.marshal((Contact)contactVector.elementAt(i),element); 85 } 86 87 parent.appendChild(element); 88 } 89 90 91 92 93 94 95 96 public static void main(String args[]) 97 throws Exception 98 { 99 HandlerMaker maker = HandlerMaker.getInstance(); 100 AbstractHandler handler = maker.lookup(ContactsHandler.TAG_NAME); 101 102 Element parent = XMLUtils.newRootElement(); 103 Element child = null; 104 105 Address address = new Address(); 106 address.setUseType("myAddressUseType"); 107 address.setSortCode("sortThis"); 108 address.setTModelKey(null); 109 address.addAddressLine(new AddressLine("AddressLine1","keyNameAttr","keyValueAttr")); 110 address.addAddressLine(new AddressLine("AddressLine2")); 111 112 Contact contact = new Contact(); 113 contact.setUseType("myAddressUseType"); 114 contact.setPersonNameValue("Bob Whatever"); 115 contact.addDescription(new Description("Bob is a big fat jerk")); 116 contact.addDescription(new Description("obBay sIay a igBay atFay erkJay","es")); 117 contact.addEmail(new Email("bob@whatever.com")); 118 contact.addPhone(new Phone("(603)559-1901")); 119 contact.addAddress(address); 120 121 Contacts contacts = new Contacts(); 122 contacts.addContact(contact); 123 contacts.addContact(contact); 124 125 System.out.println(); 126 127 RegistryObject regObject = contacts; 128 handler.marshal(regObject,parent); 129 child = (Element )parent.getFirstChild(); 130 parent.removeChild(child); 131 XMLUtils.writeXML(child,System.out); 132 133 System.out.println(); 134 135 regObject = handler.unmarshal(child); 136 handler.marshal(regObject,parent); 137 child = (Element )parent.getFirstChild(); 138 parent.removeChild(child); 139 XMLUtils.writeXML(child,System.out); 140 141 System.out.println(); 142 } 143 } | Popular Tags |