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.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;
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 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 element)
46    {
47      FindPublisher obj = new FindPublisher();
48      Vector nodeList = null;
49      AbstractHandler handler = null;
50  
51      // Attributes
52      String generic = element.getAttribute("generic");
53      if ((generic != null && (generic.trim().length() > 0)))
54        obj.setGeneric(generic);
55  
56      String 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)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)nodeList.elementAt(0)));
78      }
79  
80      return obj;
81    }
82  
83    public void marshal(RegistryObject object,Element parent)
84    {
85      FindPublisher request = (FindPublisher)object;
86      String generic = request.getGeneric();
87      generic = getGeneric(generic);
88      String namespace = getUDDINamespace(generic);
89      Element element = parent.getOwnerDocument().createElementNS(namespace,TAG_NAME);
90      AbstractHandler handler = null;
91  
92      element.setAttribute("generic",generic);
93  
94      int maxRows = request.getMaxRows();
95      if (maxRows > 0)
96        element.setAttribute("maxRows",String.valueOf(maxRows));
97  
98      FindQualifiers qualifiers = request.getFindQualifiers();
99      if ((qualifiers != null) && (qualifiers.size() > 0))
100     {
101       handler = maker.lookup(FindQualifiersHandler.TAG_NAME);
102       handler.marshal(qualifiers,element);
103     }
104 
105     Name name = request.getName();
106     if (name != null)
107     {
108       handler = maker.lookup(NameHandler.TAG_NAME);
109       handler.marshal(name,element);
110     }
111 
112     parent.appendChild(element);
113   }
114 
115 
116   /****************************************************************************/
117   /****************************** TEST DRIVER *********************************/
118   /****************************************************************************/
119 
120 
121   public static void main(String args[])
122     throws Exception
123   {
124     HandlerMaker maker = HandlerMaker.getInstance();
125     AbstractHandler handler = maker.lookup(FindPublisherHandler.TAG_NAME);
126 
127     Element parent = XMLUtils.newRootElement();
128     Element child = null;
129 
130     FindPublisher request = new FindPublisher();
131     request.setName(new Name("s","en"));
132     request.addFindQualifier(new FindQualifier(FindQualifier.SORT_BY_NAME_ASC));
133     request.setMaxRows(50);
134 
135     System.out.println();
136 
137     RegistryObject regObject = request;
138     handler.marshal(regObject,parent);
139     child = (Element)parent.getFirstChild();
140     parent.removeChild(child);
141     XMLUtils.writeXML(child,System.out);
142 
143     System.out.println();
144 
145     regObject = handler.unmarshal(child);
146     handler.marshal(regObject,parent);
147     child = (Element)parent.getFirstChild();
148     parent.removeChild(child);
149     XMLUtils.writeXML(child,System.out);
150   }
151 }