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.RegistryObject;
21  import org.apache.juddi.datatype.response.RelatedBusinessInfo;
22  import org.apache.juddi.datatype.response.RelatedBusinessInfos;
23  import org.apache.juddi.util.xml.XMLUtils;
24  import org.w3c.dom.Element;
25  
26  /***
27   * @author Steve Viens (sviens@apache.org)
28   */
29  public class RelatedBusinessInfosHandler extends AbstractHandler
30  {
31    public static final String TAG_NAME = "relatedBusinessInfos";
32  
33    private HandlerMaker maker = null;
34  
35    protected RelatedBusinessInfosHandler(HandlerMaker maker)
36    {
37      this.maker = maker;
38    }
39  
40    public RegistryObject unmarshal(Element element)
41    {
42      RelatedBusinessInfos obj = new RelatedBusinessInfos();
43      Vector nodeList = null;
44      AbstractHandler handler = null;
45  
46      // Attributes
47      // {none}
48  
49      // Text Node Value
50      // {none}
51  
52      // Child Elements
53      nodeList = XMLUtils.getChildElementsByTagName(element,RelatedBusinessInfoHandler.TAG_NAME);
54      for (int i=0; i<nodeList.size(); i++)
55      {
56        handler = maker.lookup(RelatedBusinessInfoHandler.TAG_NAME);
57        obj.addRelatedBusinessInfo((RelatedBusinessInfo)handler.unmarshal((Element)nodeList.elementAt(i)));
58      }
59  
60      return obj;
61    }
62  
63    public void marshal(RegistryObject object,Element parent)
64    {
65      RelatedBusinessInfos infos = (RelatedBusinessInfos)object;
66      String generic = getGeneric(null);
67      String namespace = getUDDINamespace(generic);
68      Element element = parent.getOwnerDocument().createElementNS(namespace,TAG_NAME);
69      AbstractHandler handler = null;
70  
71      Vector vector = infos.getRelatedBusinessInfoVector();
72      if ((vector!=null) && (vector.size() > 0))
73      {
74        handler = maker.lookup(RelatedBusinessInfoHandler.TAG_NAME);
75        for (int i=0; i < vector.size(); i++)
76          handler.marshal((RelatedBusinessInfo)vector.elementAt(i),element);
77      }
78  
79      parent.appendChild(element);
80    }
81  
82  
83    /****************************************************************************/
84    /****************************** TEST DRIVER *********************************/
85    /****************************************************************************/
86  
87  
88    public static void main(String args[])
89      throws Exception
90    {
91      HandlerMaker maker = HandlerMaker.getInstance();
92      AbstractHandler handler = maker.lookup(RelatedBusinessInfosHandler.TAG_NAME);
93      Element parent = XMLUtils.newRootElement();
94      Element child = null;
95  
96      RelatedBusinessInfos infos = new RelatedBusinessInfos();
97      infos.addRelatedBusinessInfo(new RelatedBusinessInfo());
98      infos.addRelatedBusinessInfo(new RelatedBusinessInfo());
99  
100     System.out.println();
101 
102     RegistryObject regObject = infos;
103     handler.marshal(regObject,parent);
104     child = (Element)parent.getFirstChild();
105     parent.removeChild(child);
106     XMLUtils.writeXML(child,System.out);
107 
108     System.out.println();
109 
110     regObject = handler.unmarshal(child);
111     handler.marshal(regObject,parent);
112     child = (Element)parent.getFirstChild();
113     parent.removeChild(child);
114     XMLUtils.writeXML(child,System.out);
115   }
116 }