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.Iterator;
19  import java.util.Properties;
20  import java.util.SortedSet;
21  import java.util.TreeSet;
22  import java.util.Vector;
23  
24  import org.apache.juddi.datatype.RegistryObject;
25  import org.apache.juddi.datatype.response.Property;
26  import org.apache.juddi.datatype.response.RegistryInfo;
27  import org.apache.juddi.util.xml.XMLUtils;
28  import org.w3c.dom.Element;
29  
30  /***
31   * @author Steve Viens (sviens@apache.org)
32   */
33  public class RegistryInfoHandler extends AbstractHandler
34  {
35    public static final String TAG_NAME = "registryInfo";
36  
37    private HandlerMaker maker = null;
38  
39    protected RegistryInfoHandler(HandlerMaker maker)
40    {
41      this.maker = maker;
42    }
43  
44    public RegistryObject unmarshal(Element element)
45    {
46      RegistryInfo obj = new RegistryInfo();
47      Vector nodeList = null;
48      AbstractHandler handler = null;
49  
50      // Attributes
51      // {none}
52  
53      // Text Node Value
54      // {none}
55  
56      // Child Elements
57      nodeList = XMLUtils.getChildElementsByTagName(element,PropertyHandler.TAG_NAME);
58      for (int i=0; i<nodeList.size(); i++)
59      {
60        handler = maker.lookup(PropertyHandler.TAG_NAME);
61        obj.addProperty((Property)handler.unmarshal((Element)nodeList.elementAt(i)));
62      }
63  
64      return obj;
65    }
66  
67    public void marshal(RegistryObject object,Element parent)
68    {
69      RegistryInfo info = (RegistryInfo)object;
70      String generic = info.getGeneric();
71      generic = getGeneric(generic);
72      String namespace = getUDDINamespace(generic);
73      Element element = parent.getOwnerDocument().createElementNS(namespace,TAG_NAME);
74      AbstractHandler handler = null;
75  
76      Properties props = info.getProperties();
77      if ((props!=null) && (props.size() > 0))
78      {
79        handler = maker.lookup(PropertyHandler.TAG_NAME);
80  
81        SortedSet sortedProps = new TreeSet(props.keySet());
82        for (Iterator keys = sortedProps.iterator(); keys.hasNext() ; )
83        {
84          String name = (String)keys.next();
85          String value = (String)props.getProperty(name);
86          handler.marshal(new Property(name,value),element);
87        }
88      }
89  
90      parent.appendChild(element);
91    }
92  
93  
94    /****************************************************************************/
95    /****************************** TEST DRIVER *********************************/
96    /****************************************************************************/
97  
98  
99    public static void main(String args[])
100     throws Exception
101   {
102     HandlerMaker maker = HandlerMaker.getInstance();
103     AbstractHandler handler = maker.lookup(RegistryInfoHandler.TAG_NAME);
104 
105     Element parent = XMLUtils.newRootElement();
106     Element child = null;
107 
108     RegistryInfo regInfo = new RegistryInfo();
109     regInfo.addProperty(new Property("first_name","Steve"));
110     regInfo.addProperty(new Property("last_name","Viens"));
111     regInfo.addProperty(new Property("version","1.0b1"));
112 
113     System.out.println();
114 
115     RegistryObject regObject = regInfo;
116     handler.marshal(regObject,parent);
117     child = (Element)parent.getFirstChild();
118     parent.removeChild(child);
119     XMLUtils.writeXML(child,System.out);
120 
121     System.out.println();
122 
123     regObject = handler.unmarshal(child);
124     handler.marshal(regObject,parent);
125     child = (Element)parent.getFirstChild();
126     parent.removeChild(child);
127     XMLUtils.writeXML(child,System.out);
128   }
129 }