Source Code Cross Referenced for AnyType.java in  » Web-Services » wsif » clients » zipcode » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Web Services » wsif » clients.zipcode 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * The Apache Software License, Version 1.1
003:         *
004:         *
005:         * Copyright (c) 2002 The Apache Software Foundation.  All rights 
006:         * reserved.
007:         *
008:         * Redistribution and use in source and binary forms, with or without
009:         * modification, are permitted provided that the following conditions
010:         * are met:
011:         *
012:         * 1. Redistributions of source code must retain the above copyright
013:         *    notice, this list of conditions and the following disclaimer. 
014:         *
015:         * 2. Redistributions in binary form must reproduce the above copyright
016:         *    notice, this list of conditions and the following disclaimer in
017:         *    the documentation and/or other materials provided with the
018:         *    distribution.
019:         *
020:         * 3. The end-user documentation included with the redistribution,
021:         *    if any, must include the following acknowledgment:  
022:         *       "This product includes software developed by the
023:         *        Apache Software Foundation (http://www.apache.org/)."
024:         *    Alternately, this acknowledgment may appear in the software itself,
025:         *    if and wherever such third-party acknowledgments normally appear.
026:         *
027:         * 4. The names "WSIF" and "Apache Software Foundation" must
028:         *    not be used to endorse or promote products derived from this
029:         *    software without prior written permission. For written 
030:         *    permission, please contact apache@apache.org.
031:         *
032:         * 5. Products derived from this software may not be called "Apache",
033:         *    nor may "Apache" appear in their name, without prior written
034:         *    permission of the Apache Software Foundation.
035:         *
036:         * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
037:         * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
038:         * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
039:         * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
040:         * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
041:         * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
042:         * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
043:         * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
044:         * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
045:         * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
046:         * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
047:         * SUCH DAMAGE.
048:         * ====================================================================
049:         *
050:         * This software consists of voluntary contributions made by many
051:         * individuals on behalf of the Apache Software Foundation and was
052:         * originally based on software copyright (c) 2001, 2002, International
053:         * Business Machines, Inc., http://www.apache.org.  For more
054:         * information on the Apache Software Foundation, please see
055:         * <http://www.apache.org/>.
056:         */
057:
058:        package clients.zipcode;
059:
060:        import java.io.Serializable;
061:
062:        import java.lang.reflect.Array;
063:
064:        import java.util.Arrays;
065:        import java.util.ArrayList;
066:        import java.util.Date;
067:        import java.util.GregorianCalendar;
068:        import java.util.HashMap;
069:        import java.util.Hashtable;
070:        import java.util.Iterator;
071:        import java.util.List;
072:        import java.util.ListIterator;
073:        import java.util.Map;
074:        import java.util.Vector;
075:
076:        import java.text.ParseException;
077:        import java.text.DateFormat;
078:        import java.text.SimpleDateFormat;
079:
080:        import org.apache.xerces.dom.DocumentImpl;
081:
082:        import org.w3c.dom.Attr;
083:        import org.w3c.dom.Document;
084:        import org.w3c.dom.Element;
085:        import org.w3c.dom.NamedNodeMap;
086:        import org.w3c.dom.Node;
087:        import org.w3c.dom.Text;
088:
089:        /**
090:         * This is the base class for all generated beans.
091:         */
092:        public abstract class AnyType implements  Serializable {
093:            /**
094:             * This is the namespace of the type of element that this class serializes
095:             */
096:            protected String namespaceURI;
097:
098:            /**
099:             * This is the local name of the type of element that this class serializes.
100:             */
101:            protected String localName = "default";
102:
103:            /**
104:             * This keeps track of the parent bean.
105:             */
106:            protected AnyType parent;
107:
108:            /**
109:             * This maps an attribute property URI to its value.
110:             */
111:            protected Map uriAttributeMap = new HashMap();
112:
113:            /**
114:             * This maps an element property URI to its list of values.
115:             */
116:            protected Map uriElementMap = new HashMap();
117:
118:            /**
119:             * This maps the one text property to its value.
120:             */
121:            protected Map uriTextMap = new HashMap();
122:
123:            /**
124:             * This maps a property URI to the class that represents it.
125:             */
126:            protected Map uriClassMap = new HashMap();
127:
128:            /**
129:             * This keeps an ordered list of all property URIs.
130:             */
131:            protected List uriList = new ArrayList();
132:
133:            /**
134:             * This creates a generic bean instance.
135:             */
136:            public AnyType() {
137:                this .localName = "default";
138:            }
139:
140:            /**
141:             * This  is called in the derived constructor to set up the metadata about elements.
142:             */
143:            protected void addElement(String uri, Class aClass) {
144:                List list = new ArrayList();
145:                uriElementMap.put(uri, list);
146:                uriClassMap.put(uri, aClass);
147:                uriList.add(uri);
148:            }
149:
150:            /**
151:             * This returns a map from a String representing an element-based bean property URI to a list of values stored in that property.
152:             */
153:            public Map elements() {
154:                return uriElementMap;
155:            }
156:
157:            /**
158:             * This  is called in the derived constructor to set up the metadata about attributes.
159:             */
160:            protected void addAttribute(String uri, Class aClass) {
161:                uriAttributeMap.put(uri, null);
162:                uriClassMap.put(uri, aClass);
163:                uriList.add(uri);
164:            }
165:
166:            /**
167:             * This returns a map from a String representing an attribute-based bean property URI to the value stored in that property.
168:             */
169:            public Map attributes() {
170:                return uriAttributeMap;
171:            }
172:
173:            /**
174:             * This  is called in the derived constructor to set up the metadata about text.
175:             */
176:            protected void addText(String uri, Class aClass) {
177:                uriTextMap.put(uri, null);
178:                uriClassMap.put(uri, aClass);
179:                uriList.add(uri);
180:            }
181:
182:            /**
183:             * This returns a map from a String representing a text property URI to the value stored in that property.
184:             */
185:            public Map text() {
186:                return uriTextMap;
187:            }
188:
189:            /**
190:             * This returns a map from a property URI to the class that implements it.
191:             */
192:            public Map uriToClassMap() {
193:                return uriClassMap;
194:            }
195:
196:            /**
197:             * This returns an ordered list of all the properties.
198:             */
199:            public List properties() {
200:                return uriList;
201:            }
202:
203:            /**
204:             * This returns the parent bean.
205:             */
206:            protected AnyType parent() {
207:                return parent;
208:            }
209:
210:            /**
211:             * This sets the parent bean.
212:             */
213:            protected void changeParent(AnyType parent) {
214:                this .parent = parent;
215:            }
216:
217:            /**
218:             * This returns the namespace of the type of the bean.
219:             */
220:            public String namespaceURI() {
221:                return namespaceURI;
222:            }
223:
224:            /**
225:             * This sets the namespace of the type of the bean.
226:             */
227:            public void changeNamespaceURI(String namespaceURI) {
228:                this .namespaceURI = namespaceURI;
229:            }
230:
231:            /**
232:             * This returns the local name of the type of the bean.
233:             */
234:            public String localName() {
235:                return localName;
236:            }
237:
238:            /**
239:             * This sets the local name of the type of the bean.
240:             */
241:            public void changeLocalName(String localName) {
242:                this .localName = localName;
243:            }
244:
245:            /**
246:             * This creates a default serialization of this bean and all it's children.
247:             */
248:            public Element createElement() {
249:                Document document = createDocument();
250:                Element result = createElement(document);
251:                document.appendChild(result);
252:                return result;
253:            }
254:
255:            /**
256:             * This creates a serialization of this bean and all it's children using the specified type of element.
257:             */
258:            public Element createElement(String namespaceURI, String localName) {
259:                Document document = createDocument();
260:                Element result = createElement(document, namespaceURI,
261:                        localName);
262:                document.appendChild(result);
263:                return result;
264:            }
265:
266:            /**
267:             * This creates a serialization of this bean and all it's children using the specified document; 
268:             * the element is not added to the document.
269:             */
270:            public Element createElement(Document document) {
271:                Element result = createElement(document, namespaceURI,
272:                        localName);
273:                return result;
274:            }
275:
276:            /**
277:             * This creates a serialization of this bean and all it's children using the specified type of element and document; 
278:             * the element is not added to the document.
279:             */
280:            public Element createElement(Document document,
281:                    String namespaceURI, String localName) {
282:                List namespaces = new ArrayList();
283:                Element result;
284:                if (namespaceURI == null) {
285:                    result = document.createElementNS(null, localName);
286:                } else {
287:                    namespaces.add(namespaceURI);
288:                    result = document.createElementNS(namespaceURI, "Q1:"
289:                            + localName);
290:                }
291:                populateTo(result, namespaces);
292:                for (ListIterator i = namespaces.listIterator(); i.hasNext();) {
293:                    String namespace = (String) i.next();
294:                    result.setAttributeNS("http://www.w3.org/2000/xmlns/",
295:                            "xmlns:Q" + i.nextIndex(), namespace);
296:                }
297:                return result;
298:            }
299:
300:            /**
301:             * This is a helper function that parses the URI and then delegates.
302:             */
303:            protected Element createElement(Document document, String uri,
304:                    List namespaces) {
305:                int index = uri.lastIndexOf("#");
306:                if (index == -1) {
307:                    return document.createElementNS(null, uri);
308:                } else {
309:                    String namespaceURI = uri.substring(0, index);
310:                    String localName = uri.substring(index + 1);
311:                    int namespaceIndex = namespaces.indexOf(namespaceURI);
312:                    if (namespaceIndex == -1) {
313:                        namespaces.add(namespaceURI);
314:                        namespaceIndex = namespaces.size() - 1;
315:                    }
316:                    return document.createElementNS(namespaceURI, "Q"
317:                            + (namespaceIndex + 1) + ":" + localName);
318:                }
319:            }
320:
321:            /**
322:             * This is a helper function that parses the URI and then delegates.
323:             */
324:            protected void createAttribute(Element element, String uri,
325:                    String value, List namespaces) {
326:                int index = uri.lastIndexOf("#");
327:                if (index == -1) {
328:                    createAttribute(element, null, uri, value, namespaces);
329:                } else {
330:                    createAttribute(element, uri.substring(0, index), uri
331:                            .substring(index + 1), value, namespaces);
332:                }
333:            }
334:
335:            /** 
336:             * This is called to set the element specifed attribute to the given values.
337:             */
338:            protected void createAttribute(Element element,
339:                    String namespaceURI, String localName, String value,
340:                    List namespaces) {
341:                if (value == null) {
342:                    element.removeAttributeNS(namespaceURI, localName);
343:                } else {
344:                    if (namespaceURI == null) {
345:                        element.setAttributeNS(null, localName, value);
346:                    } else {
347:                        int namespaceIndex = namespaces.indexOf(namespaceURI);
348:                        if (namespaceIndex == -1) {
349:                            namespaces.add(namespaceURI);
350:                            namespaceIndex = namespaces.size() - 1;
351:                        }
352:                        element
353:                                .setAttributeNS(namespaceURI, "Q"
354:                                        + (namespaceIndex + 1) + ":"
355:                                        + localName, value);
356:                    }
357:                }
358:            }
359:
360:            /**
361:             * This is called to create a document.
362:             */
363:            protected Document createDocument() {
364:                return new DocumentImpl();
365:            }
366:
367:            /**
368:             * This is called to create a text node.
369:             */
370:            protected Text createText(Document document, String stringValue) {
371:                return document.createTextNode(stringValue);
372:            }
373:
374:            /**
375:             * This returns a list of specified property values.
376:             */
377:            protected List basicGet(String uri) {
378:                List result = (List) uriElementMap.get(uri);
379:                return result;
380:            }
381:
382:            /**
383:             * This sets the list of property objects.
384:             */
385:            protected void basicSet(String uri, List values) {
386:                List oldList = (List) uriElementMap.get(uri);
387:                if (oldList != null) {
388:                    for (Iterator i = oldList.iterator(); i.hasNext();) {
389:                        orphan(uri, i.next());
390:                    }
391:                }
392:
393:                uriElementMap.put(uri, values);
394:                for (Iterator i = values.iterator(); i.hasNext();) {
395:                    adopt(uri, i.next());
396:                }
397:            }
398:
399:            /**
400:             * This returns the specified property object.
401:             */
402:            protected Object basicGet(String uri, int index) {
403:                List list = (List) uriElementMap.get(uri);
404:                if (list != null) {
405:                    if (index < list.size()) {
406:                        return list.get(index);
407:                    }
408:                } else if (index == 0) {
409:                    Object result = uriAttributeMap.get(uri);
410:                    if (result == null) {
411:                        result = uriTextMap.get(uri);
412:                    }
413:                    return result;
414:                }
415:
416:                return null;
417:            }
418:
419:            /**
420:             * This set the specified property object.
421:             */
422:            protected void basicSet(String uri, int index, Object value) {
423:                Object oldValue = basicGet(uri, index);
424:                orphan(uri, oldValue);
425:
426:                List list = (List) uriElementMap.get(uri);
427:                if (list != null) {
428:                    if (index < list.size()) {
429:                        list.set(index, value);
430:                    } else {
431:                        list.add(value);
432:                    }
433:                } else if (uriAttributeMap.containsKey(uri)) {
434:                    uriAttributeMap.put(uri, value);
435:                } else if (uriTextMap.containsKey(uri)) {
436:                    uriTextMap.put(uri, value);
437:                }
438:                adopt(uri, value);
439:            }
440:
441:            /**
442:             * This is called to detach the value, if it is a bean.
443:             */
444:            protected void orphan(String uri, Object value) {
445:                if (value instanceof  AnyType) {
446:                    ((AnyType) value).changeParent(null);
447:                }
448:            }
449:
450:            /**
451:             * This is called to attach the value, if it is a bean.
452:             */
453:            protected void adopt(String uri, Object value) {
454:                if (value instanceof  AnyType) {
455:                    AnyType anyType = (AnyType) value;
456:                    anyType.changeParent(this );
457:                    int i = uri.lastIndexOf("#");
458:                    if (i == -1) {
459:                        anyType.changeNamespaceURI(null);
460:                        anyType.changeLocalName(uri);
461:                    } else {
462:                        anyType.changeNamespaceURI(uri.substring(0, i));
463:                        anyType.changeLocalName(uri.substring(i + 1));
464:                    }
465:                    ((AnyType) value).changeParent(this );
466:                }
467:            }
468:
469:            /**
470:             * This set the text content to the specified value.
471:             */
472:            public void basicSetText(String value) {
473:                uriTextMap.put(uriTextMap.keySet().iterator().next(), value);
474:            }
475:
476:            /**
477:             * This is used to populate the given element with this bean's serialization.
478:             */
479:            protected void populateTo(Element element, List namespaces) {
480:                Document document = element.getOwnerDocument();
481:
482:                // Handle all the attributes.
483:                //
484:                for (Iterator attributes = uriAttributeMap.entrySet()
485:                        .iterator(); attributes.hasNext();) {
486:                    Map.Entry entry = (Map.Entry) attributes.next();
487:                    String attrURI = (String) entry.getKey();
488:                    Object value = entry.getValue();
489:                    createAttribute(element, attrURI, value == null ? null
490:                            : convertValueToString(value), namespaces);
491:                }
492:
493:                // Handle the remaining properties in order.
494:                //
495:                for (Iterator properties = properties().iterator(); properties
496:                        .hasNext();) {
497:                    String propertyURI = (String) properties.next();
498:                    List list = (List) uriElementMap.get(propertyURI);
499:                    if (list != null) {
500:                        for (Iterator values = list.iterator(); values
501:                                .hasNext();) {
502:                            Object value = values.next();
503:                            populateValueTo(value, (Class) uriClassMap
504:                                    .get(propertyURI), propertyURI, element,
505:                                    namespaces);
506:                        }
507:                    } else {
508:                        Object value = uriTextMap.get(propertyURI);
509:                        if (value != null) {
510:                            element.appendChild(createText(document,
511:                                    convertValueToString(value)));
512:                        }
513:                    }
514:                }
515:            }
516:
517:            /**
518:             * This is used to populate the value of the given propertyURI to the given element.
519:             */
520:            protected void populateValueTo(Object value, Class aClass,
521:                    String propertyURI, Element element, List namespaces) {
522:                if (AnyType.class.isAssignableFrom(aClass)) {
523:                    Element childElement = ((AnyType) value)
524:                            .createElement(element.getOwnerDocument(),
525:                                    propertyURI, namespaces);
526:                    element.appendChild(childElement);
527:                    ((AnyType) value).populateTo(childElement, namespaces);
528:                } else {
529:                    Element childElement = createElement(element
530:                            .getOwnerDocument(), propertyURI, namespaces);
531:                    if (value != null) {
532:                        if (Element.class.isAssignableFrom(aClass)) {
533:                            Element actualElement = (Element) element
534:                                    .getOwnerDocument().importNode(
535:                                            (Element) value, true);
536:                            childElement.appendChild(actualElement);
537:                        } else if (aClass.isArray()) {
538:                            for (int i = 0, length = Array.getLength(value); i < length; ++i) {
539:                                populateValueTo(Array.get(value, i),
540:                                        childElement, namespaces);
541:                            }
542:                        } else if (Hashtable.class.isAssignableFrom(aClass)) {
543:                            for (Iterator entries = ((Map) value).entrySet()
544:                                    .iterator(); entries.hasNext();) {
545:                                Map.Entry entry = (Map.Entry) entries.next();
546:                                populateValueTo(entry.getKey(), childElement,
547:                                        namespaces);
548:                                populateValueTo(entry.getValue(), childElement,
549:                                        namespaces);
550:                            }
551:                        } else if (Vector.class.isAssignableFrom(aClass)) {
552:                            for (Iterator objects = ((Vector) value).iterator(); objects
553:                                    .hasNext();) {
554:                                Object object = objects.next();
555:                                populateValueTo(object, childElement,
556:                                        namespaces);
557:                            }
558:                        } else {
559:                            childElement.appendChild(createText(element
560:                                    .getOwnerDocument(),
561:                                    convertValueToString(value)));
562:                        }
563:                    }
564:
565:                    element.appendChild(childElement);
566:                }
567:            }
568:
569:            /**
570:             * This uses the type name as the property uri.
571:             */
572:            protected void populateValueTo(Object value, Element element,
573:                    List namespaces) {
574:                if (value == null) {
575:                    populateValueTo(value, Object.class, "null", element,
576:                            namespaces);
577:                } else if (value instanceof  Element) {
578:                    Element actualElement = (Element) element
579:                            .getOwnerDocument().importNode((Element) value,
580:                                    true);
581:                    element.appendChild(actualElement);
582:                } else {
583:                    populateValueTo(value, value.getClass(), value.getClass()
584:                            .getName(), element, namespaces);
585:                }
586:            }
587:
588:            /**
589:             * This can be called to deserialize the bean from the given element.
590:             */
591:            public void populateFrom(Element element) {
592:                NamedNodeMap attributes = element.getAttributes();
593:                for (int i = 0, size = attributes.getLength(); i < size; ++i) {
594:                    Attr attr = (Attr) attributes.item(i);
595:                    String attrNamespaceURI = attr.getNamespaceURI();
596:                    String attrLocalName = attr.getLocalName();
597:                    String attrURI = (attrNamespaceURI == null ? ""
598:                            : attrNamespaceURI + "#")
599:                            + attrLocalName;
600:                    if (uriAttributeMap.containsKey(attrURI)) {
601:                        basicSet(attrURI, 0, convertStringToValue(
602:                                (Class) uriClassMap.get(attrURI), attr
603:                                        .getNodeValue()));
604:                    }
605:                }
606:
607:                for (Node child = element.getFirstChild(); child != null; child = child
608:                        .getNextSibling()) {
609:                    if (child.getNodeType() == Node.ELEMENT_NODE) {
610:                        String elementNamespaceURI = child.getNamespaceURI();
611:                        String elementLocalName = child.getLocalName();
612:                        String elementURI = (elementNamespaceURI == null ? ""
613:                                : elementNamespaceURI + "#")
614:                                + elementLocalName;
615:                        List list = (List) uriElementMap.get(elementURI);
616:                        if (list != null) {
617:                            Object object = populateValueFrom(
618:                                    (Class) uriClassMap.get(elementURI),
619:                                    (Element) child);
620:                            if (object != null) {
621:                                basicSet(elementURI, list.size(), object);
622:                            }
623:                        }
624:                    } else if (child.getNodeType() == Node.TEXT_NODE) {
625:                        if (!uriTextMap.isEmpty()) {
626:                            basicSetText(((Text) child).getData());
627:                        }
628:                    }
629:                }
630:            }
631:
632:            /**
633:             * This helper method returns the text of the element.
634:             */
635:            protected String elementText(Element element) {
636:                for (Node child = element.getFirstChild(); child != null; child = child
637:                        .getNextSibling()) {
638:                    if (child.getNodeType() == Node.TEXT_NODE) {
639:                        return ((Text) child).getData();
640:                    }
641:                }
642:
643:                return "";
644:            }
645:
646:            /**
647:             * This helper method returns the list of children converted to objects based on their tag encoding their class name.
648:             */
649:            protected List elementChildren(Element element) {
650:                List result = new ArrayList();
651:                for (Node child = element.getFirstChild(); child != null; child = child
652:                        .getNextSibling()) {
653:                    if (child.getNodeType() == Node.ELEMENT_NODE) {
654:                        Element elementChild = (Element) child;
655:                        try {
656:                            Class aClass = Class.forName(elementChild
657:                                    .getTagName());
658:                            result.add(populateValueFrom(aClass, elementChild));
659:                        } catch (ClassNotFoundException exception) {
660:                            result.add(elementChild);
661:                        }
662:                    }
663:                }
664:
665:                return result;
666:            }
667:
668:            /**
669:             * This creates a value of the given type from the given element.
670:             */
671:            protected Object populateValueFrom(Class aClass, Element element) {
672:                try {
673:                    if (Element.class.isAssignableFrom(aClass)) {
674:                        for (Node child = element.getFirstChild(); child != null; child = child
675:                                .getNextSibling()) {
676:                            if (child.getNodeType() == Node.ELEMENT_NODE) {
677:                                return child;
678:                            }
679:                        }
680:                    } else if (AnyType.class.isAssignableFrom(aClass)) {
681:                        AnyType object = (AnyType) aClass.newInstance();
682:                        object.populateFrom(element);
683:                        return object;
684:                    } else if (GregorianCalendar.class.isAssignableFrom(aClass)) {
685:                        GregorianCalendar result = new GregorianCalendar();
686:                        try {
687:                            result.setTime(gregorianCalandarDateFormat
688:                                    .parse(elementText(element)));
689:                        } catch (Exception exception) {
690:                            exception.printStackTrace();
691:                        }
692:                        return result;
693:                    } else if (Date.class.isAssignableFrom(aClass)) {
694:                        Date result = new Date();
695:                        try {
696:                            result = standardDateFormat
697:                                    .parse(elementText(element));
698:                        } catch (ParseException standardException) {
699:                            try {
700:                                result = preciseDateFormat
701:                                        .parse(elementText(element));
702:                            } catch (ParseException preciseException) {
703:                            }
704:                        }
705:                        return result;
706:                    } else if (Hashtable.class.isAssignableFrom(aClass)) {
707:                        Hashtable hashtable = new Hashtable();
708:                        for (Iterator iterator = elementChildren(element)
709:                                .iterator(); iterator.hasNext();) {
710:                            Object key = iterator.next();
711:                            Object value = iterator.next();
712:                            hashtable.put(key, value);
713:                        }
714:                        return hashtable;
715:                    } else if (Vector.class.isAssignableFrom(aClass)) {
716:                        Vector vector = new Vector();
717:                        for (Iterator iterator = elementChildren(element)
718:                                .iterator(); iterator.hasNext();) {
719:                            Object value = iterator.next();
720:                            vector.add(value);
721:                        }
722:                        return vector;
723:                    } else if (aClass == String.class) {
724:                        return elementText(element);
725:                    } else if (aClass.isArray()) {
726:                        List children = elementChildren(element);
727:                        Object result = Array.newInstance(aClass
728:                                .getComponentType(), children.size());
729:                        int index = 0;
730:                        for (Iterator iterator = elementChildren(element)
731:                                .iterator(); iterator.hasNext(); ++index) {
732:                            Object value = iterator.next();
733:                            Array.set(result, index, value);
734:                        }
735:                        return result;
736:                    } else {
737:                        try {
738:                            return aClass
739:                                    .getConstructor(stringConstructor)
740:                                    .newInstance(
741:                                            new Object[] { elementText(element) });
742:                        } catch (Exception exception) {
743:                            exception.printStackTrace();
744:
745:                            return elementText(element);
746:                        }
747:                    }
748:                } catch (Exception exception) {
749:                    exception.printStackTrace();
750:                }
751:
752:                return null;
753:            }
754:
755:            protected static Class[] stringConstructor = { String.class };
756:            protected static DateFormat gregorianCalandarDateFormat = new SimpleDateFormat(
757:                    "yyyy-MM-dd");
758:            protected static DateFormat standardDateFormat = new SimpleDateFormat(
759:                    "yyyy-MM-dd'T'HH:mm:ss'Z'");
760:            protected static DateFormat preciseDateFormat = new SimpleDateFormat(
761:                    "yyyy-MM-dd'T'HH:mm:ss'.'SSS'Z'");
762:
763:            /**
764:             * This is called to convert a string to its bean representation.
765:             */
766:            protected Object convertStringToValue(Class aClass, String string) {
767:                if (aClass == String.class) {
768:                    return string;
769:                } else {
770:                    if (GregorianCalendar.class.isAssignableFrom(aClass)) {
771:                        GregorianCalendar result = new GregorianCalendar();
772:                        try {
773:                            result.setTime(gregorianCalandarDateFormat
774:                                    .parse(string));
775:                        } catch (Exception exception) {
776:                            exception.printStackTrace();
777:                        }
778:                        return result;
779:                    } else if (Date.class.isAssignableFrom(aClass)) {
780:                        Date result = new Date();
781:                        try {
782:                            result = standardDateFormat.parse(string);
783:                        } catch (ParseException standardException) {
784:                            try {
785:                                result = preciseDateFormat.parse(string);
786:                            } catch (ParseException preciseException) {
787:                            }
788:                        }
789:                        return result;
790:                    } else {
791:                        try {
792:                            return aClass.getConstructor(stringConstructor)
793:                                    .newInstance(new Object[] { string });
794:                        } catch (Exception exception) {
795:                            exception.printStackTrace();
796:
797:                            return string;
798:                        }
799:                    }
800:                }
801:            }
802:
803:            /**
804:             * This is called to convert value to its string.
805:             */
806:            protected String convertValueToString(Object value) {
807:                if (value == null) {
808:                    return "";
809:                } else if (value instanceof  GregorianCalendar) {
810:                    String result = gregorianCalandarDateFormat
811:                            .format(((GregorianCalendar) value).getTime());
812:                    return result;
813:                } else if (value instanceof  Date) {
814:                    String result = standardDateFormat.format((Date) value);
815:                    return result;
816:                } else {
817:                    return value.toString();
818:                }
819:            }
820:
821:            /**
822:             * This fills in each index of the array with an element from the list; primitives are unwrapped.
823:             */
824:            protected Object basicToArray(List list, Object array) {
825:                int length = Array.getLength(array);
826:                for (int i = 0; i < length; ++i) {
827:                    Array.set(array, i, list.get(i));
828:                }
829:                return array;
830:            }
831:
832:            /**
833:             * This returns list of the array's contents; primitives are wrapped.
834:             */
835:            protected List basicToList(Object array) {
836:                int length = Array.getLength(array);
837:                ArrayList result = new ArrayList(length);
838:                for (int i = 0; i < length; ++i) {
839:                    result.add(Array.get(array, i));
840:                }
841:                return result;
842:            }
843:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.