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.IRegistry;
21  import org.apache.juddi.datatype.RegistryObject;
22  import org.apache.juddi.datatype.response.DispositionReport;
23  import org.apache.juddi.datatype.response.ErrInfo;
24  import org.apache.juddi.datatype.response.Result;
25  import org.apache.juddi.util.xml.XMLUtils;
26  import org.w3c.dom.Element;
27  
28  /***
29   * "Knows about the creation and populating of DispositionReport objects.
30   * Returns DispositionReport."
31   *
32   * @author Steve Viens (sviens@apache.org)
33   */
34  public class DispositionReportHandler extends AbstractHandler
35  {
36    public static final String TAG_NAME = "dispositionReport";
37  
38    private HandlerMaker maker = null;
39  
40    protected DispositionReportHandler(HandlerMaker maker)
41    {
42      this.maker = maker;
43    }
44  
45    public RegistryObject unmarshal(Element element)
46    {
47      DispositionReport obj = new DispositionReport();
48      Vector nodeList = null;
49      AbstractHandler handler = null;
50  
51      // We could use the generic attribute value to
52      // determine which version of UDDI was used to
53      // format the request XML. - Steve
54  
55      // Attributes
56      obj.setGeneric(element.getAttribute("generic"));
57      obj.setOperator(element.getAttribute("operator"));
58  
59      // We can ignore the xmlns attribute since we
60      // can always determine it's value using the
61      // "generic" attribute. - Steve
62  
63      // Text Node Value
64      // {none}
65  
66      // Child Elements
67      nodeList = XMLUtils.getChildElementsByTagName(element,ResultHandler.TAG_NAME);
68      for (int i=0; i<nodeList.size(); i++)
69      {
70        handler = maker.lookup(ResultHandler.TAG_NAME);
71        obj.addResult((Result)handler.unmarshal((Element)nodeList.elementAt(i)));
72      }
73  
74      return obj;
75    }
76  
77    public void marshal(RegistryObject object,Element parent)
78    {
79      DispositionReport report = (DispositionReport)object;
80      String generic = report.getGeneric();
81      generic = getGeneric(generic);
82      String namespace = getUDDINamespace(generic);
83      Element element = parent.getOwnerDocument().createElementNS(namespace,TAG_NAME);
84      AbstractHandler handler = null;
85  
86      element.setAttribute("generic",generic);
87  
88      String operator = report.getOperator();
89      if (operator != null)
90        element.setAttribute("operator",operator);
91  
92      Vector vector = report.getResultVector();
93      if (vector!=null)
94      {
95        handler = maker.lookup(ResultHandler.TAG_NAME);
96        for (int i=0; i < vector.size(); i++)
97        {
98          Result result = (Result)vector.elementAt(i);
99          handler.marshal(result,element);
100       }
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(DispositionReportHandler.TAG_NAME);
117 
118     Element parent = XMLUtils.newRootElement();
119     Element child = null;
120 
121     ErrInfo errInfo = new ErrInfo();
122     errInfo.setErrCode("E_accountLimitExceeded");
123     errInfo.setErrMsg("Authentication token information has timed out.");
124 
125     Result result = new Result();
126     result.setErrno(10160);
127     result.setErrInfo(errInfo);
128 
129     ErrInfo errInfo2 = new ErrInfo();
130     errInfo2.setErrCode("E_success");
131     errInfo2.setErrMsg(null);
132 
133     Result result2 = new Result();
134     result2.setErrno(Result.E_SUCCESS);
135     result2.setErrInfo(errInfo2);
136 
137     DispositionReport report = new DispositionReport();
138     report.setGeneric(IRegistry.UDDI_V2_GENERIC);
139     report.setOperator("jUDDI.org");
140     report.addResult(result);
141     report.addResult(result2);
142 
143     System.out.println();
144 
145     RegistryObject regObject = report;
146     handler.marshal(regObject,parent);
147     child = (Element)parent.getFirstChild();
148     parent.removeChild(child);
149     XMLUtils.writeXML(child,System.out);
150 
151     System.out.println();
152 
153     regObject = handler.unmarshal(child);
154     handler.marshal(regObject,parent);
155     child = (Element)parent.getFirstChild();
156     parent.removeChild(child);
157     XMLUtils.writeXML(child,System.out);
158 
159     System.out.println();
160   }
161 }