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 java.util.Vector;
19  
20  import org.apache.juddi.datatype.RegistryObject;
21  import org.apache.juddi.datatype.publisher.Publisher;
22  import org.apache.juddi.datatype.response.PublisherDetail;
23  import org.apache.juddi.util.xml.XMLUtils;
24  import org.w3c.dom.Element;
25  
26  /***
27   * "Knows about the creation and populating of ServiceDetail objects.
28   * Returns ServiceDetail."
29   *
30   * @author Steve Viens (sviens@apache.org)
31   */
32  public class PublisherDetailHandler extends AbstractHandler
33  {
34    public static final String TAG_NAME = "publisherDetail";
35  
36    private HandlerMaker maker = null;
37  
38    protected PublisherDetailHandler(HandlerMaker maker)
39    {
40      this.maker = maker;
41    }
42  
43    public RegistryObject unmarshal(Element element)
44    {
45      PublisherDetail obj = new PublisherDetail();
46      Vector nodeList = null;
47      AbstractHandler handler = null;
48  
49      // We could use the generic attribute value to
50      // determine which version of UDDI was used to
51      // format the request XML. - Steve
52  
53      // Attributes
54      obj.setGeneric(element.getAttribute("generic"));
55      obj.setOperator(element.getAttribute("operator"));
56  
57      // We can ignore the xmlns attribute since we
58      // can always determine it's value using the
59      // "generic" attribute. - Steve
60  
61      String truncValue = element.getAttribute("truncated");
62      if (truncValue != null)
63        obj.setTruncated(truncValue.equalsIgnoreCase("true"));
64  
65      // Text Node Value
66      // {none}
67  
68      // Child Elements
69      nodeList = XMLUtils.getChildElementsByTagName(element,PublisherHandler.TAG_NAME);
70      for (int i=0; i<nodeList.size(); i++)
71      {
72        handler = maker.lookup(PublisherHandler.TAG_NAME);
73        obj.addPublisher((Publisher)handler.unmarshal((Element)nodeList.elementAt(i)));
74      }
75  
76      return obj;
77    }
78  
79    public void marshal(RegistryObject object,Element parent)
80    {
81      PublisherDetail detail = (PublisherDetail)object;
82      String generic = detail.getGeneric();
83      generic = getGeneric(generic);
84      String namespace = getUDDINamespace(generic);
85      Element element = parent.getOwnerDocument().createElementNS(namespace,TAG_NAME);
86      AbstractHandler handler = null;
87  
88      element.setAttribute("generic",generic);
89  
90      String operator = detail.getOperator();
91      if (operator != null)
92        element.setAttribute("operator",operator);
93      else
94        element.setAttribute("operator","");
95  
96      boolean truncated = detail.isTruncated();
97      if (truncated)
98        element.setAttribute("truncated","true");
99  
100     Vector vector = detail.getPublisherVector();
101     if ((vector!=null) && (vector.size() > 0))
102     {
103       handler = maker.lookup(PublisherHandler.TAG_NAME);
104       for (int i=0; i < vector.size(); i++)
105         handler.marshal((Publisher)vector.elementAt(i),element);
106     }
107 
108     parent.appendChild(element);
109   }
110 
111 
112   /****************************************************************************/
113   /****************************** TEST DRIVER *********************************/
114   /****************************************************************************/
115 
116 
117   public static void main(String args[])
118     throws Exception
119   {
120     HandlerMaker maker = HandlerMaker.getInstance();
121     AbstractHandler handler = maker.lookup(PublisherDetailHandler.TAG_NAME);
122 
123     Element parent = XMLUtils.newRootElement();
124     Element 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 
132     PublisherDetail detail = new PublisherDetail();
133     detail.setGeneric("1.0");
134     detail.setOperator("jUDDI.org");
135     detail.setTruncated(false);
136     detail.addPublisher(publisher);
137     detail.addPublisher(publisher);
138 
139     System.out.println();
140 
141     RegistryObject regObject = detail;
142     handler.marshal(regObject,parent);
143     child = (Element)parent.getFirstChild();
144     parent.removeChild(child);
145     XMLUtils.writeXML(child,System.out);
146 
147     System.out.println();
148 
149     regObject = handler.unmarshal(child);
150     handler.marshal(regObject,parent);
151     child = (Element)parent.getFirstChild();
152     parent.removeChild(child);
153     XMLUtils.writeXML(child,System.out);
154 
155     System.out.println();
156   }
157 }