KickJava   Java API By Example, From Geeks To Geeks.

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


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.IdentifierBag;
21 import org.apache.juddi.datatype.KeyedReference;
22 import org.apache.juddi.datatype.RegistryObject;
23 import org.apache.juddi.util.xml.XMLUtils;
24 import org.w3c.dom.Element JavaDoc;
25
26 /**
27  * @author Steve Viens (sviens@apache.org)
28  */

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

49     // Text Node Value
50
// {none}
51

52     // Child Elements
53
nodeList = XMLUtils.getChildElementsByTagName(element,KeyedReferenceHandler.TAG_NAME);
54     for (int i=0; i<nodeList.size(); i++)
55     {
56       handler = maker.lookup(KeyedReferenceHandler.TAG_NAME);
57       obj.addKeyedReference((KeyedReference)handler.unmarshal((Element JavaDoc)nodeList.elementAt(i)));
58     }
59
60     return obj;
61   }
62
63   public void marshal(RegistryObject object,Element JavaDoc parent)
64   {
65     IdentifierBag categoryBag = (IdentifierBag)object;
66     Element JavaDoc element = parent.getOwnerDocument().createElementNS(null,TAG_NAME);
67     AbstractHandler handler = null;
68
69     Vector JavaDoc keyedRefVector = categoryBag.getKeyedReferenceVector();
70     if ((keyedRefVector!=null) && (keyedRefVector.size() > 0))
71     {
72       handler = maker.lookup(KeyedReferenceHandler.TAG_NAME);
73       for (int i=0; i < keyedRefVector.size(); i++)
74         handler.marshal((KeyedReference)keyedRefVector.elementAt(i),element);
75     }
76
77     parent.appendChild(element);
78   }
79
80
81   /***************************************************************************/
82   /***************************** TEST DRIVER *********************************/
83   /***************************************************************************/
84
85
86   public static void main(String JavaDoc args[])
87     throws Exception JavaDoc
88   {
89     HandlerMaker maker = HandlerMaker.getInstance();
90     AbstractHandler handler = maker.lookup(IdentifierBagHandler.TAG_NAME);
91     Element JavaDoc parent = XMLUtils.newRootElement();
92     Element JavaDoc child = null;
93
94     IdentifierBag identifierBag = new IdentifierBag();
95     identifierBag.addKeyedReference(new KeyedReference("idBagKeyName","idBagKeyValue"));
96     identifierBag.addKeyedReference(new KeyedReference("uuid:3860b975-9e0c-4cec-bad6-87dfe00e3864","idBagKeyName2","idBagKeyValue2"));
97
98     System.out.println();
99
100     RegistryObject regObject = identifierBag;
101     handler.marshal(regObject,parent);
102     child = (Element JavaDoc)parent.getFirstChild();
103     parent.removeChild(child);
104     XMLUtils.writeXML(child,System.out);
105
106     System.out.println();
107
108     regObject = handler.unmarshal(child);
109     handler.marshal(regObject,parent);
110     child = (Element JavaDoc)parent.getFirstChild();
111     parent.removeChild(child);
112     XMLUtils.writeXML(child,System.out);
113   }
114 }
Popular Tags