KickJava   Java API By Example, From Geeks To Geeks.

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


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 JavaDoc;
19
20 import org.apache.juddi.datatype.Address;
21 import org.apache.juddi.datatype.AddressLine;
22 import org.apache.juddi.datatype.RegistryObject;
23 import org.apache.juddi.util.xml.XMLUtils;
24 import org.w3c.dom.Element JavaDoc;
25
26 /**
27  * "Knows about the creation and populating of Address objects.
28  * Returns Address."
29  *
30  * @author Steve Viens (sviens@apache.org)
31  */

32 public class AddressHandler extends AbstractHandler
33 {
34   public static final String JavaDoc TAG_NAME = "address";
35
36   private HandlerMaker maker = null;
37
38   protected AddressHandler(HandlerMaker maker)
39   {
40     this.maker = maker;
41   }
42
43   public RegistryObject unmarshal(Element JavaDoc element)
44   {
45     Address obj = new Address();
46     Vector JavaDoc nodeList = null;
47     AbstractHandler handler = null;
48
49     // Attributes
50
String JavaDoc useType = element.getAttribute("useType");
51     if ((useType != null) && (useType.trim().length() > 0))
52       obj.setUseType(useType);
53
54     String JavaDoc sortCode = element.getAttribute("sortCode");
55     if ((sortCode != null) && (sortCode.trim().length() > 0))
56       obj.setSortCode(sortCode);
57
58     String JavaDoc tModelKey = element.getAttribute("tModelKey");
59     if ((tModelKey != null) && (tModelKey.trim().length() > 0))
60       obj.setTModelKey(tModelKey);
61
62     // Text Node Value
63
// {none}
64

65     // Child Elements
66
handler = maker.lookup(AddressLineHandler.TAG_NAME);
67     nodeList = XMLUtils.getChildElementsByTagName(element,AddressLineHandler.TAG_NAME);
68     for (int i=0; i<nodeList.size(); i++)
69     {
70       obj.addAddressLine((AddressLine)handler.unmarshal((Element JavaDoc)nodeList.elementAt(i)));
71     }
72
73     return obj;
74   }
75
76   public void marshal(RegistryObject object,Element JavaDoc parent)
77   {
78     Address address = (Address)object;
79     Element JavaDoc element = parent.getOwnerDocument().createElementNS(null,TAG_NAME);
80
81     String JavaDoc useType = address.getUseType();
82     if ((useType != null) && (useType.trim().length() > 0))
83       element.setAttribute("useType",useType);
84
85     String JavaDoc sortCode = address.getSortCode();
86     if ((sortCode != null) && (sortCode.trim().length() > 0))
87       element.setAttribute("sortCode",sortCode);
88
89     String JavaDoc tModelKey = address.getTModelKey();
90     if ((tModelKey != null) && (tModelKey.trim().length() > 0))
91       element.setAttribute("tModelKey",tModelKey);
92
93     Vector JavaDoc vector = address.getAddressLineVector();
94     if ((vector!=null) && (vector.size() > 0))
95     {
96       AbstractHandler handler = maker.lookup(AddressLineHandler.TAG_NAME);
97       for (int i=0; i < vector.size(); i++)
98       {
99         AddressLine addressLine = (AddressLine)vector.elementAt(i);
100         handler.marshal(addressLine,element);
101       }
102     }
103
104     parent.appendChild(element);
105   }
106
107
108   /***************************************************************************/
109   /***************************** TEST DRIVER *********************************/
110   /***************************************************************************/
111
112
113   public static void main(String JavaDoc args[])
114     throws Exception JavaDoc
115   {
116     HandlerMaker maker = HandlerMaker.getInstance();
117     AbstractHandler handler = maker.lookup(AddressHandler.TAG_NAME);
118     Element JavaDoc parent = XMLUtils.newRootElement();
119     Element JavaDoc child = null;
120
121     Address object = new Address();
122     object.setUseType("myAddressUseType");
123     object.setSortCode("sortThis");
124     object.setTModelKey(null);
125     object.addAddressLine(new AddressLine("AddressLine1","keyNameAttr","keyValueAttr"));
126     object.addAddressLine(new AddressLine("AddressLine2"));
127
128     System.out.println();
129
130     RegistryObject regObject = object;
131     handler.marshal(regObject,parent);
132     child = (Element JavaDoc)parent.getFirstChild();
133     parent.removeChild(child);
134     XMLUtils.writeXML(child,System.out);
135
136     System.out.println();
137
138     regObject = handler.unmarshal(child);
139     handler.marshal(regObject,parent);
140     child = (Element JavaDoc)parent.getFirstChild();
141     parent.removeChild(child);
142     XMLUtils.writeXML(child,System.out);
143
144     System.out.println();
145   }
146 }
Popular Tags