001: /* ====================================================================
002: * The Jcorporate Apache Style Software License, Version 1.2 05-07-2002
003: *
004: * Copyright (c) 1995-2002 Jcorporate Ltd. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * 1. Redistributions of source code must retain the above copyright
011: * notice, this list of conditions and the following disclaimer.
012: *
013: * 2. Redistributions in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in
015: * the documentation and/or other materials provided with the
016: * distribution.
017: *
018: * 3. The end-user documentation included with the redistribution,
019: * if any, must include the following acknowledgment:
020: * "This product includes software developed by Jcorporate Ltd.
021: * (http://www.jcorporate.com/)."
022: * Alternately, this acknowledgment may appear in the software itself,
023: * if and wherever such third-party acknowledgments normally appear.
024: *
025: * 4. "Jcorporate" and product names such as "Expresso" must
026: * not be used to endorse or promote products derived from this
027: * software without prior written permission. For written permission,
028: * please contact info@jcorporate.com.
029: *
030: * 5. Products derived from this software may not be called "Expresso",
031: * or other Jcorporate product names; nor may "Expresso" or other
032: * Jcorporate product names appear in their name, without prior
033: * written permission of Jcorporate Ltd.
034: *
035: * 6. No product derived from this software may compete in the same
036: * market space, i.e. framework, without prior written permission
037: * of Jcorporate Ltd. For written permission, please contact
038: * partners@jcorporate.com.
039: *
040: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
041: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
042: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
043: * DISCLAIMED. IN NO EVENT SHALL JCORPORATE LTD OR ITS CONTRIBUTORS
044: * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
045: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
046: * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
047: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
048: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
049: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
050: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
051: * SUCH DAMAGE.
052: * ====================================================================
053: *
054: * This software consists of voluntary contributions made by many
055: * individuals on behalf of the Jcorporate Ltd. Contributions back
056: * to the project(s) are encouraged when you make modifications.
057: * Please send them to support@jcorporate.com. For more information
058: * on Jcorporate Ltd. and its products, please see
059: * <http://www.jcorporate.com/>.
060: *
061: * Portions of this software are based upon other open source
062: * products and are subject to their respective licenses.
063: */
064:
065: package com.jcorporate.expresso.ext.xml.dbobj;
066:
067: /**
068: * XMLDBObject.java
069: *
070: * Copyright 2000, 2001 Jcorporate Ltd.
071: */
072:
073: import com.jcorporate.expresso.core.dataobjects.DataObject;
074: import com.jcorporate.expresso.core.dataobjects.jdbc.JDBCObjectMetaData;
075: import com.jcorporate.expresso.core.db.DBException;
076: import com.jcorporate.expresso.core.misc.URLUTF8Encoder;
077: import org.xml.sax.SAXException;
078: import org.xml.sax.SAXParseException;
079: import org.xml.sax.XMLReader;
080: import org.xml.sax.helpers.XMLReaderFactory;
081:
082: import java.io.BufferedOutputStream;
083: import java.io.FileNotFoundException;
084: import java.io.FileOutputStream;
085: import java.io.IOException;
086: import java.io.PrintStream;
087: import java.io.PrintWriter;
088: import java.util.Iterator;
089: import java.util.Vector;
090:
091: /**
092: * Object to "wrap" a database object and allow import and
093: * export of that object's data in XML format
094: *
095: * @author Michael Nash
096: */
097: public class XMLDBObject {
098: private DataObject myDBObject = null;
099: private static final String this Class = XMLDBObject.class.getName()
100: + ".";
101: private Vector errorList = new Vector(3);
102: private String dbName = "default";
103:
104: /**
105: * Default constructor
106: */
107: public XMLDBObject() {
108: } /* XMLDBObject() */
109:
110: /**
111: * Default constructor
112: *
113: * @param newDBObject
114: */
115: public XMLDBObject(DataObject newDBObject) {
116: myDBObject = newDBObject;
117: } /* XMLDBObject(DBObject) */
118:
119: public void setDBName(String newDBName) {
120: dbName = newDBName;
121: }
122:
123: public String getDBName() {
124: return dbName;
125: }
126:
127: /**
128: * Filename should NOT include an extension, as export writes
129: * a DTD and XML file
130: *
131: * @param fileName
132: */
133: public void exportToFile(String fileName) throws DBException,
134: IOException, FileNotFoundException {
135: exportToFileXML(fileName);
136: } /* exportToFile(String) */
137:
138: /**
139: * @param fileName
140: */
141: private void exportToFileXML(String fileName)
142: throws FileNotFoundException, IOException, DBException {
143: FileOutputStream fout = new FileOutputStream(fileName + ".xml",
144: true);
145: BufferedOutputStream bout = new BufferedOutputStream(fout);
146: PrintStream XMLFileStream = new PrintStream(bout);
147: exportToStreamXML(new PrintWriter(XMLFileStream));
148: fout.close();
149: } /* exportToFileXML(String) */
150:
151: /**
152: * Export the records for the current selection in the specified database object
153: * into an XML format output stream
154: * Creation date: (4/2/00 8:38:58 PM)
155: *
156: * @param p java.io.PrintStream
157: */
158: public void exportToStreamXML(PrintWriter p) throws DBException {
159: String myName = (this Class + "exportToStreamXML(PrintWriter)");
160: String fieldName = null;
161: DataObject oneObj = null;
162:
163: if (p == null) {
164: throw new DBException(myName
165: + ":PrintWriter cannot be null");
166: }
167: if (myDBObject == null) {
168: throw new DBException(myName
169: + ":Database object is not set - cannot export");
170: }
171: if (myDBObject.count() == 0) {
172: return;
173: }
174:
175: p.println("<DBObject>");
176: p.println(indent(1) + "<ObjectName>"
177: + myDBObject.getClass().getName() + "</ObjectName>");
178: if (myDBObject.getMetaData() instanceof JDBCObjectMetaData) {
179: p.println(indent(1)
180: + "<TableName>"
181: + ((JDBCObjectMetaData) myDBObject.getMetaData())
182: .getTargetTable() + "</TableName>");
183: }
184: p.println(indent(1) + "<Data>");
185:
186: for (Iterator rc = myDBObject.searchAndRetrieveList()
187: .iterator(); rc.hasNext();) {
188: oneObj = (DataObject) rc.next();
189: p.println(indent(2) + "<DataRecord>");
190:
191: for (Iterator e = myDBObject.getMetaData()
192: .getAllFieldsMap().keySet().iterator(); e.hasNext();) {
193: fieldName = (String) e.next();
194: p.println(indent(3) + "<Field>");
195: p.println(indent(4) + "<FieldName>" + fieldName
196: + "</FieldName>");
197:
198: try {
199: p.println(indent(4)
200: + "<FieldValue>"
201: + URLUTF8Encoder.encode(oneObj
202: .getField(fieldName))
203: + "</FieldValue>");
204: } catch (Exception ex) {
205: throw new DBException(ex);
206: }
207:
208: p.println(indent(3) + "</Field>");
209: } /* for each field */
210:
211: p.println(indent(2) + "</DataRecord>");
212: p.flush();
213: } /* for each record */
214:
215: p.println(indent(1) + "</Data>");
216: p.println("</DBObject>");
217: p.flush();
218: } /* exportToStreamXML(PrintWriter) */
219:
220: /**
221: * @return
222: */
223: public DataObject getDBObject() throws DBException {
224: return myDBObject;
225: } /* getDBObject() */
226:
227: /**
228: * @return
229: */
230: public Vector getErrorList() {
231: return errorList;
232: } /* getErrorList() */
233:
234: /**
235: * @param fileName
236: */
237: public void importFromFile(String fileName) throws DBException {
238: String myName = (this Class + "importFromFile(String)");
239:
240: try {
241: XMLImportDocument xi = new XMLImportDocument(myDBObject);
242: xi.setDBName(getDBName());
243:
244: XMLReader xr = XMLReaderFactory.createXMLReader();
245: xr.setContentHandler(xi);
246: xr.setErrorHandler(xi);
247: xr.parse(fileName + ".xml");
248:
249: //new InputSource(new FileInputStream(fileName + ".xml")), xi);
250: errorList = xi.getErrorList();
251:
252: // Idit Mike - Gets the DBObject XMLImportDocument
253: myDBObject = xi.getDBObject();
254: } catch (SAXParseException pe) {
255: throw new DBException(myName
256: + ":Parser Configuration Exception:"
257: + pe.getMessage());
258: } catch (SAXException se) {
259: throw new DBException(myName + ":SAX Exception:"
260: + se.getMessage());
261: } catch (IOException ie) {
262: throw new DBException(myName + ":IO Exception:"
263: + ie.getMessage());
264: }
265: } /* importFromFile(String) */
266:
267: /**
268: * @param importURL
269: */
270: public void importFromURL(String importURL) throws DBException {
271: String myName = (this Class + "importFromURL(String)");
272:
273: try {
274: XMLImportDocument xi = new XMLImportDocument(myDBObject);
275: XMLReader xr = XMLReaderFactory.createXMLReader();
276: xr.setContentHandler(xi);
277: xr.setErrorHandler(xi);
278: xr.parse(importURL);
279:
280: } catch (SAXParseException pe) {
281: throw new DBException(myName
282: + ":Parser Configuration Exception:"
283: + pe.getMessage());
284: } catch (SAXException se) {
285: throw new DBException(myName + ":SAX Exception:"
286: + se.getMessage());
287: } catch (IOException ie) {
288: throw new DBException(myName + ":IO Exception:"
289: + ie.getMessage());
290: }
291: } /* importFromURL(String) */
292:
293: /**
294: * Insert the method's description here.
295: * Creation date: (4/4/00 11:27:40 AM)
296: *
297: * @param i
298: * @return
299: */
300: private String indent(int i) {
301: StringBuffer tabString = new StringBuffer("");
302:
303: for (int j = 0; j < i; j++) {
304: tabString.append(" ");
305: }
306:
307: return tabString.toString();
308: } /* indent(int) */
309:
310: /**
311: * @param newDBObject
312: */
313: public void setDBObject(DataObject newDBObject) throws DBException {
314: myDBObject = newDBObject;
315: } /* setDBObject(DBObject) */
316:
317: } /* XMLDBobject */
|