KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

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

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

69     // Child Elements
70
nodeList = XMLUtils.getChildElementsByTagName(element,PublisherHandler.TAG_NAME);
71     for (int i=0; i<nodeList.size(); i++)
72     {
73       handler = maker.lookup(PublisherHandler.TAG_NAME);
74       obj.addPublisher((Publisher)handler.unmarshal((Element JavaDoc)nodeList.elementAt(i)));
75     }
76
77     return obj;
78   }
79
80   public void marshal(RegistryObject object,Element JavaDoc parent)
81   {
82     PublisherDetail detail = (PublisherDetail)object;
83     Element JavaDoc element = parent.getOwnerDocument().createElementNS(null,TAG_NAME);
84     AbstractHandler handler = null;
85
86     String JavaDoc generic = detail.getGeneric();
87     if (generic != null)
88     {
89       element.setAttribute("generic",generic);
90       element.setAttribute("xmlns",IRegistry.JUDDI_V1_NAMESPACE);
91     }
92     else
93       element.setAttribute("generic","");
94
95     String JavaDoc operator = detail.getOperator();
96     if (operator != null)
97       element.setAttribute("operator",operator);
98     else
99       element.setAttribute("operator","");
100
101     boolean truncated = detail.isTruncated();
102     if (truncated)
103       element.setAttribute("truncated","true");
104
105     Vector JavaDoc vector = detail.getPublisherVector();
106     if ((vector!=null) && (vector.size() > 0))
107     {
108       handler = maker.lookup(PublisherHandler.TAG_NAME);
109       for (int i=0; i < vector.size(); i++)
110         handler.marshal((Publisher)vector.elementAt(i),element);
111     }
112
113     parent.appendChild(element);
114   }
115
116
117   /***************************************************************************/
118   /***************************** TEST DRIVER *********************************/
119   /***************************************************************************/
120
121
122   public static void main(String JavaDoc args[])
123     throws Exception JavaDoc
124   {
125     HandlerMaker maker = HandlerMaker.getInstance();
126     AbstractHandler handler = maker.lookup(PublisherDetailHandler.TAG_NAME);
127
128     Element JavaDoc parent = XMLUtils.newRootElement();
129     Element JavaDoc child = null;
130
131     Publisher publisher = new Publisher();
132     publisher.setPublisherID("bcrosby");
133     publisher.setName("Bing Crosby");
134     publisher.setEmailAddress("bcrosby@juddi.org");
135     publisher.setAdmin(true);
136
137     PublisherDetail detail = new PublisherDetail();
138     detail.setGeneric("1.0");
139     detail.setOperator("jUDDI.org");
140     detail.setTruncated(false);
141     detail.addPublisher(publisher);
142     detail.addPublisher(publisher);
143
144     System.out.println();
145
146     RegistryObject regObject = detail;
147     handler.marshal(regObject,parent);
148     child = (Element JavaDoc)parent.getFirstChild();
149     parent.removeChild(child);
150     XMLUtils.writeXML(child,System.out);
151
152     System.out.println();
153
154     regObject = handler.unmarshal(child);
155     handler.marshal(regObject,parent);
156     child = (Element JavaDoc)parent.getFirstChild();
157     parent.removeChild(child);
158     XMLUtils.writeXML(child,System.out);
159
160     System.out.println();
161   }
162 }
Popular Tags