KickJava   Java API By Example, From Geeks To Geeks.

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


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.Description;
22 import org.apache.juddi.datatype.Name;
23 import org.apache.juddi.datatype.RegistryObject;
24 import org.apache.juddi.datatype.response.BusinessInfo;
25 import org.apache.juddi.datatype.response.BusinessInfos;
26 import org.apache.juddi.datatype.response.RegisteredInfo;
27 import org.apache.juddi.datatype.response.ServiceInfo;
28 import org.apache.juddi.datatype.response.TModelInfo;
29 import org.apache.juddi.datatype.response.TModelInfos;
30 import org.apache.juddi.util.xml.XMLUtils;
31 import org.w3c.dom.Element JavaDoc;
32
33 /**
34  * RegisteredInfoHandler
35  *
36  * @author Steve Viens (sviens@apache.org)
37  * @author Anou Mana (anou_mana@users.sourceforge.net)
38  */

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

60     // Attributes
61
obj.setGeneric(element.getAttribute("generic"));
62     obj.setOperator(element.getAttribute("operator"));
63
64     // We can ignore the xmlns attribute since we
65
// can always determine it's value using the
66
// "generic" attribute. - Steve
67

68     // Text Node Value
69
// {none}
70

71     // Child Elements
72
nodeList = XMLUtils.getChildElementsByTagName(element,BusinessInfosHandler.TAG_NAME);
73     if (nodeList.size() > 0)
74     {
75       handler = maker.lookup(BusinessInfosHandler.TAG_NAME);
76       obj.setBusinessInfos((BusinessInfos)handler.unmarshal((Element JavaDoc)nodeList.elementAt(0)));
77     }
78
79     nodeList = XMLUtils.getChildElementsByTagName(element,TModelInfosHandler.TAG_NAME);
80     if (nodeList.size() > 0)
81     {
82       handler = maker.lookup(TModelInfosHandler.TAG_NAME);
83       obj.setTModelInfos((TModelInfos)handler.unmarshal((Element JavaDoc)nodeList.elementAt(0)));
84     }
85
86     return obj;
87   }
88
89   public void marshal(RegistryObject object,Element JavaDoc parent)
90   {
91     RegisteredInfo info = (RegisteredInfo)object;
92     Element JavaDoc element = parent.getOwnerDocument().createElementNS(null,TAG_NAME);
93     AbstractHandler handler = null;
94
95     String JavaDoc generic = info.getGeneric();
96     if ((generic != null) && (generic.trim().length() > 0))
97     {
98       element.setAttribute("generic",generic);
99
100       if (generic.equals(IRegistry.UDDI_V1_GENERIC))
101         element.setAttribute("xmlns",IRegistry.UDDI_V1_NAMESPACE);
102       else if (generic.equals(IRegistry.UDDI_V2_GENERIC))
103         element.setAttribute("xmlns",IRegistry.UDDI_V2_NAMESPACE);
104       else if (generic.equals(IRegistry.UDDI_V3_GENERIC))
105         element.setAttribute("xmlns",IRegistry.UDDI_V3_NAMESPACE);
106     }
107     else // Default to UDDI v2 values
108
{
109       element.setAttribute("generic",IRegistry.UDDI_V2_GENERIC);
110       element.setAttribute("xmlns",IRegistry.UDDI_V2_NAMESPACE);
111     }
112
113     String JavaDoc operator = info.getOperator();
114     if (operator != null)
115       element.setAttribute("operator",operator);
116     else
117       element.setAttribute("operator","");
118
119     BusinessInfos bInfos = info.getBusinessInfos();
120     if (bInfos!=null)
121     {
122       handler = maker.lookup(BusinessInfosHandler.TAG_NAME);
123       handler.marshal(bInfos,element);
124     }
125
126     TModelInfos tInfos = info.getTModelInfos();
127     if (tInfos!=null)
128     {
129       handler = maker.lookup(TModelInfosHandler.TAG_NAME);
130       handler.marshal(tInfos,element);
131     }
132
133     parent.appendChild(element);
134   }
135
136
137   /***************************************************************************/
138   /***************************** TEST DRIVER *********************************/
139   /***************************************************************************/
140
141
142   public static void main(String JavaDoc args[])
143     throws Exception JavaDoc
144   {
145     HandlerMaker maker = HandlerMaker.getInstance();
146     AbstractHandler handler = maker.lookup(RegisteredInfoHandler.TAG_NAME);
147
148     Element JavaDoc parent = XMLUtils.newRootElement();
149     Element JavaDoc child = null;
150
151     ServiceInfo sInfo = new ServiceInfo();
152     sInfo.addName(new Name("regInfoServNm"));
153     sInfo.addName(new Name("regInfoServNm2","en"));
154     sInfo.setServiceKey("826e6443-e3c5-442b-9bf8-943071ca83f4");
155     sInfo.setBusinessKey("56644a68-8779-46c7-93ce-90eeb7569f3f");
156
157     BusinessInfo bInfo = new BusinessInfo();
158     bInfo.setBusinessKey("56644a68-8779-46c7-93ce-90eeb7569f3f");
159     bInfo.addName(new Name("regInfoBizNm"));
160     bInfo.addName(new Name("regInfoBizNm2","en"));
161     bInfo.addDescription(new Description("regInfoBiz whatever"));
162     bInfo.addDescription(new Description("regInfoBiz whatever too","fr"));
163     bInfo.addServiceInfo(sInfo);
164
165     BusinessInfos bInfos = new BusinessInfos();
166     bInfos.addBusinessInfo(bInfo);
167     bInfos.addBusinessInfo(bInfo);
168
169     TModelInfo tInfo = new TModelInfo();
170     tInfo.setTModelKey("uuid:e86bd2a9-03f6-44c4-b619-400ef2cd7e47");
171     tInfo.setName(new Name("RegInfoTestDriver"));
172
173     TModelInfos tInfos = new TModelInfos();
174     tInfos.addTModelInfo(tInfo);
175     tInfos.addTModelInfo(tInfo);
176
177     RegisteredInfo info = new RegisteredInfo();
178     info.setGeneric("2.0");
179     info.setOperator("jUDDI.org");
180     info.setBusinessInfos(bInfos);
181     info.setTModelInfos(tInfos);
182
183     System.out.println();
184
185     RegistryObject regObject = info;
186     handler.marshal(regObject,parent);
187     child = (Element JavaDoc)parent.getFirstChild();
188     parent.removeChild(child);
189     XMLUtils.writeXML(child,System.out);
190
191     System.out.println();
192
193     regObject = handler.unmarshal(child);
194     handler.marshal(regObject,parent);
195     child = (Element JavaDoc)parent.getFirstChild();
196     parent.removeChild(child);
197     XMLUtils.writeXML(child,System.out);
198   }
199 }
Popular Tags