summaryrefslogtreecommitdiff
path: root/src/java.sql.rowset/share/classes/com/sun/rowset/internal/WebRowSetXmlReader.java
blob: ae0239757470995d733fef89c4e75bcc6c9d63e9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/*
 * Copyright (c) 2003, 2026, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Oracle designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Oracle in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

package com.sun.rowset.internal;

import java.net.URL;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.security.PrivilegedActionException;
import java.security.PrivilegedExceptionAction;
import java.sql.*;
import javax.sql.*;
import java.io.*;

import org.xml.sax.*;
import org.xml.sax.helpers.*;

import javax.xml.XMLConstants;
import javax.xml.parsers.*;

import com.sun.rowset.*;
import java.text.MessageFormat;
import javax.sql.rowset.*;
import javax.sql.rowset.spi.*;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;

/**
 * An implementation of the <code>XmlReader</code> interface, which
 * reads and parses an XML formatted <code>WebRowSet</code> object.
 * This implementation uses an <code>org.xml.sax.Parser</code> object
 * as its parser.
 */
public class WebRowSetXmlReader implements XmlReader, Serializable {
    // standard schema
    static final String WEBROWSET_XSD = "webrowset.xsd";
    private static final String VALIDATION_PROPERTY = "jdk.sql.rowset.webrowsetValidation";

    // Error messages
    private static final String STD_SCHEMA_MESSAGE =
            "readXML : Cannot locate standard WebRowSet schema: %s.".formatted(WEBROWSET_XSD);


    private JdbcRowSetResourceBundle resBundle;

    public WebRowSetXmlReader(){
        try {
           resBundle = JdbcRowSetResourceBundle.getJdbcRowSetResourceBundle();
        } catch(IOException ioe) {
            throw new RuntimeException(ioe);
        }
    }

    /**
     * Parses the given <code>WebRowSet</code> object, getting its input from
     * the given <code>java.io.Reader</code> object.  The parser will send
     * notifications of parse events to the rowset's
     * <code>XmlReaderDocHandler</code>, which will build the rowset as
     * an XML document.
     * <P>
     * This method is called internally by the method
     * <code>WebRowSet.readXml</code>.
     * <P>
     * If a parsing error occurs, the exception thrown will include
     * information for locating the error in the original XML document.
     *
     * @param caller the <code>WebRowSet</code> object to be parsed, whose
     *        <code>xmlReader</code> field must contain a reference to
     *        this <code>XmlReader</code> object
     * @param reader the <code>java.io.Reader</code> object from which
     *        the parser will get its input
     * @exception SQLException if a database access error occurs or
     *            this <code>WebRowSetXmlReader</code> object is not the
     *            reader for the given rowset
     * @see XmlReaderContentHandler
     */
    public void readXML(WebRowSet caller, java.io.Reader reader) throws SQLException {
        readXml(caller, new InputSource(reader));
    }


    /**
     * Parses the given <code>WebRowSet</code> object, getting its input from
     * the given <code>java.io.InputStream</code> object.  The parser will send
     * notifications of parse events to the rowset's
     * <code>XmlReaderDocHandler</code>, which will build the rowset as
     * an XML document.
     * <P>
     * Using streams is a much faster way than using <code>java.io.Reader</code>
     * <P>
     * This method is called internally by the method
     * <code>WebRowSet.readXml</code>.
     * <P>
     * If a parsing error occurs, the exception thrown will include
     * information for locating the error in the original XML document.
     *
     * @param caller the <code>WebRowSet</code> object to be parsed, whose
     *        <code>xmlReader</code> field must contain a reference to
     *        this <code>XmlReader</code> object
     * @param iStream the <code>java.io.InputStream</code> object from which
     *        the parser will get its input
     * @throws SQLException if a database access error occurs or
     *            this <code>WebRowSetXmlReader</code> object is not the
     *            reader for the given rowset
     * @see XmlReaderContentHandler
     */
    public void readXML(WebRowSet caller, java.io.InputStream iStream) throws SQLException {
        readXml(caller, new InputSource(iStream));
    }

    /**
     * Parses the supplied XML input into the given {@code WebRowSet}.
     *
     * The parser validates the XML against the standard WebRowSet schema by default.
     * Validation can be disabled by setting {@systemProperty jdk.sql.rowset.webrowsetValidation}
     * to {@code false} so that the parser can read legacy WebRowSet XML.
     *
     * @param caller the rowset to populate
     * @param is the SAX input source to parse
     * @throws SQLException if parsing or rowset population fails
     */
    private void readXml(WebRowSet caller, InputSource is) throws SQLException {
        try {
            boolean validating = validationEnabled();

            SAXParserFactory spf = SAXParserFactory.newDefaultNSInstance();
            spf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
            if (validating) {
                spf.setSchema(getSchemaPrivileged());
            }

            XMLReader reader = spf.newSAXParser().getXMLReader();
            reader.setEntityResolver(new XmlResolver());
            reader.setContentHandler(new XmlReaderContentHandler(caller, validating));
            reader.setErrorHandler(new XmlErrorHandler());
            reader.parse(is);
        } catch (SAXException | ParserConfigurationException | IOException err) {
            throw new SQLException(MessageFormat.format(resBundle.handleGetObject("wrsxmlreader.readxml").toString(),
                err.getMessage()), err);
        }
    }

    /**
     * Returns the standard WebRowSet schema bundled in the
     * {@code java.sql.rowset} module.
     *
     * A new schema is created for each call so parser instances do not share
     * validator implementation state. This can be revisited once the bundled
     * schema implementation is safe to share.
     *
     * @return the WebRowSet schema
     * @throws SAXException if the schema cannot be located or parsed
     */
    private Schema getSchema() throws SAXException {
        URL stdSchema = WebRowSet.class.getResource(WEBROWSET_XSD);
        if (stdSchema == null) {
            throw new SAXException(STD_SCHEMA_MESSAGE);
        }

        SchemaFactory sf = SchemaFactory.newDefaultInstance();
        sf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
        return sf.newSchema(stdSchema);
    }

    @SuppressWarnings("removal")
    private Schema getSchemaPrivileged() throws SAXException {
        try {
            return AccessController.doPrivileged(
                    new PrivilegedExceptionAction<Schema>() {
                        @Override
                        public Schema run() throws SAXException {
                            return getSchema();
                        }
                    });
        } catch (PrivilegedActionException e) {
            throw (SAXException) e.getException();
        }
    }

    /**
     * Returns whether WebRowSet XML validation is enabled.
     *
     * Validation is enabled by default. It is disabled only when the
     * {@systemProperty jdk.sql.rowset.webrowsetValidation} is set to
     * {@code false}, case-insensitive.
     *
     * @return {@code true} if XML should be schema-validated
     */
    @SuppressWarnings("removal")
    private static boolean validationEnabled() {
        return !"false".equalsIgnoreCase(
                AccessController.doPrivileged(new PrivilegedAction<String>() {
                    @Override
                    public String run() {
                        return System.getProperty(VALIDATION_PROPERTY, "true");
                    }
                }).trim());
    }

    /**
     * For code coverage purposes only right now
     *
     */

    public void readData(RowSetInternal caller) {
    }

    /**
     * This method re populates the resBundle
     * during the deserialization process
     *
     */
    private void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException {
        // Default state initialization happens here
        ois.defaultReadObject();
        // Initialization of transient Res Bundle happens here .
        try {
           resBundle = JdbcRowSetResourceBundle.getJdbcRowSetResourceBundle();
        } catch(IOException ioe) {
            throw new RuntimeException(ioe);
        }

    }

    static final long serialVersionUID = -9127058392819008014L;
}