/*
 * Decompiled with CFR 0_110.
 * 
 * Could not load the following classes:
 *  java.io.PrintStream
 *  java.lang.Object
 *  java.lang.String
 *  java.lang.StringBuffer
 *  java.lang.System
 *  java.util.ArrayList
 *  org.w3c.dom.Document
 *  org.w3c.dom.DocumentType
 *  org.w3c.dom.NamedNodeMap
 *  org.w3c.dom.Node
 *  org.w3c.dom.NodeList
 */
package org.dom4j.io;

import java.io.PrintStream;
import java.util.ArrayList;
import org.dom4j.Branch;
import org.dom4j.Document;
import org.dom4j.DocumentFactory;
import org.dom4j.Element;
import org.dom4j.Namespace;
import org.dom4j.QName;
import org.dom4j.tree.NamespaceStack;
import org.w3c.dom.DocumentType;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class DOMReader {
    private DocumentFactory factory;
    private NamespaceStack namespaceStack;

    public DOMReader() {
        this.factory = DocumentFactory.getInstance();
        this.namespaceStack = new NamespaceStack(this.factory);
    }

    public DOMReader(DocumentFactory documentFactory) {
        this.factory = documentFactory;
        this.namespaceStack = new NamespaceStack(documentFactory);
    }

    private String getPrefix(String string2) {
        int n = string2.indexOf(58, 5);
        if (n != -1) {
            return string2.substring(n + 1);
        }
        return "";
    }

    protected void clearNamespaceStack() {
        this.namespaceStack.clear();
        if (!this.namespaceStack.contains(Namespace.XML_NAMESPACE)) {
            this.namespaceStack.push(Namespace.XML_NAMESPACE);
        }
    }

    protected Document createDocument() {
        return this.getDocumentFactory().createDocument();
    }

    public DocumentFactory getDocumentFactory() {
        return this.factory;
    }

    protected Namespace getNamespace(String string2, String string3) {
        return this.getDocumentFactory().createNamespace(string2, string3);
    }

    public Document read(org.w3c.dom.Document document) {
        if (document instanceof Document) {
            return (Document)document;
        }
        Document document2 = this.createDocument();
        this.clearNamespaceStack();
        NodeList nodeList = document.getChildNodes();
        int n = nodeList.getLength();
        for (int i = 0; i < n; ++i) {
            this.readTree(nodeList.item(i), document2);
        }
        return document2;
    }

    /*
     * Enabled aggressive block sorting
     */
    protected void readElement(Node node, Branch branch) {
        Node node2;
        NamedNodeMap namedNodeMap;
        int n = this.namespaceStack.size();
        String string2 = node.getNamespaceURI();
        if (node.getPrefix() == null) {
            // empty if block
        }
        if ((namedNodeMap = node.getAttributes()) != null && string2 == null && (node2 = namedNodeMap.getNamedItem("xmlns")) != null) {
            string2 = node2.getNodeValue();
        }
        Element element = branch.addElement(this.namespaceStack.getQName(string2, node.getLocalName(), node.getNodeName()));
        if (namedNodeMap != null) {
            int n2 = namedNodeMap.getLength();
            ArrayList arrayList = new ArrayList(n2);
            for (int i = 0; i < n2; ++i) {
                Node node3 = namedNodeMap.item(i);
                String string3 = node3.getNodeName();
                if (string3.startsWith("xmlns")) {
                    String string4 = super.getPrefix(string3);
                    String string5 = node3.getNodeValue();
                    element.add(this.namespaceStack.addNamespace(string4, string5));
                    continue;
                }
                arrayList.add((Object)node3);
            }
            int n3 = arrayList.size();
            for (int j = 0; j < n3; ++j) {
                Node node4 = (Node)arrayList.get(j);
                element.addAttribute(this.namespaceStack.getQName(node4.getNamespaceURI(), node4.getLocalName(), node4.getNodeName()), node4.getNodeValue());
            }
        }
        NodeList nodeList = node.getChildNodes();
        int n4 = nodeList.getLength();
        for (int i = 0; i < n4; ++i) {
            this.readTree(nodeList.item(i), element);
        }
        while (this.namespaceStack.size() > n) {
            this.namespaceStack.pop();
        }
        return;
    }

    /*
     * Enabled aggressive block sorting
     */
    protected void readTree(Node node, Branch branch) {
        Element element;
        Document document = null;
        if (branch instanceof Element) {
            element = (Element)branch;
        } else {
            document = (Document)branch;
            element = null;
        }
        switch (node.getNodeType()) {
            default: {
                System.out.println(new StringBuffer().append("WARNING: Unknown DOM node type: ").append((int)node.getNodeType()).toString());
                return;
            }
            case 1: {
                this.readElement(node, branch);
                return;
            }
            case 7: {
                if (branch instanceof Element) {
                    ((Element)branch).addProcessingInstruction(node.getNodeName(), node.getNodeValue());
                    return;
                }
                ((Document)branch).addProcessingInstruction(node.getNodeName(), node.getNodeValue());
                return;
            }
            case 8: {
                if (branch instanceof Element) {
                    ((Element)branch).addComment(node.getNodeValue());
                    return;
                }
                ((Document)branch).addComment(node.getNodeValue());
                return;
            }
            case 10: {
                DocumentType documentType = (DocumentType)node;
                document.addDocType(documentType.getName(), documentType.getPublicId(), documentType.getSystemId());
                return;
            }
            case 3: {
                element.addText(node.getNodeValue());
                return;
            }
            case 4: {
                element.addCDATA(node.getNodeValue());
                return;
            }
            case 5: {
                Node node2 = node.getFirstChild();
                if (node2 != null) {
                    element.addEntity(node.getNodeName(), node2.getNodeValue());
                    return;
                }
                element.addEntity(node.getNodeName(), "");
                return;
            }
            case 6: 
        }
        element.addEntity(node.getNodeName(), node.getNodeValue());
    }

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

