KickJava   Java API By Example, From Geeks To Geeks.

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


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.PersonName;
25 import org.apache.juddi.datatype.Phone;
26 import org.apache.juddi.datatype.RegistryObject;
27 import org.apache.juddi.datatype.business.Contact;
28 import org.apache.juddi.util.xml.XMLUtils;
29 import org.w3c.dom.Element JavaDoc;
30
31 /**
32  * ContactHandler
33  *
34  * "Knows about the creation and populating of KeyedReference objects.
35  * Returns Contact."
36  *
37  * @author Steve Viens (sviens@apache.org)
38  * @author Anou Mana (anou_mana@users.sourceforge.net)
39  */

40 public class ContactHandler extends AbstractHandler
41 {
42   public static final String JavaDoc TAG_NAME = "contact";
43
44   private HandlerMaker maker = null;
45
46   protected ContactHandler(HandlerMaker maker)
47   {
48     this.maker = maker;
49   }
50
51   public RegistryObject unmarshal(Element JavaDoc element)
52   {
53     Contact obj = new Contact();
54     Vector JavaDoc nodeList = null;
55     AbstractHandler handler = null;
56
57     // Attributes
58
String JavaDoc useType = element.getAttribute("useType");
59     if ((useType != null) && (useType.trim().length() > 0))
60       obj.setUseType(useType);
61
62     // Text Node Value
63
// {none}
64

65     // Child Elements
66
nodeList = XMLUtils.getChildElementsByTagName(element,PersonNameHandler.TAG_NAME);
67     if (nodeList.size() > 0)
68     {
69       handler = maker.lookup(PersonNameHandler.TAG_NAME);
70       obj.setPersonName((PersonName)handler.unmarshal((Element JavaDoc)nodeList.elementAt(0)));
71     }
72
73     nodeList = XMLUtils.getChildElementsByTagName(element,DescriptionHandler.TAG_NAME);
74     for (int i=0; i<nodeList.size(); i++)
75     {
76       handler = maker.lookup(DescriptionHandler.TAG_NAME);
77       Description descr = (Description)handler.unmarshal((Element JavaDoc)nodeList.elementAt(i));
78       if (descr != null)
79         obj.addDescription(descr);
80     }
81
82     nodeList = XMLUtils.getChildElementsByTagName(element,AddressHandler.TAG_NAME);
83     for (int i=0; i<nodeList.size(); i++)
84     {
85       handler = maker.lookup(AddressHandler.TAG_NAME);
86       obj.addAddress((Address)handler.unmarshal((Element JavaDoc)nodeList.elementAt(i)));
87     }
88
89     nodeList = XMLUtils.getChildElementsByTagName(element,EmailHandler.TAG_NAME);
90     for (int i=0; i<nodeList.size(); i++)
91     {
92       handler = maker.lookup(EmailHandler.TAG_NAME);
93       obj.addEmail((Email)handler.unmarshal((Element JavaDoc)nodeList.elementAt(i)));
94     }
95
96     nodeList = XMLUtils.getChildElementsByTagName(element,PhoneHandler.TAG_NAME);
97     for (int i=0; i<nodeList.size(); i++)
98     {
99       handler = maker.lookup(PhoneHandler.TAG_NAME);
100       obj.addPhone((Phone)handler.unmarshal((Element JavaDoc)nodeList.elementAt(i)));
101     }
102
103     return obj;
104   }
105
106   public void marshal(RegistryObject object,Element JavaDoc parent)
107   {
108     Contact contact = (Contact)object;
109     Element JavaDoc element = parent.getOwnerDocument().createElementNS(null,TAG_NAME);
110     AbstractHandler handler = null;
111
112     String JavaDoc useType = contact.getUseType();
113     if ((useType != null) && (useType.trim().length() > 0))
114       element.setAttribute("useType",useType);
115
116     Vector JavaDoc descrVector = contact.getDescriptionVector();
117     if ((descrVector!=null) && (descrVector.size() > 0))
118     {
119       handler = maker.lookup(DescriptionHandler.TAG_NAME);
120       for (int i=0; i < descrVector.size(); i++)
121         handler.marshal((Description)descrVector.elementAt(i),element);
122     }
123
124     PersonName personName = contact.getPersonName();
125     if ((personName != null))
126     {
127       handler = maker.lookup(PersonNameHandler.TAG_NAME);
128       handler.marshal(personName,element);
129     }
130
131     Vector JavaDoc phoneVector = contact.getPhoneVector();
132     if ((phoneVector!=null) && (phoneVector.size() > 0))
133     {
134       handler = maker.lookup(PhoneHandler.TAG_NAME);
135       for (int i=0; i < phoneVector.size(); i++)
136         handler.marshal((Phone)phoneVector.elementAt(i),element);
137     }
138
139     Vector JavaDoc emailVector = contact.getEmailVector();
140     if ((emailVector!=null) && (emailVector.size() > 0))
141     {
142       handler = maker.lookup(EmailHandler.TAG_NAME);
143       for (int i=0; i < emailVector.size(); i++)
144         handler.marshal((Email)emailVector.elementAt(i),element);
145     }
146
147     Vector JavaDoc addressVector = contact.getAddressVector();
148     if ((addressVector!=null) && (addressVector.size() > 0))
149     {
150       handler = maker.lookup(AddressHandler.TAG_NAME);
151       for (int i=0; i < addressVector.size(); i++)
152         handler.marshal((Address)addressVector.elementAt(i),element);
153     }
154
155     parent.appendChild(element);
156   }
157
158
159   /***************************************************************************/
160   /***************************** TEST DRIVER *********************************/
161   /***************************************************************************/
162
163
164   public static void main(String JavaDoc args[])
165     throws Exception JavaDoc
166   {
167     HandlerMaker maker = HandlerMaker.getInstance();
168     AbstractHandler handler = maker.lookup(ContactHandler.TAG_NAME);
169     Element JavaDoc parent = XMLUtils.newRootElement();
170     Element JavaDoc child = null;
171
172     Address address = new Address();
173     address.setUseType("myAddressUseType");
174     address.setSortCode("sortThis");
175     address.setTModelKey(null);
176     address.addAddressLine(new AddressLine("AddressLine1","keyNameAttr","keyValueAttr"));
177     address.addAddressLine(new AddressLine("AddressLine2"));
178
179     Contact contact = new Contact();
180     //contact.setUseType("myAddressUseType");
181
contact.setPersonNameValue("Bob Whatever");
182     contact.addDescription(new Description("Bob is a big fat jerk"));
183     contact.addDescription(new Description("obBay sIay a igBay atFay erkJay","es"));
184     contact.addEmail(new Email("bob@whatever.com"));
185     contact.addPhone(new Phone("(603)559-1901"));
186     contact.addAddress(address);
187
188     System.out.println();
189
190     RegistryObject regObject = contact;
191     handler.marshal(regObject,parent);
192     child = (Element JavaDoc)parent.getFirstChild();
193     parent.removeChild(child);
194     XMLUtils.writeXML(child,System.out);
195
196     System.out.println();
197
198     regObject = handler.unmarshal(child);
199     handler.marshal(regObject,parent);
200     child = (Element JavaDoc)parent.getFirstChild();
201     parent.removeChild(child);
202     XMLUtils.writeXML(child,System.out);
203
204     System.out.println();
205   }
206 }
Popular Tags