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.RegistryObject;
21  import org.apache.juddi.datatype.assertion.PublisherAssertion;
22  import org.apache.juddi.datatype.request.AddPublisherAssertions;
23  import org.apache.juddi.datatype.request.AuthInfo;
24  import org.apache.juddi.util.xml.XMLUtils;
25  import org.w3c.dom.Element;
26  
27  /***
28   * @author Steve Viens (sviens@apache.org)
29   */
30  public class AddPublisherAssertionsHandler extends AbstractHandler
31  {
32    public static final String TAG_NAME = "add_publisherAssertions";
33  
34    private HandlerMaker maker = null;
35  
36    protected AddPublisherAssertionsHandler(HandlerMaker maker)
37    {
38      this.maker = maker;
39    }
40  
41    public RegistryObject unmarshal(Element element)
42    {
43      AddPublisherAssertions obj = new AddPublisherAssertions();
44      Vector nodeList = null;
45      AbstractHandler handler = null;
46  
47      // Attributes
48      String generic = element.getAttribute("generic");
49      if ((generic != null && (generic.trim().length() > 0)))
50        obj.setGeneric(generic);
51  
52      // Text Node Value
53      // {none}
54  
55      // Child Elements
56      nodeList = XMLUtils.getChildElementsByTagName(element,AuthInfoHandler.TAG_NAME);
57      if (nodeList.size() > 0)
58      {
59        handler = maker.lookup(AuthInfoHandler.TAG_NAME);
60        obj.setAuthInfo((AuthInfo)handler.unmarshal((Element)nodeList.elementAt(0)));
61      }
62  
63      nodeList = XMLUtils.getChildElementsByTagName(element,PublisherAssertionHandler.TAG_NAME);
64      for (int i=0; i<nodeList.size(); i++)
65      {
66        handler = maker.lookup(PublisherAssertionHandler.TAG_NAME);
67        obj.addPublisherAssertion((PublisherAssertion)handler.unmarshal((Element)nodeList.elementAt(i)));
68      }
69  
70      return obj;
71    }
72  
73    public void marshal(RegistryObject object,Element parent)
74    {
75      AddPublisherAssertions request = (AddPublisherAssertions)object;
76      String generic = request.getGeneric();
77      generic = getGeneric(generic);
78      String nameSpace = getUDDINamespace(generic);
79      
80      Element element = parent.getOwnerDocument().createElementNS(nameSpace,TAG_NAME);
81  
82      if (generic != null)
83        element.setAttribute("generic",generic);
84  
85      AuthInfo authInfo = request.getAuthInfo();
86      if (authInfo != null)
87      {
88        AbstractHandler handler = maker.lookup(AuthInfoHandler.TAG_NAME);
89        handler.marshal(authInfo,element);
90      }
91  
92      Vector vector = request.getPublisherAssertionVector();
93      if ((vector != null) && (vector.size() > 0))
94      {
95        AbstractHandler handler = maker.lookup(PublisherAssertionHandler.TAG_NAME);
96        for (int i=0; i<vector.size(); i++)
97          handler.marshal(((PublisherAssertion)vector.elementAt(i)),element);
98      }
99  
100     parent.appendChild(element);
101   }
102 
103 
104   /****************************************************************************/
105   /****************************** TEST DRIVER *********************************/
106   /****************************************************************************/
107 
108 
109   public static void main(String args[])
110     throws Exception
111   {
112     HandlerMaker maker = HandlerMaker.getInstance();
113     AbstractHandler handler = maker.lookup(AddPublisherAssertionsHandler.TAG_NAME);
114 
115     Element parent = XMLUtils.newRootElement();
116     Element child = null;
117 
118     AuthInfo authInfo = new AuthInfo();
119     authInfo.setValue("6f157513-844e-4a95-a856-d257e6ba9726");
120 
121     PublisherAssertion assertion = new PublisherAssertion();
122     assertion.setFromKey("3379ec11-a509-4668-9fee-19b134d0d09b");
123     assertion.setToKey("3379ec11-a509-4668-9fee-19b134d0d09b");
124     assertion.setKeyName("paKeyName");
125     assertion.setKeyValue("paKeyValue");
126     assertion.setTModelKey("uuid:3379ec11-a509-4668-9fee-19b134d0d09b");
127 
128     AddPublisherAssertions service = new AddPublisherAssertions();
129     service.setGeneric("2.0");
130     service.setAuthInfo(authInfo);
131     service.addPublisherAssertion(assertion);
132     service.addPublisherAssertion(assertion);
133 
134     System.out.println();
135 
136     RegistryObject regObject = service;
137     handler.marshal(regObject,parent);
138     child = (Element)parent.getFirstChild();
139     parent.removeChild(child);
140     XMLUtils.writeXML(child,System.out);
141 
142     System.out.println();
143 
144     regObject = handler.unmarshal(child);
145     handler.marshal(regObject,parent);
146     child = (Element)parent.getFirstChild();
147     parent.removeChild(child);
148     XMLUtils.writeXML(child,System.out);
149   }
150 }