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 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;
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 TAG_NAME = "property";
31  
32    protected PropertyHandler(HandlerMaker maker)
33    {
34    }
35  
36    public RegistryObject unmarshal(Element element)
37    {
38      Property obj = new Property();
39  
40      // Attributes
41      String name = element.getAttribute("name");
42      if ((name != null) && (name.trim().length() > 0))
43        obj.setName(name);
44  
45      String value = element.getAttribute("value");
46      if ((value != null) && (value.trim().length() > 0))
47        obj.setValue(value);
48  
49      // Text Node Value
50      // {none}
51  
52      // Child Elements
53      // {none}
54  
55      return obj;
56    }
57  
58    public void marshal(RegistryObject object,Element parent)
59    {
60      Property property = (Property)object;
61      String generic = getGeneric(null);
62      String namespace = getUDDINamespace(generic);
63      Element element = parent.getOwnerDocument().createElementNS(namespace,TAG_NAME);
64  
65      String name = property.getName();
66      if ((name != null) && (name.trim().length() > 0))
67        element.setAttribute("name",name);
68  
69      String value = property.getValue();
70      if ((value != null) && (value.trim().length() > 0))
71        element.setAttribute("value",value);
72  
73      parent.appendChild(element);
74    }
75  
76  
77    /****************************************************************************/
78    /****************************** TEST DRIVER *********************************/
79    /****************************************************************************/
80  
81  
82    public static void main(String args[])
83      throws Exception
84    {
85      // create the source object
86      Property inprop = new Property("nameAttr","valueAttr");
87  
88      // unmarshal & marshal (object->xml->object)
89      HandlerMaker maker = HandlerMaker.getInstance();
90      AbstractHandler handler = maker.lookup(PropertyHandler.TAG_NAME);
91      Element element = null;
92      handler.marshal(inprop,element);
93      AddressLine outprop = (AddressLine)handler.unmarshal(element);
94  
95      // compare unmarshaled with marshaled obj
96      if (outprop.equals(inprop))
97        System.out.println("Input and output are the same.");
98      else
99        System.out.println("Input and output are NOT the same!");
100   }
101 }