KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > juddi > handler > RegistryInfoHandler


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 JavaDoc;
19 import java.util.Properties JavaDoc;
20 import java.util.SortedSet JavaDoc;
21 import java.util.TreeSet JavaDoc;
22 import java.util.Vector JavaDoc;
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 JavaDoc;
29
30 /**
31  * @author Steve Viens (sviens@apache.org)
32  */

33 public class RegistryInfoHandler extends AbstractHandler
34 {
35   public static final String JavaDoc 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 JavaDoc element)
45   {
46     RegistryInfo obj = new RegistryInfo();
47     Vector JavaDoc 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 JavaDoc)nodeList.elementAt(i)));
62     }
63
64     return obj;
65   }
66
67   public void marshal(RegistryObject object,Element JavaDoc parent)
68   {
69     RegistryInfo info = (RegistryInfo)object;
70     Element JavaDoc element = parent.getOwnerDocument().createElementNS(null,TAG_NAME);
71     AbstractHandler handler = null;
72
73     Properties JavaDoc props = info.getProperties();
74     if ((props!=null) && (props.size() > 0))
75     {
76       handler = maker.lookup(PropertyHandler.TAG_NAME);
77
78       SortedSet JavaDoc sortedProps = new TreeSet JavaDoc(props.keySet());
79       for (Iterator JavaDoc keys = sortedProps.iterator(); keys.hasNext() ; )
80       {
81         String JavaDoc name = (String JavaDoc)keys.next();
82         String JavaDoc value = (String JavaDoc)props.getProperty(name);
83         handler.marshal(new Property(name,value),element);
84       }
85     }
86
87     parent.appendChild(element);
88   }
89
90
91   /***************************************************************************/
92   /***************************** TEST DRIVER *********************************/
93   /***************************************************************************/
94
95
96   public static void main(String JavaDoc args[])
97     throws Exception JavaDoc
98   {
99     HandlerMaker maker = HandlerMaker.getInstance();
100     AbstractHandler handler = maker.lookup(RegistryInfoHandler.TAG_NAME);
101
102     Element JavaDoc parent = XMLUtils.newRootElement();
103     Element JavaDoc child = null;
104
105     RegistryInfo regInfo = new RegistryInfo();
106     regInfo.addProperty(new Property("first_name","Steve"));
107     regInfo.addProperty(new Property("last_name","Viens"));
108     regInfo.addProperty(new Property("version","1.0b1"));
109
110     System.out.println();
111
112     RegistryObject regObject = regInfo;
113     handler.marshal(regObject,parent);
114     child = (Element JavaDoc)parent.getFirstChild();
115     parent.removeChild(child);
116     XMLUtils.writeXML(child,System.out);
117
118     System.out.println();
119
120     regObject = handler.unmarshal(child);
121     handler.marshal(regObject,parent);
122     child = (Element JavaDoc)parent.getFirstChild();
123     parent.removeChild(child);
124     XMLUtils.writeXML(child,System.out);
125   }
126 }
Popular Tags