KickJava   Java API By Example, From Geeks To Geeks.

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


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

31 public class PublisherHandler extends AbstractHandler
32 {
33   public static final String JavaDoc TAG_NAME = "publisher";
34
35   private HandlerMaker maker = null;
36
37   protected PublisherHandler(HandlerMaker maker)
38   {
39     this.maker = maker;
40   }
41
42   public RegistryObject unmarshal(Element JavaDoc element)
43   {
44     Publisher obj = new Publisher();
45     Vector JavaDoc nodeList = null;
46     AbstractHandler handler = null;
47
48     // Attributes (required)
49
obj.setPublisherID(element.getAttribute("publisherID"));
50     obj.setName(element.getAttribute("publisherName"));
51
52     String JavaDoc admin = element.getAttribute("admin");
53     if ((admin != null) && (admin.length() > 0))
54       obj.setAdminValue(admin);
55     else
56       obj.setAdmin(false);
57
58     String JavaDoc enabled = element.getAttribute("enabled");
59     if ((enabled != null) && (enabled.length() > 0))
60       obj.setEnabledValue(enabled);
61     else
62       obj.setAdmin(false);
63
64     String JavaDoc emailAddress = element.getAttribute("emailAddress");
65     if ((emailAddress != null) && (emailAddress.length() > 0))
66       obj.setEmailAddress(emailAddress);
67
68     // Text Node Value
69
// {none}
70

71     // Child Elements
72
// {none}
73

74     return obj;
75   }
76
77   public void marshal(RegistryObject object,Element JavaDoc parent)
78   {
79     Publisher publisher = (Publisher)object;
80     Element JavaDoc element = parent.getOwnerDocument().createElementNS(null,TAG_NAME);
81     AbstractHandler handler = null;
82
83     // Attributes (required)
84
String JavaDoc publisherID = publisher.getPublisherID();
85     if ((publisherID != null) && (publisherID.length() > 0))
86       element.setAttribute("publisherID",publisherID);
87     else
88       element.setAttribute("publisherID","");
89
90     String JavaDoc publisherName = publisher.getName();
91     if ((publisherName != null) && (publisherName.length() > 0))
92       element.setAttribute("publisherName",publisherName);
93     else
94       element.setAttribute("publisherName","");
95
96     element.setAttribute("admin",String.valueOf(publisher.isAdmin()));
97     element.setAttribute("enabled",String.valueOf(publisher.isEnabled()));
98
99     String JavaDoc emailAddress = publisher.getEmailAddress();
100     if ((emailAddress != null) && (emailAddress.length() > 0))
101       element.setAttribute("emailAddress",emailAddress);
102
103     // Text Node Value
104
// {none}
105

106     // Child Elements
107
// {none}
108

109     parent.appendChild(element);
110   }
111
112
113   /***************************************************************************/
114   /***************************** TEST DRIVER *********************************/
115   /***************************************************************************/
116
117
118   public static void main(String JavaDoc args[])
119     throws Exception JavaDoc
120   {
121     HandlerMaker maker = HandlerMaker.getInstance();
122     AbstractHandler handler = maker.lookup(PublisherHandler.TAG_NAME);
123     Element JavaDoc parent = XMLUtils.newRootElement();
124     Element JavaDoc child = null;
125
126     Publisher publisher = new Publisher();
127     publisher.setPublisherID("bcrosby");
128     publisher.setName("Bing Crosby");
129     publisher.setEmailAddress("bcrosby@juddi.org");
130     publisher.setAdmin(true);
131     publisher.setEnabled(true);
132
133     System.out.println();
134
135     RegistryObject regObject = publisher;
136     handler.marshal(regObject,parent);
137     child = (Element JavaDoc)parent.getFirstChild();
138     parent.removeChild(child);
139     XMLUtils.writeXML(child,System.out);
140
141     System.out.println();
142
143     regObject = handler.unmarshal(child);
144     handler.marshal(regObject,parent);
145     child = (Element JavaDoc)parent.getFirstChild();
146     parent.removeChild(child);
147     XMLUtils.writeXML(child,System.out);
148   }
149 }
Popular Tags