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.util.xml.XMLUtils;
21  import org.w3c.dom.Element;
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 TAG_NAME = "addressLine";
32  
33    protected AddressLineHandler(HandlerMaker maker)
34    {
35    }
36  
37    public RegistryObject unmarshal(Element element)
38    {
39      AddressLine obj = new AddressLine();
40  
41      // Attributes
42      String keyName = element.getAttribute("keyName");
43      if ((keyName != null) && (keyName.trim().length() > 0))
44        obj.setKeyName(keyName);
45  
46      String keyValue = element.getAttribute("keyValue");
47      if ((keyValue != null) && (keyValue.trim().length() > 0))
48        obj.setKeyValue(keyValue);
49  
50      // Text Node Value
51      obj.setLineValue(XMLUtils.getText(element));
52  
53      // Child Elements
54      // {none}
55  
56      return obj;
57    }
58  
59    public void marshal(RegistryObject object,Element parent)
60    {
61      AddressLine line = (AddressLine)object;
62      
63      String generic = getGeneric(null);
64      String namespace = getUDDINamespace(generic);
65      Element element = parent.getOwnerDocument().createElementNS(namespace,TAG_NAME);
66  
67      String keyName = line.getKeyName();
68      if ((keyName != null) && (keyName.trim().length() > 0))
69        element.setAttribute("keyName",keyName);
70  
71      String keyValue = line.getKeyValue();
72      if ((keyValue != null) && (keyValue.trim().length() > 0))
73        element.setAttribute("keyValue",keyValue);
74  
75      String 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 args[])
89      throws Exception
90    {
91        HandlerMaker maker = HandlerMaker.getInstance();
92        AbstractHandler handler = maker.lookup(AddressLineHandler.TAG_NAME);
93        Element parent = XMLUtils.newRootElement();
94        Element 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)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)parent.getFirstChild();
111       parent.removeChild(child);
112       XMLUtils.writeXML(child,System.out);
113 
114       System.out.println();
115   }
116 }