KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > juddi > handler > DispositionReportHandler


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 JavaDoc;
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 JavaDoc;
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 JavaDoc 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 JavaDoc element)
46   {
47     DispositionReport obj = new DispositionReport();
48     Vector JavaDoc 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 JavaDoc)nodeList.elementAt(i)));
72     }
73
74     return obj;
75   }
76
77   public void marshal(RegistryObject object,Element JavaDoc parent)
78   {
79     DispositionReport report = (DispositionReport)object;
80     Element JavaDoc element = parent.getOwnerDocument().createElementNS(null,TAG_NAME);
81     AbstractHandler handler = null;
82
83     // We could use the generic value to determine which
84
// version of UDDI we should follow to format the
85
// response XML. For now we'll simply send back a
86
// UDDI 2.0 formatted response. - Steve
87

88     String JavaDoc generic = report.getGeneric();
89     if (generic != null)
90     {
91       element.setAttribute("generic",generic);
92
93       if (generic.equals(IRegistry.UDDI_V1_GENERIC))
94         element.setAttribute("xmlns",IRegistry.UDDI_V1_NAMESPACE);
95       else if (generic.equals(IRegistry.UDDI_V2_GENERIC))
96         element.setAttribute("xmlns",IRegistry.UDDI_V2_NAMESPACE);
97       else if (generic.equals(IRegistry.UDDI_V3_GENERIC))
98         element.setAttribute("xmlns",IRegistry.UDDI_V3_NAMESPACE);
99     }
100
101     String JavaDoc operator = report.getOperator();
102     if (operator != null)
103       element.setAttribute("operator",operator);
104
105     Vector JavaDoc vector = report.getResultVector();
106     if (vector!=null)
107     {
108       handler = maker.lookup(ResultHandler.TAG_NAME);
109       for (int i=0; i < vector.size(); i++)
110       {
111         Result result = (Result)vector.elementAt(i);
112         handler.marshal(result,element);
113       }
114     }
115
116     parent.appendChild(element);
117   }
118
119
120   /***************************************************************************/
121   /***************************** TEST DRIVER *********************************/
122   /***************************************************************************/
123
124
125   public static void main(String JavaDoc args[])
126     throws Exception JavaDoc
127   {
128     HandlerMaker maker = HandlerMaker.getInstance();
129     AbstractHandler handler = maker.lookup(DispositionReportHandler.TAG_NAME);
130
131     Element JavaDoc parent = XMLUtils.newRootElement();
132     Element JavaDoc child = null;
133
134     ErrInfo errInfo = new ErrInfo();
135     errInfo.setErrCode("E_accountLimitExceeded");
136     errInfo.setErrMsg("Authentication token information has timed out.");
137
138     Result result = new Result();
139     result.setErrno(10160);
140     result.setErrInfo(errInfo);
141
142     ErrInfo errInfo2 = new ErrInfo();
143     errInfo2.setErrCode("E_success");
144     errInfo2.setErrMsg(null);
145
146     Result result2 = new Result();
147     result2.setErrno(Result.E_SUCCESS);
148     result2.setErrInfo(errInfo2);
149
150     DispositionReport report = new DispositionReport();
151     report.setGeneric(IRegistry.UDDI_V2_GENERIC);
152     report.setOperator("jUDDI.org");
153     report.addResult(result);
154     report.addResult(result2);
155
156     System.out.println();
157
158     RegistryObject regObject = report;
159     handler.marshal(regObject,parent);
160     child = (Element JavaDoc)parent.getFirstChild();
161     parent.removeChild(child);
162     XMLUtils.writeXML(child,System.out);
163
164     System.out.println();
165
166     regObject = handler.unmarshal(child);
167     handler.marshal(regObject,parent);
168     child = (Element JavaDoc)parent.getFirstChild();
169     parent.removeChild(child);
170     XMLUtils.writeXML(child,System.out);
171
172     System.out.println();
173   }
174 }
Popular Tags