1 16 package org.apache.juddi.handler; 17 18 import java.util.Vector ; 19 20 import org.apache.juddi.datatype.KeyedReference; 21 import org.apache.juddi.datatype.RegistryObject; 22 import org.apache.juddi.datatype.SharedRelationships; 23 import org.apache.juddi.util.xml.XMLUtils; 24 import org.w3c.dom.Element ; 25 26 32 public class SharedRelationshipsHandler extends AbstractHandler 33 { 34 public static final String TAG_NAME = "sharedRelationships"; 35 36 private HandlerMaker maker = null; 37 38 protected SharedRelationshipsHandler(HandlerMaker maker) 39 { 40 this.maker = maker; 41 } 42 43 public RegistryObject unmarshal(Element element) 44 { 45 SharedRelationships obj = new SharedRelationships(); 46 Vector nodeList = null; 47 AbstractHandler handler = null; 48 49 String direction = element.getAttribute("direction"); 51 if ((direction != null && (direction.trim().length() > 0))) 52 obj.setDirection(direction); 53 54 57 nodeList = XMLUtils.getChildElementsByTagName(element,KeyedReferenceHandler.TAG_NAME); 59 for (int i=0; i<nodeList.size(); i++) 60 { 61 handler = maker.lookup(KeyedReferenceHandler.TAG_NAME); 62 obj.addKeyedReference((KeyedReference)handler.unmarshal((Element )nodeList.elementAt(i))); 63 } 64 65 return obj; 66 } 67 68 public void marshal(RegistryObject object,Element parent) 69 { 70 SharedRelationships relationships = (SharedRelationships)object; 71 Element element = parent.getOwnerDocument().createElementNS(null,TAG_NAME); 72 73 String direction = relationships.getDirection(); 74 if ((direction != null && (direction.length() > 0))) 75 element.setAttribute("direction",direction); 76 77 Vector keyedRefVector = relationships.getKeyedReferenceVector(); 78 if ((keyedRefVector!=null) && (keyedRefVector.size() > 0)) 79 { 80 AbstractHandler handler = maker.lookup(KeyedReferenceHandler.TAG_NAME); 81 for (int i=0; i < keyedRefVector.size(); i++) 82 handler.marshal((KeyedReference)keyedRefVector.elementAt(i),element); 83 } 84 85 parent.appendChild(element); 86 } 87 88 89 90 91 92 93 94 public static void main(String args[]) 95 throws Exception 96 { 97 98 HandlerMaker maker = HandlerMaker.getInstance(); 99 AbstractHandler handler = maker.lookup(SharedRelationshipsHandler.TAG_NAME); 100 101 Element parent = XMLUtils.newRootElement(); 102 Element child = null; 103 104 SharedRelationships relationships = new SharedRelationships(); 105 relationships.setDirection("toKey"); 106 relationships.addKeyedReference(new KeyedReference("sharedRefKeyName","sharedRefKeyValue")); 107 relationships.addKeyedReference(new KeyedReference("uuid:8ff45356-acde-4a4c-86bf-f953611d20c6","sharedRefKeyName2","sharedRefKeyValue2")); 108 109 System.out.println(); 110 111 RegistryObject regObject = relationships; 112 handler.marshal(regObject,parent); 113 child = (Element )parent.getFirstChild(); 114 parent.removeChild(child); 115 XMLUtils.writeXML(child,System.out); 116 117 System.out.println(); 118 119 regObject = handler.unmarshal(child); 120 handler.marshal(regObject,parent); 121 child = (Element )parent.getFirstChild(); 122 parent.removeChild(child); 123 XMLUtils.writeXML(child,System.out); 124 } 125 } | Popular Tags |