View Javadoc

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;
19  
20  import org.apache.juddi.datatype.CategoryBag;
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;
25  
26  /***
27   * "Knows about the creation and populating of CategoryBag objects.
28   * Returns CategoryBag."
29   *
30   * @author Steve Viens (sviens@apache.org)
31   */
32  public class CategoryBagHandler extends AbstractHandler
33  {
34    public static final String TAG_NAME = "categoryBag";
35  
36    private HandlerMaker maker = null;
37  
38    protected CategoryBagHandler(HandlerMaker maker)
39    {
40      this.maker = maker;
41    }
42  
43    public RegistryObject unmarshal(Element element)
44    {
45      CategoryBag obj = new CategoryBag();
46      Vector 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,KeyedReferenceHandler.TAG_NAME);
57      for (int i=0; i<nodeList.size(); i++)
58      {
59        handler = maker.lookup(KeyedReferenceHandler.TAG_NAME);
60        obj.addKeyedReference((KeyedReference)handler.unmarshal((Element)nodeList.elementAt(i)));
61      }
62  
63      return obj;
64    }
65  
66    public void marshal(RegistryObject object,Element parent)
67    {
68      CategoryBag categoryBag = (CategoryBag)object;
69      String generic = getGeneric(null);
70      String namespace = getUDDINamespace(generic);
71      Element element = parent.getOwnerDocument().createElementNS(namespace,TAG_NAME);
72  
73      Vector keyedRefVector = categoryBag.getKeyedReferenceVector();
74      if ((keyedRefVector!=null) && (keyedRefVector.size() > 0))
75      {
76        AbstractHandler handler = maker.lookup(KeyedReferenceHandler.TAG_NAME);
77        for (int i=0; i < keyedRefVector.size(); i++)
78          handler.marshal((KeyedReference)keyedRefVector.elementAt(i),element);
79      }
80  
81      parent.appendChild(element);
82    }
83  
84  
85    /****************************************************************************/
86    /****************************** TEST DRIVER *********************************/
87    /****************************************************************************/
88  
89  
90    public static void main(String args[])
91      throws Exception
92    {
93  
94      HandlerMaker maker = HandlerMaker.getInstance();
95      AbstractHandler handler = maker.lookup(CategoryBagHandler.TAG_NAME);
96  
97      Element parent = XMLUtils.newRootElement();
98      Element child = null;
99  
100     CategoryBag categoryBag = new CategoryBag();
101     categoryBag.addKeyedReference(new KeyedReference("catBagKeyName","catBagKeyValue"));
102     categoryBag.addKeyedReference(new KeyedReference("uuid:8ff45356-acde-4a4c-86bf-f953611d20c6","catBagKeyName2","catBagKeyValue2"));
103 
104     System.out.println();
105 
106     RegistryObject regObject = categoryBag;
107     handler.marshal(regObject,parent);
108     child = (Element)parent.getFirstChild();
109     parent.removeChild(child);
110     XMLUtils.writeXML(child,System.out);
111 
112     System.out.println();
113 
114     regObject = handler.unmarshal(child);
115     handler.marshal(regObject,parent);
116     child = (Element)parent.getFirstChild();
117     parent.removeChild(child);
118     XMLUtils.writeXML(child,System.out);
119   }
120 }