KickJava   Java API By Example, From Geeks To Geeks.

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


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.KeyedReference;
22 import org.apache.juddi.datatype.RegistryObject;
23 import org.apache.juddi.datatype.request.FindQualifier;
24 import org.apache.juddi.datatype.request.FindQualifiers;
25 import org.apache.juddi.datatype.request.FindRelatedBusinesses;
26 import org.apache.juddi.util.xml.XMLUtils;
27 import org.w3c.dom.Element JavaDoc;
28
29 /**
30  * @author Steve Viens (sviens@apache.org)
31  * @author Anou Mana (anou_mana@users.sourceforge.net)
32  */

33 public class FindRelatedBusinessesHandler extends AbstractHandler
34 {
35   public static final String JavaDoc TAG_NAME = "find_relatedBusinesses";
36
37   private HandlerMaker maker = null;
38
39   protected FindRelatedBusinessesHandler(HandlerMaker maker)
40   {
41     this.maker = maker;
42   }
43
44   public RegistryObject unmarshal(Element JavaDoc element)
45   {
46     FindRelatedBusinesses obj = new FindRelatedBusinesses();
47     Vector JavaDoc nodeList = null;
48     AbstractHandler handler = null;
49
50     // Attributes
51
String JavaDoc generic = element.getAttribute("generic");
52     if ((generic != null && (generic.trim().length() > 0)))
53       obj.setGeneric(generic);
54
55     String JavaDoc maxRows = element.getAttribute("maxRows");
56     if ((maxRows != null) && (maxRows.length() > 0))
57       obj.setMaxRows(maxRows);
58
59     // Text Node Value
60
// {none}
61

62     // Child Elements
63
nodeList = XMLUtils.getChildElementsByTagName(element,BusinessKeyHandler.TAG_NAME);
64     if (nodeList.size() > 0)
65     {
66       handler = maker.lookup(BusinessKeyHandler.TAG_NAME);
67       obj.setBusinessKey((BusinessKey)handler.unmarshal((Element JavaDoc)nodeList.elementAt(0)));
68     }
69
70     nodeList = XMLUtils.getChildElementsByTagName(element,FindQualifiersHandler.TAG_NAME);
71     if (nodeList.size() > 0)
72     {
73       handler = maker.lookup(FindQualifiersHandler.TAG_NAME);
74       obj.setFindQualifiers((FindQualifiers)handler.unmarshal((Element JavaDoc)nodeList.elementAt(0)));
75     }
76
77     nodeList = XMLUtils.getChildElementsByTagName(element,KeyedReferenceHandler.TAG_NAME);
78     if (nodeList.size() > 0)
79     {
80       handler = maker.lookup(KeyedReferenceHandler.TAG_NAME);
81       obj.setKeyedReference((KeyedReference)handler.unmarshal((Element JavaDoc)nodeList.elementAt(0)));
82     }
83
84     return obj;
85   }
86
87   public void marshal(RegistryObject object,Element JavaDoc parent)
88   {
89     FindRelatedBusinesses request = (FindRelatedBusinesses)object;
90     Element JavaDoc element = parent.getOwnerDocument().createElementNS(null,TAG_NAME);
91     AbstractHandler handler = null;
92
93     String JavaDoc generic = request.getGeneric();
94     if (generic != null)
95       element.setAttribute("generic",generic);
96
97     int maxRows = request.getMaxRows();
98     if (maxRows > 0)
99       element.setAttribute("maxRows",String.valueOf(maxRows));
100
101     FindQualifiers qualifiers = request.getFindQualifiers();
102     if ((qualifiers != null) && (qualifiers.size() > 0))
103     {
104       handler = maker.lookup(FindQualifiersHandler.TAG_NAME);
105       handler.marshal(qualifiers,element);
106     }
107
108   BusinessKey businessKey = new BusinessKey(request.getBusinessKey());
109   if (businessKey != null)
110   {
111     handler = maker.lookup(BusinessKeyHandler.TAG_NAME);
112     handler.marshal(businessKey,element);
113   }
114
115     KeyedReference keyedRef = request.getKeyedReference();
116     if (keyedRef != null)
117     {
118       handler = maker.lookup(KeyedReferenceHandler.TAG_NAME);
119       handler.marshal(keyedRef,element);
120     }
121
122     parent.appendChild(element);
123   }
124
125
126   /***************************************************************************/
127   /***************************** TEST DRIVER *********************************/
128   /***************************************************************************/
129
130
131   public static void main(String JavaDoc args[])
132     throws Exception JavaDoc
133   {
134     HandlerMaker maker = HandlerMaker.getInstance();
135     AbstractHandler handler = maker.lookup(FindRelatedBusinessesHandler.TAG_NAME);
136
137     Element JavaDoc parent = XMLUtils.newRootElement();
138     Element JavaDoc child = null;
139
140     FindRelatedBusinesses request = new FindRelatedBusinesses();
141     request.setBusinessKey("10ad2903-932f-49fe-aaed-bf80d0ed50f0");
142     request.addFindQualifier(new FindQualifier(FindQualifier.SORT_BY_DATE_ASC));
143     request.addFindQualifier(new FindQualifier(FindQualifier.AND_ALL_KEYS));
144     request.setMaxRows(43);
145     request.setKeyedReference(new KeyedReference("uuid:8ff45356-acde-4a4c-86bf-f953611d20c6","catBagKeyName2","catBagKeyValue2"));
146
147     System.out.println();
148
149     RegistryObject regObject = request;
150     handler.marshal(regObject,parent);
151     child = (Element JavaDoc)parent.getFirstChild();
152     parent.removeChild(child);
153     XMLUtils.writeXML(child,System.out);
154
155     System.out.println();
156
157     regObject = handler.unmarshal(child);
158     handler.marshal(regObject,parent);
159     child = (Element JavaDoc)parent.getFirstChild();
160     parent.removeChild(child);
161     XMLUtils.writeXML(child,System.out);
162   }
163 }
Popular Tags