KickJava   Java API By Example, From Geeks To Geeks.

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


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.CategoryBag;
21 import org.apache.juddi.datatype.Description;
22 import org.apache.juddi.datatype.IdentifierBag;
23 import org.apache.juddi.datatype.KeyedReference;
24 import org.apache.juddi.datatype.Name;
25 import org.apache.juddi.datatype.OverviewDoc;
26 import org.apache.juddi.datatype.RegistryObject;
27 import org.apache.juddi.datatype.tmodel.TModel;
28 import org.apache.juddi.util.xml.XMLUtils;
29 import org.w3c.dom.Element JavaDoc;
30
31 /**
32  * TModelHandler
33  *
34  * @author Steve Viens (sviens@apache.org)
35  * @author Anou Mana (anou_mana@users.sourceforge.net)
36  */

37 public class TModelHandler extends AbstractHandler
38 {
39   public static final String JavaDoc TAG_NAME = "tModel";
40
41   private HandlerMaker maker = null;
42
43   protected TModelHandler(HandlerMaker maker)
44   {
45     this.maker = maker;
46   }
47
48   public RegistryObject unmarshal(Element JavaDoc element)
49   {
50     TModel obj = new TModel();
51     Vector JavaDoc nodeList = null;
52     AbstractHandler handler = null;
53
54     // Attributes
55
obj.setTModelKey(element.getAttribute("tModelKey"));
56     obj.setOperator(element.getAttribute("operator"));
57     obj.setAuthorizedName(element.getAttribute("authorizedName"));
58
59     // Text Node Value
60
// [none]
61

62     // Child Elements
63
nodeList = XMLUtils.getChildElementsByTagName(element,NameHandler.TAG_NAME);
64     if (nodeList.size() > 0)
65     {
66       handler = maker.lookup(NameHandler.TAG_NAME);
67       Name name = (Name )handler.unmarshal((Element JavaDoc)nodeList.elementAt(0));
68       if (name != null)
69         obj.setName(name);
70     }
71
72     nodeList = XMLUtils.getChildElementsByTagName(element,DescriptionHandler.TAG_NAME);
73     for (int i=0; i<nodeList.size(); i++)
74     {
75       handler = maker.lookup(DescriptionHandler.TAG_NAME);
76       Description descr = (Description)handler.unmarshal((Element JavaDoc)nodeList.elementAt(i));
77       if (descr != null)
78         obj.addDescription(descr);
79     }
80
81     nodeList = XMLUtils.getChildElementsByTagName(element,OverviewDocHandler.TAG_NAME);
82     if (nodeList.size() > 0)
83     {
84       handler = maker.lookup(OverviewDocHandler.TAG_NAME);
85       obj.setOverviewDoc((OverviewDoc)handler.unmarshal((Element JavaDoc)nodeList.elementAt(0)));
86     }
87
88     nodeList = XMLUtils.getChildElementsByTagName(element,IdentifierBagHandler.TAG_NAME);
89     if (nodeList.size() > 0)
90     {
91       handler = maker.lookup(IdentifierBagHandler.TAG_NAME);
92       obj.setIdentifierBag((IdentifierBag)handler.unmarshal((Element JavaDoc)nodeList.elementAt(0)));
93     }
94
95     nodeList = XMLUtils.getChildElementsByTagName(element,CategoryBagHandler.TAG_NAME);
96     if (nodeList.size() > 0)
97     {
98       handler = maker.lookup(CategoryBagHandler.TAG_NAME);
99       obj.setCategoryBag((CategoryBag)handler.unmarshal((Element JavaDoc)nodeList.elementAt(0)));
100     }
101
102     return obj;
103   }
104
105   public void marshal(RegistryObject object,Element JavaDoc parent)
106   {
107     TModel tModel = (TModel)object;
108     Element JavaDoc element = parent.getOwnerDocument().createElementNS(null,TAG_NAME);
109     AbstractHandler handler = null;
110
111     String JavaDoc tModelKey = tModel.getTModelKey();
112     if (tModelKey != null)
113       element.setAttribute("tModelKey",tModelKey);
114     else
115       element.setAttribute("tModelKey","");
116
117     String JavaDoc operator = tModel.getOperator();
118     if (operator != null)
119       element.setAttribute("operator",operator);
120
121     String JavaDoc authName = tModel.getAuthorizedName();
122     if (authName != null)
123       element.setAttribute("authorizedName",authName);
124
125     String JavaDoc name = tModel.getName();
126     if (name != null)
127     {
128       handler = maker.lookup(NameHandler.TAG_NAME);
129       handler.marshal(new Name(name),element);
130     }
131
132     Vector JavaDoc descrVector = tModel.getDescriptionVector();
133     if ((descrVector!=null) && (descrVector.size() > 0))
134     {
135       handler = maker.lookup(DescriptionHandler.TAG_NAME);
136       for (int i=0; i < descrVector.size(); i++)
137         handler.marshal((Description)descrVector.elementAt(i),element);
138     }
139
140     OverviewDoc overDoc = tModel.getOverviewDoc();
141     if (overDoc != null)
142     {
143       handler = maker.lookup(OverviewDocHandler.TAG_NAME);
144       handler.marshal(overDoc,element);
145     }
146
147     IdentifierBag identifierBag = tModel.getIdentifierBag();
148     if ((identifierBag != null) && (identifierBag.getKeyedReferenceVector() != null) && (!identifierBag.getKeyedReferenceVector().isEmpty()))
149     {
150       handler = maker.lookup(IdentifierBagHandler.TAG_NAME);
151       handler.marshal(identifierBag,element);
152     }
153
154     CategoryBag categoryBag = tModel.getCategoryBag();
155     if ((categoryBag != null) && (categoryBag.getKeyedReferenceVector() != null) && (!categoryBag.getKeyedReferenceVector().isEmpty()))
156     {
157       handler = maker.lookup(CategoryBagHandler.TAG_NAME);
158       handler.marshal(categoryBag,element);
159     }
160
161     parent.appendChild(element);
162   }
163
164
165   /***************************************************************************/
166   /***************************** TEST DRIVER *********************************/
167   /***************************************************************************/
168
169
170   public static void main(String JavaDoc args[])
171     throws Exception JavaDoc
172   {
173     HandlerMaker maker = HandlerMaker.getInstance();
174     AbstractHandler handler = maker.lookup(TModelHandler.TAG_NAME);
175
176     Element JavaDoc parent = XMLUtils.newRootElement();
177     Element JavaDoc child = null;
178
179     OverviewDoc overDoc = new OverviewDoc();
180     overDoc.setOverviewURL("http://www.sviens.com/service.html");
181     overDoc.addDescription(new Description("over-doc Descr"));
182     overDoc.addDescription(new Description("over-doc Descr Two","en"));
183
184     CategoryBag catBag = new CategoryBag();
185     catBag.addKeyedReference(new KeyedReference("catBagKeyName","catBagKeyValue"));
186     catBag.addKeyedReference(new KeyedReference("uuid:dfddb58c-4853-4a71-865c-f84509017cc7","catBagKeyName2","catBagKeyValue2"));
187
188     IdentifierBag idBag = new IdentifierBag();
189     idBag.addKeyedReference(new KeyedReference("idBagKeyName","idBagkeyValue"));
190     idBag.addKeyedReference(new KeyedReference("uuid:f78a135a-4769-4e79-8604-54d440314bc0","idBagKeyName2","idBagkeyValue2"));
191
192     TModel tModel = new TModel();
193     tModel.setTModelKey("uuid:269855db-62eb-4862-8e5a-1b06f2753038");
194     tModel.setOperator("jUDDI");
195     tModel.setAuthorizedName("Steve Viens");
196     tModel.setName("jUDDI TModel");
197     tModel.addDescription(new Description("tModel whatever"));
198     tModel.addDescription(new Description("tModel whatever too","fr"));
199     tModel.setCategoryBag(catBag);
200     tModel.setIdentifierBag(idBag);
201     tModel.setOverviewDoc(overDoc);
202
203     System.out.println();
204
205     RegistryObject regObject = tModel;
206     handler.marshal(regObject,parent);
207     child = (Element JavaDoc)parent.getFirstChild();
208     parent.removeChild(child);
209     XMLUtils.writeXML(child,System.out);
210
211     System.out.println();
212
213     regObject = handler.unmarshal(child);
214     handler.marshal(regObject,parent);
215     child = (Element JavaDoc)parent.getFirstChild();
216     parent.removeChild(child);
217     XMLUtils.writeXML(child,System.out);
218   }
219 }
Popular Tags