xincludetextreader.java
来自「JAVA的一些源码 JAVA2 STANDARD EDITION DEVELO」· Java 代码 · 共 524 行 · 第 1/2 页
JAVA
524 行
/* * The Apache Software License, Version 1.1 * * * Copyright (c) 2001-2004 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The end-user documentation included with the redistribution, * if any, must include the following acknowledgment: * "This product includes software developed by the * Apache Software Foundation (http://www.apache.org/)." * Alternately, this acknowledgment may appear in the software itself, * if and wherever such third-party acknowledgments normally appear. * * 4. The names "Xerces" and "Apache Software Foundation" must * not be used to endorse or promote products derived from this * software without prior written permission. For written * permission, please contact apache@apache.org. * * 5. Products derived from this software may not be called "Apache", * nor may "Apache" appear in their name, without prior written * permission of the Apache Software Foundation. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of the Apache Software Foundation and was * originally based on software copyright (c) 2003, International * Business Machines, Inc., http://www.apache.org. For more * information on the Apache Software Foundation, please see * <http://www.apache.org/>. */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.Locale;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.impl.XMLEntityManager;import com.sun.org.apache.xerces.internal.impl.XMLErrorReporter;import com.sun.org.apache.xerces.internal.util.EncodingMap;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.util.XMLStringBuffer;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 Arun Yadav, Sun Microsystems Inc. * * @version $Id: XIncludeTextReader.java,v 1.10 2004/04/15 04:51:56 mrglavas Exp $ * * @see XIncludeHandler */public class XIncludeTextReader { private Reader fReader; private XIncludeHandler fHandler; private XMLInputSource fSource; private XMLErrorReporter fErrorReporter; // Content negotation parameters private String fAccept; private String fAcceptLanguage; /** * Construct the XIncludeReader using the XMLInputSource and XIncludeHandler. * * @param source The XMLInputSource to use. * @param handler The XIncludeHandler to use. */ public XIncludeTextReader(XMLInputSource source, XIncludeHandler handler) throws IOException { fHandler = handler; fSource = source; } /** * 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; } /** * Sets content negotation parameters to be attached to an HTTP request. * * @param accept the Accept HTTP request property * @param acceptLanguage the Accept-Language HTTP request property */ public void setHttpProperties(String accept, String acceptLanguage) { fAccept = accept; fAcceptLanguage = acceptLanguage; } /** * 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); } } 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 // content negotation parameters to the request. if (urlCon instanceof HttpURLConnection) { if( fAccept != null && fAccept.length() > 0) { urlCon.setRequestProperty(XIncludeHandler.HTTP_ACCEPT, fAccept); } if( fAcceptLanguage != null && fAcceptLanguage.length() > 0) { urlCon.setRequestProperty(XIncludeHandler.HTTP_ACCEPT_LANGUAGE, fAcceptLanguage); } } // 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);
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?