KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > clients > zipcode > AnyType


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 2002 The Apache Software Foundation. All rights
6  * reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Apache Software Foundation (http://www.apache.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "WSIF" and "Apache Software Foundation" must
28  * not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact apache@apache.org.
31  *
32  * 5. Products derived from this software may not be called "Apache",
33  * nor may "Apache" appear in their name, without prior written
34  * permission of the Apache Software Foundation.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  * ====================================================================
49  *
50  * This software consists of voluntary contributions made by many
51  * individuals on behalf of the Apache Software Foundation and was
52  * originally based on software copyright (c) 2001, 2002, International
53  * Business Machines, Inc., http://www.apache.org. For more
54  * information on the Apache Software Foundation, please see
55  * <http://www.apache.org/>.
56  */

57
58 package clients.zipcode;
59
60
61 import java.io.Serializable JavaDoc;
62
63 import java.lang.reflect.Array JavaDoc;
64
65 import java.util.Arrays JavaDoc;
66 import java.util.ArrayList JavaDoc;
67 import java.util.Date JavaDoc;
68 import java.util.GregorianCalendar JavaDoc;
69 import java.util.HashMap JavaDoc;
70 import java.util.Hashtable JavaDoc;
71 import java.util.Iterator JavaDoc;
72 import java.util.List JavaDoc;
73 import java.util.ListIterator JavaDoc;
74 import java.util.Map JavaDoc;
75 import java.util.Vector JavaDoc;
76
77 import java.text.ParseException JavaDoc;
78 import java.text.DateFormat JavaDoc;
79 import java.text.SimpleDateFormat JavaDoc;
80
81 import org.apache.xerces.dom.DocumentImpl;
82
83 import org.w3c.dom.Attr JavaDoc;
84 import org.w3c.dom.Document JavaDoc;
85 import org.w3c.dom.Element JavaDoc;
86 import org.w3c.dom.NamedNodeMap JavaDoc;
87 import org.w3c.dom.Node JavaDoc;
88 import org.w3c.dom.Text JavaDoc;
89
90
91 /**
92  * This is the base class for all generated beans.
93  */

94 public abstract class AnyType implements Serializable JavaDoc
95 {
96   /**
97    * This is the namespace of the type of element that this class serializes
98    */

99   protected String JavaDoc namespaceURI;
100
101   /**
102    * This is the local name of the type of element that this class serializes.
103    */

104   protected String JavaDoc localName = "default";
105
106   /**
107    * This keeps track of the parent bean.
108    */

109   protected AnyType parent;
110
111   /**
112    * This maps an attribute property URI to its value.
113    */

114   protected Map JavaDoc uriAttributeMap = new HashMap JavaDoc();
115
116   /**
117    * This maps an element property URI to its list of values.
118    */

119   protected Map JavaDoc uriElementMap = new HashMap JavaDoc();
120
121   /**
122    * This maps the one text property to its value.
123    */

124   protected Map JavaDoc uriTextMap = new HashMap JavaDoc();
125
126   /**
127    * This maps a property URI to the class that represents it.
128    */

129   protected Map JavaDoc uriClassMap = new HashMap JavaDoc();
130
131   /**
132    * This keeps an ordered list of all property URIs.
133    */

134   protected List JavaDoc uriList = new ArrayList JavaDoc();
135
136   /**
137    * This creates a generic bean instance.
138    */

139   public AnyType()
140   {
141     this.localName = "default";
142   }
143
144   /**
145    * This is called in the derived constructor to set up the metadata about elements.
146    */

147   protected void addElement(String JavaDoc uri, Class JavaDoc aClass)
148   {
149     List JavaDoc list = new ArrayList JavaDoc();
150     uriElementMap.put(uri, list);
151     uriClassMap.put(uri, aClass);
152     uriList.add(uri);
153   }
154
155   /**
156    * This returns a map from a String representing an element-based bean property URI to a list of values stored in that property.
157    */

158   public Map JavaDoc elements()
159   {
160     return uriElementMap;
161   }
162
163   /**
164    * This is called in the derived constructor to set up the metadata about attributes.
165    */

166   protected void addAttribute(String JavaDoc uri, Class JavaDoc aClass)
167   {
168     uriAttributeMap.put(uri, null);
169     uriClassMap.put(uri, aClass);
170     uriList.add(uri);
171   }
172
173   /**
174    * This returns a map from a String representing an attribute-based bean property URI to the value stored in that property.
175    */

176   public Map JavaDoc attributes()
177   {
178     return uriAttributeMap;
179   }
180
181   /**
182    * This is called in the derived constructor to set up the metadata about text.
183    */

184   protected void addText(String JavaDoc uri, Class JavaDoc aClass)
185   {
186     uriTextMap.put(uri, null);
187     uriClassMap.put(uri, aClass);
188     uriList.add(uri);
189   }
190
191   /**
192    * This returns a map from a String representing a text property URI to the value stored in that property.
193    */

194   public Map JavaDoc text()
195   {
196     return uriTextMap;
197   }
198
199   /**
200    * This returns a map from a property URI to the class that implements it.
201    */

202   public Map JavaDoc uriToClassMap()
203   {
204     return uriClassMap;
205   }
206
207   /**
208    * This returns an ordered list of all the properties.
209    */

210   public List JavaDoc properties()
211   {
212     return uriList;
213   }
214
215   /**
216    * This returns the parent bean.
217    */

218   protected AnyType parent()
219   {
220     return parent;
221   }
222
223   /**
224    * This sets the parent bean.
225    */

226   protected void changeParent(AnyType parent)
227   {
228     this.parent = parent;
229   }
230
231   /**
232    * This returns the namespace of the type of the bean.
233    */

234   public String JavaDoc namespaceURI()
235   {
236     return namespaceURI;
237   }
238
239   /**
240    * This sets the namespace of the type of the bean.
241    */

242   public void changeNamespaceURI(String JavaDoc namespaceURI)
243   {
244     this.namespaceURI = namespaceURI;
245   }
246
247   /**
248    * This returns the local name of the type of the bean.
249    */

250   public String JavaDoc localName()
251   {
252     return localName;
253   }
254
255   /**
256    * This sets the local name of the type of the bean.
257    */

258   public void changeLocalName(String JavaDoc localName)
259   {
260     this.localName = localName;
261   }
262
263   /**
264    * This creates a default serialization of this bean and all it's children.
265    */

266   public Element JavaDoc createElement()
267   {
268     Document JavaDoc document = createDocument();
269     Element JavaDoc result = createElement(document);
270     document.appendChild(result);
271     return result;
272   }
273
274   /**
275    * This creates a serialization of this bean and all it's children using the specified type of element.
276    */

277   public Element JavaDoc createElement(String JavaDoc namespaceURI, String JavaDoc localName)
278   {
279     Document JavaDoc document = createDocument();
280     Element JavaDoc result = createElement(document, namespaceURI, localName);
281     document.appendChild(result);
282     return result;
283   }
284
285   /**
286    * This creates a serialization of this bean and all it's children using the specified document;
287    * the element is not added to the document.
288    */

289   public Element JavaDoc createElement(Document JavaDoc document)
290   {
291     Element JavaDoc result = createElement(document, namespaceURI, localName);
292     return result;
293   }
294
295   /**
296    * This creates a serialization of this bean and all it's children using the specified type of element and document;
297    * the element is not added to the document.
298    */

299   public Element JavaDoc createElement(Document JavaDoc document, String JavaDoc namespaceURI, String JavaDoc localName)
300   {
301     List JavaDoc namespaces = new ArrayList JavaDoc();
302     Element JavaDoc result;
303     if (namespaceURI == null)
304     {
305       result = document.createElementNS(null, localName);
306     }
307     else
308     {
309       namespaces.add(namespaceURI);
310       result = document.createElementNS(namespaceURI, "Q1:" + localName);
311     }
312     populateTo(result, namespaces);
313     for (ListIterator JavaDoc i = namespaces.listIterator(); i.hasNext(); )
314     {
315       String JavaDoc namespace = (String JavaDoc)i.next();
316       result.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:Q" + i.nextIndex(), namespace);
317     }
318     return result;
319   }
320
321   /**
322    * This is a helper function that parses the URI and then delegates.
323    */

324   protected Element JavaDoc createElement(Document JavaDoc document, String JavaDoc uri, List JavaDoc namespaces)
325   {
326     int index = uri.lastIndexOf("#");
327     if (index == -1)
328     {
329       return document.createElementNS(null, uri);
330     }
331     else
332     {
333       String JavaDoc namespaceURI = uri.substring(0, index);
334       String JavaDoc localName = uri.substring(index + 1);
335       int namespaceIndex = namespaces.indexOf(namespaceURI);
336       if (namespaceIndex == -1)
337       {
338         namespaces.add(namespaceURI);
339         namespaceIndex = namespaces.size() - 1;
340       }
341       return document.createElementNS(namespaceURI, "Q" + (namespaceIndex + 1) + ":" + localName);
342     }
343   }
344
345   /**
346    * This is a helper function that parses the URI and then delegates.
347    */

348   protected void createAttribute(Element JavaDoc element, String JavaDoc uri, String JavaDoc value, List JavaDoc namespaces)
349   {
350     int index = uri.lastIndexOf("#");
351     if (index == -1)
352     {
353       createAttribute(element, null, uri, value, namespaces);
354     }
355     else
356     {
357       createAttribute(element, uri.substring(0, index), uri.substring(index + 1), value, namespaces);
358     }
359   }
360
361   /**
362    * This is called to set the element specifed attribute to the given values.
363    */

364   protected void createAttribute(Element JavaDoc element, String JavaDoc namespaceURI, String JavaDoc localName, String JavaDoc value, List JavaDoc namespaces)
365   {
366     if (value == null)
367     {
368       element.removeAttributeNS(namespaceURI, localName);
369     }
370     else
371     {
372       if (namespaceURI == null)
373       {
374         element.setAttributeNS(null, localName, value);
375       }
376       else
377       {
378         int namespaceIndex = namespaces.indexOf(namespaceURI);
379         if (namespaceIndex == -1)
380         {
381           namespaces.add(namespaceURI);
382           namespaceIndex = namespaces.size() - 1;
383         }
384         element.setAttributeNS(namespaceURI, "Q" + (namespaceIndex + 1) + ":" + localName, value);
385       }
386     }
387   }
388
389   /**
390    * This is called to create a document.
391    */

392   protected Document JavaDoc createDocument()
393   {
394     return new DocumentImpl();
395   }
396
397   /**
398    * This is called to create a text node.
399    */

400   protected Text JavaDoc createText(Document JavaDoc document, String JavaDoc stringValue)
401   {
402     return document.createTextNode(stringValue);
403   }
404
405   /**
406    * This returns a list of specified property values.
407    */

408   protected List JavaDoc basicGet(String JavaDoc uri)
409   {
410     List JavaDoc result = (List JavaDoc)uriElementMap.get(uri);
411     return result;
412   }
413
414   /**
415    * This sets the list of property objects.
416    */

417   protected void basicSet(String JavaDoc uri, List JavaDoc values)
418   {
419     List JavaDoc oldList = (List JavaDoc)uriElementMap.get(uri);
420     if (oldList != null)
421     {
422       for (Iterator JavaDoc i = oldList.iterator(); i.hasNext(); )
423       {
424         orphan(uri, i.next());
425       }
426     }
427
428     uriElementMap.put(uri, values);
429     for (Iterator JavaDoc i = values.iterator(); i.hasNext(); )
430     {
431       adopt(uri, i.next());
432     }
433   }
434
435   /**
436    * This returns the specified property object.
437    */

438   protected Object JavaDoc basicGet(String JavaDoc uri, int index)
439   {
440     List JavaDoc list = (List JavaDoc)uriElementMap.get(uri);
441     if (list != null)
442     {
443       if (index < list.size() )
444       {
445         return list.get(index);
446       }
447     }
448     else if (index == 0)
449     {
450       Object JavaDoc result = uriAttributeMap.get(uri);
451       if (result == null)
452       {
453         result = uriTextMap.get(uri);
454       }
455       return result;
456     }
457
458     return null;
459   }
460
461   /**
462    * This set the specified property object.
463    */

464   protected void basicSet(String JavaDoc uri, int index, Object JavaDoc value)
465   {
466     Object JavaDoc oldValue = basicGet(uri, index);
467     orphan(uri, oldValue);
468
469     List JavaDoc list = (List JavaDoc)uriElementMap.get(uri);
470     if (list != null)
471     {
472       if (index < list.size())
473       {
474         list.set(index, value);
475       }
476       else
477       {
478         list.add(value);
479       }
480     }
481     else if (uriAttributeMap.containsKey(uri))
482     {
483       uriAttributeMap.put(uri, value);
484     }
485     else if (uriTextMap.containsKey(uri))
486     {
487       uriTextMap.put(uri, value);
488     }
489     adopt(uri, value);
490   }
491
492   /**
493    * This is called to detach the value, if it is a bean.
494    */

495   protected void orphan(String JavaDoc uri, Object JavaDoc value)
496   {
497     if (value instanceof AnyType)
498     {
499       ((AnyType)value).changeParent(null);
500     }
501   }
502
503   /**
504    * This is called to attach the value, if it is a bean.
505    */

506   protected void adopt(String JavaDoc uri, Object JavaDoc value)
507   {
508     if (value instanceof AnyType)
509     {
510       AnyType anyType = (AnyType)value;
511       anyType.changeParent(this);
512       int i = uri.lastIndexOf("#");
513       if (i == -1)
514       {
515         anyType.changeNamespaceURI(null);
516         anyType.changeLocalName(uri);
517       }
518       else
519       {
520         anyType.changeNamespaceURI(uri.substring(0, i));
521         anyType.changeLocalName(uri.substring(i + 1));
522       }
523       ((AnyType)value).changeParent(this);
524     }
525   }
526
527   /**
528    * This set the text content to the specified value.
529    */

530   public void basicSetText(String JavaDoc value)
531   {
532     uriTextMap.put(uriTextMap.keySet().iterator().next(), value);
533   }
534
535   /**
536    * This is used to populate the given element with this bean's serialization.
537    */

538   protected void populateTo(Element JavaDoc element, List JavaDoc namespaces)
539   {
540     Document JavaDoc document = element.getOwnerDocument();
541
542     // Handle all the attributes.
543
//
544
for (Iterator JavaDoc attributes = uriAttributeMap.entrySet().iterator(); attributes.hasNext(); )
545     {
546       Map.Entry JavaDoc entry = (Map.Entry JavaDoc)attributes.next();
547       String JavaDoc attrURI = (String JavaDoc)entry.getKey();
548       Object JavaDoc value = entry.getValue();
549       createAttribute(element, attrURI, value == null ? null : convertValueToString(value), namespaces);
550     }
551
552     // Handle the remaining properties in order.
553
//
554
for (Iterator JavaDoc properties = properties().iterator(); properties.hasNext(); )
555     {
556       String JavaDoc propertyURI = (String JavaDoc)properties.next();
557       List JavaDoc list = (List JavaDoc)uriElementMap.get(propertyURI);
558       if (list != null)
559       {
560         for (Iterator JavaDoc values = list.iterator(); values.hasNext(); )
561         {
562           Object JavaDoc value = values.next();
563           populateValueTo(value, (Class JavaDoc)uriClassMap.get(propertyURI), propertyURI, element, namespaces);
564         }
565       }
566       else
567       {
568         Object JavaDoc value = uriTextMap.get(propertyURI);
569         if (value != null)
570         {
571           element.appendChild(createText(document, convertValueToString(value)));
572         }
573       }
574     }
575   }
576
577   /**
578    * This is used to populate the value of the given propertyURI to the given element.
579    */

580   protected void populateValueTo(Object JavaDoc value, Class JavaDoc aClass, String JavaDoc propertyURI, Element JavaDoc element, List JavaDoc namespaces)
581   {
582     if (AnyType.class.isAssignableFrom(aClass))
583     {
584       Element JavaDoc childElement = ((AnyType)value).createElement(element.getOwnerDocument(), propertyURI, namespaces);
585       element.appendChild(childElement);
586       ((AnyType)value).populateTo(childElement, namespaces);
587     }
588     else
589     {
590       Element JavaDoc childElement = createElement(element.getOwnerDocument(), propertyURI, namespaces);
591       if (value != null)
592       {
593          if (Element JavaDoc.class.isAssignableFrom(aClass))
594          {
595            Element JavaDoc actualElement = (Element JavaDoc)element.getOwnerDocument().importNode((Element JavaDoc)value, true);
596            childElement.appendChild(actualElement);
597          }
598         else if (aClass.isArray())
599         {
600           for (int i = 0, length = Array.getLength(value); i < length; ++i)
601           {
602             populateValueTo(Array.get(value, i), childElement, namespaces);
603           }
604         }
605         else if (Hashtable JavaDoc.class.isAssignableFrom(aClass))
606         {
607           for (Iterator JavaDoc entries = ((Map JavaDoc)value).entrySet().iterator(); entries.hasNext(); )
608           {
609             Map.Entry JavaDoc entry = (Map.Entry JavaDoc)entries.next();
610             populateValueTo(entry.getKey(), childElement, namespaces);
611             populateValueTo(entry.getValue(), childElement, namespaces);
612           }
613         }
614         else if (Vector JavaDoc.class.isAssignableFrom(aClass))
615         {
616           for (Iterator JavaDoc objects = ((Vector JavaDoc)value).iterator(); objects.hasNext(); )
617           {
618             Object JavaDoc object = objects.next();
619             populateValueTo(object, childElement, namespaces);
620           }
621         }
622         else
623         {
624           childElement.appendChild(createText(element.getOwnerDocument(), convertValueToString(value)));
625         }
626       }
627
628       element.appendChild(childElement);
629     }
630   }
631
632   /**
633    * This uses the type name as the property uri.
634    */

635   protected void populateValueTo(Object JavaDoc value, Element JavaDoc element, List JavaDoc namespaces)
636   {
637     if (value == null)
638     {
639       populateValueTo(value, Object JavaDoc.class, "null", element, namespaces);
640     }
641     else if (value instanceof Element JavaDoc)
642     {
643       Element JavaDoc actualElement = (Element JavaDoc)element.getOwnerDocument().importNode((Element JavaDoc)value, true);
644       element.appendChild(actualElement);
645     }
646     else
647     {
648       populateValueTo(value, value.getClass(), value.getClass().getName(), element, namespaces);
649     }
650   }
651
652   /**
653    * This can be called to deserialize the bean from the given element.
654    */

655   public void populateFrom(Element JavaDoc element)
656   {
657     NamedNodeMap JavaDoc attributes = element.getAttributes();
658     for (int i = 0, size = attributes.getLength(); i < size; ++i)
659     {
660       Attr JavaDoc attr = (Attr JavaDoc)attributes.item(i);
661       String JavaDoc attrNamespaceURI = attr.getNamespaceURI();
662       String JavaDoc attrLocalName = attr.getLocalName();
663       String JavaDoc attrURI = (attrNamespaceURI == null ? "" : attrNamespaceURI + "#") + attrLocalName;
664       if (uriAttributeMap.containsKey(attrURI))
665       {
666         basicSet(attrURI, 0, convertStringToValue((Class JavaDoc)uriClassMap.get(attrURI), attr.getNodeValue()));
667       }
668     }
669
670     for (Node JavaDoc child = element.getFirstChild(); child != null; child = child.getNextSibling())
671     {
672       if (child.getNodeType() == Node.ELEMENT_NODE)
673       {
674         String JavaDoc elementNamespaceURI = child.getNamespaceURI();
675         String JavaDoc elementLocalName = child.getLocalName();
676         String JavaDoc elementURI = (elementNamespaceURI == null ? "" : elementNamespaceURI + "#") + elementLocalName;
677         List JavaDoc list = (List JavaDoc)uriElementMap.get(elementURI);
678         if (list != null)
679         {
680           Object JavaDoc object = populateValueFrom((Class JavaDoc)uriClassMap.get(elementURI), (Element JavaDoc)child);
681           if (object != null)
682           {
683             basicSet(elementURI, list.size(), object);
684           }
685         }
686       }
687       else if (child.getNodeType() == Node.TEXT_NODE)
688       {
689         if (!uriTextMap.isEmpty())
690         {
691           basicSetText(((Text JavaDoc)child).getData());
692         }
693       }
694     }
695   }
696
697   /**
698    * This helper method returns the text of the element.
699    */

700   protected String JavaDoc elementText(Element JavaDoc element)
701   {
702     for (Node JavaDoc child = element.getFirstChild(); child != null; child = child.getNextSibling())
703     {
704       if (child.getNodeType() == Node.TEXT_NODE)
705       {
706         return ((Text JavaDoc)child).getData();
707       }
708     }
709
710     return "";
711   }
712
713   /**
714    * This helper method returns the list of children converted to objects based on their tag encoding their class name.
715    */

716   protected List JavaDoc elementChildren(Element JavaDoc element)
717   {
718     List JavaDoc result = new ArrayList JavaDoc();
719     for (Node JavaDoc child = element.getFirstChild(); child != null; child = child.getNextSibling())
720     {
721       if (child.getNodeType() == Node.ELEMENT_NODE)
722       {
723         Element JavaDoc elementChild = (Element JavaDoc)child;
724         try
725         {
726           Class JavaDoc aClass = Class.forName(elementChild.getTagName());
727           result.add(populateValueFrom(aClass, elementChild));
728         }
729         catch (ClassNotFoundException JavaDoc exception)
730         {
731           result.add(elementChild);
732         }
733       }
734     }
735
736     return result;
737   }
738
739   /**
740    * This creates a value of the given type from the given element.
741    */

742   protected Object JavaDoc populateValueFrom(Class JavaDoc aClass, Element JavaDoc element)
743   {
744     try
745     {
746       if (Element JavaDoc.class.isAssignableFrom(aClass))
747       {
748         for (Node JavaDoc child = element.getFirstChild(); child != null; child = child.getNextSibling())
749         {
750           if (child.getNodeType() == Node.ELEMENT_NODE)
751           {
752             return child;
753           }
754         }
755       }
756       else if (AnyType.class.isAssignableFrom(aClass))
757       {
758         AnyType object = (AnyType)aClass.newInstance();
759         object.populateFrom(element);
760         return object;
761       }
762       else if (GregorianCalendar JavaDoc.class.isAssignableFrom(aClass))
763       {
764         GregorianCalendar JavaDoc result = new GregorianCalendar JavaDoc();
765         try
766         {
767           result.setTime(gregorianCalandarDateFormat.parse(elementText(element)));
768         }
769         catch (Exception JavaDoc exception)
770         {
771           exception.printStackTrace();
772         }
773         return result;
774       }
775       else if (Date JavaDoc.class.isAssignableFrom(aClass))
776       {
777         Date JavaDoc result = new Date JavaDoc();
778         try
779         {
780           result = standardDateFormat.parse(elementText(element));
781         }
782         catch (ParseException JavaDoc standardException)
783         {
784           try
785           {
786             result = preciseDateFormat.parse(elementText(element));
787           }
788           catch (ParseException JavaDoc preciseException)
789           {
790           }
791         }
792         return result;
793       }
794       else if (Hashtable JavaDoc.class.isAssignableFrom(aClass))
795       {
796         Hashtable JavaDoc hashtable = new Hashtable JavaDoc();
797         for (Iterator JavaDoc iterator = elementChildren(element).iterator(); iterator.hasNext(); )
798         {
799           Object JavaDoc key = iterator.next();
800           Object JavaDoc value = iterator.next();
801           hashtable.put(key, value);
802         }
803         return hashtable;
804       }
805       else if (Vector JavaDoc.class.isAssignableFrom(aClass))
806       {
807         Vector JavaDoc vector = new Vector JavaDoc();
808         for (Iterator JavaDoc iterator = elementChildren(element).iterator(); iterator.hasNext(); )
809         {
810           Object JavaDoc value = iterator.next();
811           vector.add(value);
812         }
813         return vector;
814       }
815       else if (aClass == String JavaDoc.class)
816       {
817         return elementText(element);
818       }
819       else if (aClass.isArray())
820       {
821         List JavaDoc children = elementChildren(element);
822         Object JavaDoc result = Array.newInstance(aClass.getComponentType(), children.size());
823         int index = 0;
824         for (Iterator JavaDoc iterator = elementChildren(element).iterator(); iterator.hasNext(); ++index)
825         {
826           Object JavaDoc value = iterator.next();
827           Array.set(result, index, value);
828         }
829         return result;
830       }
831       else
832       {
833         try
834         {
835           return aClass.getConstructor(stringConstructor).newInstance(new Object JavaDoc [] { elementText(element) });
836         }
837         catch (Exception JavaDoc exception)
838         {
839           exception.printStackTrace();
840
841           return elementText(element);
842         }
843       }
844     }
845     catch (Exception JavaDoc exception)
846     {
847       exception.printStackTrace();
848     }
849
850     return null;
851   }
852
853   protected static Class JavaDoc [] stringConstructor = { String JavaDoc.class };
854   protected static DateFormat JavaDoc gregorianCalandarDateFormat = new SimpleDateFormat JavaDoc("yyyy-MM-dd");
855   protected static DateFormat JavaDoc standardDateFormat = new SimpleDateFormat JavaDoc("yyyy-MM-dd'T'HH:mm:ss'Z'");
856   protected static DateFormat JavaDoc preciseDateFormat = new SimpleDateFormat JavaDoc("yyyy-MM-dd'T'HH:mm:ss'.'SSS'Z'");
857
858   /**
859    * This is called to convert a string to its bean representation.
860    */

861   protected Object JavaDoc convertStringToValue(Class JavaDoc aClass, String JavaDoc string)
862   {
863     if (aClass == String JavaDoc.class)
864     {
865       return string;
866     }
867     else
868     {
869       if (GregorianCalendar JavaDoc.class.isAssignableFrom(aClass))
870       {
871         GregorianCalendar JavaDoc result = new GregorianCalendar JavaDoc();
872         try
873         {
874           result.setTime(gregorianCalandarDateFormat.parse(string));
875         }
876         catch (Exception JavaDoc exception)
877         {
878           exception.printStackTrace();
879         }
880         return result;
881       }
882       else if (Date JavaDoc.class.isAssignableFrom(aClass))
883       {
884         Date JavaDoc result = new Date JavaDoc();
885         try
886         {
887           result = standardDateFormat.parse(string);
888         }
889         catch (ParseException JavaDoc standardException)
890         {
891           try
892           {
893             result = preciseDateFormat.parse(string);
894           }
895           catch (ParseException JavaDoc preciseException)
896           {
897           }
898         }
899         return result;
900       }
901       else
902       {
903         try
904         {
905           return aClass.getConstructor(stringConstructor).newInstance(new Object JavaDoc [] { string });
906         }
907         catch (Exception JavaDoc exception)
908         {
909           exception.printStackTrace();
910
911           return string;
912         }
913       }
914     }
915   }
916
917   /**
918    * This is called to convert value to its string.
919    */

920   protected String JavaDoc convertValueToString(Object JavaDoc value)
921   {
922     if (value == null)
923     {
924       return "";
925     }
926     else if (value instanceof GregorianCalendar JavaDoc)
927     {
928       String JavaDoc result = gregorianCalandarDateFormat.format(((GregorianCalendar JavaDoc)value).getTime());
929       return result;
930     }
931     else if (value instanceof Date JavaDoc)
932     {
933       String JavaDoc result = standardDateFormat.format((Date JavaDoc)value);
934       return result;
935     }
936     else
937     {
938       return value.toString();
939     }
940   }
941
942   /**
943    * This fills in each index of the array with an element from the list; primitives are unwrapped.
944    */

945   protected Object JavaDoc basicToArray(List JavaDoc list, Object JavaDoc array)
946   {
947     int length = Array.getLength(array);
948     for (int i = 0; i < length; ++i)
949     {
950       Array.set(array, i, list.get(i));
951     }
952     return array;
953   }
954
955   /**
956    * This returns list of the array's contents; primitives are wrapped.
957    */

958   protected List JavaDoc basicToList(Object JavaDoc array)
959   {
960     int length = Array.getLength(array);
961     ArrayList JavaDoc result = new ArrayList JavaDoc(length);
962     for (int i = 0; i < length; ++i)
963     {
964       result.add(Array.get(array, i));
965     }
966     return result;
967   }
968 }
969
Popular Tags