KickJava   Java API By Example, From Geeks To Geeks.

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


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.RegistryObject;
22 import org.apache.juddi.datatype.response.PublisherInfo;
23 import org.apache.juddi.datatype.response.PublisherInfos;
24 import org.apache.juddi.datatype.response.PublisherList;
25 import org.apache.juddi.util.xml.XMLUtils;
26 import org.w3c.dom.Element JavaDoc;
27
28 /**
29  * "Knows about the creation and populating of PublisherList objects.
30  * Returns PublisherList."
31  *
32  * @author Steve Viens (sviens@apache.org)
33  */

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

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

63     String JavaDoc truncValue = element.getAttribute("truncated");
64     if (truncValue != null)
65       obj.setTruncated(truncValue.equalsIgnoreCase("true"));
66
67     // Text Node Value
68
// {none}
69

70     // Child Elements
71
nodeList = XMLUtils.getChildElementsByTagName(element,PublisherInfosHandler.TAG_NAME);
72     if (nodeList.size() > 0)
73     {
74       handler = maker.lookup(PublisherInfosHandler.TAG_NAME);
75       obj.setPublisherInfos((PublisherInfos)handler.unmarshal((Element JavaDoc)nodeList.elementAt(0)));
76     }
77
78     return obj;
79   }
80
81   public void marshal(RegistryObject object,Element JavaDoc parent)
82   {
83     PublisherList list = (PublisherList)object;
84     Element JavaDoc element = parent.getOwnerDocument().createElementNS(null,TAG_NAME);
85     AbstractHandler handler = null;
86
87     String JavaDoc generic = list.getGeneric();
88     if ((generic != null) && (generic.trim().length() > 0))
89     {
90       element.setAttribute("generic",generic);
91       element.setAttribute("xmlns",IRegistry.JUDDI_V1_NAMESPACE);
92     }
93     else // Default to UDDI v2 and jUDDI v1 values
94
{
95       element.setAttribute("generic",IRegistry.UDDI_V2_GENERIC);
96       element.setAttribute("xmlns",IRegistry.JUDDI_V1_NAMESPACE);
97     }
98
99     String JavaDoc operator = list.getOperator();
100     if (operator != null)
101       element.setAttribute("operator",operator);
102     else
103       element.setAttribute("operator","");
104
105     boolean truncated = list.isTruncated();
106     if (truncated)
107       element.setAttribute("truncated","true");
108
109     PublisherInfos infos = list.getPublisherInfos();
110     if (infos != null)
111     {
112       handler = maker.lookup(PublisherInfosHandler.TAG_NAME);
113       handler.marshal(infos,element);
114     }
115
116     parent.appendChild(element);
117   }
118
119
120   /***************************************************************************/
121   /***************************** TEST DRIVER *********************************/
122   /***************************************************************************/
123
124
125   public static void main(String JavaDoc args[])
126     throws Exception JavaDoc
127   {
128     HandlerMaker maker = HandlerMaker.getInstance();
129     AbstractHandler handler = maker.lookup(PublisherListHandler.TAG_NAME);
130
131     Element JavaDoc parent = XMLUtils.newRootElement();
132     Element JavaDoc child = null;
133
134     PublisherList list = new PublisherList();
135     list.setGeneric("1.0");
136     list.setOperator("jUDDI.org");
137     list.setTruncated(false);
138     list.addPublisherInfo(new PublisherInfo("sviens","Steve Viens"));
139     list.addPublisherInfo(new PublisherInfo("jdoe","John Doe"));
140
141     System.out.println();
142
143     RegistryObject regObject = list;
144     handler.marshal(regObject,parent);
145     child = (Element JavaDoc)parent.getFirstChild();
146     parent.removeChild(child);
147     XMLUtils.writeXML(child,System.out);
148
149     System.out.println();
150
151     regObject = handler.unmarshal(child);
152     handler.marshal(regObject,parent);
153     child = (Element JavaDoc)parent.getFirstChild();
154     parent.removeChild(child);
155     XMLUtils.writeXML(child,System.out);
156
157     System.out.println();
158   }
159 }
Popular Tags