View Javadoc

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 org.apache.juddi.datatype.RegistryObject;
19  import org.apache.juddi.datatype.publisher.Publisher;
20  import org.apache.juddi.util.Config;
21  import org.apache.juddi.util.xml.XMLUtils;
22  import org.w3c.dom.Element;
23  
24  /***
25   * "Knows about the creation and populating of Publisher objects.
26   * Returns Publisher."
27   *
28   * @author Steve Viens (sviens@apache.org)
29   */
30  public class PublisherHandler extends AbstractHandler
31  {
32    public static final String TAG_NAME = "publisher";
33  
34    protected PublisherHandler(HandlerMaker maker)
35    {
36    }
37  
38    public RegistryObject unmarshal(Element element)
39    {
40      Publisher obj = new Publisher();
41  
42      // Attributes (required)
43      obj.setPublisherID(element.getAttribute("publisherID"));
44      obj.setName(element.getAttribute("publisherName"));
45  
46      String admin = element.getAttribute("admin");
47      if ((admin != null) && (admin.length() > 0))
48        obj.setAdminValue(admin);
49      else
50        obj.setAdmin(false);
51  
52      String enabled = element.getAttribute("enabled");
53      if ((enabled != null) && (enabled.length() > 0))
54        obj.setEnabledValue(enabled);
55      else
56        obj.setAdmin(false);
57  
58      String emailAddress = element.getAttribute("emailAddress");
59      if ((emailAddress != null) && (emailAddress.length() > 0))
60        obj.setEmailAddress(emailAddress);
61      
62      // maxBusinesses
63      try {
64      	String attrValue = element.getAttribute("maxBusinesses");
65      	if ((attrValue != null) && (attrValue.length() > 0))
66       		obj.setMaxBusinesses(Integer.parseInt(attrValue));
67      	else 
68      		obj.setMaxBusinesses(Config.getMaxBusinessesPerPublisher());
69      }
70      catch (Exception ex) {
71      	obj.setMaxBusinesses(
72      			Config.getMaxBusinessesPerPublisher());
73      }
74      
75      // maxServicesPerBusiness
76      try {
77      	String attrValue = element.getAttribute("maxServicesPerBusiness");
78      	if ((attrValue != null) && (attrValue.length() > 0))
79      		obj.setMaxServicesPerBusiness(Integer.parseInt(attrValue));
80      	else 
81      		obj.setMaxServicesPerBusiness(Config.getMaxServicesPerBusiness());
82      }
83      catch (Exception ex) {
84      	obj.setMaxServicesPerBusiness(
85      			Config.getMaxServicesPerBusiness());
86      }
87  
88      // maxBindingsPerService
89      try {
90      	String attrValue = element.getAttribute("maxBindingsPerService");
91      	if ((attrValue != null) && (attrValue.length() > 0))
92      		obj.setMaxBindingsPerService(Integer.parseInt(attrValue));
93      	else 
94      		obj.setMaxBindingsPerService(Config.getMaxBindingsPerService());
95      }
96      catch (Exception ex) {
97      	obj.setMaxBindingsPerService(
98      			Config.getMaxBindingsPerService());
99      }
100 
101     // maxTModels
102     try {
103     	String attrValue = element.getAttribute("maxTModels");
104     	if ((attrValue != null) && (attrValue.length() > 0))
105     		obj.setMaxTModels(Integer.parseInt(attrValue));
106     	else 
107     		obj.setMaxTModels(Config.getMaxTModelsPerPublisher());
108     }
109     catch (Exception ex) {
110     	obj.setMaxTModels(
111     			Config.getMaxTModelsPerPublisher());
112     }
113     
114     // Text Node Value
115     // {none}
116 
117     // Child Elements
118     // {none}
119 
120     return obj;
121   }
122 
123   public void marshal(RegistryObject object,Element parent)
124   {
125     Publisher publisher = (Publisher)object;
126     String generic = getGeneric(null);
127     String namespace = getUDDINamespace(generic);
128     Element element = parent.getOwnerDocument().createElementNS(namespace,TAG_NAME);
129 
130     // Attributes (required)
131     String publisherID = publisher.getPublisherID();
132     if ((publisherID != null) && (publisherID.length() > 0))
133       element.setAttribute("publisherID",publisherID);
134     else
135       element.setAttribute("publisherID","");
136 
137     String publisherName = publisher.getName();
138     if ((publisherName != null) && (publisherName.length() > 0))
139       element.setAttribute("publisherName",publisherName);
140     else
141       element.setAttribute("publisherName","");
142 
143     element.setAttribute("admin",String.valueOf(publisher.isAdmin()));
144     element.setAttribute("enabled",String.valueOf(publisher.isEnabled()));
145 
146     String emailAddress = publisher.getEmailAddress();
147     if ((emailAddress != null) && (emailAddress.length() > 0))
148       element.setAttribute("emailAddress",emailAddress);
149 
150     element.setAttribute("maxBusinessEntities",String.valueOf(publisher.getMaxBusinesses()));
151     element.setAttribute("maxServicesPerBusiness",String.valueOf(publisher.getMaxServicesPerBusiness()));
152     element.setAttribute("maxBindingsPerService",String.valueOf(publisher.getMaxBindingsPerService()));
153     element.setAttribute("maxTModels",String.valueOf(publisher.getMaxTModels()));
154 
155     // Text Node Value
156     // {none}
157 
158     // Child Elements
159     // {none}
160 
161     parent.appendChild(element);
162   }
163 
164 
165   /****************************************************************************/
166   /****************************** TEST DRIVER *********************************/
167   /****************************************************************************/
168 
169 
170   public static void main(String args[])
171     throws Exception
172   {
173     HandlerMaker maker = HandlerMaker.getInstance();
174     AbstractHandler handler = maker.lookup(PublisherHandler.TAG_NAME);
175     Element parent = XMLUtils.newRootElement();
176     Element child = null;
177 
178     Publisher publisher = new Publisher();
179     publisher.setPublisherID("bcrosby");
180     publisher.setName("Bing Crosby");
181     publisher.setEmailAddress("bcrosby@juddi.org");
182     publisher.setAdmin(true);
183     publisher.setEnabled(true);
184     publisher.setMaxBusinesses(5);
185     publisher.setMaxServicesPerBusiness(10);
186     publisher.setMaxBindingsPerService(25);
187     publisher.setMaxTModels(10);
188 
189     System.out.println();
190 
191     RegistryObject regObject = publisher;
192     handler.marshal(regObject,parent);
193     child = (Element)parent.getFirstChild();
194     parent.removeChild(child);
195     XMLUtils.writeXML(child,System.out);
196 
197     System.out.println();
198 
199     regObject = handler.unmarshal(child);
200     handler.marshal(regObject,parent);
201     child = (Element)parent.getFirstChild();
202     parent.removeChild(child);
203     XMLUtils.writeXML(child,System.out);
204   }
205 }