KickJava   Java API By Example, From Geeks To Geeks.

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


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

41 public class FindBindingHandler extends AbstractHandler
42 {
43   public static final String JavaDoc TAG_NAME = "find_binding";
44
45   private HandlerMaker maker = null;
46
47   protected FindBindingHandler(HandlerMaker maker)
48   {
49     this.maker = maker;
50   }
51
52   public RegistryObject unmarshal(Element JavaDoc element)
53   {
54     FindBinding obj = new FindBinding();
55     Vector JavaDoc nodeList = null;
56     AbstractHandler handler = null;
57
58     // Attributes
59
String JavaDoc serviceKey = element.getAttribute("serviceKey");
60     if ((serviceKey != null && (serviceKey.trim().length() > 0)))
61       obj.setServiceKey(serviceKey);
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.trim().length() > 0))
69       obj.setMaxRows(maxRows);
70
71     // Text Node Value
72
// {none}
73

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