View Javadoc

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;
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;
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 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 element)
48    {
49      BindingTemplate obj = new BindingTemplate();
50      Vector 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)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)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)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)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 parent)
103   {
104     BindingTemplate binding = (BindingTemplate)object;
105     String generic = getGeneric(null);
106     String namespace = getUDDINamespace(generic);
107     Element element = parent.getOwnerDocument().createElementNS(namespace,TAG_NAME);
108     AbstractHandler handler = null;
109 
110     String bindingKey = binding.getBindingKey();
111     if (bindingKey != null)
112       element.setAttribute("bindingKey",bindingKey);
113     else
114       element.setAttribute("bindingKey","");
115 
116     String serviceKey = binding.getServiceKey();
117     if (serviceKey != null)
118       element.setAttribute("serviceKey",serviceKey);
119 
120     Vector descrVector = binding.getDescriptionVector();
121     if ((descrVector!=null) && (descrVector.size() > 0))
122     {
123       handler = maker.lookup(DescriptionHandler.TAG_NAME);
124       for (int i=0; i < descrVector.size(); i++)
125         handler.marshal((Description)descrVector.elementAt(i),element);
126     }
127 
128     AccessPoint accessPoint = binding.getAccessPoint();
129     if (accessPoint != null)
130     {
131       handler = maker.lookup(AccessPointHandler.TAG_NAME);
132       handler.marshal(accessPoint,element);
133     }
134     else // Can never contain both a AccessPoint and a HostingRedirector
135     {
136       HostingRedirector redirector = binding.getHostingRedirector();
137       if (redirector != null)
138       {
139         handler = maker.lookup(HostingRedirectorHandler.TAG_NAME);
140         handler.marshal(redirector,element);
141       }
142     }
143     
144     TModelInstanceDetails tModInstDet = binding.getTModelInstanceDetails();
145     if (tModInstDet != null)
146     {
147       handler = maker.lookup(TModelInstanceDetailsHandler.TAG_NAME);
148       handler.marshal(tModInstDet,element);
149     }
150 
151 //  TODO (UDDI v3) This code should be uncommented when jUDDI is ready to support UDDI v3.0
152 //    CategoryBag categoryBag = binding.getCategoryBag(); // UDDI v3.0
153 //    if ((categoryBag != null) && (categoryBag.getKeyedReferenceVector() != null) && (!categoryBag.getKeyedReferenceVector().isEmpty()))
154 //    {
155 //      handler = maker.lookup(CategoryBagHandler.TAG_NAME);
156 //      handler.marshal(categoryBag,element);
157 //    }
158 
159     parent.appendChild(element);
160   }
161 
162 
163   /****************************************************************************/
164   /****************************** TEST DRIVER *********************************/
165   /****************************************************************************/
166 
167 
168   public static void main(String args[])
169     throws Exception
170   {
171     HandlerMaker maker = HandlerMaker.getInstance();
172     AbstractHandler handler = maker.lookup(BindingTemplateHandler.TAG_NAME);
173     Element parent = XMLUtils.newRootElement();
174     Element child = null;
175 
176     OverviewDoc overDoc = new OverviewDoc();
177     overDoc.setOverviewURL("http://www.sviens.com/service.html");
178     overDoc.addDescription(new Description("over-doc Descr"));
179     overDoc.addDescription(new Description("over-doc Descr Two","en"));
180 
181     InstanceDetails instDetails = new InstanceDetails();
182     instDetails.addDescription(new Description("body-isa-wunder"));
183     instDetails.addDescription(new Description("sweetchild-o-mine","it"));
184     instDetails.setInstanceParms("inst-det parameters");
185     instDetails.setOverviewDoc(overDoc);
186 
187     TModelInstanceInfo tModInstInfo = new TModelInstanceInfo();
188     tModInstInfo.setTModelKey("uuid:ae0f9fd4-287f-40c9-be91-df47a7c72fd9");
189     tModInstInfo.addDescription(new Description("tMod-Inst-Info"));
190     tModInstInfo.addDescription(new Description("tMod-Inst-Info too","es"));
191     tModInstInfo.setInstanceDetails(instDetails);
192 
193     TModelInstanceDetails tModInstDet = new TModelInstanceDetails();
194     tModInstDet.addTModelInstanceInfo(tModInstInfo);
195 
196     BindingTemplate binding =  new BindingTemplate();
197     binding.setBindingKey("c9613c3c-fe55-4f34-a3da-b3167afbca4a");
198     binding.setServiceKey("997a55bc-563d-4c04-8a94-781604870d31");
199     binding.addDescription(new Description("whatever"));
200     binding.addDescription(new Description("whatever too","fr"));
201     binding.setHostingRedirector(new HostingRedirector("92658289-0bd7-443c-8948-0bb4460b44c0"));
202     binding.setAccessPoint(new AccessPoint(AccessPoint.HTTP,"http://www.sviens.com/service.wsdl"));
203     binding.setTModelInstanceDetails(tModInstDet);
204 
205     System.out.println();
206 
207     RegistryObject regObject = binding;
208     handler.marshal(regObject,parent);
209     child = (Element)parent.getFirstChild();
210     parent.removeChild(child);
211     XMLUtils.writeXML(child,System.out);
212 
213     System.out.println();
214 
215     regObject = handler.unmarshal(child);
216     handler.marshal(regObject,parent);
217     child = (Element)parent.getFirstChild();
218     parent.removeChild(child);
219     XMLUtils.writeXML(child,System.out);
220 
221     System.out.println();
222   }
223 }