KickJava   Java API By Example, From Geeks To Geeks.

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


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.TModelBag;
22 import org.apache.juddi.datatype.TModelKey;
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 TModelBag objects.
28  * Returns TModelBag."
29  *
30  * @author Steve Viens (sviens@apache.org)
31  */

32 public class TModelBagHandler extends AbstractHandler
33 {
34   public static final String JavaDoc TAG_NAME = "tModelBag";
35
36   private HandlerMaker maker = null;
37
38   protected TModelBagHandler(HandlerMaker maker)
39   {
40     this.maker = maker;
41   }
42
43   public RegistryObject unmarshal(Element JavaDoc element)
44   {
45     TModelBag obj = new TModelBag();
46     Vector JavaDoc nodeList = null;
47     AbstractHandler handler = null;
48
49     // Attributes
50
// {none}
51

52    // Text Node Value
53
// {none}
54

55     // Child Elements
56
nodeList = XMLUtils.getChildElementsByTagName(element,TModelKeyHandler.TAG_NAME);
57     for (int i=0; i<nodeList.size(); i++)
58     {
59       handler = maker.lookup(TModelKeyHandler.TAG_NAME);
60       obj.addTModelKey((TModelKey)handler.unmarshal((Element JavaDoc)nodeList.elementAt(i)));
61     }
62
63     return obj;
64   }
65
66   public void marshal(RegistryObject object,Element JavaDoc parent)
67   {
68     TModelBag bag = (TModelBag)object;
69     Element JavaDoc element = parent.getOwnerDocument().createElementNS(null,TAG_NAME);
70     AbstractHandler handler = null;
71
72     Vector JavaDoc keyVector = bag.getTModelKeyVector();
73     if ((keyVector!=null) && (keyVector.size() > 0))
74     {
75       handler = maker.lookup(TModelKeyHandler.TAG_NAME);
76       for (int i=0; i < keyVector.size(); i++)
77         handler.marshal(new TModelKey((String JavaDoc)keyVector.elementAt(i)),element);
78     }
79
80     parent.appendChild(element);
81   }
82
83
84   /***************************************************************************/
85   /***************************** TEST DRIVER *********************************/
86   /***************************************************************************/
87
88
89   public static void main(String JavaDoc args[])
90     throws Exception JavaDoc
91   {
92     HandlerMaker maker = HandlerMaker.getInstance();
93     AbstractHandler handler = maker.lookup(TModelBagHandler.TAG_NAME);
94
95     Element JavaDoc parent = XMLUtils.newRootElement();
96     Element JavaDoc child = null;
97
98     TModelBag bag = new TModelBag();
99     bag.addTModelKey("uuid:35d9793b-9911-4b85-9f0e-5d4c65b4f253");
100     bag.addTModelKey(new TModelKey("uuid:c5ab113f-0d6b-4247-b3c4-8c012726acd8"));
101
102     System.out.println();
103
104     RegistryObject regObject = bag;
105     handler.marshal(regObject,parent);
106     child = (Element JavaDoc)parent.getFirstChild();
107     parent.removeChild(child);
108     XMLUtils.writeXML(child,System.out);
109
110     System.out.println();
111
112     regObject = handler.unmarshal(child);
113     handler.marshal(regObject,parent);
114     child = (Element JavaDoc)parent.getFirstChild();
115     parent.removeChild(child);
116     XMLUtils.writeXML(child,System.out);
117   }
118 }
Popular Tags