Source Code Cross Referenced for XMLBeanWriter.java in  » Database-Client » squirrel-sql-2.6.5a » net » sourceforge » squirrel_sql » fw » xml » 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 » Database Client » squirrel sql 2.6.5a » net.sourceforge.squirrel_sql.fw.xml 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package net.sourceforge.squirrel_sql.fw.xml;
002:
003:        /*
004:         * Copyright (C) 2001 Colin Bell
005:         * colbell@users.sourceforge.net
006:         *
007:         * This library is free software; you can redistribute it and/or
008:         * modify it under the terms of the GNU Lesser General Public
009:         * License as published by the Free Software Foundation; either
010:         * version 2.1 of the License, or (at your option) any later version.
011:         *
012:         * This library is distributed in the hope that it will be useful,
013:         * but WITHOUT ANY WARRANTY; without even the implied warranty of
014:         * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
015:         * Lesser General Public License for more details.
016:         *
017:         * You should have received a copy of the GNU Lesser General Public
018:         * License along with this library; if not, write to the Free Software
019:         * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
020:         */
021:        import java.beans.BeanInfo;
022:        import java.beans.IntrospectionException;
023:        import java.beans.Introspector;
024:        import java.beans.PropertyDescriptor;
025:        import java.io.BufferedOutputStream;
026:        import java.io.File;
027:        import java.io.FileOutputStream;
028:        import java.io.IOException;
029:        import java.lang.reflect.Method;
030:        import java.util.Iterator;
031:
032:        import net.n3.nanoxml.IXMLElement;
033:        import net.n3.nanoxml.XMLElement;
034:        import net.n3.nanoxml.XMLWriter;
035:
036:        import net.sourceforge.squirrel_sql.fw.util.beanwrapper.StringWrapper;
037:
038:        public final class XMLBeanWriter {
039:            private IXMLElement _rootElement;
040:
041:            public XMLBeanWriter() throws XMLException {
042:                this (null);
043:            }
044:
045:            public XMLBeanWriter(Object bean) throws XMLException {
046:                super ();
047:                _rootElement = new XMLElement(XMLConstants.ROOT_ELEMENT_NAME);
048:                if (bean != null) {
049:                    addToRoot(bean);
050:                }
051:            }
052:
053:            public void addIteratorToRoot(Iterator it) throws XMLException {
054:                while (it.hasNext()) {
055:                    addToRoot(it.next());
056:                }
057:            }
058:
059:            public void addToRoot(Object bean) throws XMLException {
060:                try {
061:                    _rootElement.addChild(createElement(bean, null));
062:                } catch (Exception ex) {
063:                    throw new XMLException(ex);
064:                }
065:            }
066:
067:            public void save(String fileName) throws IOException {
068:                save(new File(fileName));
069:            }
070:
071:            public void save(File file) throws IOException {
072:                BufferedOutputStream os = new BufferedOutputStream(
073:                        new FileOutputStream(file));
074:                try {
075:                    XMLWriter wtr = new XMLWriter(os);
076:                    wtr.write(_rootElement, true);
077:                } finally {
078:                    os.close();
079:                }
080:            }
081:
082:            private IXMLElement createElement(Object bean, String name)
083:                    throws XMLException {
084:                IXMLElement elem = null;
085:                BeanInfo info = null;
086:                try {
087:                    if (bean != null) {
088:                        info = Introspector.getBeanInfo(bean.getClass(),
089:                                Object.class);
090:                    }
091:                } catch (IntrospectionException ex) {
092:                    throw new XMLException(ex);
093:                }
094:                elem = new XMLElement(name != null ? name
095:                        : XMLConstants.BEAN_ELEMENT_NAME);
096:                if (info != null) {
097:                    if (bean instanceof  IXMLAboutToBeWritten) {
098:                        ((IXMLAboutToBeWritten) bean).aboutToBeWritten();
099:                    }
100:                    PropertyDescriptor[] propDesc = info
101:                            .getPropertyDescriptors();
102:                    elem = new XMLElement(name != null ? name
103:                            : XMLConstants.BEAN_ELEMENT_NAME);
104:                    elem.setAttribute(XMLConstants.CLASS_ATTRIBUTE_NAME, bean
105:                            .getClass().getName());
106:                    for (int i = 0; i < propDesc.length; ++i) {
107:                        processProperty(propDesc[i], bean, elem);
108:                    }
109:                }
110:                return elem;
111:            }
112:
113:            private void processProperty(PropertyDescriptor propDescr,
114:                    Object bean, IXMLElement beanElem) throws XMLException {
115:                final Method getter = propDescr.getReadMethod();
116:                if (getter != null) {
117:                    try {
118:                        final String propName = propDescr.getName();
119:                        Class returnType = getter.getReturnType();
120:                        if (returnType.isArray()) {
121:                            final boolean isStringArray = returnType.getName()
122:                                    .equals("[Ljava.lang.String;");
123:                            Object[] props = (Object[]) getter.invoke(bean,
124:                                    (Object[]) null);
125:                            if (props != null) {
126:                                IXMLElement indexElem = new XMLElement(propName);
127:                                indexElem.setAttribute(XMLConstants.INDEXED,
128:                                        "true");
129:                                beanElem.addChild(indexElem);
130:                                for (int i = 0; i < props.length; ++i) {
131:                                    if (isStringArray) {
132:                                        StringWrapper sw = new StringWrapper(
133:                                                (String) props[i]);
134:                                        indexElem
135:                                                .addChild(createElement(
136:                                                        sw,
137:                                                        XMLConstants.BEAN_ELEMENT_NAME));
138:                                    } else {
139:                                        indexElem
140:                                                .addChild(createElement(
141:                                                        props[i],
142:                                                        XMLConstants.BEAN_ELEMENT_NAME));
143:                                    }
144:                                }
145:                            }
146:                        } else if (returnType == boolean.class
147:                                || returnType == int.class
148:                                || returnType == short.class
149:                                || returnType == long.class
150:                                || returnType == float.class
151:                                || returnType == double.class
152:                                || returnType == char.class) {
153:                            IXMLElement propElem = new XMLElement(propName);
154:                            propElem.setContent(""
155:                                    + getter.invoke(bean, (Object[]) null));
156:                            beanElem.addChild(propElem);
157:                        } else if (returnType == String.class) {
158:                            IXMLElement propElem = new XMLElement(propName);
159:                            propElem.setContent((String) getter.invoke(bean,
160:                                    (Object[]) null));
161:                            beanElem.addChild(propElem);
162:                        } else {
163:                            beanElem.addChild(createElement(getter.invoke(bean,
164:                                    (Object[]) null), propName));
165:                        }
166:                    } catch (Exception ex) {
167:                        throw new XMLException(ex);
168:                    }
169:                }
170:            }
171:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.