KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jcorporate > expresso > ext > xml > dbobj > XMLDBObject


1 /* ====================================================================
2  * The Jcorporate Apache Style Software License, Version 1.2 05-07-2002
3  *
4  * Copyright (c) 1995-2002 Jcorporate Ltd. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  *
18  * 3. The end-user documentation included with the redistribution,
19  * if any, must include the following acknowledgment:
20  * "This product includes software developed by Jcorporate Ltd.
21  * (http://www.jcorporate.com/)."
22  * Alternately, this acknowledgment may appear in the software itself,
23  * if and wherever such third-party acknowledgments normally appear.
24  *
25  * 4. "Jcorporate" and product names such as "Expresso" must
26  * not be used to endorse or promote products derived from this
27  * software without prior written permission. For written permission,
28  * please contact info@jcorporate.com.
29  *
30  * 5. Products derived from this software may not be called "Expresso",
31  * or other Jcorporate product names; nor may "Expresso" or other
32  * Jcorporate product names appear in their name, without prior
33  * written permission of Jcorporate Ltd.
34  *
35  * 6. No product derived from this software may compete in the same
36  * market space, i.e. framework, without prior written permission
37  * of Jcorporate Ltd. For written permission, please contact
38  * partners@jcorporate.com.
39  *
40  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
41  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
42  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
43  * DISCLAIMED. IN NO EVENT SHALL JCORPORATE LTD OR ITS CONTRIBUTORS
44  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
45  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
46  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
47  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
48  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
49  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
50  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  * ====================================================================
53  *
54  * This software consists of voluntary contributions made by many
55  * individuals on behalf of the Jcorporate Ltd. Contributions back
56  * to the project(s) are encouraged when you make modifications.
57  * Please send them to support@jcorporate.com. For more information
58  * on Jcorporate Ltd. and its products, please see
59  * <http://www.jcorporate.com/>.
60  *
61  * Portions of this software are based upon other open source
62  * products and are subject to their respective licenses.
63  */

64
65 package com.jcorporate.expresso.ext.xml.dbobj;
66
67 /**
68  * XMLDBObject.java
69  *
70  * Copyright 2000, 2001 Jcorporate Ltd.
71  */

72
73 import com.jcorporate.expresso.core.dataobjects.DataObject;
74 import com.jcorporate.expresso.core.dataobjects.jdbc.JDBCObjectMetaData;
75 import com.jcorporate.expresso.core.db.DBException;
76 import com.jcorporate.expresso.core.misc.URLUTF8Encoder;
77 import org.xml.sax.SAXException JavaDoc;
78 import org.xml.sax.SAXParseException JavaDoc;
79 import org.xml.sax.XMLReader JavaDoc;
80 import org.xml.sax.helpers.XMLReaderFactory JavaDoc;
81
82 import java.io.BufferedOutputStream JavaDoc;
83 import java.io.FileNotFoundException JavaDoc;
84 import java.io.FileOutputStream JavaDoc;
85 import java.io.IOException JavaDoc;
86 import java.io.PrintStream JavaDoc;
87 import java.io.PrintWriter JavaDoc;
88 import java.util.Iterator JavaDoc;
89 import java.util.Vector JavaDoc;
90
91
92 /**
93  * Object to "wrap" a database object and allow import and
94  * export of that object's data in XML format
95  *
96  * @author Michael Nash
97  */

98 public class XMLDBObject {
99     private DataObject myDBObject = null;
100     private static final String JavaDoc thisClass = XMLDBObject.class.getName() + ".";
101     private Vector JavaDoc errorList = new Vector JavaDoc(3);
102     private String JavaDoc 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
120     public void setDBName(String JavaDoc newDBName) {
121         dbName = newDBName;
122     }
123
124     public String JavaDoc getDBName() {
125         return dbName;
126     }
127
128     /**
129      * Filename should NOT include an extension, as export writes
130      * a DTD and XML file
131      *
132      * @param fileName
133      */

134     public void exportToFile(String JavaDoc fileName)
135             throws DBException, IOException JavaDoc, FileNotFoundException JavaDoc {
136         exportToFileXML(fileName);
137     } /* exportToFile(String) */
138
139
140     /**
141      * @param fileName
142      */

143     private void exportToFileXML(String JavaDoc fileName)
144             throws FileNotFoundException JavaDoc, IOException JavaDoc,
145             DBException {
146         FileOutputStream JavaDoc fout = new FileOutputStream JavaDoc(fileName + ".xml", true);
147         BufferedOutputStream JavaDoc bout = new BufferedOutputStream JavaDoc(fout);
148         PrintStream JavaDoc XMLFileStream = new PrintStream JavaDoc(bout);
149         exportToStreamXML(new PrintWriter JavaDoc(XMLFileStream));
150         fout.close();
151     } /* exportToFileXML(String) */
152
153
154     /**
155      * Export the records for the current selection in the specified database object
156      * into an XML format output stream
157      * Creation date: (4/2/00 8:38:58 PM)
158      *
159      * @param p java.io.PrintStream
160      */

161     public void exportToStreamXML(PrintWriter JavaDoc p)
162             throws DBException {
163         String JavaDoc myName = (thisClass +
164                 "exportToStreamXML(PrintWriter)");
165         String JavaDoc fieldName = null;
166         DataObject oneObj = null;
167
168         if (p == null) {
169             throw new DBException(myName + ":PrintWriter cannot be null");
170         }
171         if (myDBObject == null) {
172             throw new DBException(myName +
173                     ":Database object is not set - cannot export");
174         }
175         if (myDBObject.count() == 0) {
176             return;
177         }
178
179         p.println("<DBObject>");
180         p.println(indent(1) + "<ObjectName>" +
181                 myDBObject.getClass().getName() + "</ObjectName>");
182         if (myDBObject.getMetaData() instanceof JDBCObjectMetaData) {
183             p.println(indent(1) + "<TableName>" + ((JDBCObjectMetaData) myDBObject.getMetaData()).getTargetTable() +
184                     "</TableName>");
185         }
186         p.println(indent(1) + "<Data>");
187
188         for (Iterator JavaDoc rc = myDBObject.searchAndRetrieveList().iterator();
189              rc.hasNext();) {
190             oneObj = (DataObject) rc.next();
191             p.println(indent(2) + "<DataRecord>");
192
193             for (Iterator JavaDoc e = myDBObject.getMetaData().getAllFieldsMap().keySet().iterator();
194                  e.hasNext();) {
195                 fieldName = (String JavaDoc) e.next();
196                 p.println(indent(3) + "<Field>");
197                 p.println(indent(4) + "<FieldName>" + fieldName +
198                         "</FieldName>");
199
200                 try {
201                     p.println(indent(4) + "<FieldValue>" +
202                             URLUTF8Encoder.encode(oneObj.getField(fieldName)) +
203                             "</FieldValue>");
204                 } catch (Exception JavaDoc 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
216         p.println(indent(1) + "</Data>");
217         p.println("</DBObject>");
218         p.flush();
219     } /* exportToStreamXML(PrintWriter) */
220
221
222     /**
223      * @return
224      */

225     public DataObject getDBObject()
226             throws DBException {
227         return myDBObject;
228     } /* getDBObject() */
229
230
231     /**
232      * @return
233      */

234     public Vector JavaDoc getErrorList() {
235         return errorList;
236     } /* getErrorList() */
237
238     /**
239      * @param fileName
240      */

241     public void importFromFile(String JavaDoc fileName)
242             throws DBException {
243         String JavaDoc myName = (thisClass + "importFromFile(String)");
244
245         try {
246             XMLImportDocument xi = new XMLImportDocument(myDBObject);
247             xi.setDBName(getDBName());
248
249             XMLReader JavaDoc xr = XMLReaderFactory.createXMLReader();
250             xr.setContentHandler(xi);
251             xr.setErrorHandler(xi);
252             xr.parse(fileName + ".xml");
253
254             //new InputSource(new FileInputStream(fileName + ".xml")), xi);
255
errorList = xi.getErrorList();
256
257             // Idit Mike - Gets the DBObject XMLImportDocument
258
myDBObject = xi.getDBObject();
259         } catch (SAXParseException JavaDoc pe) {
260             throw new DBException(myName + ":Parser Configuration Exception:" +
261                     pe.getMessage());
262         } catch (SAXException JavaDoc se) {
263             throw new DBException(myName + ":SAX Exception:" +
264                     se.getMessage());
265         } catch (IOException JavaDoc ie) {
266             throw new DBException(myName + ":IO Exception:" + ie.getMessage());
267         }
268     } /* importFromFile(String) */
269
270
271     /**
272      * @param importURL
273      */

274     public void importFromURL(String JavaDoc importURL)
275             throws DBException {
276         String JavaDoc myName = (thisClass + "importFromURL(String)");
277
278         try {
279             XMLImportDocument xi = new XMLImportDocument(myDBObject);
280             XMLReader JavaDoc xr = XMLReaderFactory.createXMLReader();
281             xr.setContentHandler(xi);
282             xr.setErrorHandler(xi);
283             xr.parse(importURL);
284
285         } catch (SAXParseException JavaDoc pe) {
286             throw new DBException(myName + ":Parser Configuration Exception:" +
287                     pe.getMessage());
288         } catch (SAXException JavaDoc se) {
289             throw new DBException(myName + ":SAX Exception:" +
290                     se.getMessage());
291         } catch (IOException JavaDoc ie) {
292             throw new DBException(myName + ":IO Exception:" + ie.getMessage());
293         }
294     } /* importFromURL(String) */
295
296
297     /**
298      * Insert the method's description here.
299      * Creation date: (4/4/00 11:27:40 AM)
300      *
301      * @param i
302      * @return
303      */

304     private String JavaDoc indent(int i) {
305         StringBuffer JavaDoc tabString = new StringBuffer JavaDoc("");
306
307         for (int j = 0; j < i; j++) {
308             tabString.append(" ");
309         }
310
311         return tabString.toString();
312     } /* indent(int) */
313
314     /**
315      * @param newDBObject
316      */

317     public void setDBObject(DataObject newDBObject)
318             throws DBException {
319         myDBObject = newDBObject;
320     } /* setDBObject(DBObject) */
321
322
323 } /* XMLDBobject */
324
325
Popular Tags