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.PersonName; 25 import org.apache.juddi.datatype.Phone; 26 import org.apache.juddi.datatype.RegistryObject; 27 import org.apache.juddi.datatype.business.Contact; 28 import org.apache.juddi.util.xml.XMLUtils; 29 import org.w3c.dom.Element ; 30 31 40 public class ContactHandler extends AbstractHandler 41 { 42 public static final String TAG_NAME = "contact"; 43 44 private HandlerMaker maker = null; 45 46 protected ContactHandler(HandlerMaker maker) 47 { 48 this.maker = maker; 49 } 50 51 public RegistryObject unmarshal(Element element) 52 { 53 Contact obj = new Contact(); 54 Vector nodeList = null; 55 AbstractHandler handler = null; 56 57 String useType = element.getAttribute("useType"); 59 if ((useType != null) && (useType.trim().length() > 0)) 60 obj.setUseType(useType); 61 62 65 nodeList = XMLUtils.getChildElementsByTagName(element,PersonNameHandler.TAG_NAME); 67 if (nodeList.size() > 0) 68 { 69 handler = maker.lookup(PersonNameHandler.TAG_NAME); 70 obj.setPersonName((PersonName)handler.unmarshal((Element )nodeList.elementAt(0))); 71 } 72 73 nodeList = XMLUtils.getChildElementsByTagName(element,DescriptionHandler.TAG_NAME); 74 for (int i=0; i<nodeList.size(); i++) 75 { 76 handler = maker.lookup(DescriptionHandler.TAG_NAME); 77 Description descr = (Description)handler.unmarshal((Element )nodeList.elementAt(i)); 78 if (descr != null) 79 obj.addDescription(descr); 80 } 81 82 nodeList = XMLUtils.getChildElementsByTagName(element,AddressHandler.TAG_NAME); 83 for (int i=0; i<nodeList.size(); i++) 84 { 85 handler = maker.lookup(AddressHandler.TAG_NAME); 86 obj.addAddress((Address)handler.unmarshal((Element )nodeList.elementAt(i))); 87 } 88 89 nodeList = XMLUtils.getChildElementsByTagName(element,EmailHandler.TAG_NAME); 90 for (int i=0; i<nodeList.size(); i++) 91 { 92 handler = maker.lookup(EmailHandler.TAG_NAME); 93 obj.addEmail((Email)handler.unmarshal((Element )nodeList.elementAt(i))); 94 } 95 96 nodeList = XMLUtils.getChildElementsByTagName(element,PhoneHandler.TAG_NAME); 97 for (int i=0; i<nodeList.size(); i++) 98 { 99 handler = maker.lookup(PhoneHandler.TAG_NAME); 100 obj.addPhone((Phone)handler.unmarshal((Element )nodeList.elementAt(i))); 101 } 102 103 return obj; 104 } 105 106 public void marshal(RegistryObject object,Element parent) 107 { 108 Contact contact = (Contact)object; 109 Element element = parent.getOwnerDocument().createElementNS(null,TAG_NAME); 110 AbstractHandler handler = null; 111 112 String useType = contact.getUseType(); 113 if ((useType != null) && (useType.trim().length() > 0)) 114 element.setAttribute("useType",useType); 115 116 Vector descrVector = contact.getDescriptionVector(); 117 if ((descrVector!=null) && (descrVector.size() > 0)) 118 { 119 handler = maker.lookup(DescriptionHandler.TAG_NAME); 120 for (int i=0; i < descrVector.size(); i++) 121 handler.marshal((Description)descrVector.elementAt(i),element); 122 } 123 124 PersonName personName = contact.getPersonName(); 125 if ((personName != null)) 126 { 127 handler = maker.lookup(PersonNameHandler.TAG_NAME); 128 handler.marshal(personName,element); 129 } 130 131 Vector phoneVector = contact.getPhoneVector(); 132 if ((phoneVector!=null) && (phoneVector.size() > 0)) 133 { 134 handler = maker.lookup(PhoneHandler.TAG_NAME); 135 for (int i=0; i < phoneVector.size(); i++) 136 handler.marshal((Phone)phoneVector.elementAt(i),element); 137 } 138 139 Vector emailVector = contact.getEmailVector(); 140 if ((emailVector!=null) && (emailVector.size() > 0)) 141 { 142 handler = maker.lookup(EmailHandler.TAG_NAME); 143 for (int i=0; i < emailVector.size(); i++) 144 handler.marshal((Email)emailVector.elementAt(i),element); 145 } 146 147 Vector addressVector = contact.getAddressVector(); 148 if ((addressVector!=null) && (addressVector.size() > 0)) 149 { 150 handler = maker.lookup(AddressHandler.TAG_NAME); 151 for (int i=0; i < addressVector.size(); i++) 152 handler.marshal((Address)addressVector.elementAt(i),element); 153 } 154 155 parent.appendChild(element); 156 } 157 158 159 160 161 162 163 164 public static void main(String args[]) 165 throws Exception 166 { 167 HandlerMaker maker = HandlerMaker.getInstance(); 168 AbstractHandler handler = maker.lookup(ContactHandler.TAG_NAME); 169 Element parent = XMLUtils.newRootElement(); 170 Element child = null; 171 172 Address address = new Address(); 173 address.setUseType("myAddressUseType"); 174 address.setSortCode("sortThis"); 175 address.setTModelKey(null); 176 address.addAddressLine(new AddressLine("AddressLine1","keyNameAttr","keyValueAttr")); 177 address.addAddressLine(new AddressLine("AddressLine2")); 178 179 Contact contact = new Contact(); 180 contact.setPersonNameValue("Bob Whatever"); 182 contact.addDescription(new Description("Bob is a big fat jerk")); 183 contact.addDescription(new Description("obBay sIay a igBay atFay erkJay","es")); 184 contact.addEmail(new Email("bob@whatever.com")); 185 contact.addPhone(new Phone("(603)559-1901")); 186 contact.addAddress(address); 187 188 System.out.println(); 189 190 RegistryObject regObject = contact; 191 handler.marshal(regObject,parent); 192 child = (Element )parent.getFirstChild(); 193 parent.removeChild(child); 194 XMLUtils.writeXML(child,System.out); 195 196 System.out.println(); 197 198 regObject = handler.unmarshal(child); 199 handler.marshal(regObject,parent); 200 child = (Element )parent.getFirstChild(); 201 parent.removeChild(child); 202 XMLUtils.writeXML(child,System.out); 203 204 System.out.println(); 205 } 206 } | Popular Tags |