CarrotChat v2.8.60.240131版本的 MD5 值为:2baf8b9f31830b86b5c5a85ae6b47e05

以下内容为反编译后的 SAXReader.java 源代码,内容仅作参考


package org.dom4j.io;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.Reader;
import java.io.Serializable;
import java.net.URL;
import org.apache.commons.httpclient.cookie.CookieSpec;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentFactory;
import org.dom4j.ElementHandler;
import org.xml.sax.EntityResolver;
import org.xml.sax.ErrorHandler;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.XMLFilter;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.helpers.XMLReaderFactory;
public class SAXReader {
    private static final String SAX_DECL_HANDLER = "http://xml.org/sax/properties/declaration-handler";
    private static final String SAX_LEXICALHANDLER = "http://xml.org/sax/handlers/LexicalHandler";
    private static final String SAX_LEXICAL_HANDLER = "http://xml.org/sax/properties/lexical-handler";
    private static final String SAX_NAMESPACES = "http://xml.org/sax/features/namespaces";
    private static final String SAX_NAMESPACE_PREFIXES = "http://xml.org/sax/features/namespace-prefixes";
    private static final String SAX_STRING_INTERNING = "http://xml.org/sax/features/string-interning";
    private DispatchHandler dispatchHandler;
    private EntityResolver entityResolver;
    private ErrorHandler errorHandler;
    private DocumentFactory factory;
    private boolean validating;
    private XMLFilter xmlFilter;
    private XMLReader xmlReader;
    private boolean stringInternEnabled = true;
    private boolean includeInternalDTDDeclarations = false;
    private boolean includeExternalDTDDeclarations = false;
    private boolean mergeAdjacentText = false;
    private boolean stripWhitespaceText = false;
    private boolean ignoreComments = false;
    private String encoding = null;

    public static class SAXEntityResolver implements EntityResolver, Serializable {
        protected String uriPrefix;

        public SAXEntityResolver(String str) {
            this.uriPrefix = str;
        }

        @Override
        public InputSource resolveEntity(String str, String str2) {
            if (str2 != null && str2.length() > 0 && this.uriPrefix != null && str2.indexOf(58) <= 0) {
                StringBuffer stringBuffer = new StringBuffer();
                stringBuffer.append(this.uriPrefix);
                stringBuffer.append(str2);
                str2 = stringBuffer.toString();
            }
            return new InputSource(str2);
        }
    }

    public SAXReader() {
    }

    public void addHandler(String str, ElementHandler elementHandler) {
        getDispatchHandler().addHandler(str, elementHandler);
    }

    protected void configureReader(XMLReader xMLReader, DefaultHandler defaultHandler) throws DocumentException {
        SAXHelper.setParserProperty(xMLReader, SAX_LEXICALHANDLER, defaultHandler);
        SAXHelper.setParserProperty(xMLReader, SAX_LEXICAL_HANDLER, defaultHandler);
        if (this.includeInternalDTDDeclarations || this.includeExternalDTDDeclarations) {
            SAXHelper.setParserProperty(xMLReader, SAX_DECL_HANDLER, defaultHandler);
        }
        SAXHelper.setParserFeature(xMLReader, SAX_NAMESPACES, true);
        SAXHelper.setParserFeature(xMLReader, SAX_NAMESPACE_PREFIXES, false);
        SAXHelper.setParserFeature(xMLReader, SAX_STRING_INTERNING, isStringInternEnabled());
        SAXHelper.setParserFeature(xMLReader, "http://xml.org/sax/features/use-locator2", true);
        try {
            xMLReader.setFeature("http://xml.org/sax/features/validation", isValidating());
            ErrorHandler errorHandler = this.errorHandler;
            if (errorHandler != null) {
                xMLReader.setErrorHandler(errorHandler);
            } else {
                xMLReader.setErrorHandler(defaultHandler);
            }
        } catch (Exception e2) {
            if (isValidating()) {
                StringBuffer stringBuffer = new StringBuffer();
                stringBuffer.append("Validation not supported for XMLReader: ");
                stringBuffer.append(xMLReader);
                throw new DocumentException(stringBuffer.toString(), e2);
            }
        }
    }

    protected SAXContentHandler createContentHandler(XMLReader xMLReader) {
        return new SAXContentHandler(getDocumentFactory(), this.dispatchHandler);
    }

    protected EntityResolver createDefaultEntityResolver(String str) {
        int lastIndexOf;
        return new SAXEntityResolver((str == null || str.length() <= 0 || (lastIndexOf = str.lastIndexOf(47)) <= 0) ? null : str.substring(0, lastIndexOf + 1));
    }

    protected XMLReader createXMLReader() throws SAXException {
        return SAXHelper.createXMLReader(isValidating());
    }

    public DispatchHandler getDispatchHandler() {
        if (this.dispatchHandler == null) {
            this.dispatchHandler = new DispatchHandler();
        }
        return this.dispatchHandler;
    }

    public DocumentFactory getDocumentFactory() {
        if (this.factory == null) {
            this.factory = DocumentFactory.getInstance();
        }
        return this.factory;
    }

    public String getEncoding() {
        return this.encoding;
    }

    public EntityResolver getEntityResolver() {
        return this.entityResolver;
    }

    public ErrorHandler getErrorHandler() {
        return this.errorHandler;
    }

    public XMLFilter getXMLFilter() {
        return this.xmlFilter;
    }

    public XMLReader getXMLReader() throws SAXException {
        if (this.xmlReader == null) {
            this.xmlReader = createXMLReader();
        }
        return this.xmlReader;
    }

    protected XMLReader installXMLFilter(XMLReader xMLReader) {
        XMLFilter xMLFilter = getXMLFilter();
        if (xMLFilter == null) {
            return xMLReader;
        }
        XMLFilter xMLFilter2 = xMLFilter;
        while (true) {
            XMLReader parent = xMLFilter2.getParent();
            if (parent instanceof XMLFilter) {
                xMLFilter2 = (XMLFilter) parent;
            } else {
                xMLFilter2.setParent(xMLReader);
                return xMLFilter;
            }
        }
    }

    public boolean isIgnoreComments() {
        return this.ignoreComments;
    }

    public boolean isIncludeExternalDTDDeclarations() {
        return this.includeExternalDTDDeclarations;
    }

    public boolean isIncludeInternalDTDDeclarations() {
        return this.includeInternalDTDDeclarations;
    }

    public boolean isMergeAdjacentText() {
        return this.mergeAdjacentText;
    }

    public boolean isStringInternEnabled() {
        return this.stringInternEnabled;
    }

    public boolean isStripWhitespaceText() {
        return this.stripWhitespaceText;
    }

    public boolean isValidating() {
        return this.validating;
    }

    public Document read(File file) throws DocumentException {
        try {
            InputSource inputSource = new InputSource(new FileInputStream(file));
            String str = this.encoding;
            if (str != null) {
                inputSource.setEncoding(str);
            }
            String absolutePath = file.getAbsolutePath();
            if (absolutePath != null) {
                StringBuffer stringBuffer = new StringBuffer("file://");
                if (!absolutePath.startsWith(File.separator)) {
                    stringBuffer.append(CookieSpec.PATH_DELIM);
                }
                stringBuffer.append(absolutePath.replace('\\', '/'));
                inputSource.setSystemId(stringBuffer.toString());
            }
            return read(inputSource);
        } catch (FileNotFoundException e2) {
            throw new DocumentException(e2.getMessage(), e2);
        }
    }

    public void removeHandler(String str) {
        getDispatchHandler().removeHandler(str);
    }

    public void resetHandlers() {
        getDispatchHandler().resetHandlers();
    }

    public void setDefaultHandler(ElementHandler elementHandler) {
        getDispatchHandler().setDefaultHandler(elementHandler);
    }

    public void setDispatchHandler(DispatchHandler dispatchHandler) {
        this.dispatchHandler = dispatchHandler;
    }

    public void setDocumentFactory(DocumentFactory documentFactory) {
        this.factory = documentFactory;
    }

    public void setEncoding(String str) {
        this.encoding = str;
    }

    public void setEntityResolver(EntityResolver entityResolver) {
        this.entityResolver = entityResolver;
    }

    public void setErrorHandler(ErrorHandler errorHandler) {
        this.errorHandler = errorHandler;
    }

    public void setFeature(String str, boolean z) throws SAXException {
        getXMLReader().setFeature(str, z);
    }

    public void setIgnoreComments(boolean z) {
        this.ignoreComments = z;
    }

    public void setIncludeExternalDTDDeclarations(boolean z) {
        this.includeExternalDTDDeclarations = z;
    }

    public void setIncludeInternalDTDDeclarations(boolean z) {
        this.includeInternalDTDDeclarations = z;
    }

    public void setMergeAdjacentText(boolean z) {
        this.mergeAdjacentText = z;
    }

    public void setProperty(String str, Object obj) throws SAXException {
        getXMLReader().setProperty(str, obj);
    }

    public void setStringInternEnabled(boolean z) {
        this.stringInternEnabled = z;
    }

    public void setStripWhitespaceText(boolean z) {
        this.stripWhitespaceText = z;
    }

    public void setValidation(boolean z) {
        this.validating = z;
    }

    public void setXMLFilter(XMLFilter xMLFilter) {
        this.xmlFilter = xMLFilter;
    }

    public void setXMLReader(XMLReader xMLReader) {
        this.xmlReader = xMLReader;
    }

    public void setXMLReaderClassName(String str) throws SAXException {
        setXMLReader(XMLReaderFactory.createXMLReader(str));
    }

    public SAXReader(boolean z) {
        this.validating = z;
    }

    public Document read(URL url) throws DocumentException {
        InputSource inputSource = new InputSource(url.toExternalForm());
        String str = this.encoding;
        if (str != null) {
            inputSource.setEncoding(str);
        }
        return read(inputSource);
    }

    public SAXReader(DocumentFactory documentFactory) {
        this.factory = documentFactory;
    }

    public Document read(String str) throws DocumentException {
        InputSource inputSource = new InputSource(str);
        String str2 = this.encoding;
        if (str2 != null) {
            inputSource.setEncoding(str2);
        }
        return read(inputSource);
    }

    public Document read(InputStream inputStream) throws DocumentException {
        InputSource inputSource = new InputSource(inputStream);
        String str = this.encoding;
        if (str != null) {
            inputSource.setEncoding(str);
        }
        return read(inputSource);
    }

    public Document read(Reader reader) throws DocumentException {
        InputSource inputSource = new InputSource(reader);
        String str = this.encoding;
        if (str != null) {
            inputSource.setEncoding(str);
        }
        return read(inputSource);
    }

    public SAXReader(DocumentFactory documentFactory, boolean z) {
        this.factory = documentFactory;
        this.validating = z;
    }

    public Document read(InputStream inputStream, String str) throws DocumentException {
        InputSource inputSource = new InputSource(inputStream);
        inputSource.setSystemId(str);
        String str2 = this.encoding;
        if (str2 != null) {
            inputSource.setEncoding(str2);
        }
        return read(inputSource);
    }

    public Document read(Reader reader, String str) throws DocumentException {
        InputSource inputSource = new InputSource(reader);
        inputSource.setSystemId(str);
        String str2 = this.encoding;
        if (str2 != null) {
            inputSource.setEncoding(str2);
        }
        return read(inputSource);
    }

    public SAXReader(XMLReader xMLReader) {
        this.xmlReader = xMLReader;
    }

    public Document read(InputSource inputSource) throws DocumentException {
        try {
            XMLReader installXMLFilter = installXMLFilter(getXMLReader());
            EntityResolver entityResolver = this.entityResolver;
            if (entityResolver == null) {
                entityResolver = createDefaultEntityResolver(inputSource.getSystemId());
                this.entityResolver = entityResolver;
            }
            installXMLFilter.setEntityResolver(entityResolver);
            SAXContentHandler createContentHandler = createContentHandler(installXMLFilter);
            createContentHandler.setEntityResolver(entityResolver);
            createContentHandler.setInputSource(inputSource);
            boolean isIncludeInternalDTDDeclarations = isIncludeInternalDTDDeclarations();
            boolean isIncludeExternalDTDDeclarations = isIncludeExternalDTDDeclarations();
            createContentHandler.setIncludeInternalDTDDeclarations(isIncludeInternalDTDDeclarations);
            createContentHandler.setIncludeExternalDTDDeclarations(isIncludeExternalDTDDeclarations);
            createContentHandler.setMergeAdjacentText(isMergeAdjacentText());
            createContentHandler.setStripWhitespaceText(isStripWhitespaceText());
            createContentHandler.setIgnoreComments(isIgnoreComments());
            installXMLFilter.setContentHandler(createContentHandler);
            configureReader(installXMLFilter, createContentHandler);
            installXMLFilter.parse(inputSource);
            return createContentHandler.getDocument();
        } catch (Exception e2) {
            if (e2 instanceof SAXParseException) {
                SAXParseException sAXParseException = (SAXParseException) e2;
                String systemId = sAXParseException.getSystemId();
                if (systemId == null) {
                    systemId = "";
                }
                StringBuffer stringBuffer = new StringBuffer();
                stringBuffer.append("Error on line ");
                stringBuffer.append(sAXParseException.getLineNumber());
                stringBuffer.append(" of document ");
                stringBuffer.append(systemId);
                stringBuffer.append(" : ");
                stringBuffer.append(sAXParseException.getMessage());
                throw new DocumentException(stringBuffer.toString(), e2);
            }
            throw new DocumentException(e2.getMessage(), e2);
        }
    }

    public SAXReader(XMLReader xMLReader, boolean z) {
        this.xmlReader = xMLReader;
        this.validating = z;
    }

    public SAXReader(String str) throws SAXException {
        if (str != null) {
            this.xmlReader = XMLReaderFactory.createXMLReader(str);
        }
    }

    public SAXReader(String str, boolean z) throws SAXException {
        if (str != null) {
            this.xmlReader = XMLReaderFactory.createXMLReader(str);
        }
        this.validating = z;
    }
}