Path: blob/master/src/java.sql.rowset/share/classes/com/sun/rowset/internal/WebRowSetXmlReader.java
40948 views
/*1* Copyright (c) 2003, 2010, 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.internal;2627import java.sql.*;28import javax.sql.*;29import java.io.*;3031import org.xml.sax.*;32import org.xml.sax.helpers.*;33import javax.xml.parsers.*;3435import com.sun.rowset.*;36import java.text.MessageFormat;37import javax.sql.rowset.*;38import javax.sql.rowset.spi.*;3940/**41* An implementation of the <code>XmlReader</code> interface, which42* reads and parses an XML formatted <code>WebRowSet</code> object.43* This implementation uses an <code>org.xml.sax.Parser</code> object44* as its parser.45*/46public class WebRowSetXmlReader implements XmlReader, Serializable {474849private JdbcRowSetResourceBundle resBundle;5051public WebRowSetXmlReader(){52try {53resBundle = JdbcRowSetResourceBundle.getJdbcRowSetResourceBundle();54} catch(IOException ioe) {55throw new RuntimeException(ioe);56}57}5859/**60* Parses the given <code>WebRowSet</code> object, getting its input from61* the given <code>java.io.Reader</code> object. The parser will send62* notifications of parse events to the rowset's63* <code>XmlReaderDocHandler</code>, which will build the rowset as64* an XML document.65* <P>66* This method is called internally by the method67* <code>WebRowSet.readXml</code>.68* <P>69* If a parsing error occurs, the exception thrown will include70* information for locating the error in the original XML document.71*72* @param caller the <code>WebRowSet</code> object to be parsed, whose73* <code>xmlReader</code> field must contain a reference to74* this <code>XmlReader</code> object75* @param reader the <code>java.io.Reader</code> object from which76* the parser will get its input77* @exception SQLException if a database access error occurs or78* this <code>WebRowSetXmlReader</code> object is not the79* reader for the given rowset80* @see XmlReaderContentHandler81*/82public void readXML(WebRowSet caller, java.io.Reader reader) throws SQLException {83try {84// Crimson Parser(as in J2SE 1.4.1 is NOT able to handle85// Reader(s)(FileReader).86//87// But getting the file as a Stream works fine. So we are going to take88// the reader but send it as a InputStream to the parser. Note that this89// functionality needs to work against any parser90// Crimson(J2SE 1.4.x) / Xerces(J2SE 1.5.x).91InputSource is = new InputSource(reader);92DefaultHandler dh = new XmlErrorHandler();93XmlReaderContentHandler hndr = new XmlReaderContentHandler((RowSet)caller);94SAXParserFactory factory = SAXParserFactory.newInstance();95factory.setNamespaceAware(true);96factory.setValidating(true);97SAXParser parser = factory.newSAXParser() ;9899parser.setProperty(100"http://java.sun.com/xml/jaxp/properties/schemaLanguage", "http://www.w3.org/2001/XMLSchema");101102XMLReader reader1 = parser.getXMLReader() ;103reader1.setEntityResolver(new XmlResolver());104reader1.setContentHandler(hndr);105106reader1.setErrorHandler(dh);107108reader1.parse(is);109110} catch (SAXParseException err) {111System.out.println (MessageFormat.format(resBundle.handleGetObject("wrsxmlreader.parseerr").toString(), new Object[]{ err.getMessage (), err.getLineNumber(), err.getSystemId()}));112err.printStackTrace();113throw new SQLException(err.getMessage());114115} catch (SAXException e) {116Exception x = e;117if (e.getException () != null)118x = e.getException();119x.printStackTrace ();120throw new SQLException(x.getMessage());121122}123124// Will be here if trying to write beyond the RowSet limits125126catch (ArrayIndexOutOfBoundsException aie) {127throw new SQLException(resBundle.handleGetObject("wrsxmlreader.invalidcp").toString());128}129catch (Throwable e) {130throw new SQLException(MessageFormat.format(resBundle.handleGetObject("wrsxmlreader.readxml").toString() , e.getMessage()));131}132133}134135136/**137* Parses the given <code>WebRowSet</code> object, getting its input from138* the given <code>java.io.InputStream</code> object. The parser will send139* notifications of parse events to the rowset's140* <code>XmlReaderDocHandler</code>, which will build the rowset as141* an XML document.142* <P>143* Using streams is a much faster way than using <code>java.io.Reader</code>144* <P>145* This method is called internally by the method146* <code>WebRowSet.readXml</code>.147* <P>148* If a parsing error occurs, the exception thrown will include149* information for locating the error in the original XML document.150*151* @param caller the <code>WebRowSet</code> object to be parsed, whose152* <code>xmlReader</code> field must contain a reference to153* this <code>XmlReader</code> object154* @param iStream the <code>java.io.InputStream</code> object from which155* the parser will get its input156* @throws SQLException if a database access error occurs or157* this <code>WebRowSetXmlReader</code> object is not the158* reader for the given rowset159* @see XmlReaderContentHandler160*/161public void readXML(WebRowSet caller, java.io.InputStream iStream) throws SQLException {162try {163InputSource is = new InputSource(iStream);164DefaultHandler dh = new XmlErrorHandler();165166XmlReaderContentHandler hndr = new XmlReaderContentHandler((RowSet)caller);167SAXParserFactory factory = SAXParserFactory.newInstance();168factory.setNamespaceAware(true);169factory.setValidating(true);170171SAXParser parser = factory.newSAXParser() ;172173parser.setProperty("http://java.sun.com/xml/jaxp/properties/schemaLanguage",174"http://www.w3.org/2001/XMLSchema");175176XMLReader reader1 = parser.getXMLReader() ;177reader1.setEntityResolver(new XmlResolver());178reader1.setContentHandler(hndr);179180reader1.setErrorHandler(dh);181182reader1.parse(is);183184} catch (SAXParseException err) {185System.out.println (MessageFormat.format(resBundle.handleGetObject("wrsxmlreader.parseerr").toString(), new Object[]{err.getLineNumber(), err.getSystemId() }));186System.out.println(" " + err.getMessage ());187err.printStackTrace();188throw new SQLException(err.getMessage());189190} catch (SAXException e) {191Exception x = e;192if (e.getException () != null)193x = e.getException();194x.printStackTrace ();195throw new SQLException(x.getMessage());196197}198199// Will be here if trying to write beyond the RowSet limits200201catch (ArrayIndexOutOfBoundsException aie) {202throw new SQLException(resBundle.handleGetObject("wrsxmlreader.invalidcp").toString());203}204205catch (Throwable e) {206throw new SQLException(MessageFormat.format(resBundle.handleGetObject("wrsxmlreader.readxml").toString() , e.getMessage()));207}208}209210/**211* For code coverage purposes only right now212*213*/214215public void readData(RowSetInternal caller) {216}217218/**219* This method re populates the resBundle220* during the deserialization process221*222*/223private void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException {224// Default state initialization happens here225ois.defaultReadObject();226// Initialization of transient Res Bundle happens here .227try {228resBundle = JdbcRowSetResourceBundle.getJdbcRowSetResourceBundle();229} catch(IOException ioe) {230throw new RuntimeException(ioe);231}232233}234235static final long serialVersionUID = -9127058392819008014L;236}237238239