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.Address;
21  import org.apache.juddi.datatype.AddressLine;
22  import org.apache.juddi.datatype.Description;
23  import org.apache.juddi.datatype.Email;
24  import org.apache.juddi.datatype.Phone;
25  import org.apache.juddi.datatype.RegistryObject;
26  import org.apache.juddi.datatype.business.Contact;
27  import org.apache.juddi.datatype.business.Contacts;
28  import org.apache.juddi.util.xml.XMLUtils;
29  import org.w3c.dom.Element;
30  
31  /***
32   * ContactsHandler
33   *
34   * "Knows about the creation and populating of KeyedReference objects.
35   * Returns Contacts."
36   *
37   * @author Steve Viens (sviens@apache.org)
38   */
39  public class ContactsHandler extends AbstractHandler
40  {
41    public static final String TAG_NAME = "contacts";
42  
43    private HandlerMaker maker = null;
44  
45    protected ContactsHandler(HandlerMaker maker)
46    {
47      this.maker = maker;
48    }
49  
50    public RegistryObject unmarshal(Element element)
51    {
52      Contacts obj = new Contacts();
53      Vector nodeList = null;
54      AbstractHandler handler = null;
55  
56      // Attributes
57      // {none}
58  
59      // Text Node Value
60      // {none}
61  
62      // Child Elements
63      nodeList = XMLUtils.getChildElementsByTagName(element,ContactHandler.TAG_NAME);
64      for (int i=0; i<nodeList.size(); i++)
65      {
66        handler = maker.lookup(ContactHandler.TAG_NAME);
67        obj.addContact((Contact)handler.unmarshal((Element)nodeList.elementAt(i)));
68      }
69  
70      return obj;
71    }
72  
73    public void marshal(RegistryObject object,Element parent)
74    {
75      Contacts contacts = (Contacts)object;
76      String generic = getGeneric(null);
77      String namespace = getUDDINamespace(generic);
78      Element element = parent.getOwnerDocument().createElementNS(namespace,TAG_NAME);
79      AbstractHandler handler = null;
80  
81      Vector contactVector = contacts.getContactVector();
82      if ((contactVector!=null) && (contactVector.size() > 0))
83      {
84        handler = maker.lookup(ContactHandler.TAG_NAME);
85        for (int i=0; i < contactVector.size(); i++)
86          handler.marshal((Contact)contactVector.elementAt(i),element);
87      }
88  
89      parent.appendChild(element);
90    }
91  
92  
93    /****************************************************************************/
94    /****************************** TEST DRIVER *********************************/
95    /****************************************************************************/
96  
97  
98    public static void main(String args[])
99      throws Exception
100   {
101     HandlerMaker maker = HandlerMaker.getInstance();
102     AbstractHandler handler = maker.lookup(ContactsHandler.TAG_NAME);
103 
104     Element parent = XMLUtils.newRootElement();
105     Element child = null;
106 
107     Address address = new Address();
108     address.setUseType("myAddressUseType");
109     address.setSortCode("sortThis");
110     address.setTModelKey(null);
111     address.addAddressLine(new AddressLine("AddressLine1","keyNameAttr","keyValueAttr"));
112     address.addAddressLine(new AddressLine("AddressLine2"));
113 
114     Contact contact = new Contact();
115     contact.setUseType("myAddressUseType");
116     contact.setPersonNameValue("Bob Whatever");
117     contact.addDescription(new Description("Bob is a big fat jerk"));
118     contact.addDescription(new Description("obBay sIay a igBay atFay erkJay","es"));
119     contact.addEmail(new Email("bob@whatever.com"));
120     contact.addPhone(new Phone("(603)559-1901"));
121     contact.addAddress(address);
122 
123     Contacts contacts = new Contacts();
124     contacts.addContact(contact);
125     contacts.addContact(contact);
126 
127     System.out.println();
128 
129     RegistryObject regObject = contacts;
130     handler.marshal(regObject,parent);
131     child = (Element)parent.getFirstChild();
132     parent.removeChild(child);
133     XMLUtils.writeXML(child,System.out);
134 
135     System.out.println();
136 
137     regObject = handler.unmarshal(child);
138     handler.marshal(regObject,parent);
139     child = (Element)parent.getFirstChild();
140     parent.removeChild(child);
141     XMLUtils.writeXML(child,System.out);
142 
143     System.out.println();
144   }
145 }