xincludetextreader.java

来自「JAVA 所有包」· Java 代码 · 共 514 行 · 第 1/2 页

JAVA
514
字号
/* * Copyright 2003-2005 The Apache Software Foundation. *  * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at *  *      http://www.apache.org/licenses/LICENSE-2.0 *  * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package com.sun.org.apache.xerces.internal.xinclude;import java.io.BufferedInputStream;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.Reader;import java.net.HttpURLConnection;import java.net.URL;import java.net.URLConnection;import java.util.Iterator;import java.util.Locale;import java.util.Map;import com.sun.org.apache.xerces.internal.impl.XMLEntityManager;import com.sun.org.apache.xerces.internal.impl.XMLErrorReporter;import com.sun.org.apache.xerces.internal.impl.io.ASCIIReader;import com.sun.org.apache.xerces.internal.impl.io.UTF8Reader;import com.sun.org.apache.xerces.internal.impl.msg.XMLMessageFormatter;import com.sun.org.apache.xerces.internal.util.EncodingMap;import com.sun.org.apache.xerces.internal.util.HTTPInputSource;import com.sun.org.apache.xerces.internal.util.MessageFormatter;import com.sun.org.apache.xerces.internal.util.XMLChar;import com.sun.org.apache.xerces.internal.xni.XMLString;import com.sun.org.apache.xerces.internal.xni.parser.XMLInputSource;/** * This class is used for reading resources requested in <include> elements, * when the parse attribute of the <include> element is "text".  Using this * class will open the location, detect the encoding, and discard the byte order * mark, if applicable. *  * REVISIT: * Much of the code in this class is taken from XMLEntityManager.  It would be nice * if this code could be shared in some way.  However, since XMLEntityManager is used * for reading files as XML, and this needs to read files as text, there would need * to be some refactoring done. *  * @author Michael Glavassevich, IBM * @author Peter McCracken, IBM * @author Ankit Pasricha, IBM * @author Arun Yadav, Sun Microsystems Inc. * * @version $Id: XIncludeTextReader.java,v 1.3 2005/09/26 13:03:04 sunithareddy Exp $ * * @see XIncludeHandler */public class XIncludeTextReader {    private Reader fReader;    private XIncludeHandler fHandler;    private XMLInputSource fSource;    private XMLErrorReporter fErrorReporter;    private XMLString fTempString = new XMLString();     /**     * Construct the XIncludeReader using the XMLInputSource and XIncludeHandler.     *     * @param source The XMLInputSource to use.     * @param handler The XIncludeHandler to use.     * @param bufferSize The size of this text reader's buffer.     */    public XIncludeTextReader(XMLInputSource source, XIncludeHandler handler, int bufferSize)        throws IOException {        fHandler = handler;        fSource = source;        fTempString = new XMLString(new char[bufferSize + 1], 0, 0);    }        /**     * Sets the XMLErrorReporter used for reporting errors while     * reading the text include.     *     * @param errorReporter the XMLErrorReporter to be used for     * reporting errors.     */    public void setErrorReporter(XMLErrorReporter errorReporter) {        fErrorReporter = errorReporter;    }    /**     * Return the Reader for given XMLInputSource.     *     * @param source The XMLInputSource to use.     */    protected Reader getReader(XMLInputSource source) throws IOException {        if (source.getCharacterStream() != null) {            return source.getCharacterStream();        }        else {            InputStream stream = null;            String encoding = source.getEncoding();            if (encoding == null) {                encoding = "UTF-8";            }            if (source.getByteStream() != null) {                stream = source.getByteStream();                // Wrap the InputStream so that it is possible to rewind it.                if (!(stream instanceof BufferedInputStream)) {                    stream = new BufferedInputStream(stream, fTempString.ch.length);                }            }            else {                String expandedSystemId = XMLEntityManager.expandSystemId(source.getSystemId(), source.getBaseSystemId(), false);                URL url = new URL(expandedSystemId);                URLConnection urlCon = url.openConnection();                                // If this is an HTTP connection attach any request properties to the request.                if (urlCon instanceof HttpURLConnection && source instanceof HTTPInputSource) {                    final HttpURLConnection urlConnection = (HttpURLConnection) urlCon;                    final HTTPInputSource httpInputSource = (HTTPInputSource) source;                                        // set request properties                    Iterator propIter = httpInputSource.getHTTPRequestProperties();                    while (propIter.hasNext()) {                        Map.Entry entry = (Map.Entry) propIter.next();                        urlConnection.setRequestProperty((String) entry.getKey(), (String) entry.getValue());                    }                                        // set preference for redirection                    boolean followRedirects = httpInputSource.getFollowHTTPRedirects();                    if (!followRedirects) {                        XMLEntityManager.setInstanceFollowRedirects(urlConnection, followRedirects);                    }                }                                // Wrap the InputStream so that it is possible to rewind it.                stream = new BufferedInputStream(urlCon.getInputStream());                                // content type will be string like "text/xml; charset=UTF-8" or "text/xml"                String rawContentType = urlCon.getContentType();                                // text/xml and application/xml offer only one optional parameter                int index = (rawContentType != null) ? rawContentType.indexOf(';') : -1;                String contentType = null;                String charset = null;                if (index != -1) {                    // this should be something like "text/xml"                    contentType = rawContentType.substring(0, index).trim();                    // this should be something like "charset=UTF-8", but we want to                    // strip it down to just "UTF-8"                    charset = rawContentType.substring(index + 1).trim();                    if (charset.startsWith("charset=")) {                        // 8 is the length of "charset="                        charset = charset.substring(8).trim();                        // strip quotes, if present                        if ((charset.charAt(0) == '"'                            && charset.charAt(charset.length() - 1) == '"')                            || (charset.charAt(0) == '\''                                && charset.charAt(charset.length() - 1)                                    == '\'')) {                            charset =                                charset.substring(1, charset.length() - 1);                        }                    }                    else {                        charset = null;                    }                }                else {                    contentType = rawContentType.trim();                }                String detectedEncoding = null;                /**  The encoding of such a resource is determined by:                    1 external encoding information, if available, otherwise                         -- the most common type of external information is the "charset" parameter of a MIME package                    2 if the media type of the resource is text/xml, application/xml, or matches the conventions text/*+xml or application/*+xml as described in XML Media Types [IETF RFC 3023], the encoding is recognized as specified in XML 1.0, otherwise                    3 the value of the encoding attribute if one exists, otherwise                    4 UTF-8.                 **/                if (contentType.equals("text/xml")) {                    if (charset != null) {                        detectedEncoding = charset;                    }                    else {                        // see RFC2376 or 3023, section 3.1                        detectedEncoding = "US-ASCII";                    }                }                else if (contentType.equals("application/xml")) {                    if (charset != null) {                        detectedEncoding = charset;                    }                    else {                        // see RFC2376 or 3023, section 3.2                        detectedEncoding = getEncodingName(stream);                    }                }                else if (contentType.endsWith("+xml")) {                    detectedEncoding = getEncodingName(stream);                }                if (detectedEncoding != null) {                    encoding = detectedEncoding;                }                // else 3 or 4.            }                        encoding = encoding.toUpperCase(Locale.ENGLISH);                        // eat the Byte Order Mark            encoding = consumeBOM(stream, encoding);                        // If the document is UTF-8 or US-ASCII use             // the Xerces readers for these encodings. For            // US-ASCII consult the encoding map since            // this encoding has many aliases.            if (encoding.equals("UTF-8")) {                return new UTF8Reader(stream,                     fTempString.ch.length,                     fErrorReporter.getMessageFormatter(XMLMessageFormatter.XML_DOMAIN),                     fErrorReporter.getLocale() );            }                        // Try to use a Java reader.            String javaEncoding = EncodingMap.getIANA2JavaMapping(encoding);                        // If the specified encoding wasn't a recognized IANA encoding throw an IOException.            // The XIncludeHandler will report this as a ResourceError and then will            // attempt to include a fallback if there is one.            if (javaEncoding == null) {                MessageFormatter aFormatter =                     fErrorReporter.getMessageFormatter(XMLMessageFormatter.XML_DOMAIN);                Locale aLocale = fErrorReporter.getLocale();                throw new IOException( aFormatter.formatMessage( aLocale,                     "EncodingDeclInvalid",                     new Object[] {encoding} ) );            }            else if (javaEncoding.equals("ASCII")) {                return new ASCIIReader(stream,                    fTempString.ch.length,                    fErrorReporter.getMessageFormatter(XMLMessageFormatter.XML_DOMAIN),                     fErrorReporter.getLocale() );            }                        return new InputStreamReader(stream, javaEncoding);        }

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?