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.BusinessKey;
21  import org.apache.juddi.datatype.RegistryObject;
22  import org.apache.juddi.datatype.response.RelatedBusinessInfo;
23  import org.apache.juddi.datatype.response.RelatedBusinessInfos;
24  import org.apache.juddi.datatype.response.RelatedBusinessesList;
25  import org.apache.juddi.util.xml.XMLUtils;
26  import org.w3c.dom.Element;
27  
28  /***
29   * @author Steve Viens (sviens@apache.org)
30   * @author Anou Mana (anou_mana@users.sourceforge.net)
31   */
32  public class RelatedBusinessesListHandler extends AbstractHandler
33  {
34    public static final String TAG_NAME = "relatedBusinessList";
35  
36    private HandlerMaker maker = null;
37  
38    protected RelatedBusinessesListHandler(HandlerMaker maker)
39    {
40      this.maker = maker;
41    }
42  
43    public RegistryObject unmarshal(Element element)
44    {
45      RelatedBusinessesList obj = new RelatedBusinessesList();
46      Vector nodeList = null;
47      AbstractHandler handler = null;
48  
49      // We could use the generic attribute value to
50      // determine which version of UDDI was used to
51      // format the request XML. - Steve
52  
53      // Attributes
54      obj.setGeneric(element.getAttribute("generic"));
55      obj.setOperator(element.getAttribute("operator"));
56  
57      // We can ignore the xmlns attribute since we
58      // can always determine it's value using the
59      // "generic" attribute. - Steve
60  
61      String truncValue = element.getAttribute("truncated");
62      if (truncValue != null)
63        obj.setTruncated(truncValue.equalsIgnoreCase("true"));
64  
65      // Text Node Value
66      // {none}
67  
68      // Child Elements
69      nodeList = XMLUtils.getChildElementsByTagName(element,BusinessKeyHandler.TAG_NAME);
70      if (nodeList.size() > 0)
71      {
72        handler = maker.lookup(BusinessKeyHandler.TAG_NAME);
73        obj.setBusinessKey((BusinessKey)handler.unmarshal((Element)nodeList.elementAt(0)));
74      }
75  
76      nodeList = XMLUtils.getChildElementsByTagName(element,RelatedBusinessInfosHandler.TAG_NAME);
77      if (nodeList.size() > 0)
78      {
79        handler = maker.lookup(RelatedBusinessInfosHandler.TAG_NAME);
80        obj.setRelatedBusinessInfos((RelatedBusinessInfos)handler.unmarshal((Element)nodeList.elementAt(0)));
81      }
82  
83      return obj;
84    }
85  
86    public void marshal(RegistryObject object,Element parent)
87    {
88      RelatedBusinessesList list = (RelatedBusinessesList)object;
89      String generic = list.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 = list.getOperator();
98      if (operator != null)
99        element.setAttribute("operator",operator);
100     else
101       element.setAttribute("operator","");
102 
103     boolean truncated = list.isTruncated();
104     if (truncated)
105       element.setAttribute("truncated","true");
106 
107     BusinessKey key = list.getBusinessKey();
108     if (key != null)
109     {
110       handler = maker.lookup(BusinessKeyHandler.TAG_NAME);
111       handler.marshal(key,element);
112     }
113 
114     RelatedBusinessInfos infos = list.getRelatedBusinessInfos();
115     if (infos != null)
116     {
117       handler = maker.lookup(RelatedBusinessInfosHandler.TAG_NAME);
118       handler.marshal(infos,element);
119     }
120 
121     parent.appendChild(element);
122   }
123 
124 
125   /****************************************************************************/
126   /****************************** TEST DRIVER *********************************/
127   /****************************************************************************/
128 
129 
130   public static void main(String args[])
131     throws Exception
132   {
133     HandlerMaker maker = HandlerMaker.getInstance();
134     AbstractHandler handler = maker.lookup(RelatedBusinessesListHandler.TAG_NAME);
135 
136     Element parent = XMLUtils.newRootElement();
137     Element child = null;
138 
139     RelatedBusinessesList list = new RelatedBusinessesList();
140     list.setGeneric("2.0");
141     list.setOperator("jUDDI.org");
142     list.setTruncated(true);
143     list.setBusinessKey(new BusinessKey("f9f0c35f-06ab-4bec-9c7d-b1469e73f0eb"));
144     list.addRelatedBusinessInfo(new RelatedBusinessInfo("abc"));
145     list.addRelatedBusinessInfo(new RelatedBusinessInfo("xyz"));
146 
147     System.out.println();
148 
149     RegistryObject regObject = list;
150     handler.marshal(regObject,parent);
151     child = (Element)parent.getFirstChild();
152     parent.removeChild(child);
153     XMLUtils.writeXML(child,System.out);
154 
155     System.out.println();
156 
157     regObject = handler.unmarshal(child);
158     handler.marshal(regObject,parent);
159     child = (Element)parent.getFirstChild();
160     parent.removeChild(child);
161     XMLUtils.writeXML(child,System.out);
162   }
163 }