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.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;
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 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 element)
52    {
53      Contact obj = new Contact();
54      Vector nodeList = null;
55      AbstractHandler handler = null;
56  
57      // Attributes
58      String 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)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)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)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)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)nodeList.elementAt(i)));
101     }
102 
103     return obj;
104   }
105 
106   public void marshal(RegistryObject object,Element parent)
107   {
108     Contact contact = (Contact)object;
109     String generic = getGeneric(null);
110     String namespace = getUDDINamespace(generic);
111     Element element = parent.getOwnerDocument().createElementNS(namespace,TAG_NAME);
112     AbstractHandler handler = null;
113 
114     String useType = contact.getUseType();
115     if ((useType != null) && (useType.trim().length() > 0))
116       element.setAttribute("useType",useType);
117 
118     Vector descrVector = contact.getDescriptionVector();
119     if ((descrVector!=null) && (descrVector.size() > 0))
120     {
121       handler = maker.lookup(DescriptionHandler.TAG_NAME);
122       for (int i=0; i < descrVector.size(); i++)
123         handler.marshal((Description)descrVector.elementAt(i),element);
124     }
125 
126     PersonName personName = contact.getPersonName();
127     if ((personName != null))
128     {
129       handler = maker.lookup(PersonNameHandler.TAG_NAME);
130       handler.marshal(personName,element);
131     }
132 
133     Vector phoneVector = contact.getPhoneVector();
134     if ((phoneVector!=null) && (phoneVector.size() > 0))
135     {
136       handler = maker.lookup(PhoneHandler.TAG_NAME);
137       for (int i=0; i < phoneVector.size(); i++)
138         handler.marshal((Phone)phoneVector.elementAt(i),element);
139     }
140 
141     Vector emailVector = contact.getEmailVector();
142     if ((emailVector!=null) && (emailVector.size() > 0))
143     {
144       handler = maker.lookup(EmailHandler.TAG_NAME);
145       for (int i=0; i < emailVector.size(); i++)
146         handler.marshal((Email)emailVector.elementAt(i),element);
147     }
148 
149     Vector addressVector = contact.getAddressVector();
150     if ((addressVector!=null) && (addressVector.size() > 0))
151     {
152       handler = maker.lookup(AddressHandler.TAG_NAME);
153       for (int i=0; i < addressVector.size(); i++)
154         handler.marshal((Address)addressVector.elementAt(i),element);
155     }
156 
157     parent.appendChild(element);
158   }
159 
160 
161   /****************************************************************************/
162   /****************************** TEST DRIVER *********************************/
163   /****************************************************************************/
164 
165 
166   public static void main(String args[])
167     throws Exception
168   {
169     HandlerMaker maker = HandlerMaker.getInstance();
170     AbstractHandler handler = maker.lookup(ContactHandler.TAG_NAME);
171     Element parent = XMLUtils.newRootElement();
172     Element child = null;
173 
174     Address address = new Address();
175     address.setUseType("myAddressUseType");
176     address.setSortCode("sortThis");
177     address.setTModelKey(null);
178     address.addAddressLine(new AddressLine("AddressLine1","keyNameAttr","keyValueAttr"));
179     address.addAddressLine(new AddressLine("AddressLine2"));
180 
181     Contact contact = new Contact();
182     //contact.setUseType("myAddressUseType");
183     contact.setPersonNameValue("Bob Whatever");
184     contact.addDescription(new Description("Bob is a big fat jerk"));
185     contact.addDescription(new Description("obBay sIay a igBay atFay erkJay","es"));
186     contact.addEmail(new Email("bob@whatever.com"));
187     contact.addPhone(new Phone("(603)559-1901"));
188     contact.addAddress(address);
189 
190     System.out.println();
191 
192     RegistryObject regObject = contact;
193     handler.marshal(regObject,parent);
194     child = (Element)parent.getFirstChild();
195     parent.removeChild(child);
196     XMLUtils.writeXML(child,System.out);
197 
198     System.out.println();
199 
200     regObject = handler.unmarshal(child);
201     handler.marshal(regObject,parent);
202     child = (Element)parent.getFirstChild();
203     parent.removeChild(child);
204     XMLUtils.writeXML(child,System.out);
205 
206     System.out.println();
207   }
208 }