blob: 558f4875f75cb816ed46cb1ca3db73be82ecc60c [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2003-2006 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25
26package com.sun.rowset;
27
28import java.sql.*;
29import javax.sql.*;
30import java.io.*;
31import java.math.*;
32import java.util.*;
33import java.text.*;
34
35import org.xml.sax.*;
36
37import javax.sql.rowset.*;
38import javax.sql.rowset.spi.*;
39
40import com.sun.rowset.providers.*;
41import com.sun.rowset.internal.*;
42
43/**
44 * The standard implementation of the <code>WebRowSet</code> interface. See the interface
45 * defintion for full behaviour and implementation requirements.
46 *
47 * @author Jonathan Bruce, Amit Handa
48 */
49public class WebRowSetImpl extends CachedRowSetImpl implements WebRowSet {
50
51 /**
52 * The <code>WebRowSetXmlReader</code> object that this
53 * <code>WebRowSet</code> object will call when the method
54 * <code>WebRowSet.readXml</code> is invoked.
55 */
56 private WebRowSetXmlReader xmlReader;
57
58 /**
59 * The <code>WebRowSetXmlWriter</code> object that this
60 * <code>WebRowSet</code> object will call when the method
61 * <code>WebRowSet.writeXml</code> is invoked.
62 */
63 private WebRowSetXmlWriter xmlWriter;
64
65 /* This stores the cursor position prior to calling the writeXML.
66 * This variable is used after the write to restore the position
67 * to the point where the writeXml was called.
68 */
69 private int curPosBfrWrite;
70
71 private SyncProvider provider;
72
73 /**
74 * Constructs a new <code>WebRowSet</code> object initialized with the
75 * default values for a <code>CachedRowSet</code> object instance. This
76 * provides the <code>RIOptimistic</code> provider to deliver
77 * synchronization capabilities to relational datastores and a default
78 * <code>WebRowSetXmlReader</code> object and a default
79 * <code>WebRowSetXmlWriter</code> object to enable XML output
80 * capabilities.
81 *
82 * @throws SQLException if an error occurs in configuring the default
83 * synchronization providers for relational and XML providers.
84 */
85 public WebRowSetImpl() throws SQLException {
86 super();
87
88 // %%%
89 // Needs to use to SPI XmlReader,XmlWriters
90 //
91 xmlReader = new WebRowSetXmlReader();
92 xmlWriter = new WebRowSetXmlWriter();
93 }
94
95 /**
96 * Constructs a new <code>WebRowSet</code> object initialized with the the
97 * synchronization SPI provider properties as specified in the <code>Hashtable</code>. If
98 * this hashtable is empty or is <code>null</code> the default constructor is invoked.
99 *
100 * @throws SQLException if an error occurs in configuring the specified
101 * synchronization providers for the relational and XML providers; or
102 * if the Hashtanle is null
103 */
104 public WebRowSetImpl(Hashtable env) throws SQLException {
105
106 if ( env == null) {
107 throw new SQLException(resBundle.handleGetObject("webrowsetimpl.nullhash").toString());
108 }
109
110 String providerName =
111 (String)env.get(javax.sql.rowset.spi.SyncFactory.ROWSET_SYNC_PROVIDER);
112
113 // set the Reader, this maybe overridden latter
114 provider = (SyncProvider)SyncFactory.getInstance(providerName);
115
116 // xmlReader = provider.getRowSetReader();
117 // xmlWriter = provider.getRowSetWriter();
118 }
119
120 /**
121 * Populates this <code>WebRowSet</code> object with the
122 * data in the given <code>ResultSet</code> object and writes itself
123 * to the given <code>java.io.Writer</code> object in XML format.
124 * This includes the rowset's data, properties, and metadata.
125 *
126 * @throws SQLException if an error occurs writing out the rowset
127 * contents to XML
128 */
129 public void writeXml(ResultSet rs, java.io.Writer writer)
130 throws SQLException {
131 // WebRowSetImpl wrs = new WebRowSetImpl();
132 this.populate(rs);
133
134 // Store the cursor position before writing
135 curPosBfrWrite = this.getRow();
136
137 this.writeXml(writer);
138 }
139
140 /**
141 * Writes this <code>WebRowSet</code> object to the given
142 * <code>java.io.Writer</code> object in XML format. This
143 * includes the rowset's data, properties, and metadata.
144 *
145 * @throws SQLException if an error occurs writing out the rowset
146 * contents to XML
147 */
148 public void writeXml(java.io.Writer writer) throws SQLException {
149 // %%%
150 // This will change to a XmlReader, which over-rides the default
151 // Xml that is used when a WRS is instantiated.
152 // WebRowSetXmlWriter xmlWriter = getXmlWriter();
153 if (xmlWriter != null) {
154
155 // Store the cursor position before writing
156 curPosBfrWrite = this.getRow();
157
158 xmlWriter.writeXML(this, writer);
159 } else {
160 throw new SQLException(resBundle.handleGetObject("webrowsetimpl.invalidwr").toString());
161 }
162 }
163
164 /**
165 * Reads this <code>WebRowSet</code> object in its XML format.
166 *
167 * @throws SQLException if a database access error occurs
168 */
169 public void readXml(java.io.Reader reader) throws SQLException {
170 // %%%
171 // This will change to a XmlReader, which over-rides the default
172 // Xml that is used when a WRS is instantiated.
173 //WebRowSetXmlReader xmlReader = getXmlReader();
174 try {
175 if (reader != null) {
176 xmlReader.readXML(this, reader);
177
178 // Position is before the first row
179 // The cursor position is to be stored while serializng
180 // and deserializing the WebRowSet Object.
181 if(curPosBfrWrite == 0) {
182 this.beforeFirst();
183 }
184
185 // Return the position back to place prior to callin writeXml
186 else {
187 this.absolute(curPosBfrWrite);
188 }
189
190 } else {
191 throw new SQLException(resBundle.handleGetObject("webrowsetimpl.invalidrd").toString());
192 }
193 } catch (Exception e) {
194 throw new SQLException(e.getMessage());
195 }
196 }
197
198 // Stream based methods
199 /**
200 * Reads a stream based XML input to populate this <code>WebRowSet</code>
201 * object.
202 *
203 * @throws SQLException if a data source access error occurs
204 * @throws IOException if a IO exception occurs
205 */
206 public void readXml(java.io.InputStream iStream) throws SQLException, IOException {
207 if (iStream != null) {
208 xmlReader.readXML(this, iStream);
209
210 // Position is before the first row
211 // The cursor position is to be stored while serializng
212 // and deserializing the WebRowSet Object.
213 if(curPosBfrWrite == 0) {
214 this.beforeFirst();
215 }
216
217 // Return the position back to place prior to callin writeXml
218 else {
219 this.absolute(curPosBfrWrite);
220 }
221
222 } else {
223 throw new SQLException(resBundle.handleGetObject("webrowsetimpl.invalidrd").toString());
224 }
225 }
226
227 /**
228 * Writes this <code>WebRowSet</code> object to the given <code> OutputStream</code>
229 * object in XML format.
230 * Creates an an output stream of the internal state and contents of a
231 * <code>WebRowSet</code> for XML proceessing
232 *
233 * @throws SQLException if a datasource access error occurs
234 * @throws IOException if an IO exception occurs
235 */
236 public void writeXml(java.io.OutputStream oStream) throws SQLException, IOException {
237 if (xmlWriter != null) {
238
239 // Store the cursor position before writing
240 curPosBfrWrite = this.getRow();
241
242 xmlWriter.writeXML(this, oStream);
243 } else {
244 throw new SQLException(resBundle.handleGetObject("webrowsetimpl.invalidwr").toString());
245 }
246
247 }
248
249 /**
250 * Populates this <code>WebRowSet</code> object with the
251 * data in the given <code>ResultSet</code> object and writes itself
252 * to the given <code>java.io.OutputStream</code> object in XML format.
253 * This includes the rowset's data, properties, and metadata.
254 *
255 * @throws SQLException if a datasource access error occurs
256 * @throws IOException if an IO exception occurs
257 */
258 public void writeXml(ResultSet rs, java.io.OutputStream oStream) throws SQLException, IOException {
259 this.populate(rs);
260
261 // Store the cursor position before writing
262 curPosBfrWrite = this.getRow();
263
264 this.writeXml(oStream);
265 }
266static final long serialVersionUID = -8771775154092422943L;
267}