KickJava   Java API By Example, From Geeks To Geeks.

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


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.CategoryBag;
22 import org.apache.juddi.datatype.Description;
23 import org.apache.juddi.datatype.IdentifierBag;
24 import org.apache.juddi.datatype.KeyedReference;
25 import org.apache.juddi.datatype.OverviewDoc;
26 import org.apache.juddi.datatype.RegistryObject;
27 import org.apache.juddi.datatype.response.TModelDetail;
28 import org.apache.juddi.datatype.tmodel.TModel;
29 import org.apache.juddi.util.xml.XMLUtils;
30 import org.w3c.dom.Element JavaDoc;
31
32 /**
33  * "Knows about the creation and populating of TModelDetail objects.
34  * Returns TModelDetail."
35  *
36  * @author Steve Viens (sviens@apache.org)
37  */

38 public class TModelDetailHandler extends AbstractHandler
39 {
40   public static final String JavaDoc TAG_NAME = "tModelDetail";
41
42   private HandlerMaker maker = null;
43
44   protected TModelDetailHandler(HandlerMaker maker)
45   {
46     this.maker = maker;
47   }
48
49   public RegistryObject unmarshal(Element JavaDoc element)
50   {
51     TModelDetail obj = new TModelDetail();
52     Vector JavaDoc nodeList = null;
53     AbstractHandler handler = null;
54
55     // We could use the generic attribute value to
56
// determine which version of UDDI was used to
57
// format the request XML. - Steve
58

59     // Attributes
60
obj.setGeneric(element.getAttribute("generic"));
61     obj.setOperator(element.getAttribute("operator"));
62
63     // We can ignore the xmlns attribute since we
64
// can always determine it's value using the
65
// "generic" attribute. - Steve
66

67     String JavaDoc truncValue = element.getAttribute("truncated");
68     if (truncValue != null)
69       obj.setTruncated(truncValue.equalsIgnoreCase("true"));
70
71     // Text Node Value
72
// {none}
73

74     // Child Elements
75
nodeList = XMLUtils.getChildElementsByTagName(element,TModelHandler.TAG_NAME);
76     for (int i=0; i<nodeList.size(); i++)
77     {
78       handler = maker.lookup(TModelHandler.TAG_NAME);
79       obj.addTModel((TModel)handler.unmarshal((Element JavaDoc)nodeList.elementAt(i)));
80     }
81
82     return obj;
83   }
84
85   public void marshal(RegistryObject object,Element JavaDoc parent)
86   {
87     TModelDetail detail = (TModelDetail)object;
88     Element JavaDoc element = parent.getOwnerDocument().createElementNS(null,TAG_NAME);
89     AbstractHandler handler = null;
90
91     String JavaDoc generic = detail.getGeneric();
92     if ((generic != null) && (generic.trim().length() > 0))
93     {
94       element.setAttribute("generic",generic);
95
96       if (generic.equals(IRegistry.UDDI_V1_GENERIC))
97         element.setAttribute("xmlns",IRegistry.UDDI_V1_NAMESPACE);
98       else if (generic.equals(IRegistry.UDDI_V2_GENERIC))
99         element.setAttribute("xmlns",IRegistry.UDDI_V2_NAMESPACE);
100       else if (generic.equals(IRegistry.UDDI_V3_GENERIC))
101         element.setAttribute("xmlns",IRegistry.UDDI_V3_NAMESPACE);
102     }
103     else // Default to UDDI v2 values
104
{
105       element.setAttribute("generic",IRegistry.UDDI_V2_GENERIC);
106       element.setAttribute("xmlns",IRegistry.UDDI_V2_NAMESPACE);
107     }
108
109     String JavaDoc operator = detail.getOperator();
110     if (operator != null)
111       element.setAttribute("operator",operator);
112     else
113       element.setAttribute("operator","");
114
115     boolean truncated = detail.isTruncated();
116     if (truncated)
117       element.setAttribute("truncated","true");
118
119     Vector JavaDoc vector = detail.getTModelVector();
120     if ((vector!=null) && (vector.size() > 0))
121     {
122       handler = maker.lookup(TModelHandler.TAG_NAME);
123       for (int i=0; i < vector.size(); i++)
124         handler.marshal((TModel)vector.elementAt(i),element);
125     }
126
127     parent.appendChild(element);
128   }
129
130
131   /***************************************************************************/
132   /***************************** TEST DRIVER *********************************/
133   /***************************************************************************/
134
135
136   public static void main(String JavaDoc args[])
137     throws Exception JavaDoc
138   {
139     HandlerMaker maker = HandlerMaker.getInstance();
140     AbstractHandler handler = maker.lookup(TModelDetailHandler.TAG_NAME);
141
142     Element JavaDoc parent = XMLUtils.newRootElement();
143     Element JavaDoc child = null;
144
145     OverviewDoc overDoc = new OverviewDoc();
146     overDoc.setOverviewURL("http://www.sviens.com/service.html");
147     overDoc.addDescription(new Description("over-doc Descr"));
148     overDoc.addDescription(new Description("over-doc Descr Two","en"));
149
150     CategoryBag catBag = new CategoryBag();
151     catBag.addKeyedReference(new KeyedReference("catBagKeyName","catBagKeyValue"));
152     catBag.addKeyedReference(new KeyedReference("uuid:dfddb58c-4853-4a71-865c-f84509017cc7","catBagKeyName2","catBagKeyValue2"));
153
154     IdentifierBag idBag = new IdentifierBag();
155     idBag.addKeyedReference(new KeyedReference("idBagKeyName","idBagkeyValue"));
156     idBag.addKeyedReference(new KeyedReference("uuid:f78a135a-4769-4e79-8604-54d440314bc0","idBagKeyName2","idBagkeyValue2"));
157
158     TModel tModel = new TModel();
159     tModel.setTModelKey("uuid:269855db-62eb-4862-8e5a-1b06f2753038");
160     tModel.setOperator("jUDDI");
161     tModel.setAuthorizedName("Steve Viens");
162     tModel.setName("jUDDI TModel");
163     tModel.addDescription(new Description("tModel whatever"));
164     tModel.addDescription(new Description("tModel whatever too","fr"));
165     tModel.setCategoryBag(catBag);
166     tModel.setIdentifierBag(idBag);
167     tModel.setOverviewDoc(overDoc);
168
169     TModelDetail detail = new TModelDetail();
170     detail.setGeneric("2.0");
171     detail.setOperator("jUDDI.org");
172     detail.setTruncated(true);
173     detail.addTModel(tModel);
174     detail.addTModel(tModel);
175
176     System.out.println();
177
178     RegistryObject regObject = detail;
179     handler.marshal(regObject,parent);
180     child = (Element JavaDoc)parent.getFirstChild();
181     parent.removeChild(child);
182     XMLUtils.writeXML(child,System.out);
183
184     System.out.println();
185
186     regObject = handler.unmarshal(child);
187     handler.marshal(regObject,parent);
188     child = (Element JavaDoc)parent.getFirstChild();
189     parent.removeChild(child);
190     XMLUtils.writeXML(child,System.out);
191   }
192 }
Popular Tags