KickJava   Java API By Example, From Geeks To Geeks.

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


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.datatype.RegistryObject;
21 import org.apache.juddi.datatype.response.ErrInfo;
22 import org.apache.juddi.datatype.response.Result;
23 import org.apache.juddi.util.xml.XMLUtils;
24 import org.w3c.dom.Element JavaDoc;
25
26 /**
27  * "Knows about the creation and populating of Result objects.
28  * Returns Result."
29  *
30  * @author Steve Viens (sviens@apache.org)
31  */

32 public class ResultHandler extends AbstractHandler
33 {
34   public static final String JavaDoc TAG_NAME = "result";
35
36   private HandlerMaker maker = null;
37
38   protected ResultHandler(HandlerMaker maker)
39   {
40     this.maker = maker;
41   }
42
43   public RegistryObject unmarshal(Element JavaDoc element)
44   {
45     Result obj = new Result();
46     Vector JavaDoc nodeList = null;
47     AbstractHandler handler = null;
48
49     // Attributes
50
obj.setErrno(element.getAttribute("errno"));
51
52     // Text Node Value
53
// {none}
54

55     // Child Elements
56
nodeList = XMLUtils.getChildElementsByTagName(element,ErrInfoHandler.TAG_NAME);
57     if (nodeList.size() > 0)
58     {
59       handler = maker.lookup(ErrInfoHandler.TAG_NAME);
60       obj.setErrInfo((ErrInfo)handler.unmarshal((Element JavaDoc)nodeList.elementAt(0)));
61     }
62
63     return obj;
64   }
65
66   public void marshal(RegistryObject object,Element JavaDoc parent)
67   {
68     Result result = (Result)object;
69     Element JavaDoc element = parent.getOwnerDocument().createElementNS(null,TAG_NAME);
70     AbstractHandler handler = null;
71
72     element.setAttribute("errno",String.valueOf(result.getErrno()));
73
74     ErrInfo errInfo = result.getErrInfo();
75     if (errInfo!=null)
76     {
77       handler = maker.lookup(ErrInfoHandler.TAG_NAME);
78       handler.marshal(errInfo,element);
79     }
80
81     parent.appendChild(element);
82   }
83
84
85   /***************************************************************************/
86   /***************************** TEST DRIVER *********************************/
87   /***************************************************************************/
88
89
90   public static void main(String JavaDoc args[])
91     throws Exception JavaDoc
92   {
93     HandlerMaker maker = HandlerMaker.getInstance();
94     AbstractHandler handler = maker.lookup(ResultHandler.TAG_NAME);
95
96     Element JavaDoc parent = XMLUtils.newRootElement();
97     Element JavaDoc child = null;
98
99     ErrInfo errInfo = new ErrInfo();
100     errInfo.setErrCode("E_accountLimitExceeded");
101     errInfo.setErrMsg("Authentication token information has timed out.");
102
103     Result result = new Result();
104     result.setErrno(10160);
105     result.setErrInfo(errInfo);
106
107     System.out.println();
108
109     RegistryObject regObject = result;
110     handler.marshal(regObject,parent);
111     child = (Element JavaDoc)parent.getFirstChild();
112     parent.removeChild(child);
113     XMLUtils.writeXML(child,System.out);
114
115     System.out.println();
116
117     regObject = handler.unmarshal(child);
118     handler.marshal(regObject,parent);
119     child = (Element JavaDoc)parent.getFirstChild();
120     parent.removeChild(child);
121     XMLUtils.writeXML(child,System.out);
122
123     System.out.println();
124   }
125 }
Popular Tags