KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

74     // Child Elements
75
nodeList = XMLUtils.getChildElementsByTagName(element,NameHandler.TAG_NAME);
76     for (int i=0; i<nodeList.size(); i++)
77     {
78       handler = maker.lookup(NameHandler.TAG_NAME);
79       Name name = (Name )handler.unmarshal((Element JavaDoc)nodeList.elementAt(i));
80       if (name != null)
81         obj.addName(name);
82     }
83
84     nodeList = XMLUtils.getChildElementsByTagName(element,FindQualifiersHandler.TAG_NAME);
85     if (nodeList.size() > 0)
86     {
87       handler = maker.lookup(FindQualifiersHandler.TAG_NAME);
88       obj.setFindQualifiers((FindQualifiers)handler.unmarshal((Element JavaDoc)nodeList.elementAt(0)));
89     }
90
91     nodeList = XMLUtils.getChildElementsByTagName(element,TModelBagHandler.TAG_NAME);
92     if (nodeList.size() > 0)
93     {
94       handler = maker.lookup(TModelBagHandler.TAG_NAME);
95       obj.setTModelBag((TModelBag)handler.unmarshal((Element JavaDoc)nodeList.elementAt(0)));
96     }
97
98     nodeList = XMLUtils.getChildElementsByTagName(element,CategoryBagHandler.TAG_NAME);
99     if (nodeList.size() > 0)
100     {
101       handler = maker.lookup(CategoryBagHandler.TAG_NAME);
102       obj.setCategoryBag((CategoryBag)handler.unmarshal((Element JavaDoc)nodeList.elementAt(0)));
103     }
104
105     return obj;
106   }
107
108   public void marshal(RegistryObject object,Element JavaDoc parent)
109   {
110     FindService request = (FindService)object;
111     Element JavaDoc element = parent.getOwnerDocument().createElementNS(null,TAG_NAME);
112     AbstractHandler handler = null;
113
114     String JavaDoc key = request.getBusinessKey();
115     if (key != null)
116       element.setAttribute("businessKey",key);
117
118     String JavaDoc generic = request.getGeneric();
119     if (generic != null)
120       element.setAttribute("generic",generic);
121
122     int maxRows = request.getMaxRows();
123     if (maxRows > 0)
124       element.setAttribute("maxRows",String.valueOf(maxRows));
125
126     FindQualifiers qualifiers = request.getFindQualifiers();
127     if ((qualifiers != null) && (qualifiers.size() > 0))
128     {
129       handler = maker.lookup(FindQualifiersHandler.TAG_NAME);
130       handler.marshal(qualifiers,element);
131     }
132
133     Vector JavaDoc nameVector = request.getNameVector();
134     if ((nameVector!=null) && (nameVector.size() > 0))
135     {
136       handler = maker.lookup(NameHandler.TAG_NAME);
137       for (int i=0; i < nameVector.size(); i++)
138         handler.marshal((Name)nameVector.elementAt(i),element);
139     }
140
141     CategoryBag catBag = request.getCategoryBag();
142     if (catBag != null)
143     {
144       handler = maker.lookup(CategoryBagHandler.TAG_NAME);
145       handler.marshal(catBag,element);
146     }
147
148     TModelBag tModBag = request.getTModelBag();
149     if (tModBag != null)
150     {
151       handler = maker.lookup(TModelBagHandler.TAG_NAME);
152       handler.marshal(tModBag,element);
153     }
154
155     parent.appendChild(element);
156   }
157
158
159   /***************************************************************************/
160   /***************************** TEST DRIVER *********************************/
161   /***************************************************************************/
162
163
164   public static void main(String JavaDoc args[])
165     throws Exception JavaDoc
166   {
167     HandlerMaker maker = HandlerMaker.getInstance();
168     AbstractHandler handler = maker.lookup(FindServiceHandler.TAG_NAME);
169
170     Element JavaDoc parent = XMLUtils.newRootElement();
171     Element JavaDoc child = null;
172
173     CategoryBag catBag = new CategoryBag();
174     catBag.addKeyedReference(new KeyedReference("catBagKeyName","catBagKeyValue"));
175     catBag.addKeyedReference(new KeyedReference("uuid:8ff45356-acde-4a4c-86bf-f953611d20c6","catBagKeyName2","catBagKeyValue2"));
176
177     TModelBag tModBag = new TModelBag();
178     tModBag.addTModelKey("uuid:35d9793b-9911-4b85-9f0e-5d4c65b4f253");
179     tModBag.addTModelKey(new TModelKey("uuid:c5ab113f-0d6b-4247-b3c4-8c012726acd8"));
180
181     FindService request = new FindService();
182     request.addName(new Name("serviceNm"));
183     request.addName(new Name("serviceNm2","en"));
184     request.addFindQualifier(new FindQualifier(FindQualifier.SORT_BY_DATE_ASC));
185     request.addFindQualifier(new FindQualifier(FindQualifier.AND_ALL_KEYS));
186     request.setMaxRows(50);
187     request.setBusinessKey("fd36dbce-bc3e-468b-8346-5374975a0843");
188     request.setTModelBag(tModBag);
189     request.setCategoryBag(catBag);
190
191     System.out.println();
192
193     RegistryObject regObject = request;
194     handler.marshal(regObject,parent);
195     child = (Element JavaDoc)parent.getFirstChild();
196     parent.removeChild(child);
197     XMLUtils.writeXML(child,System.out);
198
199     System.out.println();
200
201     regObject = handler.unmarshal(child);
202     handler.marshal(regObject,parent);
203     child = (Element JavaDoc)parent.getFirstChild();
204     parent.removeChild(child);
205     XMLUtils.writeXML(child,System.out);
206   }
207 }
Popular Tags