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.assertion.PublisherAssertion;
23  import org.apache.juddi.datatype.response.PublisherAssertions;
24  import org.apache.juddi.util.xml.XMLUtils;
25  import org.w3c.dom.Element;
26  
27  /***
28   * @author Steve Viens (sviens@apache.org)
29   */
30  public class PublisherAssertionsHandler extends AbstractHandler
31  {
32    public static final String TAG_NAME = "publisherAssertions";
33  
34    private HandlerMaker maker = null;
35  
36    protected PublisherAssertionsHandler(HandlerMaker maker)
37    {
38      this.maker = maker;
39    }
40  
41    public RegistryObject unmarshal(Element element)
42    {
43      PublisherAssertions obj = new PublisherAssertions();
44      Vector nodeList = null;
45      AbstractHandler handler = null;
46  
47      // We could use the generic attribute value to
48      // determine which version of UDDI was used to
49      // format the request XML. - Steve
50  
51      // Attributes
52      obj.setGeneric(element.getAttribute("generic"));
53      obj.setOperator(element.getAttribute("operator"));
54  
55      // We can ignore the xmlns attribute since we
56      // can always determine it's value using the
57      // "generic" attribute. - Steve
58  
59      // Text Node Value
60      // {none}
61  
62      // Child Elements
63      nodeList = XMLUtils.getChildElementsByTagName(element,PublisherAssertionHandler.TAG_NAME);
64      for (int i=0; i<nodeList.size(); i++)
65      {
66        handler = maker.lookup(PublisherAssertionHandler.TAG_NAME);
67        obj.addPublisherAssertion((PublisherAssertion)handler.unmarshal((Element)nodeList.elementAt(i)));
68      }
69  
70      return obj;
71    }
72  
73    public void marshal(RegistryObject object,Element parent)
74    {
75      PublisherAssertions assertions = (PublisherAssertions)object;
76      String generic = assertions.getGeneric();
77      generic = getGeneric(generic);
78      String namespace = getUDDINamespace(generic);
79      Element element = parent.getOwnerDocument().createElementNS(namespace,TAG_NAME);
80      AbstractHandler handler = null;
81  
82      element.setAttribute("generic",generic);
83  
84      String operator = assertions.getOperator();
85      if (operator != null)
86        element.setAttribute("operator",operator);
87      else
88        element.setAttribute("operator","");
89  
90      Vector vector = assertions.getPublisherAssertionVector();
91      if ((vector!=null) && (vector.size() > 0))
92      {
93        handler = maker.lookup(PublisherAssertionHandler.TAG_NAME);
94        for (int i=0; i < vector.size(); i++)
95          handler.marshal((PublisherAssertion)vector.elementAt(i),element);
96      }
97  
98      parent.appendChild(element);
99    }
100 
101 
102   /****************************************************************************/
103   /****************************** TEST DRIVER *********************************/
104   /****************************************************************************/
105 
106 
107   public static void main(String args[])
108     throws Exception
109   {
110     HandlerMaker maker = HandlerMaker.getInstance();
111     AbstractHandler handler = maker.lookup(PublisherAssertionsHandler.TAG_NAME);
112     Element parent = XMLUtils.newRootElement();
113     Element child = null;
114 
115     PublisherAssertion assertion = new PublisherAssertion();
116     assertion.setFromKey("b2f072e7-6013-4385-93b4-9c1c2ece1c8f");
117     assertion.setToKey("115be72d-0c04-4b5f-a729-79a522629c19");
118     assertion.setKeyedReference(new KeyedReference("uuid:8ff45356-acde-4a4c-86bf-f953611d20c6","catBagKeyName2","catBagKeyValue2"));
119 
120     PublisherAssertions assertions = new PublisherAssertions();
121     assertions.setGeneric("2.0");
122     assertions.setOperator("jUDDI.org");
123     assertions.addPublisherAssertion(assertion);
124     assertions.addPublisherAssertion(assertion);
125 
126     System.out.println();
127 
128     RegistryObject regObject = assertions;
129     handler.marshal(regObject,parent);
130     child = (Element)parent.getFirstChild();
131     parent.removeChild(child);
132     XMLUtils.writeXML(child,System.out);
133 
134     System.out.println();
135 
136     regObject = handler.unmarshal(child);
137     handler.marshal(regObject,parent);
138     child = (Element)parent.getFirstChild();
139     parent.removeChild(child);
140     XMLUtils.writeXML(child,System.out);
141   }
142 }