KickJava   Java API By Example, From Geeks To Geeks.

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


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.datatype.Description;
21 import org.apache.juddi.datatype.OverviewDoc;
22 import org.apache.juddi.datatype.RegistryObject;
23 import org.apache.juddi.datatype.binding.AccessPoint;
24 import org.apache.juddi.datatype.binding.BindingTemplate;
25 import org.apache.juddi.datatype.binding.HostingRedirector;
26 import org.apache.juddi.datatype.binding.InstanceDetails;
27 import org.apache.juddi.datatype.binding.TModelInstanceDetails;
28 import org.apache.juddi.datatype.binding.TModelInstanceInfo;
29 import org.apache.juddi.util.xml.XMLUtils;
30 import org.w3c.dom.Element JavaDoc;
31
32 /**
33  * @author Steve Viens (sviens@apache.org)
34  * @author Anou Mana (anou_mana@users.sourceforge.net)
35  */

36 public class BindingTemplateHandler extends AbstractHandler
37 {
38   public static final String JavaDoc TAG_NAME = "bindingTemplate";
39
40   private HandlerMaker maker = null;
41
42   protected BindingTemplateHandler(HandlerMaker maker)
43   {
44     this.maker = maker;
45   }
46
47   public RegistryObject unmarshal(Element JavaDoc element)
48   {
49     BindingTemplate obj = new BindingTemplate();
50     Vector JavaDoc nodeList = null;
51     AbstractHandler handler = null;
52
53     // Attributes
54
obj.setServiceKey(element.getAttribute("serviceKey"));
55     obj.setBindingKey(element.getAttribute("bindingKey"));
56
57     // Text Node Value
58
// {none}
59

60     // Child Elements
61
nodeList = XMLUtils.getChildElementsByTagName(element,DescriptionHandler.TAG_NAME);
62     for (int i=0; i<nodeList.size(); i++)
63     {
64       handler = maker.lookup(DescriptionHandler.TAG_NAME);
65       Description descr = (Description)handler.unmarshal((Element JavaDoc)nodeList.elementAt(i));
66       if (descr != null)
67         obj.addDescription(descr);
68     }
69
70     nodeList = XMLUtils.getChildElementsByTagName(element,AccessPointHandler.TAG_NAME);
71     if (nodeList.size() > 0)
72     {
73       handler = maker.lookup(AccessPointHandler.TAG_NAME);
74       obj.setAccessPoint((AccessPoint)handler.unmarshal((Element JavaDoc)nodeList.elementAt(0)));
75     }
76
77     nodeList = XMLUtils.getChildElementsByTagName(element,HostingRedirectorHandler.TAG_NAME);
78     if (nodeList.size() > 0)
79     {
80       handler = maker.lookup(HostingRedirectorHandler.TAG_NAME);
81       obj.setHostingRedirector((HostingRedirector)handler.unmarshal((Element JavaDoc)nodeList.elementAt(0)));
82     }
83
84     nodeList = XMLUtils.getChildElementsByTagName(element,TModelInstanceDetailsHandler.TAG_NAME);
85     if (nodeList.size() > 0)
86     {
87       handler = maker.lookup(TModelInstanceDetailsHandler.TAG_NAME);
88       obj.setTModelInstanceDetails((TModelInstanceDetails)handler.unmarshal((Element JavaDoc)nodeList.elementAt(0)));
89     }
90
91 // TODO (UDDI v3) This code should be uncommented when jUDDI is ready to support UDDI v3.0
92
// nodeList = XMLUtils.getChildElementsByTagName(element,CategoryBagHandler.TAG_NAME); // UDDI v3.0
93
// if (nodeList.size() > 0)
94
// {
95
// handler = maker.lookup(CategoryBagHandler.TAG_NAME);
96
// obj.setCategoryBag((CategoryBag)handler.unmarshal((Element)nodeList.elementAt(0)));
97
// }
98

99     return obj;
100   }
101
102   public void marshal(RegistryObject object,Element JavaDoc parent)
103   {
104     BindingTemplate binding = (BindingTemplate)object;
105     Element JavaDoc element = parent.getOwnerDocument().createElementNS(null,TAG_NAME);
106     AbstractHandler handler = null;
107
108     String JavaDoc bindingKey = binding.getBindingKey();
109     if (bindingKey != null)
110       element.setAttribute("bindingKey",bindingKey);
111     else
112       element.setAttribute("bindingKey","");
113
114     String JavaDoc serviceKey = binding.getServiceKey();
115     if (serviceKey != null)
116       element.setAttribute("serviceKey",serviceKey);
117
118     Vector JavaDoc descrVector = binding.getDescriptionVector();
119     if ((descrVector!=null) && (descrVector.size() > 0))
120     {
121       handler = maker.lookup(DescriptionHandler.TAG_NAME);
122       for (int i=0; i < descrVector.size(); i++)
123         handler.marshal((Description)descrVector.elementAt(i),element);
124     }
125
126     AccessPoint accessPoint = binding.getAccessPoint();
127     if (accessPoint != null)
128     {
129       handler = maker.lookup(AccessPointHandler.TAG_NAME);
130       handler.marshal(accessPoint,element);
131     }
132     else // Can never contain both a AccessPoint and a HostingRedirector
133
{
134       HostingRedirector redirector = binding.getHostingRedirector();
135       if (redirector != null)
136       {
137         handler = maker.lookup(HostingRedirectorHandler.TAG_NAME);
138         handler.marshal(redirector,element);
139       }
140     }
141     
142     TModelInstanceDetails tModInstDet = binding.getTModelInstanceDetails();
143     if (tModInstDet != null)
144     {
145       handler = maker.lookup(TModelInstanceDetailsHandler.TAG_NAME);
146       handler.marshal(tModInstDet,element);
147     }
148
149 // TODO (UDDI v3) This code should be uncommented when jUDDI is ready to support UDDI v3.0
150
// CategoryBag categoryBag = binding.getCategoryBag(); // UDDI v3.0
151
// if ((categoryBag != null) && (categoryBag.getKeyedReferenceVector() != null) && (!categoryBag.getKeyedReferenceVector().isEmpty()))
152
// {
153
// handler = maker.lookup(CategoryBagHandler.TAG_NAME);
154
// handler.marshal(categoryBag,element);
155
// }
156

157     parent.appendChild(element);
158   }
159
160
161   /***************************************************************************/
162   /***************************** TEST DRIVER *********************************/
163   /***************************************************************************/
164
165
166   public static void main(String JavaDoc args[])
167     throws Exception JavaDoc
168   {
169     HandlerMaker maker = HandlerMaker.getInstance();
170     AbstractHandler handler = maker.lookup(BindingTemplateHandler.TAG_NAME);
171     Element JavaDoc parent = XMLUtils.newRootElement();
172     Element JavaDoc child = null;
173
174     OverviewDoc overDoc = new OverviewDoc();
175     overDoc.setOverviewURL("http://www.sviens.com/service.html");
176     overDoc.addDescription(new Description("over-doc Descr"));
177     overDoc.addDescription(new Description("over-doc Descr Two","en"));
178
179     InstanceDetails instDetails = new InstanceDetails();
180     instDetails.addDescription(new Description("body-isa-wunder"));
181     instDetails.addDescription(new Description("sweetchild-o-mine","it"));
182     instDetails.setInstanceParms("inst-det parameters");
183     instDetails.setOverviewDoc(overDoc);
184
185     TModelInstanceInfo tModInstInfo = new TModelInstanceInfo();
186     tModInstInfo.setTModelKey("uuid:ae0f9fd4-287f-40c9-be91-df47a7c72fd9");
187     tModInstInfo.addDescription(new Description("tMod-Inst-Info"));
188     tModInstInfo.addDescription(new Description("tMod-Inst-Info too","es"));
189     tModInstInfo.setInstanceDetails(instDetails);
190
191     TModelInstanceDetails tModInstDet = new TModelInstanceDetails();
192     tModInstDet.addTModelInstanceInfo(tModInstInfo);
193
194     BindingTemplate binding = new BindingTemplate();
195     binding.setBindingKey("c9613c3c-fe55-4f34-a3da-b3167afbca4a");
196     binding.setServiceKey("997a55bc-563d-4c04-8a94-781604870d31");
197     binding.addDescription(new Description("whatever"));
198     binding.addDescription(new Description("whatever too","fr"));
199     binding.setHostingRedirector(new HostingRedirector("92658289-0bd7-443c-8948-0bb4460b44c0"));
200     binding.setAccessPoint(new AccessPoint(AccessPoint.HTTP,"http://www.sviens.com/service.wsdl"));
201     binding.setTModelInstanceDetails(tModInstDet);
202
203     System.out.println();
204
205     RegistryObject regObject = binding;
206     handler.marshal(regObject,parent);
207     child = (Element JavaDoc)parent.getFirstChild();
208     parent.removeChild(child);
209     XMLUtils.writeXML(child,System.out);
210
211     System.out.println();
212
213     regObject = handler.unmarshal(child);
214     handler.marshal(regObject,parent);
215     child = (Element JavaDoc)parent.getFirstChild();
216     parent.removeChild(child);
217     XMLUtils.writeXML(child,System.out);
218
219     System.out.println();
220   }
221 }
Popular Tags