001: /*
002: * Copyright 2003-2006 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package com.sun.rowset;
027:
028: import java.sql.*;
029: import javax.sql.*;
030: import java.io.*;
031: import java.math.*;
032: import java.util.*;
033: import java.text.*;
034:
035: import org.xml.sax.*;
036:
037: import javax.sql.rowset.*;
038: import javax.sql.rowset.spi.*;
039:
040: import com.sun.rowset.providers.*;
041: import com.sun.rowset.internal.*;
042:
043: /**
044: * The standard implementation of the <code>WebRowSet</code> interface. See the interface
045: * defintion for full behaviour and implementation requirements.
046: *
047: * @author Jonathan Bruce, Amit Handa
048: */
049: public class WebRowSetImpl extends CachedRowSetImpl implements
050: WebRowSet {
051:
052: /**
053: * The <code>WebRowSetXmlReader</code> object that this
054: * <code>WebRowSet</code> object will call when the method
055: * <code>WebRowSet.readXml</code> is invoked.
056: */
057: private WebRowSetXmlReader xmlReader;
058:
059: /**
060: * The <code>WebRowSetXmlWriter</code> object that this
061: * <code>WebRowSet</code> object will call when the method
062: * <code>WebRowSet.writeXml</code> is invoked.
063: */
064: private WebRowSetXmlWriter xmlWriter;
065:
066: /* This stores the cursor position prior to calling the writeXML.
067: * This variable is used after the write to restore the position
068: * to the point where the writeXml was called.
069: */
070: private int curPosBfrWrite;
071:
072: private SyncProvider provider;
073:
074: /**
075: * Constructs a new <code>WebRowSet</code> object initialized with the
076: * default values for a <code>CachedRowSet</code> object instance. This
077: * provides the <code>RIOptimistic</code> provider to deliver
078: * synchronization capabilities to relational datastores and a default
079: * <code>WebRowSetXmlReader</code> object and a default
080: * <code>WebRowSetXmlWriter</code> object to enable XML output
081: * capabilities.
082: *
083: * @throws SQLException if an error occurs in configuring the default
084: * synchronization providers for relational and XML providers.
085: */
086: public WebRowSetImpl() throws SQLException {
087: super ();
088:
089: // %%%
090: // Needs to use to SPI XmlReader,XmlWriters
091: //
092: xmlReader = new WebRowSetXmlReader();
093: xmlWriter = new WebRowSetXmlWriter();
094: }
095:
096: /**
097: * Constructs a new <code>WebRowSet</code> object initialized with the the
098: * synchronization SPI provider properties as specified in the <code>Hashtable</code>. If
099: * this hashtable is empty or is <code>null</code> the default constructor is invoked.
100: *
101: * @throws SQLException if an error occurs in configuring the specified
102: * synchronization providers for the relational and XML providers; or
103: * if the Hashtanle is null
104: */
105: public WebRowSetImpl(Hashtable env) throws SQLException {
106:
107: if (env == null) {
108: throw new SQLException(resBundle.handleGetObject(
109: "webrowsetimpl.nullhash").toString());
110: }
111:
112: String providerName = (String) env
113: .get(javax.sql.rowset.spi.SyncFactory.ROWSET_SYNC_PROVIDER);
114:
115: // set the Reader, this maybe overridden latter
116: provider = (SyncProvider) SyncFactory.getInstance(providerName);
117:
118: // xmlReader = provider.getRowSetReader();
119: // xmlWriter = provider.getRowSetWriter();
120: }
121:
122: /**
123: * Populates this <code>WebRowSet</code> object with the
124: * data in the given <code>ResultSet</code> object and writes itself
125: * to the given <code>java.io.Writer</code> object in XML format.
126: * This includes the rowset's data, properties, and metadata.
127: *
128: * @throws SQLException if an error occurs writing out the rowset
129: * contents to XML
130: */
131: public void writeXml(ResultSet rs, java.io.Writer writer)
132: throws SQLException {
133: // WebRowSetImpl wrs = new WebRowSetImpl();
134: this .populate(rs);
135:
136: // Store the cursor position before writing
137: curPosBfrWrite = this .getRow();
138:
139: this .writeXml(writer);
140: }
141:
142: /**
143: * Writes this <code>WebRowSet</code> object to the given
144: * <code>java.io.Writer</code> object in XML format. This
145: * includes the rowset's data, properties, and metadata.
146: *
147: * @throws SQLException if an error occurs writing out the rowset
148: * contents to XML
149: */
150: public void writeXml(java.io.Writer writer) throws SQLException {
151: // %%%
152: // This will change to a XmlReader, which over-rides the default
153: // Xml that is used when a WRS is instantiated.
154: // WebRowSetXmlWriter xmlWriter = getXmlWriter();
155: if (xmlWriter != null) {
156:
157: // Store the cursor position before writing
158: curPosBfrWrite = this .getRow();
159:
160: xmlWriter.writeXML(this , writer);
161: } else {
162: throw new SQLException(resBundle.handleGetObject(
163: "webrowsetimpl.invalidwr").toString());
164: }
165: }
166:
167: /**
168: * Reads this <code>WebRowSet</code> object in its XML format.
169: *
170: * @throws SQLException if a database access error occurs
171: */
172: public void readXml(java.io.Reader reader) throws SQLException {
173: // %%%
174: // This will change to a XmlReader, which over-rides the default
175: // Xml that is used when a WRS is instantiated.
176: //WebRowSetXmlReader xmlReader = getXmlReader();
177: try {
178: if (reader != null) {
179: xmlReader.readXML(this , reader);
180:
181: // Position is before the first row
182: // The cursor position is to be stored while serializng
183: // and deserializing the WebRowSet Object.
184: if (curPosBfrWrite == 0) {
185: this .beforeFirst();
186: }
187:
188: // Return the position back to place prior to callin writeXml
189: else {
190: this .absolute(curPosBfrWrite);
191: }
192:
193: } else {
194: throw new SQLException(resBundle.handleGetObject(
195: "webrowsetimpl.invalidrd").toString());
196: }
197: } catch (Exception e) {
198: throw new SQLException(e.getMessage());
199: }
200: }
201:
202: // Stream based methods
203: /**
204: * Reads a stream based XML input to populate this <code>WebRowSet</code>
205: * object.
206: *
207: * @throws SQLException if a data source access error occurs
208: * @throws IOException if a IO exception occurs
209: */
210: public void readXml(java.io.InputStream iStream)
211: throws SQLException, IOException {
212: if (iStream != null) {
213: xmlReader.readXML(this , iStream);
214:
215: // Position is before the first row
216: // The cursor position is to be stored while serializng
217: // and deserializing the WebRowSet Object.
218: if (curPosBfrWrite == 0) {
219: this .beforeFirst();
220: }
221:
222: // Return the position back to place prior to callin writeXml
223: else {
224: this .absolute(curPosBfrWrite);
225: }
226:
227: } else {
228: throw new SQLException(resBundle.handleGetObject(
229: "webrowsetimpl.invalidrd").toString());
230: }
231: }
232:
233: /**
234: * Writes this <code>WebRowSet</code> object to the given <code> OutputStream</code>
235: * object in XML format.
236: * Creates an an output stream of the internal state and contents of a
237: * <code>WebRowSet</code> for XML proceessing
238: *
239: * @throws SQLException if a datasource access error occurs
240: * @throws IOException if an IO exception occurs
241: */
242: public void writeXml(java.io.OutputStream oStream)
243: throws SQLException, IOException {
244: if (xmlWriter != null) {
245:
246: // Store the cursor position before writing
247: curPosBfrWrite = this .getRow();
248:
249: xmlWriter.writeXML(this , oStream);
250: } else {
251: throw new SQLException(resBundle.handleGetObject(
252: "webrowsetimpl.invalidwr").toString());
253: }
254:
255: }
256:
257: /**
258: * Populates this <code>WebRowSet</code> object with the
259: * data in the given <code>ResultSet</code> object and writes itself
260: * to the given <code>java.io.OutputStream</code> object in XML format.
261: * This includes the rowset's data, properties, and metadata.
262: *
263: * @throws SQLException if a datasource access error occurs
264: * @throws IOException if an IO exception occurs
265: */
266: public void writeXml(ResultSet rs, java.io.OutputStream oStream)
267: throws SQLException, IOException {
268: this .populate(rs);
269:
270: // Store the cursor position before writing
271: curPosBfrWrite = this .getRow();
272:
273: this .writeXml(oStream);
274: }
275:
276: static final long serialVersionUID = -8771775154092422943L;
277: }
|