随身WiFi小助手 v1.1.1版本的 MD5 值为:b7707ff0ac20cdf6109b08a72f633bc9

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


package org.mozilla.javascript.xmlimpl;

import androidx.activity.d;
import androidx.emoji2.text.flatbuffer.a;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.Serializable;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.LinkedBlockingDeque;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.mozilla.javascript.Context;
import org.mozilla.javascript.ScriptRuntime;
import org.w3c.dom.Attr;
import org.w3c.dom.Comment;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.ProcessingInstruction;
import org.w3c.dom.Text;
import org.xml.sax.ErrorHandler;
import org.xml.sax.InputSource;
import org.xml.sax.SAXParseException;
public class XmlProcessor implements Serializable {
    private static final String ACCESS_EXTERNAL_DTD = "http://javax.xml.XMLConstants/property/accessExternalDTD";
    private static final String ACCESS_EXTERNAL_STYLESHEET = "http://javax.xml.XMLConstants/property/accessExternalStylesheet";
    private static final long serialVersionUID = 6903514433204808713L;
    private transient LinkedBlockingDeque<DocumentBuilder> documentBuilderPool;
    private transient DocumentBuilderFactory dom;
    private RhinoSAXErrorHandler errorHandler = new RhinoSAXErrorHandler();
    private boolean ignoreComments;
    private boolean ignoreProcessingInstructions;
    private boolean ignoreWhitespace;
    private int prettyIndent;
    private boolean prettyPrint;
    private transient TransformerFactory xform;

    public static class RhinoSAXErrorHandler implements ErrorHandler, Serializable {
        private static final long serialVersionUID = 6918417235413084055L;

        private RhinoSAXErrorHandler() {
        }

        private void throwError(SAXParseException sAXParseException) {
            throw ScriptRuntime.constructError("TypeError", sAXParseException.getMessage(), sAXParseException.getLineNumber() - 1);
        }

        @Override
        public void error(SAXParseException sAXParseException) {
            throwError(sAXParseException);
        }

        @Override
        public void fatalError(SAXParseException sAXParseException) {
            throwError(sAXParseException);
        }

        @Override
        public void warning(SAXParseException sAXParseException) {
            Context.reportWarning(sAXParseException.getMessage());
        }
    }

    public XmlProcessor() {
        setDefault();
        DocumentBuilderFactory newInstance = DocumentBuilderFactory.newInstance();
        this.dom = newInstance;
        newInstance.setNamespaceAware(true);
        this.dom.setIgnoringComments(false);
        this.xform = TransformerFactory.newInstance();
        Context currentContext = Context.getCurrentContext();
        if (currentContext == null || currentContext.hasFeature(20)) {
            configureSecureDBF(this.dom);
            configureSecureTF(this.xform);
        }
        this.documentBuilderPool = new LinkedBlockingDeque<>(Runtime.getRuntime().availableProcessors() * 2);
    }

    private void addCommentsTo(List<Node> list, Node node) {
        if (node instanceof Comment) {
            list.add(node);
        }
        if (node.getChildNodes() != null) {
            for (int i7 = 0; i7 < node.getChildNodes().getLength(); i7++) {
                addProcessingInstructionsTo(list, node.getChildNodes().item(i7));
            }
        }
    }

    private void addProcessingInstructionsTo(List<Node> list, Node node) {
        if (node instanceof ProcessingInstruction) {
            list.add(node);
        }
        if (node.getChildNodes() != null) {
            for (int i7 = 0; i7 < node.getChildNodes().getLength(); i7++) {
                addProcessingInstructionsTo(list, node.getChildNodes().item(i7));
            }
        }
    }

    private void addTextNodesToRemoveAndTrim(List<Node> list, Node node) {
        if (node instanceof Text) {
            Text text = (Text) node;
            text.setData(text.getData().trim());
            if (text.getData().length() == 0) {
                list.add(node);
            }
        }
        if (node.getChildNodes() != null) {
            for (int i7 = 0; i7 < node.getChildNodes().getLength(); i7++) {
                addTextNodesToRemoveAndTrim(list, node.getChildNodes().item(i7));
            }
        }
    }

    private void beautifyElement(Element element, int i7) {
        StringBuilder b8 = a.b('\n');
        for (int i8 = 0; i8 < i7; i8++) {
            b8.append(' ');
        }
        String sb = b8.toString();
        for (int i9 = 0; i9 < this.prettyIndent; i9++) {
            b8.append(' ');
        }
        String sb2 = b8.toString();
        ArrayList arrayList = new ArrayList();
        boolean z7 = false;
        for (int i10 = 0; i10 < element.getChildNodes().getLength(); i10++) {
            if (i10 == 1) {
                z7 = true;
            }
            if (element.getChildNodes().item(i10) instanceof Text) {
                arrayList.add(element.getChildNodes().item(i10));
            } else {
                arrayList.add(element.getChildNodes().item(i10));
                z7 = true;
            }
        }
        if (z7) {
            for (int i11 = 0; i11 < arrayList.size(); i11++) {
                element.insertBefore(element.getOwnerDocument().createTextNode(sb2), (Node) arrayList.get(i11));
            }
        }
        NodeList childNodes = element.getChildNodes();
        ArrayList arrayList2 = new ArrayList();
        for (int i12 = 0; i12 < childNodes.getLength(); i12++) {
            if (childNodes.item(i12) instanceof Element) {
                arrayList2.add((Element) childNodes.item(i12));
            }
        }
        Iterator it = arrayList2.iterator();
        while (it.hasNext()) {
            beautifyElement((Element) it.next(), this.prettyIndent + i7);
        }
        if (z7) {
            element.appendChild(element.getOwnerDocument().createTextNode(sb));
        }
    }

    private void configureSecureDBF(DocumentBuilderFactory documentBuilderFactory) {
        try {
            documentBuilderFactory.setFeature("http://javax.xml.XMLConstants/feature/secure-processing", true);
            documentBuilderFactory.setXIncludeAware(false);
            try {
                documentBuilderFactory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
            } catch (ParserConfigurationException unused) {
            }
            try {
                documentBuilderFactory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
            } catch (ParserConfigurationException unused2) {
            }
        } catch (ParserConfigurationException e7) {
            throw new RuntimeException("XML parser (DocumentBuilderFactory) cannot be securely configured.", e7);
        }
    }

    private void configureSecureTF(TransformerFactory transformerFactory) {
        try {
            transformerFactory.setFeature("http://javax.xml.XMLConstants/feature/secure-processing", true);
            try {
                transformerFactory.setAttribute(ACCESS_EXTERNAL_DTD, "");
            } catch (IllegalArgumentException unused) {
            }
            try {
                transformerFactory.setAttribute(ACCESS_EXTERNAL_STYLESHEET, "");
            } catch (IllegalArgumentException unused2) {
            }
        } catch (TransformerConfigurationException e7) {
            throw new RuntimeException("XML parser (TransformerFactory) cannot be securely configured.", e7);
        }
    }

    private String elementToXmlString(Element element) {
        Element element2 = (Element) element.cloneNode(true);
        if (this.prettyPrint) {
            beautifyElement(element2, 0);
        }
        return toString(element2);
    }

    private String escapeElementValue(String str) {
        return escapeTextValue(str);
    }

    private DocumentBuilder getDocumentBuilderFromPool() {
        DocumentBuilder pollFirst = this.documentBuilderPool.pollFirst();
        if (pollFirst == null) {
            pollFirst = getDomFactory().newDocumentBuilder();
        }
        pollFirst.setErrorHandler(this.errorHandler);
        return pollFirst;
    }

    private DocumentBuilderFactory getDomFactory() {
        return this.dom;
    }

    private void readObject(ObjectInputStream objectInputStream) {
        objectInputStream.defaultReadObject();
        DocumentBuilderFactory newInstance = DocumentBuilderFactory.newInstance();
        this.dom = newInstance;
        newInstance.setNamespaceAware(true);
        this.dom.setIgnoringComments(false);
        this.xform = TransformerFactory.newInstance();
        Context currentContext = Context.getCurrentContext();
        if (currentContext == null || currentContext.hasFeature(20)) {
            configureSecureDBF(this.dom);
            configureSecureTF(this.xform);
        }
        this.documentBuilderPool = new LinkedBlockingDeque<>(Runtime.getRuntime().availableProcessors() * 2);
    }

    private void returnDocumentBuilderToPool(DocumentBuilder documentBuilder) {
        try {
            documentBuilder.reset();
            if (documentBuilder.isNamespaceAware()) {
                this.documentBuilderPool.offerFirst(documentBuilder);
            }
        } catch (UnsupportedOperationException unused) {
        }
    }

    private String toString(Node node) {
        DOMSource dOMSource = new DOMSource(node);
        StringWriter stringWriter = new StringWriter();
        StreamResult streamResult = new StreamResult(stringWriter);
        try {
            Transformer newTransformer = this.xform.newTransformer();
            newTransformer.setOutputProperty("omit-xml-declaration", "yes");
            newTransformer.setOutputProperty("indent", "no");
            newTransformer.setOutputProperty("method", "xml");
            newTransformer.transform(dOMSource, streamResult);
            return toXmlNewlines(stringWriter.toString());
        } catch (TransformerConfigurationException e7) {
            throw new RuntimeException(e7);
        } catch (TransformerException e8) {
            throw new RuntimeException(e8);
        }
    }

    private String toXmlNewlines(String str) {
        StringBuilder sb = new StringBuilder(str.length());
        for (int i7 = 0; i7 < str.length(); i7++) {
            char charAt = str.charAt(i7);
            if (charAt != '\r') {
                sb.append(charAt);
            } else if (str.charAt(i7 + 1) != '\n') {
                sb.append('\n');
            }
        }
        return sb.toString();
    }

    public final String ecmaToXmlString(Node node) {
        String elementToXmlString;
        StringBuilder d8;
        String str;
        StringBuilder sb = new StringBuilder();
        if (node instanceof Text) {
            String data = ((Text) node).getData();
            if (this.prettyPrint) {
                data = data.trim();
            }
            elementToXmlString = escapeElementValue(data);
        } else if (node instanceof Attr) {
            elementToXmlString = escapeAttributeValue(((Attr) node).getValue());
        } else {
            if (node instanceof Comment) {
                d8 = d.d("<!--");
                d8.append(((Comment) node).getNodeValue());
                str = "-->";
            } else if (node instanceof ProcessingInstruction) {
                ProcessingInstruction processingInstruction = (ProcessingInstruction) node;
                d8 = d.d("<?");
                d8.append(processingInstruction.getTarget());
                d8.append(" ");
                d8.append(processingInstruction.getData());
                str = "?>";
            } else {
                elementToXmlString = elementToXmlString((Element) node);
            }
            d8.append(str);
            elementToXmlString = d8.toString();
        }
        sb.append(elementToXmlString);
        return sb.toString();
    }

    public String escapeAttributeValue(Object obj) {
        String scriptRuntime = ScriptRuntime.toString(obj);
        if (scriptRuntime.length() == 0) {
            return "";
        }
        Element createElement = newDocument().createElement("a");
        createElement.setAttribute("b", scriptRuntime);
        String xmlProcessor = toString(createElement);
        return xmlProcessor.substring(xmlProcessor.indexOf(34) + 1, xmlProcessor.lastIndexOf(34));
    }

    public String escapeTextValue(Object obj) {
        if (obj instanceof XMLObjectImpl) {
            return ((XMLObjectImpl) obj).toXMLString();
        }
        String scriptRuntime = ScriptRuntime.toString(obj);
        if (scriptRuntime.length() == 0) {
            return scriptRuntime;
        }
        Element createElement = newDocument().createElement("a");
        createElement.setTextContent(scriptRuntime);
        String xmlProcessor = toString(createElement);
        int indexOf = xmlProcessor.indexOf(62) + 1;
        int lastIndexOf = xmlProcessor.lastIndexOf(60);
        return indexOf < lastIndexOf ? xmlProcessor.substring(indexOf, lastIndexOf) : "";
    }

    public final int getPrettyIndent() {
        return this.prettyIndent;
    }

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

    public final boolean isIgnoreProcessingInstructions() {
        return this.ignoreProcessingInstructions;
    }

    public final boolean isIgnoreWhitespace() {
        return this.ignoreWhitespace;
    }

    public final boolean isPrettyPrinting() {
        return this.prettyPrint;
    }

    public Document newDocument() {
        DocumentBuilder documentBuilder = null;
        try {
            try {
                documentBuilder = getDocumentBuilderFromPool();
                Document newDocument = documentBuilder.newDocument();
                returnDocumentBuilderToPool(documentBuilder);
                return newDocument;
            } catch (ParserConfigurationException e7) {
                throw new RuntimeException(e7);
            }
        } catch (Throwable th) {
            if (documentBuilder != null) {
                returnDocumentBuilderToPool(documentBuilder);
            }
            throw th;
        }
    }

    public final void setDefault() {
        setIgnoreComments(true);
        setIgnoreProcessingInstructions(true);
        setIgnoreWhitespace(true);
        setPrettyPrinting(true);
        setPrettyIndent(2);
    }

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

    public final void setIgnoreProcessingInstructions(boolean z7) {
        this.ignoreProcessingInstructions = z7;
    }

    public final void setIgnoreWhitespace(boolean z7) {
        this.ignoreWhitespace = z7;
    }

    public final void setPrettyIndent(int i7) {
        this.prettyIndent = i7;
    }

    public final void setPrettyPrinting(boolean z7) {
        this.prettyPrint = z7;
    }

    public final Node toXml(String str, String str2) {
        try {
            try {
                try {
                    DocumentBuilder documentBuilderFromPool = getDocumentBuilderFromPool();
                    Document parse = documentBuilderFromPool.parse(new InputSource(new StringReader("<parent xmlns=\"" + str + "\">" + str2 + "</parent>")));
                    if (this.ignoreProcessingInstructions) {
                        ArrayList arrayList = new ArrayList();
                        addProcessingInstructionsTo(arrayList, parse);
                        Iterator it = arrayList.iterator();
                        while (it.hasNext()) {
                            Node node = (Node) it.next();
                            node.getParentNode().removeChild(node);
                        }
                    }
                    if (this.ignoreComments) {
                        ArrayList arrayList2 = new ArrayList();
                        addCommentsTo(arrayList2, parse);
                        Iterator it2 = arrayList2.iterator();
                        while (it2.hasNext()) {
                            Node node2 = (Node) it2.next();
                            node2.getParentNode().removeChild(node2);
                        }
                    }
                    if (this.ignoreWhitespace) {
                        ArrayList arrayList3 = new ArrayList();
                        addTextNodesToRemoveAndTrim(arrayList3, parse);
                        Iterator it3 = arrayList3.iterator();
                        while (it3.hasNext()) {
                            Node node3 = (Node) it3.next();
                            node3.getParentNode().removeChild(node3);
                        }
                    }
                    NodeList childNodes = parse.getDocumentElement().getChildNodes();
                    if (childNodes.getLength() <= 1) {
                        if (childNodes.getLength() == 0) {
                            Text createTextNode = parse.createTextNode("");
                            returnDocumentBuilderToPool(documentBuilderFromPool);
                            return createTextNode;
                        }
                        Node item = childNodes.item(0);
                        parse.getDocumentElement().removeChild(item);
                        returnDocumentBuilderToPool(documentBuilderFromPool);
                        return item;
                    }
                    throw ScriptRuntime.constructError("SyntaxError", "XML objects may contain at most one node.");
                } catch (ParserConfigurationException e7) {
                    throw new RuntimeException(e7);
                }
            } catch (IOException unused) {
                throw new RuntimeException("Unreachable.");
            }
        } catch (Throwable th) {
            if (0 != 0) {
                returnDocumentBuilderToPool(null);
            }
            throw th;
        }
    }
}