KickJava   Java API By Example, From Geeks To Geeks.

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


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.datatype.response.Property;
21 import org.w3c.dom.Element JavaDoc;
22
23 /**
24  * Knows about the creation and populating of Property objects.
25  *
26  * @author Steve Viens (sviens@apache.org)
27  */

28 public class PropertyHandler extends AbstractHandler
29 {
30   public static final String JavaDoc TAG_NAME = "property";
31
32   private HandlerMaker maker = null;
33
34   protected PropertyHandler(HandlerMaker maker)
35   {
36     this.maker = maker;
37   }
38
39   public RegistryObject unmarshal(Element JavaDoc element)
40   {
41     Property obj = new Property();
42
43     // Attributes
44
String JavaDoc name = element.getAttribute("name");
45     if ((name != null) && (name.trim().length() > 0))
46       obj.setName(name);
47
48     String JavaDoc value = element.getAttribute("value");
49     if ((value != null) && (value.trim().length() > 0))
50       obj.setValue(value);
51
52     // Text Node Value
53
// {none}
54

55     // Child Elements
56
// {none}
57

58     return obj;
59   }
60
61   public void marshal(RegistryObject object,Element JavaDoc parent)
62   {
63     Property property = (Property)object;
64     Element JavaDoc element = parent.getOwnerDocument().createElementNS(null,TAG_NAME);
65
66     String JavaDoc name = property.getName();
67     if ((name != null) && (name.trim().length() > 0))
68       element.setAttribute("name",name);
69
70     String JavaDoc value = property.getValue();
71     if ((value != null) && (value.trim().length() > 0))
72       element.setAttribute("value",value);
73
74     parent.appendChild(element);
75   }
76
77
78   /***************************************************************************/
79   /***************************** TEST DRIVER *********************************/
80   /***************************************************************************/
81
82
83   public static void main(String JavaDoc args[])
84     throws Exception JavaDoc
85   {
86     // create the source object
87
Property inprop = new Property("nameAttr","valueAttr");
88
89     // unmarshal & marshal (object->xml->object)
90
HandlerMaker maker = HandlerMaker.getInstance();
91     AbstractHandler handler = maker.lookup(PropertyHandler.TAG_NAME);
92     Element JavaDoc element = null;
93     handler.marshal(inprop,element);
94     AddressLine outprop = (AddressLine)handler.unmarshal(element);
95
96     // compare unmarshaled with marshaled obj
97
if (outprop.equals(inprop))
98       System.out.println("Input and output are the same.");
99     else
100       System.out.println("Input and output are NOT the same!");
101   }
102 }
Popular Tags