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.IdentifierBag;
22  import org.apache.juddi.datatype.KeyedReference;
23  import org.apache.juddi.datatype.Name;
24  import org.apache.juddi.datatype.RegistryObject;
25  import org.apache.juddi.datatype.request.FindQualifier;
26  import org.apache.juddi.datatype.request.FindQualifiers;
27  import org.apache.juddi.datatype.request.FindTModel;
28  import org.apache.juddi.util.xml.XMLUtils;
29  import org.w3c.dom.Element;
30  
31  /***
32   * FindTModelHandler
33   *
34   * "Knows about the creation and populating of FindTModel objects.
35   * Returns FindTModel."
36   *
37   * @author Steve Viens (sviens@apache.org)
38   * @author Anou Mana (anou_mana@users.sourceforge.net)
39   */
40  public class FindTModelHandler extends AbstractHandler
41  {
42    public static final String TAG_NAME = "find_tModel";
43  
44    private HandlerMaker maker = null;
45  
46    protected FindTModelHandler(HandlerMaker maker)
47    {
48      this.maker = maker;
49    }
50  
51    public RegistryObject unmarshal(Element element)
52    {
53      FindTModel obj = new FindTModel();
54      Vector nodeList = null;
55      AbstractHandler handler = null;
56  
57      // Attributes
58      String generic = element.getAttribute("generic");
59      if ((generic != null && (generic.trim().length() > 0)))
60        obj.setGeneric(generic);
61  
62      String maxRows = element.getAttribute("maxRows");
63      if ((maxRows != null) && (maxRows.length() > 0))
64        obj.setMaxRows(maxRows);
65  
66      // Text Node Value
67      // {none}
68  
69      // Child Elements
70      nodeList = XMLUtils.getChildElementsByTagName(element,NameHandler.TAG_NAME);
71      if (nodeList.size() > 0)
72      {
73        handler = maker.lookup(NameHandler.TAG_NAME);
74        Name name = (Name )handler.unmarshal((Element)nodeList.elementAt(0));
75        if (name != null)
76          obj.setName(name);    
77      }
78  
79      nodeList = XMLUtils.getChildElementsByTagName(element,FindQualifiersHandler.TAG_NAME);
80      if (nodeList.size() > 0)
81      {
82        handler = maker.lookup(FindQualifiersHandler.TAG_NAME);
83        obj.setFindQualifiers((FindQualifiers)handler.unmarshal((Element)nodeList.elementAt(0)));
84      }
85  
86      nodeList = XMLUtils.getChildElementsByTagName(element,IdentifierBagHandler.TAG_NAME);
87      if (nodeList.size() > 0)
88      {
89        handler = maker.lookup(IdentifierBagHandler.TAG_NAME);
90        obj.setIdentifierBag((IdentifierBag)handler.unmarshal((Element)nodeList.elementAt(0)));
91      }
92  
93      nodeList = XMLUtils.getChildElementsByTagName(element,CategoryBagHandler.TAG_NAME);
94      if (nodeList.size() > 0)
95      {
96        handler = maker.lookup(CategoryBagHandler.TAG_NAME);
97        obj.setCategoryBag((CategoryBag)handler.unmarshal((Element)nodeList.elementAt(0)));
98      }
99  
100     return obj;
101   }
102 
103   public void marshal(RegistryObject object,Element parent)
104   {
105     FindTModel request = (FindTModel)object;
106     String generic = request.getGeneric();
107     generic = getGeneric(generic);
108     String namespace = getUDDINamespace(generic);
109     Element element = parent.getOwnerDocument().createElementNS(namespace,TAG_NAME);
110     AbstractHandler handler = null;
111     
112     element.setAttribute("generic",generic);
113 
114     int maxRows = request.getMaxRows();
115     if (maxRows > 0)
116       element.setAttribute("maxRows",String.valueOf(maxRows));
117 
118     FindQualifiers qualifiers = request.getFindQualifiers();
119     if ((qualifiers != null) && (qualifiers.size() > 0))
120     {
121       handler = maker.lookup(FindQualifiersHandler.TAG_NAME);
122       handler.marshal(qualifiers,element);
123     }
124 
125     Name name = request.getName();
126     if (name != null)
127     {
128       handler = maker.lookup(NameHandler.TAG_NAME);
129       handler.marshal(name,element);
130     }
131 
132     IdentifierBag idBag = request.getIdentifierBag();
133     if (idBag != null)
134     {
135       handler = maker.lookup(IdentifierBagHandler.TAG_NAME);
136       handler.marshal(idBag,element);
137     }
138 
139     CategoryBag catBag = request.getCategoryBag();
140     if (catBag != null)
141     {
142       handler = maker.lookup(CategoryBagHandler.TAG_NAME);
143       handler.marshal(catBag,element);
144     }
145 
146     parent.appendChild(element);
147   }
148 
149 
150   /****************************************************************************/
151   /****************************** TEST DRIVER *********************************/
152   /****************************************************************************/
153 
154 
155   public static void main(String args[])
156     throws Exception
157   {
158     HandlerMaker maker = HandlerMaker.getInstance();
159     AbstractHandler handler = maker.lookup(FindTModelHandler.TAG_NAME);
160 
161     Element parent = XMLUtils.newRootElement();
162     Element child = null;
163 
164     IdentifierBag idBag = new IdentifierBag();
165     idBag.addKeyedReference(new KeyedReference("idBagKeyName","idBagKeyValue"));
166     idBag.addKeyedReference(new KeyedReference("uuid:3860b975-9e0c-4cec-bad6-87dfe00e3864","idBagKeyName2","idBagKeyValue2"));
167 
168     CategoryBag catBag = new CategoryBag();
169     catBag.addKeyedReference(new KeyedReference("catBagKeyName","catBagKeyValue"));
170     catBag.addKeyedReference(new KeyedReference("uuid:8ff45356-acde-4a4c-86bf-f953611d20c6","catBagKeyName2","catBagKeyValue2"));
171 
172     FindTModel request = new FindTModel();
173     request.setName(new Name("serviceNm2","en"));
174     request.addFindQualifier(new FindQualifier(FindQualifier.SORT_BY_DATE_ASC));
175     request.addFindQualifier(new FindQualifier(FindQualifier.AND_ALL_KEYS));
176     request.setMaxRows(37);
177     request.setIdentifierBag(idBag);
178     request.setCategoryBag(catBag);
179 
180     System.out.println();
181 
182     RegistryObject regObject = request;
183     handler.marshal(regObject,parent);
184     child = (Element)parent.getFirstChild();
185     parent.removeChild(child);
186     XMLUtils.writeXML(child,System.out);
187 
188     System.out.println();
189 
190     regObject = handler.unmarshal(child);
191     handler.marshal(regObject,parent);
192     child = (Element)parent.getFirstChild();
193     parent.removeChild(child);
194     XMLUtils.writeXML(child,System.out);
195   }
196 }