KickJava   Java API By Example, From Geeks To Geeks.

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


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.business.BusinessEntity;
22 import org.apache.juddi.datatype.request.ValidateValues;
23 import org.apache.juddi.datatype.service.BusinessService;
24 import org.apache.juddi.datatype.tmodel.TModel;
25 import org.apache.juddi.util.xml.XMLUtils;
26 import org.w3c.dom.Element JavaDoc;
27
28 /**
29  * @author Steve Viens (sviens@apache.org)
30  */

31 public class ValidateValuesHandler extends AbstractHandler
32 {
33   public static final String JavaDoc TAG_NAME = "validate_values";
34
35   private HandlerMaker maker = null;
36
37   protected ValidateValuesHandler(HandlerMaker maker)
38   {
39     this.maker = maker;
40   }
41
42   public RegistryObject unmarshal(Element JavaDoc element)
43   {
44     ValidateValues obj = new ValidateValues();
45     Vector JavaDoc nodeList = null;
46     AbstractHandler handler = null;
47
48     // Attributes
49
String JavaDoc generic = element.getAttribute("generic");
50     if ((generic != null && (generic.trim().length() > 0)))
51       obj.setGeneric(generic);
52
53     // Text Node Value
54
// {none}
55

56     // Child Elements
57
nodeList = XMLUtils.getChildElementsByTagName(element,BusinessEntityHandler.TAG_NAME);
58     for (int i=0; i<nodeList.size(); i++)
59     {
60       handler = maker.lookup(BusinessEntityHandler.TAG_NAME);
61       obj.addBusinessEntity((BusinessEntity)handler.unmarshal((Element JavaDoc)nodeList.elementAt(i)));
62     }
63
64     nodeList = XMLUtils.getChildElementsByTagName(element,BusinessServiceHandler.TAG_NAME);
65     for (int i=0; i<nodeList.size(); i++)
66     {
67       handler = maker.lookup(BusinessServiceHandler.TAG_NAME);
68       obj.addBusinessService((BusinessService)handler.unmarshal((Element JavaDoc)nodeList.elementAt(i)));
69     }
70
71     nodeList = XMLUtils.getChildElementsByTagName(element,TModelHandler.TAG_NAME);
72     for (int i=0; i<nodeList.size(); i++)
73     {
74       handler = maker.lookup(TModelHandler.TAG_NAME);
75       obj.addTModel((TModel)handler.unmarshal((Element JavaDoc)nodeList.elementAt(i)));
76     }
77
78     return obj;
79   }
80
81   public void marshal(RegistryObject object,Element JavaDoc parent)
82   {
83     ValidateValues request = (ValidateValues)object;
84     Element JavaDoc element = parent.getOwnerDocument().createElementNS(null,TAG_NAME);
85     AbstractHandler handler = null;
86
87     String JavaDoc generic = request.getGeneric();
88     if (generic != null)
89       element.setAttribute("generic",generic);
90
91     Vector JavaDoc businessVector = request.getBusinessEntityVector();
92     if ((businessVector!=null) && (businessVector.size() > 0))
93     {
94       handler = maker.lookup(BusinessEntityHandler.TAG_NAME);
95       for (int i=0; i < businessVector.size(); i++)
96         handler.marshal((BusinessEntity)businessVector.elementAt(i),element);
97     }
98
99     Vector JavaDoc serviceVector = request.getBusinessServiceVector();
100     if ((serviceVector!=null) && (serviceVector.size() > 0))
101     {
102       handler = maker.lookup(BusinessServiceHandler.TAG_NAME);
103       for (int i=0; i < serviceVector.size(); i++)
104         handler.marshal((BusinessService)serviceVector.elementAt(i),element);
105     }
106
107     Vector JavaDoc tModelVector = request.getTModelVector();
108     if ((tModelVector!=null) && (tModelVector.size() > 0))
109     {
110       handler = maker.lookup(TModelHandler.TAG_NAME);
111       for (int i=0; i < tModelVector.size(); i++)
112         handler.marshal((TModel)tModelVector.elementAt(i),element);
113     }
114
115     parent.appendChild(element);
116   }
117
118
119   /***************************************************************************/
120   /***************************** TEST DRIVER *********************************/
121   /***************************************************************************/
122
123
124   public static void main(String JavaDoc args[])
125     throws Exception JavaDoc
126   {
127     HandlerMaker maker = HandlerMaker.getInstance();
128   AbstractHandler handler = maker.lookup(ValidateValuesHandler.TAG_NAME);
129     Element JavaDoc parent = XMLUtils.newRootElement();
130     Element JavaDoc child = null;
131
132     BusinessEntity business = new BusinessEntity();
133     BusinessService service = new BusinessService();
134     TModel tModel = new TModel();
135
136     ValidateValues request = new ValidateValues();
137     request.addBusinessEntity(business);
138     request.addBusinessService(service);
139     request.addTModel(tModel);
140
141     System.out.println();
142
143     RegistryObject regObject = request;
144     handler.marshal(regObject,parent);
145     child = (Element JavaDoc)parent.getFirstChild();
146     parent.removeChild(child);
147     XMLUtils.writeXML(child,System.out);
148
149     System.out.println();
150
151     regObject = handler.unmarshal(child);
152     handler.marshal(regObject,parent);
153     child = (Element JavaDoc)parent.getFirstChild();
154     parent.removeChild(child);
155     XMLUtils.writeXML(child,System.out);
156
157     System.out.println();
158   }
159 }
Popular Tags