KickJava   Java API By Example, From Geeks To Geeks.

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


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.Name;
21 import org.apache.juddi.datatype.RegistryObject;
22 import org.apache.juddi.datatype.request.FindPublisher;
23 import org.apache.juddi.datatype.request.FindQualifier;
24 import org.apache.juddi.datatype.request.FindQualifiers;
25 import org.apache.juddi.util.xml.XMLUtils;
26 import org.w3c.dom.Element JavaDoc;
27
28 /**
29  * Knows about the creation and populating of FindPublisher objects.
30  * Returns FindPublisher.
31  *
32  * @author Steve Viens (sviens@apache.org)
33  */

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

63     // Child Elements
64
nodeList = XMLUtils.getChildElementsByTagName(element,NameHandler.TAG_NAME);
65     if (nodeList.size() > 0)
66     {
67       handler = maker.lookup(NameHandler.TAG_NAME);
68       Name name = (Name )handler.unmarshal((Element JavaDoc)nodeList.elementAt(0));
69       if (name != null)
70         obj.setName(name);
71     }
72
73     nodeList = XMLUtils.getChildElementsByTagName(element,FindQualifiersHandler.TAG_NAME);
74     if (nodeList.size() > 0)
75     {
76       handler = maker.lookup(FindQualifiersHandler.TAG_NAME);
77       obj.setFindQualifiers((FindQualifiers)handler.unmarshal((Element JavaDoc)nodeList.elementAt(0)));
78     }
79
80     return obj;
81   }
82
83   public void marshal(RegistryObject object,Element JavaDoc parent)
84   {
85     FindPublisher request = (FindPublisher)object;
86     Element JavaDoc element = parent.getOwnerDocument().createElementNS(null,TAG_NAME);
87     AbstractHandler handler = null;
88
89     String JavaDoc generic = request.getGeneric();
90     if (generic != null)
91       element.setAttribute("generic",generic);
92
93     int maxRows = request.getMaxRows();
94     if (maxRows > 0)
95       element.setAttribute("maxRows",String.valueOf(maxRows));
96
97     FindQualifiers qualifiers = request.getFindQualifiers();
98     if ((qualifiers != null) && (qualifiers.size() > 0))
99     {
100       handler = maker.lookup(FindQualifiersHandler.TAG_NAME);
101       handler.marshal(qualifiers,element);
102     }
103
104     Name name = request.getName();
105     if (name != null)
106     {
107       handler = maker.lookup(NameHandler.TAG_NAME);
108       handler.marshal(name,element);
109     }
110
111     parent.appendChild(element);
112   }
113
114
115   /***************************************************************************/
116   /***************************** TEST DRIVER *********************************/
117   /***************************************************************************/
118
119
120   public static void main(String JavaDoc args[])
121     throws Exception JavaDoc
122   {
123     HandlerMaker maker = HandlerMaker.getInstance();
124     AbstractHandler handler = maker.lookup(FindPublisherHandler.TAG_NAME);
125
126     Element JavaDoc parent = XMLUtils.newRootElement();
127     Element JavaDoc child = null;
128
129     FindPublisher request = new FindPublisher();
130     request.setName(new Name("s","en"));
131     request.addFindQualifier(new FindQualifier(FindQualifier.SORT_BY_NAME_ASC));
132     request.setMaxRows(50);
133
134     System.out.println();
135
136     RegistryObject regObject = request;
137     handler.marshal(regObject,parent);
138     child = (Element JavaDoc)parent.getFirstChild();
139     parent.removeChild(child);
140     XMLUtils.writeXML(child,System.out);
141
142     System.out.println();
143
144     regObject = handler.unmarshal(child);
145     handler.marshal(regObject,parent);
146     child = (Element JavaDoc)parent.getFirstChild();
147     parent.removeChild(child);
148     XMLUtils.writeXML(child,System.out);
149   }
150 }
Popular Tags