Path: blob/master/src/java.sql.rowset/share/classes/com/sun/rowset/WebRowSetImpl.java
40948 views
/*1* Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package com.sun.rowset;2627import java.sql.*;28import javax.sql.*;29import java.io.*;30import java.math.*;31import java.util.*;32import java.text.*;3334import org.xml.sax.*;3536import javax.sql.rowset.*;37import javax.sql.rowset.spi.*;3839import com.sun.rowset.providers.*;40import com.sun.rowset.internal.*;4142/**43* The standard implementation of the <code>WebRowSet</code> interface. See the interface44* definition for full behavior and implementation requirements.45*46* @author Jonathan Bruce, Amit Handa47*/48public class WebRowSetImpl extends CachedRowSetImpl implements WebRowSet {4950/**51* The <code>WebRowSetXmlReader</code> object that this52* <code>WebRowSet</code> object will call when the method53* <code>WebRowSet.readXml</code> is invoked.54*/55private WebRowSetXmlReader xmlReader;5657/**58* The <code>WebRowSetXmlWriter</code> object that this59* <code>WebRowSet</code> object will call when the method60* <code>WebRowSet.writeXml</code> is invoked.61*/62private WebRowSetXmlWriter xmlWriter;6364/* This stores the cursor position prior to calling the writeXML.65* This variable is used after the write to restore the position66* to the point where the writeXml was called.67*/68private int curPosBfrWrite;6970private SyncProvider provider;7172/**73* Constructs a new <code>WebRowSet</code> object initialized with the74* default values for a <code>CachedRowSet</code> object instance. This75* provides the <code>RIOptimistic</code> provider to deliver76* synchronization capabilities to relational datastores and a default77* <code>WebRowSetXmlReader</code> object and a default78* <code>WebRowSetXmlWriter</code> object to enable XML output79* capabilities.80*81* @throws SQLException if an error occurs in configuring the default82* synchronization providers for relational and XML providers.83*/84public WebRowSetImpl() throws SQLException {85super();8687// %%%88// Needs to use to SPI XmlReader,XmlWriters89//90xmlReader = new WebRowSetXmlReader();91xmlWriter = new WebRowSetXmlWriter();92}9394/**95* Constructs a new <code>WebRowSet</code> object initialized with the96* synchronization SPI provider properties as specified in the <code>Hashtable</code>. If97* this hashtable is empty or is <code>null</code> the default constructor is invoked.98*99* @throws SQLException if an error occurs in configuring the specified100* synchronization providers for the relational and XML providers; or101* if the Hashtanle is null102*/103@SuppressWarnings("rawtypes")104public WebRowSetImpl(Hashtable env) throws SQLException {105106try {107resBundle = JdbcRowSetResourceBundle.getJdbcRowSetResourceBundle();108} catch(IOException ioe) {109throw new RuntimeException(ioe);110}111112if ( env == null) {113throw new SQLException(resBundle.handleGetObject("webrowsetimpl.nullhash").toString());114}115116String providerName =117(String)env.get(javax.sql.rowset.spi.SyncFactory.ROWSET_SYNC_PROVIDER);118119// set the Reader, this maybe overridden latter120provider = SyncFactory.getInstance(providerName);121122// xmlReader = provider.getRowSetReader();123// xmlWriter = provider.getRowSetWriter();124}125126/**127* Populates this <code>WebRowSet</code> object with the128* data in the given <code>ResultSet</code> object and writes itself129* to the given <code>java.io.Writer</code> object in XML format.130* This includes the rowset's data, properties, and metadata.131*132* @throws SQLException if an error occurs writing out the rowset133* contents to XML134*/135public void writeXml(ResultSet rs, java.io.Writer writer)136throws SQLException {137// WebRowSetImpl wrs = new WebRowSetImpl();138this.populate(rs);139140// Store the cursor position before writing141curPosBfrWrite = this.getRow();142143this.writeXml(writer);144}145146/**147* Writes this <code>WebRowSet</code> object to the given148* <code>java.io.Writer</code> object in XML format. This149* includes the rowset's data, properties, and metadata.150*151* @throws SQLException if an error occurs writing out the rowset152* contents to XML153*/154public void writeXml(java.io.Writer writer) throws SQLException {155// %%%156// This will change to a XmlReader, which over-rides the default157// Xml that is used when a WRS is instantiated.158// WebRowSetXmlWriter xmlWriter = getXmlWriter();159if (xmlWriter != null) {160161// Store the cursor position before writing162curPosBfrWrite = this.getRow();163164xmlWriter.writeXML(this, writer);165} else {166throw new SQLException(resBundle.handleGetObject("webrowsetimpl.invalidwr").toString());167}168}169170/**171* Reads this <code>WebRowSet</code> object in its XML format.172*173* @throws SQLException if a database access error occurs174*/175public void readXml(java.io.Reader reader) throws SQLException {176// %%%177// This will change to a XmlReader, which over-rides the default178// Xml that is used when a WRS is instantiated.179//WebRowSetXmlReader xmlReader = getXmlReader();180try {181if (reader != null) {182xmlReader.readXML(this, reader);183184// Position is before the first row185// The cursor position is to be stored while serializng186// and deserializing the WebRowSet Object.187if(curPosBfrWrite == 0) {188this.beforeFirst();189}190191// Return the position back to place prior to callin writeXml192else {193this.absolute(curPosBfrWrite);194}195196} else {197throw new SQLException(resBundle.handleGetObject("webrowsetimpl.invalidrd").toString());198}199} catch (Exception e) {200throw new SQLException(e.getMessage());201}202}203204// Stream based methods205/**206* Reads a stream based XML input to populate this <code>WebRowSet</code>207* object.208*209* @throws SQLException if a data source access error occurs210* @throws IOException if a IO exception occurs211*/212public void readXml(java.io.InputStream iStream) throws SQLException, IOException {213if (iStream != null) {214xmlReader.readXML(this, iStream);215216// Position is before the first row217// The cursor position is to be stored while serializng218// and deserializing the WebRowSet Object.219if(curPosBfrWrite == 0) {220this.beforeFirst();221}222223// Return the position back to place prior to callin writeXml224else {225this.absolute(curPosBfrWrite);226}227228} else {229throw new SQLException(resBundle.handleGetObject("webrowsetimpl.invalidrd").toString());230}231}232233/**234* Writes this <code>WebRowSet</code> object to the given <code> OutputStream</code>235* object in XML format.236* Creates an output stream of the internal state and contents of a237* <code>WebRowSet</code> for XML proceessing238*239* @throws SQLException if a datasource access error occurs240* @throws IOException if an IO exception occurs241*/242public void writeXml(java.io.OutputStream oStream) throws SQLException, IOException {243if (xmlWriter != null) {244245// Store the cursor position before writing246curPosBfrWrite = this.getRow();247248xmlWriter.writeXML(this, oStream);249} else {250throw new SQLException(resBundle.handleGetObject("webrowsetimpl.invalidwr").toString());251}252253}254255/**256* Populates this <code>WebRowSet</code> object with the257* data in the given <code>ResultSet</code> object and writes itself258* to the given <code>java.io.OutputStream</code> object in XML format.259* This includes the rowset's data, properties, and metadata.260*261* @throws SQLException if a datasource access error occurs262* @throws IOException if an IO exception occurs263*/264public void writeXml(ResultSet rs, java.io.OutputStream oStream) throws SQLException, IOException {265this.populate(rs);266267// Store the cursor position before writing268curPosBfrWrite = this.getRow();269270this.writeXml(oStream);271}272273/**274* This method re populates the resBundle275* during the deserialization process276*277*/278private void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException {279// Default state initialization happens here280ois.defaultReadObject();281// Initialization of transient Res Bundle happens here .282try {283resBundle = JdbcRowSetResourceBundle.getJdbcRowSetResourceBundle();284} catch(IOException ioe) {285throw new RuntimeException(ioe);286}287288}289290static final long serialVersionUID = -8771775154092422943L;291}292293294