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.Description;
22  import org.apache.juddi.datatype.KeyedReference;
23  import org.apache.juddi.datatype.Name;
24  import org.apache.juddi.datatype.RegistryObject;
25  import org.apache.juddi.datatype.SharedRelationships;
26  import org.apache.juddi.datatype.response.RelatedBusinessInfo;
27  import org.apache.juddi.util.xml.XMLUtils;
28  import org.w3c.dom.Element;
29  
30  /***
31   * @author Steve Viens (sviens@apache.org)
32   */
33  public class RelatedBusinessInfoHandler extends AbstractHandler
34  {
35    public static final String TAG_NAME = "relatedBusinessInfo";
36  
37    private HandlerMaker maker = null;
38  
39    protected RelatedBusinessInfoHandler(HandlerMaker maker)
40    {
41      this.maker = maker;
42    }
43  
44    public RegistryObject unmarshal(Element element)
45    {
46      RelatedBusinessInfo obj = new RelatedBusinessInfo();
47      Vector nodeList = null;
48      AbstractHandler handler = null;
49  
50      // Attributes
51      // {none}
52  
53      // Text Node Value
54      // {none}
55  
56      // Child Elements
57      nodeList = XMLUtils.getChildElementsByTagName(element,BusinessKeyHandler.TAG_NAME);
58      if (nodeList.size() > 0)
59      {
60        handler = maker.lookup(BusinessKeyHandler.TAG_NAME);
61        obj.setBusinessKey((BusinessKey)handler.unmarshal((Element)nodeList.elementAt(0)));
62      }
63  
64      nodeList = XMLUtils.getChildElementsByTagName(element,NameHandler.TAG_NAME);
65      for (int i=0; i<nodeList.size(); i++)
66      {
67        handler = maker.lookup(NameHandler.TAG_NAME);
68        Name name = (Name )handler.unmarshal((Element)nodeList.elementAt(i));
69        if (name != null)
70          obj.addName(name);
71      }
72  
73      nodeList = XMLUtils.getChildElementsByTagName(element,DescriptionHandler.TAG_NAME);
74      for (int i=0; i<nodeList.size(); i++)
75      {
76        handler = maker.lookup(DescriptionHandler.TAG_NAME);
77        Description descr = (Description)handler.unmarshal((Element)nodeList.elementAt(i));
78        if (descr != null)
79          obj.addDescription(descr);
80      }
81  
82      nodeList = XMLUtils.getChildElementsByTagName(element,SharedRelationshipsHandler.TAG_NAME);
83      if (nodeList.size() > 0)
84      {
85        handler = maker.lookup(SharedRelationshipsHandler.TAG_NAME);
86        obj.setSharedRelationships((SharedRelationships)handler.unmarshal((Element)nodeList.elementAt(0)));
87      }
88  
89      return obj;
90    }
91  
92    /***
93     *
94     */
95    public void marshal(RegistryObject object,Element parent)
96    {
97      RelatedBusinessInfo info = (RelatedBusinessInfo)object;
98      String generic = getGeneric(null);
99      String namespace = getUDDINamespace(generic);
100     Element element = parent.getOwnerDocument().createElementNS(namespace,TAG_NAME);
101     AbstractHandler handler = null;
102 
103     String businessKey = info.getBusinessKey();
104     if (businessKey != null)
105     {
106       handler = maker.lookup(BusinessKeyHandler.TAG_NAME);
107       handler.marshal(new BusinessKey(businessKey),element);
108     }
109 
110     Vector nameVector = info.getNameVector();
111     if ((nameVector!=null) && (nameVector.size() > 0))
112     {
113       handler = maker.lookup(NameHandler.TAG_NAME);
114       for (int i=0; i < nameVector.size(); i++)
115         handler.marshal((org.apache.juddi.datatype.Name)nameVector.elementAt(i),element);
116     }
117 
118     Vector descVector = info.getDescriptionVector();
119     if ((descVector!=null) && (descVector.size() > 0))
120     {
121       handler = maker.lookup(DescriptionHandler.TAG_NAME);
122       for (int i=0; i < descVector.size(); i++)
123         handler.marshal((Description)descVector.elementAt(i),element);
124     }
125 
126     SharedRelationships relationships = info.getSharedRelationships();
127     if (relationships != null)
128     {
129       handler = maker.lookup(SharedRelationshipsHandler.TAG_NAME);
130       handler.marshal(relationships,element);
131     }
132 
133     parent.appendChild(element);
134   }
135 
136 
137   /****************************************************************************/
138   /****************************** TEST DRIVER *********************************/
139   /****************************************************************************/
140 
141 
142   public static void main(String args[])
143     throws Exception
144   {
145     HandlerMaker maker = HandlerMaker.getInstance();
146     AbstractHandler handler = maker.lookup(RelatedBusinessInfoHandler.TAG_NAME);
147     Element parent = XMLUtils.newRootElement();
148     Element child = null;
149 
150     RelatedBusinessInfo info = new RelatedBusinessInfo();
151     info.setBusinessKey("");
152     info.setBusinessKey("6c0ac186-d36b-4b81-bd27-066a5fe0fc1f");
153     info.addName(new Name("businessNm"));
154     info.addName(new Name("businessNm2","en"));
155     info.addDescription(new Description("business whatever"));
156     info.addDescription(new Description("business whatever too","fr"));
157     info.addKeyedReference(new KeyedReference("idBagKeyName","idBagkeyValue"));
158     info.addKeyedReference(new KeyedReference("uuid:f78a135a-4769-4e79-8604-54d440314bc0","idBagKeyName2","idBagkeyValue2"));
159 
160     System.out.println();
161 
162     RegistryObject regObject = info;
163     handler.marshal(regObject,parent);
164     child = (Element)parent.getFirstChild();
165     parent.removeChild(child);
166     XMLUtils.writeXML(child,System.out);
167 
168     System.out.println();
169 
170     regObject = handler.unmarshal(child);
171     handler.marshal(regObject,parent);
172     child = (Element)parent.getFirstChild();
173     parent.removeChild(child);
174     XMLUtils.writeXML(child,System.out);
175   }
176 }