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.response.ErrInfo;
22  import org.apache.juddi.datatype.response.Result;
23  import org.apache.juddi.util.xml.XMLUtils;
24  import org.w3c.dom.Element;
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 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 element)
44    {
45      Result obj = new Result();
46      Vector 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)nodeList.elementAt(0)));
61      }
62  
63      return obj;
64    }
65  
66    public void marshal(RegistryObject object,Element parent)
67    {
68      Result result = (Result)object;
69      String generic = getGeneric(null);
70      String namespace = getUDDINamespace(generic);
71      Element element = parent.getOwnerDocument().createElementNS(namespace,TAG_NAME);
72      AbstractHandler handler = null;
73  
74      element.setAttribute("errno",String.valueOf(result.getErrno()));
75  
76      ErrInfo errInfo = result.getErrInfo();
77      if (errInfo!=null)
78      {
79        handler = maker.lookup(ErrInfoHandler.TAG_NAME);
80        handler.marshal(errInfo,element);
81      }
82  
83      parent.appendChild(element);
84    }
85  
86  
87    /****************************************************************************/
88    /****************************** TEST DRIVER *********************************/
89    /****************************************************************************/
90  
91  
92    public static void main(String args[])
93      throws Exception
94    {
95      HandlerMaker maker = HandlerMaker.getInstance();
96      AbstractHandler handler = maker.lookup(ResultHandler.TAG_NAME);
97  
98      Element parent = XMLUtils.newRootElement();
99      Element child = null;
100 
101     ErrInfo errInfo = new ErrInfo();
102     errInfo.setErrCode("E_accountLimitExceeded");
103     errInfo.setErrMsg("Authentication token information has timed out.");
104 
105     Result result = new Result();
106     result.setErrno(10160);
107     result.setErrInfo(errInfo);
108 
109     System.out.println();
110 
111     RegistryObject regObject = result;
112     handler.marshal(regObject,parent);
113     child = (Element)parent.getFirstChild();
114     parent.removeChild(child);
115     XMLUtils.writeXML(child,System.out);
116 
117     System.out.println();
118 
119     regObject = handler.unmarshal(child);
120     handler.marshal(regObject,parent);
121     child = (Element)parent.getFirstChild();
122     parent.removeChild(child);
123     XMLUtils.writeXML(child,System.out);
124 
125     System.out.println();
126   }
127 }