KickJava   Java API By Example, From Geeks To Geeks.

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


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.BusinessKey;
22 import org.apache.juddi.datatype.RegistryObject;
23 import org.apache.juddi.datatype.response.RelatedBusinessInfo;
24 import org.apache.juddi.datatype.response.RelatedBusinessInfos;
25 import org.apache.juddi.datatype.response.RelatedBusinessesList;
26 import org.apache.juddi.util.xml.XMLUtils;
27 import org.w3c.dom.Element JavaDoc;
28
29 /**
30  * @author Steve Viens (sviens@apache.org)
31  * @author Anou Mana (anou_mana@users.sourceforge.net)
32  */

33 public class RelatedBusinessesListHandler extends AbstractHandler
34 {
35   public static final String JavaDoc TAG_NAME = "relatedBusinessList";
36
37   private HandlerMaker maker = null;
38
39   protected RelatedBusinessesListHandler(HandlerMaker maker)
40   {
41     this.maker = maker;
42   }
43
44   public RegistryObject unmarshal(Element JavaDoc element)
45   {
46     RelatedBusinessesList obj = new RelatedBusinessesList();
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,BusinessKeyHandler.TAG_NAME);
71     if (nodeList.size() > 0)
72     {
73       handler = maker.lookup(BusinessKeyHandler.TAG_NAME);
74       obj.setBusinessKey((BusinessKey)handler.unmarshal((Element JavaDoc)nodeList.elementAt(0)));
75     }
76
77     nodeList = XMLUtils.getChildElementsByTagName(element,RelatedBusinessInfosHandler.TAG_NAME);
78     if (nodeList.size() > 0)
79     {
80       handler = maker.lookup(RelatedBusinessInfosHandler.TAG_NAME);
81       obj.setRelatedBusinessInfos((RelatedBusinessInfos)handler.unmarshal((Element JavaDoc)nodeList.elementAt(0)));
82     }
83
84     return obj;
85   }
86
87   public void marshal(RegistryObject object,Element JavaDoc parent)
88   {
89     RelatedBusinessesList list = (RelatedBusinessesList)object;
90     Element JavaDoc element = parent.getOwnerDocument().createElementNS(null,TAG_NAME);
91     AbstractHandler handler = null;
92
93     String JavaDoc generic = list.getGeneric();
94     if ((generic != null) && (generic.trim().length() > 0))
95     {
96       element.setAttribute("generic",generic);
97
98       if (generic.equals(IRegistry.UDDI_V1_GENERIC))
99         element.setAttribute("xmlns",IRegistry.UDDI_V1_NAMESPACE);
100       else if (generic.equals(IRegistry.UDDI_V2_GENERIC))
101         element.setAttribute("xmlns",IRegistry.UDDI_V2_NAMESPACE);
102       else if (generic.equals(IRegistry.UDDI_V3_GENERIC))
103         element.setAttribute("xmlns",IRegistry.UDDI_V3_NAMESPACE);
104     }
105     else // Default to UDDI v2 values
106
{
107       element.setAttribute("generic",IRegistry.UDDI_V2_GENERIC);
108       element.setAttribute("xmlns",IRegistry.UDDI_V2_NAMESPACE);
109     }
110
111     String JavaDoc operator = list.getOperator();
112     if (operator != null)
113       element.setAttribute("operator",operator);
114     else
115       element.setAttribute("operator","");
116
117     boolean truncated = list.isTruncated();
118     if (truncated)
119       element.setAttribute("truncated","true");
120
121     BusinessKey key = list.getBusinessKey();
122     if (key != null)
123     {
124       handler = maker.lookup(BusinessKeyHandler.TAG_NAME);
125       handler.marshal(key,element);
126     }
127
128     RelatedBusinessInfos infos = list.getRelatedBusinessInfos();
129     if (infos != null)
130     {
131       handler = maker.lookup(RelatedBusinessInfosHandler.TAG_NAME);
132       handler.marshal(infos,element);
133     }
134
135     parent.appendChild(element);
136   }
137
138
139   /***************************************************************************/
140   /***************************** TEST DRIVER *********************************/
141   /***************************************************************************/
142
143
144   public static void main(String JavaDoc args[])
145     throws Exception JavaDoc
146   {
147     HandlerMaker maker = HandlerMaker.getInstance();
148     AbstractHandler handler = maker.lookup(RelatedBusinessesListHandler.TAG_NAME);
149
150     Element JavaDoc parent = XMLUtils.newRootElement();
151     Element JavaDoc child = null;
152
153     RelatedBusinessesList list = new RelatedBusinessesList();
154     list.setGeneric("2.0");
155     list.setOperator("jUDDI.org");
156     list.setTruncated(true);
157     list.setBusinessKey(new BusinessKey("f9f0c35f-06ab-4bec-9c7d-b1469e73f0eb"));
158     list.addRelatedBusinessInfo(new RelatedBusinessInfo("abc"));
159     list.addRelatedBusinessInfo(new RelatedBusinessInfo("xyz"));
160
161     System.out.println();
162
163     RegistryObject regObject = list;
164     handler.marshal(regObject,parent);
165     child = (Element JavaDoc)parent.getFirstChild();
166     parent.removeChild(child);
167     XMLUtils.writeXML(child,System.out);
168
169     System.out.println();
170
171     regObject = handler.unmarshal(child);
172     handler.marshal(regObject,parent);
173     child = (Element JavaDoc)parent.getFirstChild();
174     parent.removeChild(child);
175     XMLUtils.writeXML(child,System.out);
176   }
177 }
Popular Tags