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.request.AuthInfo;
22  import org.apache.juddi.datatype.request.SaveSubscription;
23  import org.apache.juddi.datatype.subscription.Subscription;
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 SaveSubscriptionHandler extends AbstractHandler
31  {
32    public static final String TAG_NAME = "save_subscription";
33  
34    private HandlerMaker maker = null;
35  
36    protected SaveSubscriptionHandler(HandlerMaker maker)
37    {
38      this.maker = maker;
39    }
40  
41    public RegistryObject unmarshal(Element element)
42    {
43      SaveSubscription obj = new SaveSubscription();
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,SubscriptionHandler.TAG_NAME);
64      for (int i=0; i<nodeList.size(); i++)
65      {
66        handler = maker.lookup(SubscriptionHandler.TAG_NAME);
67        obj.addSubscription((Subscription)handler.unmarshal((Element)nodeList.elementAt(i)));
68      }
69  
70      return obj;
71    }
72  
73    public void marshal(RegistryObject object,Element parent)
74    {
75      SaveSubscription request = (SaveSubscription)object;
76      String generic = request.getGeneric();
77      generic = getGeneric(generic);
78      String namespace = getUDDINamespace(generic);
79      Element element = parent.getOwnerDocument().createElementNS(namespace,TAG_NAME);
80      AbstractHandler handler = null;
81  
82      element.setAttribute("generic",generic);
83  
84      AuthInfo authInfo = request.getAuthInfo();
85      if (authInfo != null)
86      {
87        handler = maker.lookup(AuthInfoHandler.TAG_NAME);
88        handler.marshal(authInfo,element);
89      }
90  
91      Vector subscriptionVector = request.getSubscriptionVector();
92      if ((subscriptionVector!=null) && (subscriptionVector.size() > 0))
93      {
94        handler = maker.lookup(SubscriptionHandler.TAG_NAME);
95        for (int i=0; i<subscriptionVector.size(); i++)
96          handler.marshal((Subscription)subscriptionVector.elementAt(i),element);
97      }
98  
99      parent.appendChild(element);
100   }
101 
102 
103   /****************************************************************************/
104   /****************************** TEST DRIVER *********************************/
105   /****************************************************************************/
106 
107 
108   public static void main(String args[])
109     throws Exception
110   {
111     HandlerMaker maker = HandlerMaker.getInstance();
112     AbstractHandler handler = maker.lookup(SaveSubscriptionHandler.TAG_NAME);
113 
114     Element parent = XMLUtils.newRootElement();
115     Element child = null;
116 
117     AuthInfo authInfo = new AuthInfo();
118     authInfo.setValue("6f157513-844e-4a95-a856-d257e6ba9726");
119 
120     Subscription subscription = new Subscription();
121     subscription.setSubscriptionKey("uuid:269855db-62eb-4862-8e5a-1b06f2753038");
122 
123     SaveSubscription request = new SaveSubscription();
124     request.setAuthInfo(authInfo);
125     request.addSubscription(subscription);
126     request.addSubscription(subscription);
127 
128     System.out.println();
129 
130     RegistryObject regObject = request;
131     handler.marshal(regObject,parent);
132     child = (Element)parent.getFirstChild();
133     parent.removeChild(child);
134     XMLUtils.writeXML(child,System.out);
135 
136     System.out.println();
137 
138     regObject = handler.unmarshal(child);
139     handler.marshal(regObject,parent);
140     child = (Element)parent.getFirstChild();
141     parent.removeChild(child);
142     XMLUtils.writeXML(child,System.out);
143   }
144 }