KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > juddi > handler > RelatedBusinessInfoHandler


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 JavaDoc;
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 JavaDoc;
29
30 /**
31  * @author Steve Viens (sviens@apache.org)
32  */

33 public class RelatedBusinessInfoHandler extends AbstractHandler
34 {
35   public static final String JavaDoc 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 JavaDoc element)
45   {
46     RelatedBusinessInfo obj = new RelatedBusinessInfo();
47     Vector JavaDoc 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 JavaDoc)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 JavaDoc)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 JavaDoc)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 JavaDoc)nodeList.elementAt(0)));
87     }
88
89     return obj;
90   }
91
92   /**
93    *
94    */

95   public void marshal(RegistryObject object,Element JavaDoc parent)
96   {
97     RelatedBusinessInfo info = (RelatedBusinessInfo)object;
98     Element JavaDoc element = parent.getOwnerDocument().createElementNS(null,TAG_NAME);
99     AbstractHandler handler = null;
100
101     String JavaDoc businessKey = info.getBusinessKey();
102     if (businessKey != null)
103     {
104       handler = maker.lookup(BusinessKeyHandler.TAG_NAME);
105       handler.marshal(new BusinessKey(businessKey),element);
106     }
107
108     Vector JavaDoc nameVector = info.getNameVector();
109     if ((nameVector!=null) && (nameVector.size() > 0))
110     {
111       handler = maker.lookup(NameHandler.TAG_NAME);
112       for (int i=0; i < nameVector.size(); i++)
113         handler.marshal((org.apache.juddi.datatype.Name)nameVector.elementAt(i),element);
114     }
115
116     Vector JavaDoc descVector = info.getDescriptionVector();
117     if ((descVector!=null) && (descVector.size() > 0))
118     {
119       handler = maker.lookup(DescriptionHandler.TAG_NAME);
120       for (int i=0; i < descVector.size(); i++)
121         handler.marshal((Description)descVector.elementAt(i),element);
122     }
123
124     SharedRelationships relationships = info.getSharedRelationships();
125     if (relationships != null)
126     {
127       handler = maker.lookup(SharedRelationshipsHandler.TAG_NAME);
128       handler.marshal(relationships,element);
129     }
130
131     parent.appendChild(element);
132   }
133
134
135   /***************************************************************************/
136   /***************************** TEST DRIVER *********************************/
137   /***************************************************************************/
138
139
140   public static void main(String JavaDoc args[])
141     throws Exception JavaDoc
142   {
143     HandlerMaker maker = HandlerMaker.getInstance();
144     AbstractHandler handler = maker.lookup(RelatedBusinessInfoHandler.TAG_NAME);
145     Element JavaDoc parent = XMLUtils.newRootElement();
146     Element JavaDoc child = null;
147
148     RelatedBusinessInfo info = new RelatedBusinessInfo();
149     info.setBusinessKey("");
150     info.setBusinessKey("6c0ac186-d36b-4b81-bd27-066a5fe0fc1f");
151     info.addName(new Name("businessNm"));
152     info.addName(new Name("businessNm2","en"));
153     info.addDescription(new Description("business whatever"));
154     info.addDescription(new Description("business whatever too","fr"));
155     info.addKeyedReference(new KeyedReference("idBagKeyName","idBagkeyValue"));
156     info.addKeyedReference(new KeyedReference("uuid:f78a135a-4769-4e79-8604-54d440314bc0","idBagKeyName2","idBagkeyValue2"));
157
158     System.out.println();
159
160     RegistryObject regObject = info;
161     handler.marshal(regObject,parent);
162     child = (Element JavaDoc)parent.getFirstChild();
163     parent.removeChild(child);
164     XMLUtils.writeXML(child,System.out);
165
166     System.out.println();
167
168     regObject = handler.unmarshal(child);
169     handler.marshal(regObject,parent);
170     child = (Element JavaDoc)parent.getFirstChild();
171     parent.removeChild(child);
172     XMLUtils.writeXML(child,System.out);
173   }
174 }
Popular Tags