KickJava   Java API By Example, From Geeks To Geeks.

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


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

29 public class AddressLineHandler extends AbstractHandler
30 {
31   public static final String JavaDoc TAG_NAME = "addressLine";
32
33   private HandlerMaker maker = null;
34
35   protected AddressLineHandler(HandlerMaker maker)
36   {
37     this.maker = maker;
38   }
39
40   public RegistryObject unmarshal(Element JavaDoc element)
41   {
42     AddressLine obj = new AddressLine();
43
44     // Attributes
45
String JavaDoc keyName = element.getAttribute("keyName");
46     if ((keyName != null) && (keyName.trim().length() > 0))
47       obj.setKeyName(keyName);
48
49     String JavaDoc keyValue = element.getAttribute("keyValue");
50     if ((keyValue != null) && (keyValue.trim().length() > 0))
51       obj.setKeyValue(keyValue);
52
53     // Text Node Value
54
obj.setLineValue(XMLUtils.getText(element));
55
56     // Child Elements
57
// {none}
58

59     return obj;
60   }
61
62   public void marshal(RegistryObject object,Element JavaDoc parent)
63   {
64     AddressLine line = (AddressLine)object;
65     Element JavaDoc element = parent.getOwnerDocument().createElementNS(null,TAG_NAME);
66
67     String JavaDoc keyName = line.getKeyName();
68     if ((keyName != null) && (keyName.trim().length() > 0))
69       element.setAttribute("keyName",keyName);
70
71     String JavaDoc keyValue = line.getKeyValue();
72     if ((keyValue != null) && (keyValue.trim().length() > 0))
73       element.setAttribute("keyValue",keyValue);
74
75     String JavaDoc lineValue = line.getLineValue();
76     if (lineValue != null)
77       element.appendChild(parent.getOwnerDocument().createTextNode(lineValue));
78
79     parent.appendChild(element);
80   }
81
82
83   /***************************************************************************/
84   /***************************** TEST DRIVER *********************************/
85   /***************************************************************************/
86
87
88   public static void main(String JavaDoc args[])
89     throws Exception JavaDoc
90   {
91       HandlerMaker maker = HandlerMaker.getInstance();
92       AbstractHandler handler = maker.lookup(AddressLineHandler.TAG_NAME);
93       Element JavaDoc parent = XMLUtils.newRootElement();
94       Element JavaDoc child = null;
95
96       AddressLine object = new AddressLine("AddressLine1","keyNameAttr","keyValueAttr");
97
98       System.out.println();
99
100       RegistryObject regObject = object;
101       handler.marshal(regObject,parent);
102       child = (Element JavaDoc)parent.getFirstChild();
103       parent.removeChild(child);
104       XMLUtils.writeXML(child,System.out);
105
106       System.out.println();
107
108       regObject = handler.unmarshal(child);
109       handler.marshal(regObject,parent);
110       child = (Element JavaDoc)parent.getFirstChild();
111       parent.removeChild(child);
112       XMLUtils.writeXML(child,System.out);
113
114       System.out.println();
115   }
116 }
Popular Tags