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.datatype.response.BindingDetail;
30  import org.apache.juddi.util.xml.XMLUtils;
31  import org.w3c.dom.Element;
32  
33  /***
34   * "Knows about the creation and populating of BindingDetail objects.
35   * Returns BindingDetail."
36   *
37   * @author Steve Viens (sviens@apache.org)
38   */
39  public class BindingDetailHandler extends AbstractHandler
40  {
41    public static final String TAG_NAME = "bindingDetail";
42  
43    private HandlerMaker maker = null;
44  
45    protected BindingDetailHandler(HandlerMaker maker)
46    {
47      this.maker = maker;
48    }
49  
50    public RegistryObject unmarshal(Element element)
51    {
52      BindingDetail obj = new BindingDetail();
53      Vector 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      String truncValue = element.getAttribute("truncated");
69      if (truncValue != null)
70        obj.setTruncated(truncValue.equalsIgnoreCase("true"));
71  
72      // Text Node Value
73      // {none}
74  
75      // Child Elements
76      nodeList = XMLUtils.getChildElementsByTagName(element,BindingTemplateHandler.TAG_NAME);
77      for (int i=0; i<nodeList.size(); i++)
78      {
79        handler = maker.lookup(BindingTemplateHandler.TAG_NAME);
80        obj.addBindingTemplate((BindingTemplate)handler.unmarshal((Element)nodeList.elementAt(i)));
81      }
82  
83      return obj;
84    }
85  
86    public void marshal(RegistryObject object,Element parent)
87    {
88      BindingDetail detail = (BindingDetail)object;
89      String generic = detail.getGeneric();
90      generic = getGeneric(generic);
91      String namespace = getUDDINamespace(generic);
92      Element element = parent.getOwnerDocument().createElementNS(namespace,TAG_NAME);
93      AbstractHandler handler = null;
94  
95      element.setAttribute("generic",generic);
96  
97      String operator = detail.getOperator();
98      if (operator != null)
99        element.setAttribute("operator",operator);
100     else
101       element.setAttribute("operator","");
102 
103     boolean truncated = detail.isTruncated();
104     if (truncated)
105       element.setAttribute("truncated","true");
106 
107     Vector vector = detail.getBindingTemplateVector();
108     if ((vector!=null) && (vector.size() > 0))
109     {
110       handler = maker.lookup(BindingTemplateHandler.TAG_NAME);
111       for (int i=0; i < vector.size(); i++)
112         handler.marshal((BindingTemplate)vector.elementAt(i),element);
113     }
114 
115     parent.appendChild(element);
116   }
117 
118 
119   /****************************************************************************/
120   /****************************** TEST DRIVER *********************************/
121   /****************************************************************************/
122 
123 
124   public static void main(String args[])
125     throws Exception
126   {
127     HandlerMaker maker = HandlerMaker.getInstance();
128     AbstractHandler handler = maker.lookup(BindingDetailHandler.TAG_NAME);
129     Element parent = XMLUtils.newRootElement();
130     Element child = null;
131 
132     OverviewDoc overDoc = new OverviewDoc();
133     overDoc.setOverviewURL("http://www.sviens.com/service.html");
134     overDoc.addDescription(new Description("over-doc Descr"));
135     overDoc.addDescription(new Description("over-doc Descr Two","en"));
136 
137     InstanceDetails instDetails = new InstanceDetails();
138     instDetails.addDescription(new Description("body-isa-wunder"));
139     instDetails.addDescription(new Description("sweetchild-o-mine","it"));
140     instDetails.setInstanceParms("inst-det parameters");
141     instDetails.setOverviewDoc(overDoc);
142 
143     TModelInstanceInfo tModInstInfo = new TModelInstanceInfo();
144     tModInstInfo.setTModelKey("uuid:ae0f9fd4-287f-40c9-be91-df47a7c72fd9");
145     tModInstInfo.addDescription(new Description("tMod-Inst-Info"));
146     tModInstInfo.addDescription(new Description("tMod-Inst-Info too","es"));
147     tModInstInfo.setInstanceDetails(instDetails);
148 
149     TModelInstanceDetails tModInstDet = new TModelInstanceDetails();
150     tModInstDet.addTModelInstanceInfo(tModInstInfo);
151 
152     BindingTemplate binding =  new BindingTemplate();
153     binding.setBindingKey("c9613c3c-fe55-4f34-a3da-b3167afbca4a");
154     binding.setServiceKey("997a55bc-563d-4c04-8a94-781604870d31");
155     binding.addDescription(new Description("whatever"));
156     binding.addDescription(new Description("whatever too","fr"));
157     binding.setHostingRedirector(new HostingRedirector("92658289-0bd7-443c-8948-0bb4460b44c0"));
158     binding.setAccessPoint(new AccessPoint(AccessPoint.HTTP,"http://www.sviens.com/service.wsdl"));
159     binding.setTModelInstanceDetails(tModInstDet);
160 
161     BindingDetail detail = new BindingDetail();
162     detail.setGeneric("2.0");
163     detail.setOperator("jUDDI.org");
164     detail.setTruncated(true);
165     detail.addBindingTemplate(binding);
166     detail.addBindingTemplate(binding);
167 
168     System.out.println();
169 
170     RegistryObject regObject = detail;
171     handler.marshal(regObject,parent);
172     child = (Element)parent.getFirstChild();
173     parent.removeChild(child);
174     XMLUtils.writeXML(child,System.out);
175 
176     System.out.println();
177 
178     regObject = handler.unmarshal(child);
179     handler.marshal(regObject,parent);
180     child = (Element)parent.getFirstChild();
181     parent.removeChild(child);
182     XMLUtils.writeXML(child,System.out);
183 
184     System.out.println();
185   }
186 }