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 java.util.Vector;
19  
20  import org.apache.juddi.datatype.KeyedReference;
21  import org.apache.juddi.datatype.RegistryObject;
22  import org.apache.juddi.datatype.assertion.PublisherAssertion;
23  import org.apache.juddi.util.xml.XMLUtils;
24  import org.w3c.dom.Element;
25  
26  /***
27   * @author Steve Viens (sviens@apache.org)
28   * @author Anou Mana (anou_mana@users.sourceforge.net)
29   */
30  public class PublisherAssertionHandler extends AbstractHandler
31  {
32    public static final String TAG_NAME = "publisherAssertion";
33  
34    private HandlerMaker maker = null;
35  
36    protected PublisherAssertionHandler(HandlerMaker maker)
37    {
38      this.maker = maker;
39    }
40  
41    public RegistryObject unmarshal(Element element)
42    {
43      PublisherAssertion obj = new PublisherAssertion();
44      Vector nodeList = null;
45      AbstractHandler handler = null;
46  
47      // Child Elements
48      nodeList = XMLUtils.getChildElementsByTagName(element,"fromKey");
49      if (nodeList.size() > 0)
50      {
51        String fromKey = XMLUtils.getText((Element)nodeList.elementAt(0));
52        obj.setFromKey(fromKey);
53      }
54  
55      nodeList = XMLUtils.getChildElementsByTagName(element,"toKey");
56      if (nodeList.size() > 0)
57      {
58        String toKey = XMLUtils.getText((Element)nodeList.elementAt(0));
59        obj.setToKey(toKey);
60      }
61  
62      nodeList = XMLUtils.getChildElementsByTagName(element,KeyedReferenceHandler.TAG_NAME);
63      if (nodeList.size() > 0)
64      {
65        handler = maker.lookup(KeyedReferenceHandler.TAG_NAME);
66        obj.setKeyedReference((KeyedReference)handler.unmarshal((Element)nodeList.elementAt(0)));
67      }
68  
69      return obj;
70    }
71  
72    public void marshal(RegistryObject object,Element parent)
73    {
74      PublisherAssertion assertion = (PublisherAssertion)object;
75      String generic = getGeneric(null);
76      String namespace = getUDDINamespace(generic);
77      Element element = parent.getOwnerDocument().createElementNS(namespace,TAG_NAME);
78      AbstractHandler handler = null;
79  
80      String fromKey = assertion.getFromKey();
81      if (fromKey != null)
82      {
83        Element fkElement = parent.getOwnerDocument().createElementNS(namespace,"fromKey");
84        fkElement.appendChild(parent.getOwnerDocument().createTextNode(fromKey));
85        element.appendChild(fkElement);
86      }
87  
88      String toKey = assertion.getToKey();
89      if (toKey != null)
90      {
91        Element tkElement = parent.getOwnerDocument().createElementNS(namespace,"toKey");
92        tkElement.appendChild(parent.getOwnerDocument().createTextNode(toKey));
93        element.appendChild(tkElement);
94      }
95  
96      KeyedReference keyedRef = assertion.getKeyedReference();
97      if (keyedRef != null)
98      {
99        handler = maker.lookup(KeyedReferenceHandler.TAG_NAME);
100       handler.marshal(keyedRef,element);
101     }
102 
103     parent.appendChild(element);
104   }
105 
106 
107   /****************************************************************************/
108   /****************************** TEST DRIVER *********************************/
109   /****************************************************************************/
110 
111 
112   public static void main(String args[])
113     throws Exception
114   {
115     HandlerMaker maker = HandlerMaker.getInstance();
116     AbstractHandler handler = maker.lookup(PublisherAssertionHandler.TAG_NAME);
117     Element parent = XMLUtils.newRootElement();
118     Element child = null;
119 
120     PublisherAssertion assertion = new PublisherAssertion();
121     assertion.setFromKey("b2f072e7-6013-4385-93b4-9c1c2ece1c8f");
122     assertion.setToKey("115be72d-0c04-4b5f-a729-79a522629c19");
123     assertion.setKeyedReference(new KeyedReference("uuid:8ff45356-acde-4a4c-86bf-f953611d20c6","catBagKeyName2","catBagKeyValue2"));
124 
125     System.out.println();
126 
127     RegistryObject regObject = assertion;
128     handler.marshal(regObject,parent);
129     child = (Element)parent.getFirstChild();
130     parent.removeChild(child);
131     XMLUtils.writeXML(child,System.out);
132 
133     System.out.println();
134 
135     regObject = handler.unmarshal(child);
136     handler.marshal(regObject,parent);
137     child = (Element)parent.getFirstChild();
138     parent.removeChild(child);
139     XMLUtils.writeXML(child,System.out);
140   }
141 }