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: }
|