KickJava   Java API By Example, From Geeks To Geeks.

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


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.CategoryBag;
21 import org.apache.juddi.datatype.DiscoveryURL;
22 import org.apache.juddi.datatype.DiscoveryURLs;
23 import org.apache.juddi.datatype.IdentifierBag;
24 import org.apache.juddi.datatype.KeyedReference;
25 import org.apache.juddi.datatype.Name;
26 import org.apache.juddi.datatype.RegistryObject;
27 import org.apache.juddi.datatype.TModelBag;
28 import org.apache.juddi.datatype.TModelKey;
29 import org.apache.juddi.datatype.request.FindBusiness;
30 import org.apache.juddi.datatype.request.FindQualifier;
31 import org.apache.juddi.datatype.request.FindQualifiers;
32 import org.apache.juddi.util.xml.XMLUtils;
33 import org.w3c.dom.Element JavaDoc;
34
35 /**
36  * FindBusinessHandler
37  *
38  * "Knows about the creation and populating of FindBusiness objects.
39  * Returns FindBusiness."
40  *
41  * @author Steve Viens (sviens@apache.org)
42  * @author Anou Mana (anou_mana@users.sourceforge.net)
43  */

44 public class FindBusinessHandler extends AbstractHandler
45 {
46   public static final String JavaDoc TAG_NAME = "find_business";
47
48   private HandlerMaker maker = null;
49
50   protected FindBusinessHandler(HandlerMaker maker)
51   {
52     this.maker = maker;
53   }
54
55   public RegistryObject unmarshal(Element JavaDoc element)
56   {
57     FindBusiness obj = new FindBusiness();
58     Vector JavaDoc nodeList = null;
59     AbstractHandler handler = null;
60
61     // Attributes
62
String JavaDoc generic = element.getAttribute("generic");
63     if ((generic != null && (generic.trim().length() > 0)))
64       obj.setGeneric(generic);
65
66     String JavaDoc maxRows = element.getAttribute("maxRows");
67     if ((maxRows != null) && (maxRows.length() > 0))
68       obj.setMaxRows(maxRows);
69
70     // Text Node Value
71
// {none}
72

73     // Child Elements
74
nodeList = XMLUtils.getChildElementsByTagName(element,FindQualifiersHandler.TAG_NAME);
75     if (nodeList.size() > 0)
76     {
77       handler = maker.lookup(FindQualifiersHandler.TAG_NAME);
78       obj.setFindQualifiers((FindQualifiers)handler.unmarshal((Element JavaDoc)nodeList.elementAt(0)));
79     }
80
81     nodeList = XMLUtils.getChildElementsByTagName(element,NameHandler.TAG_NAME);
82     for (int i=0; i<nodeList.size(); i++)
83     {
84       handler = maker.lookup(NameHandler.TAG_NAME);
85       Name name = (Name )handler.unmarshal((Element JavaDoc)nodeList.elementAt(i));
86       if (name != null)
87         obj.addName(name);
88     }
89
90     nodeList = XMLUtils.getChildElementsByTagName(element,IdentifierBagHandler.TAG_NAME);
91     if (nodeList.size() > 0)
92     {
93       handler = maker.lookup(IdentifierBagHandler.TAG_NAME);
94       obj.setIdentifierBag((IdentifierBag)handler.unmarshal((Element JavaDoc)nodeList.elementAt(0)));
95     }
96
97     nodeList = XMLUtils.getChildElementsByTagName(element,CategoryBagHandler.TAG_NAME);
98     if (nodeList.size() > 0)
99     {
100       handler = maker.lookup(CategoryBagHandler.TAG_NAME);
101       obj.setCategoryBag((CategoryBag)handler.unmarshal((Element JavaDoc)nodeList.elementAt(0)));
102     }
103
104     nodeList = XMLUtils.getChildElementsByTagName(element,TModelBagHandler.TAG_NAME);
105     if (nodeList.size() > 0)
106     {
107       handler = maker.lookup(TModelBagHandler.TAG_NAME);
108       obj.setTModelBag((TModelBag)handler.unmarshal((Element JavaDoc)nodeList.elementAt(0)));
109     }
110
111     nodeList = XMLUtils.getChildElementsByTagName(element,DiscoveryURLsHandler.TAG_NAME);
112     if (nodeList.size() > 0)
113     {
114       handler = maker.lookup(DiscoveryURLsHandler.TAG_NAME);
115       obj.setDiscoveryURLs((DiscoveryURLs)handler.unmarshal((Element JavaDoc)nodeList.elementAt(0)));
116     }
117
118     return obj;
119   }
120
121   public void marshal(RegistryObject object,Element JavaDoc parent)
122   {
123     FindBusiness request = (FindBusiness)object;
124     Element JavaDoc element = parent.getOwnerDocument().createElementNS(null,TAG_NAME);
125     AbstractHandler handler = null;
126
127     String JavaDoc generic = request.getGeneric();
128     if (generic != null)
129       element.setAttribute("generic",generic);
130
131     int maxRows = request.getMaxRows();
132     if (maxRows > 0)
133       element.setAttribute("maxRows",String.valueOf(maxRows));
134
135
136   FindQualifiers qualifiers = request.getFindQualifiers();
137   if ((qualifiers != null) && (qualifiers.size() > 0))
138   {
139     handler = maker.lookup(FindQualifiersHandler.TAG_NAME);
140     handler.marshal(qualifiers,element);
141   }
142
143     Vector JavaDoc nameVector = request.getNameVector();
144     if ((nameVector!=null) && (nameVector.size() > 0))
145     {
146       handler = maker.lookup(NameHandler.TAG_NAME);
147       for (int i=0; i < nameVector.size(); i++)
148         handler.marshal((Name)nameVector.elementAt(i),element);
149     }
150
151     IdentifierBag idBag = request.getIdentifierBag();
152     if (idBag != null)
153     {
154       handler = maker.lookup(IdentifierBagHandler.TAG_NAME);
155       handler.marshal(idBag,element);
156     }
157
158     CategoryBag catBag = request.getCategoryBag();
159     if (catBag != null)
160     {
161       handler = maker.lookup(CategoryBagHandler.TAG_NAME);
162       handler.marshal(catBag,element);
163     }
164
165     TModelBag tModBag = request.getTModelBag();
166     if (tModBag != null)
167     {
168       handler = maker.lookup(TModelBagHandler.TAG_NAME);
169       handler.marshal(tModBag,element);
170     }
171
172   DiscoveryURLs discURLs = request.getDiscoveryURLs();
173   if (discURLs != null)
174   {
175     handler = maker.lookup(DiscoveryURLsHandler.TAG_NAME);
176     handler.marshal(discURLs,element);
177   }
178
179     parent.appendChild(element);
180   }
181
182
183   /***************************************************************************/
184   /***************************** TEST DRIVER *********************************/
185   /***************************************************************************/
186
187
188   public static void main(String JavaDoc args[])
189     throws Exception JavaDoc
190   {
191     HandlerMaker maker = HandlerMaker.getInstance();
192     AbstractHandler handler = maker.lookup(FindBusinessHandler.TAG_NAME);
193
194     Element JavaDoc parent = XMLUtils.newRootElement();
195     Element JavaDoc child = null;
196
197     DiscoveryURLs discURLs = new DiscoveryURLs();
198     discURLs.addDiscoveryURL(new DiscoveryURL("businessEntity","http://www.sviens.com"));
199
200     IdentifierBag idBag = new IdentifierBag();
201     idBag.addKeyedReference(new KeyedReference("idBagKeyName","idBagKeyValue"));
202     idBag.addKeyedReference(new KeyedReference("uuid:3860b975-9e0c-4cec-bad6-87dfe00e3864","idBagKeyName2","idBagKeyValue2"));
203
204     CategoryBag catBag = new CategoryBag();
205     catBag.addKeyedReference(new KeyedReference("catBagKeyName","catBagKeyValue"));
206     catBag.addKeyedReference(new KeyedReference("uuid:8ff45356-acde-4a4c-86bf-f953611d20c6","catBagKeyName2","catBagKeyValue2"));
207
208     TModelBag tModBag = new TModelBag();
209     tModBag.addTModelKey("uuid:35d9793b-9911-4b85-9f0e-5d4c65b4f253");
210     tModBag.addTModelKey(new TModelKey("uuid:c5ab113f-0d6b-4247-b3c4-8c012726acd8"));
211
212     FindBusiness request = new FindBusiness();
213     request.addName(new Name("serviceNm"));
214     request.addName(new Name("serviceNm2","en"));
215     request.addFindQualifier(new FindQualifier(FindQualifier.SORT_BY_DATE_ASC));
216     request.addFindQualifier(new FindQualifier(FindQualifier.AND_ALL_KEYS));
217     request.setMaxRows(43);
218     request.setDiscoveryURLs(discURLs);
219     request.setIdentifierBag(idBag);
220     request.setTModelBag(tModBag);
221     request.setCategoryBag(catBag);
222
223     System.out.println();
224
225     RegistryObject regObject = request;
226     handler.marshal(regObject,parent);
227     child = (Element JavaDoc)parent.getFirstChild();
228     parent.removeChild(child);
229     XMLUtils.writeXML(child,System.out);
230
231     System.out.println();
232
233     regObject = handler.unmarshal(child);
234     handler.marshal(regObject,parent);
235     child = (Element JavaDoc)parent.getFirstChild();
236     parent.removeChild(child);
237     XMLUtils.writeXML(child,System.out);
238   }
239 }
Popular Tags