KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > juddi > handler > PublisherAssertionsHandler


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 JavaDoc;
19
20 import org.apache.juddi.IRegistry;
21 import org.apache.juddi.datatype.KeyedReference;
22 import org.apache.juddi.datatype.RegistryObject;
23 import org.apache.juddi.datatype.assertion.PublisherAssertion;
24 import org.apache.juddi.datatype.response.PublisherAssertions;
25 import org.apache.juddi.util.xml.XMLUtils;
26 import org.w3c.dom.Element JavaDoc;
27
28 /**
29  * @author Steve Viens (sviens@apache.org)
30  */

31 public class PublisherAssertionsHandler extends AbstractHandler
32 {
33   public static final String JavaDoc TAG_NAME = "publisherAssertions";
34
35   private HandlerMaker maker = null;
36
37   protected PublisherAssertionsHandler(HandlerMaker maker)
38   {
39     this.maker = maker;
40   }
41
42   public RegistryObject unmarshal(Element JavaDoc element)
43   {
44     PublisherAssertions obj = new PublisherAssertions();
45     Vector JavaDoc nodeList = null;
46     AbstractHandler handler = null;
47
48     // We could use the generic attribute value to
49
// determine which version of UDDI was used to
50
// format the request XML. - Steve
51

52     // Attributes
53
obj.setGeneric(element.getAttribute("generic"));
54     obj.setOperator(element.getAttribute("operator"));
55
56     // We can ignore the xmlns attribute since we
57
// can always determine it's value using the
58
// "generic" attribute. - Steve
59

60     // Text Node Value
61
// {none}
62

63     // Child Elements
64
nodeList = XMLUtils.getChildElementsByTagName(element,PublisherAssertionHandler.TAG_NAME);
65     for (int i=0; i<nodeList.size(); i++)
66     {
67       handler = maker.lookup(PublisherAssertionHandler.TAG_NAME);
68       obj.addPublisherAssertion((PublisherAssertion)handler.unmarshal((Element JavaDoc)nodeList.elementAt(i)));
69     }
70
71     return obj;
72   }
73
74   public void marshal(RegistryObject object,Element JavaDoc parent)
75   {
76     PublisherAssertions assertions = (PublisherAssertions)object;
77     Element JavaDoc element = parent.getOwnerDocument().createElementNS(null,TAG_NAME);
78     AbstractHandler handler = null;
79
80     String JavaDoc generic = assertions.getGeneric();
81     if ((generic != null) && (generic.trim().length() > 0))
82     {
83       element.setAttribute("generic",generic);
84
85       if (generic.equals(IRegistry.UDDI_V1_GENERIC))
86         element.setAttribute("xmlns",IRegistry.UDDI_V1_NAMESPACE);
87       else if (generic.equals(IRegistry.UDDI_V2_GENERIC))
88         element.setAttribute("xmlns",IRegistry.UDDI_V2_NAMESPACE);
89       else if (generic.equals(IRegistry.UDDI_V3_GENERIC))
90         element.setAttribute("xmlns",IRegistry.UDDI_V3_NAMESPACE);
91     }
92     else // Default to UDDI v2 values
93
{
94       element.setAttribute("generic",IRegistry.UDDI_V2_GENERIC);
95       element.setAttribute("xmlns",IRegistry.UDDI_V2_NAMESPACE);
96     }
97
98     String JavaDoc operator = assertions.getOperator();
99     if (operator != null)
100       element.setAttribute("operator",operator);
101     else
102       element.setAttribute("operator","");
103
104     Vector JavaDoc vector = assertions.getPublisherAssertionVector();
105     if ((vector!=null) && (vector.size() > 0))
106     {
107       handler = maker.lookup(PublisherAssertionHandler.TAG_NAME);
108       for (int i=0; i < vector.size(); i++)
109         handler.marshal((PublisherAssertion)vector.elementAt(i),element);
110     }
111
112     parent.appendChild(element);
113   }
114
115
116   /***************************************************************************/
117   /***************************** TEST DRIVER *********************************/
118   /***************************************************************************/
119
120
121   public static void main(String JavaDoc args[])
122     throws Exception JavaDoc
123   {
124     HandlerMaker maker = HandlerMaker.getInstance();
125     AbstractHandler handler = maker.lookup(PublisherAssertionsHandler.TAG_NAME);
126     Element JavaDoc parent = XMLUtils.newRootElement();
127     Element JavaDoc child = null;
128
129     PublisherAssertion assertion = new PublisherAssertion();
130     assertion.setFromKey("b2f072e7-6013-4385-93b4-9c1c2ece1c8f");
131     assertion.setToKey("115be72d-0c04-4b5f-a729-79a522629c19");
132     assertion.setKeyedReference(new KeyedReference("uuid:8ff45356-acde-4a4c-86bf-f953611d20c6","catBagKeyName2","catBagKeyValue2"));
133
134     PublisherAssertions assertions = new PublisherAssertions();
135     assertions.setGeneric("2.0");
136     assertions.setOperator("jUDDI.org");
137     assertions.addPublisherAssertion(assertion);
138     assertions.addPublisherAssertion(assertion);
139
140     System.out.println();
141
142     RegistryObject regObject = assertions;
143     handler.marshal(regObject,parent);
144     child = (Element JavaDoc)parent.getFirstChild();
145     parent.removeChild(child);
146     XMLUtils.writeXML(child,System.out);
147
148     System.out.println();
149
150     regObject = handler.unmarshal(child);
151     handler.marshal(regObject,parent);
152     child = (Element JavaDoc)parent.getFirstChild();
153     parent.removeChild(child);
154     XMLUtils.writeXML(child,System.out);
155   }
156 }
Popular Tags