View Javadoc

1   /*
2    * Copyright 2001-2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
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  /***
27   * "Knows about the creation and populating of SharedRelationships objects.
28   * Returns SharedRelationships."
29   *
30   * @author Steve Viens (sviens@apache.org)
31   */
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      // Attributes
50      String direction = element.getAttribute("direction");
51      if ((direction != null && (direction.trim().length() > 0)))
52        obj.setDirection(direction);
53  
54      // Text Node Value
55      // {none}
56  
57      // Child Elements
58      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      String generic = getGeneric(null);
72      String namespace = getUDDINamespace(generic);
73      Element element = parent.getOwnerDocument().createElementNS(namespace,TAG_NAME);
74  
75      String direction = relationships.getDirection();
76      if ((direction != null && (direction.length() > 0)))
77        element.setAttribute("direction",direction);
78  
79      Vector keyedRefVector = relationships.getKeyedReferenceVector();
80      if ((keyedRefVector!=null) && (keyedRefVector.size() > 0))
81      {
82        AbstractHandler handler = maker.lookup(KeyedReferenceHandler.TAG_NAME);
83        for (int i=0; i < keyedRefVector.size(); i++)
84          handler.marshal((KeyedReference)keyedRefVector.elementAt(i),element);
85      }
86  
87      parent.appendChild(element);
88    }
89  
90  
91    /****************************************************************************/
92    /****************************** TEST DRIVER *********************************/
93    /****************************************************************************/
94  
95  
96    public static void main(String args[])
97      throws Exception
98    {
99  
100     HandlerMaker maker = HandlerMaker.getInstance();
101     AbstractHandler handler = maker.lookup(SharedRelationshipsHandler.TAG_NAME);
102 
103     Element parent = XMLUtils.newRootElement();
104     Element child = null;
105 
106     SharedRelationships relationships = new SharedRelationships();
107     relationships.setDirection("toKey");
108     relationships.addKeyedReference(new KeyedReference("sharedRefKeyName","sharedRefKeyValue"));
109     relationships.addKeyedReference(new KeyedReference("uuid:8ff45356-acde-4a4c-86bf-f953611d20c6","sharedRefKeyName2","sharedRefKeyValue2"));
110 
111     System.out.println();
112 
113     RegistryObject regObject = relationships;
114     handler.marshal(regObject,parent);
115     child = (Element)parent.getFirstChild();
116     parent.removeChild(child);
117     XMLUtils.writeXML(child,System.out);
118 
119     System.out.println();
120 
121     regObject = handler.unmarshal(child);
122     handler.marshal(regObject,parent);
123     child = (Element)parent.getFirstChild();
124     parent.removeChild(child);
125     XMLUtils.writeXML(child,System.out);
126   }
127 }