KickJava   Java API By Example, From Geeks To Geeks.

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


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.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 JavaDoc;
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 JavaDoc 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 JavaDoc element)
51   {
52     Contacts obj = new Contacts();
53     Vector JavaDoc 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 JavaDoc)nodeList.elementAt(i)));
68     }
69
70     return obj;
71   }
72
73   public void marshal(RegistryObject object,Element JavaDoc parent)
74   {
75     Contacts contacts = (Contacts)object;
76     Element JavaDoc element = parent.getOwnerDocument().createElementNS(null,TAG_NAME);
77     AbstractHandler handler = null;
78
79     Vector JavaDoc contactVector = contacts.getContactVector();
80     if ((contactVector!=null) && (contactVector.size() > 0))
81     {
82       handler = maker.lookup(ContactHandler.TAG_NAME);
83       for (int i=0; i < contactVector.size(); i++)
84         handler.marshal((Contact)contactVector.elementAt(i),element);
85     }
86
87     parent.appendChild(element);
88   }
89
90
91   /***************************************************************************/
92   /***************************** TEST DRIVER *********************************/
93   /***************************************************************************/
94
95
96   public static void main(String JavaDoc args[])
97     throws Exception JavaDoc
98   {
99     HandlerMaker maker = HandlerMaker.getInstance();
100     AbstractHandler handler = maker.lookup(ContactsHandler.TAG_NAME);
101
102     Element JavaDoc parent = XMLUtils.newRootElement();
103     Element JavaDoc child = null;
104
105     Address address = new Address();
106     address.setUseType("myAddressUseType");
107     address.setSortCode("sortThis");
108     address.setTModelKey(null);
109     address.addAddressLine(new AddressLine("AddressLine1","keyNameAttr","keyValueAttr"));
110     address.addAddressLine(new AddressLine("AddressLine2"));
111
112     Contact contact = new Contact();
113     contact.setUseType("myAddressUseType");
114     contact.setPersonNameValue("Bob Whatever");
115     contact.addDescription(new Description("Bob is a big fat jerk"));
116     contact.addDescription(new Description("obBay sIay a igBay atFay erkJay","es"));
117     contact.addEmail(new Email("bob@whatever.com"));
118     contact.addPhone(new Phone("(603)559-1901"));
119     contact.addAddress(address);
120
121     Contacts contacts = new Contacts();
122     contacts.addContact(contact);
123     contacts.addContact(contact);
124
125     System.out.println();
126
127     RegistryObject regObject = contacts;
128     handler.marshal(regObject,parent);
129     child = (Element JavaDoc)parent.getFirstChild();
130     parent.removeChild(child);
131     XMLUtils.writeXML(child,System.out);
132
133     System.out.println();
134
135     regObject = handler.unmarshal(child);
136     handler.marshal(regObject,parent);
137     child = (Element JavaDoc)parent.getFirstChild();
138     parent.removeChild(child);
139     XMLUtils.writeXML(child,System.out);
140
141     System.out.println();
142   }
143 }
Popular Tags