KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

69     // Child Elements
70
nodeList = XMLUtils.getChildElementsByTagName(element,NameHandler.TAG_NAME);
71     if (nodeList.size() > 0)
72     {
73       handler = maker.lookup(NameHandler.TAG_NAME);
74       Name name = (Name )handler.unmarshal((Element JavaDoc)nodeList.elementAt(0));
75       if (name != null)
76         obj.setName(name);
77     }
78
79     nodeList = XMLUtils.getChildElementsByTagName(element,FindQualifiersHandler.TAG_NAME);
80     if (nodeList.size() > 0)
81     {
82       handler = maker.lookup(FindQualifiersHandler.TAG_NAME);
83       obj.setFindQualifiers((FindQualifiers)handler.unmarshal((Element JavaDoc)nodeList.elementAt(0)));
84     }
85
86     nodeList = XMLUtils.getChildElementsByTagName(element,IdentifierBagHandler.TAG_NAME);
87     if (nodeList.size() > 0)
88     {
89       handler = maker.lookup(IdentifierBagHandler.TAG_NAME);
90       obj.setIdentifierBag((IdentifierBag)handler.unmarshal((Element JavaDoc)nodeList.elementAt(0)));
91     }
92
93     nodeList = XMLUtils.getChildElementsByTagName(element,CategoryBagHandler.TAG_NAME);
94     if (nodeList.size() > 0)
95     {
96       handler = maker.lookup(CategoryBagHandler.TAG_NAME);
97       obj.setCategoryBag((CategoryBag)handler.unmarshal((Element JavaDoc)nodeList.elementAt(0)));
98     }
99
100     return obj;
101   }
102
103   public void marshal(RegistryObject object,Element JavaDoc parent)
104   {
105     FindTModel request = (FindTModel)object;
106     Element JavaDoc element = parent.getOwnerDocument().createElementNS(null,TAG_NAME);
107     AbstractHandler handler = null;
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     Name name = request.getName();
125     if (name != null)
126     {
127       handler = maker.lookup(NameHandler.TAG_NAME);
128       handler.marshal(name,element);
129     }
130
131     IdentifierBag idBag = request.getIdentifierBag();
132     if (idBag != null)
133     {
134       handler = maker.lookup(IdentifierBagHandler.TAG_NAME);
135       handler.marshal(idBag,element);
136     }
137
138     CategoryBag catBag = request.getCategoryBag();
139     if (catBag != null)
140     {
141       handler = maker.lookup(CategoryBagHandler.TAG_NAME);
142       handler.marshal(catBag,element);
143     }
144
145     parent.appendChild(element);
146   }
147
148
149   /***************************************************************************/
150   /***************************** TEST DRIVER *********************************/
151   /***************************************************************************/
152
153
154   public static void main(String JavaDoc args[])
155     throws Exception JavaDoc
156   {
157     HandlerMaker maker = HandlerMaker.getInstance();
158     AbstractHandler handler = maker.lookup(FindTModelHandler.TAG_NAME);
159
160     Element JavaDoc parent = XMLUtils.newRootElement();
161     Element JavaDoc child = null;
162
163     IdentifierBag idBag = new IdentifierBag();
164     idBag.addKeyedReference(new KeyedReference("idBagKeyName","idBagKeyValue"));
165     idBag.addKeyedReference(new KeyedReference("uuid:3860b975-9e0c-4cec-bad6-87dfe00e3864","idBagKeyName2","idBagKeyValue2"));
166
167     CategoryBag catBag = new CategoryBag();
168     catBag.addKeyedReference(new KeyedReference("catBagKeyName","catBagKeyValue"));
169     catBag.addKeyedReference(new KeyedReference("uuid:8ff45356-acde-4a4c-86bf-f953611d20c6","catBagKeyName2","catBagKeyValue2"));
170
171     FindTModel request = new FindTModel();
172     request.setName(new Name("serviceNm2","en"));
173     request.addFindQualifier(new FindQualifier(FindQualifier.SORT_BY_DATE_ASC));
174     request.addFindQualifier(new FindQualifier(FindQualifier.AND_ALL_KEYS));
175     request.setMaxRows(37);
176     request.setIdentifierBag(idBag);
177     request.setCategoryBag(catBag);
178
179     System.out.println();
180
181     RegistryObject regObject = request;
182     handler.marshal(regObject,parent);
183     child = (Element JavaDoc)parent.getFirstChild();
184     parent.removeChild(child);
185     XMLUtils.writeXML(child,System.out);
186
187     System.out.println();
188
189     regObject = handler.unmarshal(child);
190     handler.marshal(regObject,parent);
191     child = (Element JavaDoc)parent.getFirstChild();
192     parent.removeChild(child);
193     XMLUtils.writeXML(child,System.out);
194   }
195 }
Popular Tags