0N/A/*
2741N/A * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage com.sun.rowset.internal;
0N/A
0N/Aimport java.sql.*;
0N/Aimport javax.sql.*;
0N/Aimport java.io.*;
0N/A
0N/Aimport org.xml.sax.*;
0N/Aimport org.xml.sax.helpers.*;
0N/Aimport javax.xml.parsers.*;
0N/A
0N/Aimport com.sun.rowset.*;
0N/Aimport java.text.MessageFormat;
0N/Aimport javax.sql.rowset.*;
0N/Aimport javax.sql.rowset.spi.*;
0N/A
0N/A/**
0N/A * An implementation of the <code>XmlReader</code> interface, which
0N/A * reads and parses an XML formatted <code>WebRowSet</code> object.
0N/A * This implementation uses an <code>org.xml.sax.Parser</code> object
0N/A * as its parser.
0N/A */
0N/Apublic class WebRowSetXmlReader implements XmlReader, Serializable {
0N/A
1654N/A
1654N/A private JdbcRowSetResourceBundle resBundle;
1654N/A
1654N/A public WebRowSetXmlReader(){
1654N/A try {
1654N/A resBundle = JdbcRowSetResourceBundle.getJdbcRowSetResourceBundle();
1654N/A } catch(IOException ioe) {
1654N/A throw new RuntimeException(ioe);
1654N/A }
1654N/A }
1654N/A
0N/A /**
0N/A * Parses the given <code>WebRowSet</code> object, getting its input from
0N/A * the given <code>java.io.Reader</code> object. The parser will send
0N/A * notifications of parse events to the rowset's
0N/A * <code>XmlReaderDocHandler</code>, which will build the rowset as
0N/A * an XML document.
0N/A * <P>
0N/A * This method is called internally by the method
0N/A * <code>WebRowSet.readXml</code>.
0N/A * <P>
0N/A * If a parsing error occurs, the exception thrown will include
0N/A * information for locating the error in the original XML document.
0N/A *
0N/A * @param caller the <code>WebRowSet</code> object to be parsed, whose
0N/A * <code>xmlReader</code> field must contain a reference to
0N/A * this <code>XmlReader</code> object
0N/A * @param reader the <code>java.io.Reader</code> object from which
0N/A * the parser will get its input
0N/A * @exception SQLException if a database access error occurs or
0N/A * this <code>WebRowSetXmlReader</code> object is not the
0N/A * reader for the given rowset
0N/A * @see XmlReaderContentHandler
0N/A */
0N/A public void readXML(WebRowSet caller, java.io.Reader reader) throws SQLException {
0N/A try {
0N/A // Crimson Parser(as in J2SE 1.4.1 is NOT able to handle
0N/A // Reader(s)(FileReader).
0N/A //
0N/A // But getting the file as a Stream works fine. So we are going to take
0N/A // the reader but send it as a InputStream to the parser. Note that this
0N/A // functionality needs to work against any parser
0N/A // Crimson(J2SE 1.4.x) / Xerces(J2SE 1.5.x).
0N/A InputSource is = new InputSource(reader);
0N/A DefaultHandler dh = new XmlErrorHandler();
0N/A XmlReaderContentHandler hndr = new XmlReaderContentHandler((RowSet)caller);
0N/A SAXParserFactory factory = SAXParserFactory.newInstance();
0N/A factory.setNamespaceAware(true);
0N/A factory.setValidating(true);
0N/A SAXParser parser = factory.newSAXParser() ;
0N/A
0N/A parser.setProperty(
0N/A "http://java.sun.com/xml/jaxp/properties/schemaLanguage", "http://www.w3.org/2001/XMLSchema");
0N/A
0N/A XMLReader reader1 = parser.getXMLReader() ;
0N/A reader1.setEntityResolver(new XmlResolver());
0N/A reader1.setContentHandler(hndr);
0N/A
0N/A reader1.setErrorHandler(dh);
0N/A
0N/A reader1.parse(is);
0N/A
0N/A } catch (SAXParseException err) {
0N/A System.out.println (MessageFormat.format(resBundle.handleGetObject("wrsxmlreader.parseerr").toString(), new Object[]{ err.getMessage (), err.getLineNumber(), err.getSystemId()}));
0N/A err.printStackTrace();
0N/A throw new SQLException(err.getMessage());
0N/A
0N/A } catch (SAXException e) {
0N/A Exception x = e;
0N/A if (e.getException () != null)
0N/A x = e.getException();
0N/A x.printStackTrace ();
0N/A throw new SQLException(x.getMessage());
0N/A
0N/A }
0N/A
0N/A // Will be here if trying to write beyond the RowSet limits
0N/A
0N/A catch (ArrayIndexOutOfBoundsException aie) {
0N/A throw new SQLException(resBundle.handleGetObject("wrsxmlreader.invalidcp").toString());
0N/A }
0N/A catch (Throwable e) {
0N/A throw new SQLException(MessageFormat.format(resBundle.handleGetObject("wrsxmlreader.readxml").toString() , e.getMessage()));
0N/A }
0N/A
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Parses the given <code>WebRowSet</code> object, getting its input from
0N/A * the given <code>java.io.InputStream</code> object. The parser will send
0N/A * notifications of parse events to the rowset's
0N/A * <code>XmlReaderDocHandler</code>, which will build the rowset as
0N/A * an XML document.
0N/A * <P>
0N/A * Using streams is a much faster way than using <code>java.io.Reader</code>
0N/A * <P>
0N/A * This method is called internally by the method
0N/A * <code>WebRowSet.readXml</code>.
0N/A * <P>
0N/A * If a parsing error occurs, the exception thrown will include
0N/A * information for locating the error in the original XML document.
0N/A *
0N/A * @param caller the <code>WebRowSet</code> object to be parsed, whose
0N/A * <code>xmlReader</code> field must contain a reference to
0N/A * this <code>XmlReader</code> object
0N/A * @param iStream the <code>java.io.InputStream</code> object from which
0N/A * the parser will get its input
0N/A * @throws SQLException if a database access error occurs or
0N/A * this <code>WebRowSetXmlReader</code> object is not the
0N/A * reader for the given rowset
0N/A * @see XmlReaderContentHandler
0N/A */
0N/A public void readXML(WebRowSet caller, java.io.InputStream iStream) throws SQLException {
0N/A try {
0N/A InputSource is = new InputSource(iStream);
0N/A DefaultHandler dh = new XmlErrorHandler();
0N/A
0N/A XmlReaderContentHandler hndr = new XmlReaderContentHandler((RowSet)caller);
0N/A SAXParserFactory factory = SAXParserFactory.newInstance();
0N/A factory.setNamespaceAware(true);
0N/A factory.setValidating(true);
0N/A
0N/A SAXParser parser = factory.newSAXParser() ;
0N/A
0N/A parser.setProperty("http://java.sun.com/xml/jaxp/properties/schemaLanguage",
0N/A "http://www.w3.org/2001/XMLSchema");
0N/A
0N/A XMLReader reader1 = parser.getXMLReader() ;
0N/A reader1.setEntityResolver(new XmlResolver());
0N/A reader1.setContentHandler(hndr);
0N/A
0N/A reader1.setErrorHandler(dh);
0N/A
0N/A reader1.parse(is);
0N/A
0N/A } catch (SAXParseException err) {
0N/A System.out.println (MessageFormat.format(resBundle.handleGetObject("wrsxmlreader.parseerr").toString(), new Object[]{err.getLineNumber(), err.getSystemId() }));
0N/A System.out.println(" " + err.getMessage ());
0N/A err.printStackTrace();
0N/A throw new SQLException(err.getMessage());
0N/A
0N/A } catch (SAXException e) {
0N/A Exception x = e;
0N/A if (e.getException () != null)
0N/A x = e.getException();
0N/A x.printStackTrace ();
0N/A throw new SQLException(x.getMessage());
0N/A
0N/A }
0N/A
0N/A // Will be here if trying to write beyond the RowSet limits
0N/A
0N/A catch (ArrayIndexOutOfBoundsException aie) {
0N/A throw new SQLException(resBundle.handleGetObject("wrsxmlreader.invalidcp").toString());
0N/A }
0N/A
0N/A catch (Throwable e) {
0N/A throw new SQLException(MessageFormat.format(resBundle.handleGetObject("wrsxmlreader.readxml").toString() , e.getMessage()));
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * For code coverage purposes only right now
0N/A *
0N/A */
0N/A
0N/A public void readData(RowSetInternal caller) {
0N/A }
0N/A
2741N/A /**
2741N/A * This method re populates the resBundle
2741N/A * during the deserialization process
2741N/A *
2741N/A */
2741N/A private void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException {
2741N/A // Default state initialization happens here
2741N/A ois.defaultReadObject();
2741N/A // Initialization of transient Res Bundle happens here .
2741N/A try {
2741N/A resBundle = JdbcRowSetResourceBundle.getJdbcRowSetResourceBundle();
2741N/A } catch(IOException ioe) {
2741N/A throw new RuntimeException(ioe);
2741N/A }
2741N/A
2741N/A }
2741N/A
2741N/A static final long serialVersionUID = -9127058392819008014L;
0N/A}