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.RegistryObject; 23 import org.apache.juddi.util.xml.XMLUtils; 24 import org.w3c.dom.Element ; 25 26 32 public class AddressHandler extends AbstractHandler 33 { 34 public static final String TAG_NAME = "address"; 35 36 private HandlerMaker maker = null; 37 38 protected AddressHandler(HandlerMaker maker) 39 { 40 this.maker = maker; 41 } 42 43 public RegistryObject unmarshal(Element element) 44 { 45 Address obj = new Address(); 46 Vector nodeList = null; 47 AbstractHandler handler = null; 48 49 String useType = element.getAttribute("useType"); 51 if ((useType != null) && (useType.trim().length() > 0)) 52 obj.setUseType(useType); 53 54 String sortCode = element.getAttribute("sortCode"); 55 if ((sortCode != null) && (sortCode.trim().length() > 0)) 56 obj.setSortCode(sortCode); 57 58 String tModelKey = element.getAttribute("tModelKey"); 59 if ((tModelKey != null) && (tModelKey.trim().length() > 0)) 60 obj.setTModelKey(tModelKey); 61 62 65 handler = maker.lookup(AddressLineHandler.TAG_NAME); 67 nodeList = XMLUtils.getChildElementsByTagName(element,AddressLineHandler.TAG_NAME); 68 for (int i=0; i<nodeList.size(); i++) 69 { 70 obj.addAddressLine((AddressLine)handler.unmarshal((Element )nodeList.elementAt(i))); 71 } 72 73 return obj; 74 } 75 76 public void marshal(RegistryObject object,Element parent) 77 { 78 Address address = (Address)object; 79 Element element = parent.getOwnerDocument().createElementNS(null,TAG_NAME); 80 81 String useType = address.getUseType(); 82 if ((useType != null) && (useType.trim().length() > 0)) 83 element.setAttribute("useType",useType); 84 85 String sortCode = address.getSortCode(); 86 if ((sortCode != null) && (sortCode.trim().length() > 0)) 87 element.setAttribute("sortCode",sortCode); 88 89 String tModelKey = address.getTModelKey(); 90 if ((tModelKey != null) && (tModelKey.trim().length() > 0)) 91 element.setAttribute("tModelKey",tModelKey); 92 93 Vector vector = address.getAddressLineVector(); 94 if ((vector!=null) && (vector.size() > 0)) 95 { 96 AbstractHandler handler = maker.lookup(AddressLineHandler.TAG_NAME); 97 for (int i=0; i < vector.size(); i++) 98 { 99 AddressLine addressLine = (AddressLine)vector.elementAt(i); 100 handler.marshal(addressLine,element); 101 } 102 } 103 104 parent.appendChild(element); 105 } 106 107 108 109 110 111 112 113 public static void main(String args[]) 114 throws Exception 115 { 116 HandlerMaker maker = HandlerMaker.getInstance(); 117 AbstractHandler handler = maker.lookup(AddressHandler.TAG_NAME); 118 Element parent = XMLUtils.newRootElement(); 119 Element child = null; 120 121 Address object = new Address(); 122 object.setUseType("myAddressUseType"); 123 object.setSortCode("sortThis"); 124 object.setTModelKey(null); 125 object.addAddressLine(new AddressLine("AddressLine1","keyNameAttr","keyValueAttr")); 126 object.addAddressLine(new AddressLine("AddressLine2")); 127 128 System.out.println(); 129 130 RegistryObject regObject = object; 131 handler.marshal(regObject,parent); 132 child = (Element )parent.getFirstChild(); 133 parent.removeChild(child); 134 XMLUtils.writeXML(child,System.out); 135 136 System.out.println(); 137 138 regObject = handler.unmarshal(child); 139 handler.marshal(regObject,parent); 140 child = (Element )parent.getFirstChild(); 141 parent.removeChild(child); 142 XMLUtils.writeXML(child,System.out); 143 144 System.out.println(); 145 } 146 } | Popular Tags |