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.KeyedReference;
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 KeyedReference objects.
25   * Returns KeyedReference."
26   *
27   * @author Steve Viens (sviens@apache.org)
28   */
29  public class KeyedReferenceHandler extends AbstractHandler
30  {
31    public static final String TAG_NAME = "keyedReference";
32  
33    protected KeyedReferenceHandler(HandlerMaker maker)
34    {
35    }
36  
37    public RegistryObject unmarshal(Element element)
38    {
39      KeyedReference obj = new KeyedReference();
40  
41      // Attributes
42      obj.setTModelKey(element.getAttribute("tModelKey"));
43      obj.setKeyName(element.getAttribute("keyName"));
44      obj.setKeyValue(element.getAttribute("keyValue"));
45  
46      // Text Node Value
47      // {none}
48  
49      // Child Elements
50      // {none}
51  
52      return obj;
53    }
54  
55    public void marshal(RegistryObject object,Element parent)
56    {
57      KeyedReference keyedRef = (KeyedReference)object;
58      String generic = getGeneric(null);
59      String namespace = getUDDINamespace(generic);
60      Element element = parent.getOwnerDocument().createElementNS(namespace,TAG_NAME);
61  
62      String tModelKey = keyedRef.getTModelKey();
63      if (tModelKey != null)
64        element.setAttribute("tModelKey",tModelKey);
65  
66      String keyName = keyedRef.getKeyName();
67      if (keyName != null)
68        element.setAttribute("keyName",keyName);
69  
70      String keyValue = keyedRef.getKeyValue();
71      if (keyValue != null)
72        element.setAttribute("keyValue",keyValue);
73  
74      parent.appendChild(element);
75    }
76  
77  
78    /****************************************************************************/
79    /****************************** TEST DRIVER *********************************/
80    /****************************************************************************/
81  
82  
83    public static void main(String args[])
84      throws Exception
85    {
86      HandlerMaker maker = HandlerMaker.getInstance();
87    AbstractHandler handler = maker.lookup(KeyedReferenceHandler.TAG_NAME);
88  
89      Element parent = XMLUtils.newRootElement();
90      Element child = null;
91  
92      KeyedReference keyedRef = new KeyedReference();
93      keyedRef.setTModelKey("uuid:3860b975-9e0c-4cec-bad6-87dfe00e3864");
94      keyedRef.setKeyName("idBagKeyName2");
95      keyedRef.setKeyValue("idBagKeyValue2");
96  
97      System.out.println();
98  
99      RegistryObject regObject = keyedRef;
100     handler.marshal(regObject,parent);
101     child = (Element)parent.getFirstChild();
102     parent.removeChild(child);
103     XMLUtils.writeXML(child,System.out);
104 
105     System.out.println();
106 
107     regObject = handler.unmarshal(child);
108     handler.marshal(regObject,parent);
109     child = (Element)parent.getFirstChild();
110     parent.removeChild(child);
111     XMLUtils.writeXML(child,System.out);
112   }
113 }