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.response.PublisherInfo;
22  import org.apache.juddi.datatype.response.PublisherInfos;
23  import org.apache.juddi.datatype.response.PublisherList;
24  import org.apache.juddi.util.xml.XMLUtils;
25  import org.w3c.dom.Element;
26  
27  /***
28   * "Knows about the creation and populating of PublisherList objects.
29   * Returns PublisherList."
30   *
31   * @author Steve Viens (sviens@apache.org)
32   */
33  public class PublisherListHandler extends AbstractHandler
34  {
35    public static final String TAG_NAME = "publisherList";
36  
37    private HandlerMaker maker = null;
38  
39    protected PublisherListHandler(HandlerMaker maker)
40    {
41      this.maker = maker;
42    }
43  
44    public RegistryObject unmarshal(Element element)
45    {
46      PublisherList obj = new PublisherList();
47      Vector nodeList = null;
48      AbstractHandler handler = null;
49  
50      // We could use the generic attribute value to
51      // determine which version of UDDI was used to
52      // format the request XML. - Steve
53  
54      // Attributes
55      obj.setGeneric(element.getAttribute("generic"));
56      obj.setOperator(element.getAttribute("operator"));
57  
58      // We can ignore the xmlns attribute since we
59      // can always determine it's value using the
60      // "generic" attribute. - Steve
61  
62      String truncValue = element.getAttribute("truncated");
63      if (truncValue != null)
64        obj.setTruncated(truncValue.equalsIgnoreCase("true"));
65  
66      // Text Node Value
67      // {none}
68  
69      // Child Elements
70      nodeList = XMLUtils.getChildElementsByTagName(element,PublisherInfosHandler.TAG_NAME);
71      if (nodeList.size() > 0)
72      {
73        handler = maker.lookup(PublisherInfosHandler.TAG_NAME);
74        obj.setPublisherInfos((PublisherInfos)handler.unmarshal((Element)nodeList.elementAt(0)));
75      }
76  
77      return obj;
78    }
79  
80    public void marshal(RegistryObject object,Element parent)
81    {
82      PublisherList list = (PublisherList)object;
83      String generic = list.getGeneric();
84      generic = getGeneric(generic);
85      String namespace = getUDDINamespace(generic);
86      Element element = parent.getOwnerDocument().createElementNS(namespace,TAG_NAME);
87      AbstractHandler handler = null;
88  
89      element.setAttribute("generic",generic);
90  
91      String operator = list.getOperator();
92      if (operator != null)
93        element.setAttribute("operator",operator);
94      else
95        element.setAttribute("operator","");
96  
97      boolean truncated = list.isTruncated();
98      if (truncated)
99        element.setAttribute("truncated","true");
100 
101     PublisherInfos infos = list.getPublisherInfos();
102     if (infos != null)
103     {
104       handler = maker.lookup(PublisherInfosHandler.TAG_NAME);
105       handler.marshal(infos,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(PublisherListHandler.TAG_NAME);
122 
123     Element parent = XMLUtils.newRootElement();
124     Element child = null;
125 
126     PublisherList list = new PublisherList();
127     list.setGeneric("1.0");
128     list.setOperator("jUDDI.org");
129     list.setTruncated(false);
130     list.addPublisherInfo(new PublisherInfo("sviens","Steve Viens"));
131     list.addPublisherInfo(new PublisherInfo("jdoe","John Doe"));
132 
133     System.out.println();
134 
135     RegistryObject regObject = list;
136     handler.marshal(regObject,parent);
137     child = (Element)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)parent.getFirstChild();
146     parent.removeChild(child);
147     XMLUtils.writeXML(child,System.out);
148 
149     System.out.println();
150   }
151 }