/*
 * Decompiled with CFR 0_110.
 * 
 * Could not load the following classes:
 *  java.io.PrintStream
 *  java.lang.Class
 *  java.lang.ClassLoader
 *  java.lang.ClassNotFoundException
 *  java.lang.Exception
 *  java.lang.NoClassDefFoundError
 *  java.lang.Object
 *  java.lang.String
 *  java.lang.StringBuffer
 *  java.lang.System
 *  java.lang.Throwable
 *  java.util.List
 *  org.w3c.dom.CDATASection
 *  org.w3c.dom.Comment
 *  org.w3c.dom.DOMImplementation
 *  org.w3c.dom.Document
 *  org.w3c.dom.DocumentType
 *  org.w3c.dom.Element
 *  org.w3c.dom.EntityReference
 *  org.w3c.dom.Node
 *  org.w3c.dom.ProcessingInstruction
 *  org.w3c.dom.Text
 */
package org.dom4j.io;

import java.io.PrintStream;
import java.util.List;
import org.dom4j.Attribute;
import org.dom4j.CDATA;
import org.dom4j.Comment;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Entity;
import org.dom4j.Namespace;
import org.dom4j.ProcessingInstruction;
import org.dom4j.Text;
import org.dom4j.io.JAXPHelper;
import org.dom4j.io.SAXHelper;
import org.dom4j.tree.NamespaceStack;
import org.w3c.dom.CDATASection;
import org.w3c.dom.DOMImplementation;
import org.w3c.dom.DocumentType;
import org.w3c.dom.EntityReference;
import org.w3c.dom.Node;

public class DOMWriter {
    private static final String[] DEFAULT_DOM_DOCUMENT_CLASSES;
    static Class class$org$dom4j$io$DOMWriter;
    private static boolean loggedWarning;
    private Class domDocumentClass;
    private NamespaceStack namespaceStack;

    static {
        loggedWarning = false;
        DEFAULT_DOM_DOCUMENT_CLASSES = new String[]{"org.apache.xerces.dom.DocumentImpl", "gnu.xml.dom.DomDocument", "org.apache.crimson.tree.XmlDocument", "com.sun.xml.tree.XmlDocument", "oracle.xml.parser.v2.XMLDocument", "oracle.xml.parser.XMLDocument", "org.dom4j.dom.DOMDocument"};
    }

    public DOMWriter() {
        this.namespaceStack = new NamespaceStack();
    }

    public DOMWriter(Class class_) {
        this.namespaceStack = new NamespaceStack();
        this.domDocumentClass = class_;
    }

    static Class class$(String string2) {
        try {
            Class class_ = Class.forName((String)string2);
            return class_;
        }
        catch (ClassNotFoundException var1_2) {
            throw new NoClassDefFoundError(var1_2.getMessage());
        }
    }

    protected void appendDOMTree(org.w3c.dom.Document document, Node node, String string2) {
        node.appendChild((Node)document.createTextNode(string2));
    }

    /*
     * Enabled aggressive block sorting
     */
    protected void appendDOMTree(org.w3c.dom.Document document, Node node, List list) {
        int n = list.size();
        int n2 = 0;
        while (n2 < n) {
            Object object = list.get(n2);
            if (object instanceof Element) {
                this.appendDOMTree(document, node, (Element)object);
            } else if (object instanceof String) {
                this.appendDOMTree(document, node, (String)object);
            } else if (object instanceof Text) {
                this.appendDOMTree(document, node, ((Text)object).getText());
            } else if (object instanceof CDATA) {
                this.appendDOMTree(document, node, (CDATA)object);
            } else if (object instanceof Comment) {
                this.appendDOMTree(document, node, (Comment)object);
            } else if (object instanceof Entity) {
                this.appendDOMTree(document, node, (Entity)object);
            } else if (object instanceof ProcessingInstruction) {
                this.appendDOMTree(document, node, (ProcessingInstruction)object);
            }
            ++n2;
        }
    }

    protected void appendDOMTree(org.w3c.dom.Document document, Node node, CDATA cDATA) {
        node.appendChild((Node)document.createCDATASection(cDATA.getText()));
    }

    protected void appendDOMTree(org.w3c.dom.Document document, Node node, Comment comment) {
        node.appendChild((Node)document.createComment(comment.getText()));
    }

    protected void appendDOMTree(org.w3c.dom.Document document, Node node, Element element) {
        org.w3c.dom.Element element2 = document.createElementNS(element.getNamespaceURI(), element.getQualifiedName());
        int n = this.namespaceStack.size();
        Namespace namespace = element.getNamespace();
        if (this.isNamespaceDeclaration(namespace)) {
            this.namespaceStack.push(namespace);
            this.writeNamespace(element2, namespace);
        }
        List list = element.declaredNamespaces();
        int n2 = list.size();
        for (int i = 0; i < n2; ++i) {
            Namespace namespace2 = (Namespace)list.get(i);
            if (!this.isNamespaceDeclaration(namespace2)) continue;
            this.namespaceStack.push(namespace2);
            this.writeNamespace(element2, namespace2);
        }
        int n3 = element.attributeCount();
        for (int j = 0; j < n3; ++j) {
            Attribute attribute = element.attribute(j);
            element2.setAttributeNS(attribute.getNamespaceURI(), attribute.getQualifiedName(), attribute.getValue());
        }
        this.appendDOMTree(document, (Node)element2, element.content());
        node.appendChild((Node)element2);
        while (this.namespaceStack.size() > n) {
            this.namespaceStack.pop();
        }
    }

    protected void appendDOMTree(org.w3c.dom.Document document, Node node, Entity entity) {
        node.appendChild((Node)document.createEntityReference(entity.getName()));
    }

    protected void appendDOMTree(org.w3c.dom.Document document, Node node, ProcessingInstruction processingInstruction) {
        node.appendChild((Node)document.createProcessingInstruction(processingInstruction.getTarget(), processingInstruction.getText()));
    }

    protected String attributeNameForNamespace(Namespace namespace) {
        String string2 = "xmlns";
        String string3 = namespace.getPrefix();
        if (string3.length() > 0) {
            string2 = new StringBuffer().append(string2).append(":").append(string3).toString();
        }
        return string2;
    }

    /*
     * Enabled force condition propagation
     * Lifted jumps to return sites
     */
    protected org.w3c.dom.Document createDomDocument(Document document) throws DocumentException {
        if (this.domDocumentClass != null) {
            try {
                return (org.w3c.dom.Document)this.domDocumentClass.newInstance();
            }
            catch (Exception var6_3) {
                throw new DocumentException(new StringBuffer().append("Could not instantiate an instance of DOM Document with class: ").append(this.domDocumentClass.getName()).toString(), (Throwable)var6_3);
            }
        }
        org.w3c.dom.Document document2 = this.createDomDocumentViaJAXP();
        if (document2 != null) return document2;
        Class class_ = this.getDomDocumentClass();
        try {
            return (org.w3c.dom.Document)class_.newInstance();
        }
        catch (Exception var4_6) {
            throw new DocumentException(new StringBuffer().append("Could not instantiate an instance of DOM Document with class: ").append(class_.getName()).toString(), (Throwable)var4_6);
        }
    }

    protected org.w3c.dom.Document createDomDocument(Document document, DOMImplementation dOMImplementation) throws DocumentException {
        return dOMImplementation.createDocument(null, null, null);
    }

    /*
     * Enabled force condition propagation
     * Lifted jumps to return sites
     */
    protected org.w3c.dom.Document createDomDocumentViaJAXP() throws DocumentException {
        try {
            return JAXPHelper.createDocument(false, true);
        }
        catch (Throwable var1_2) {
            if (loggedWarning) return null;
            loggedWarning = true;
            if (SAXHelper.isVerboseErrorReporting()) {
                System.out.println("Warning: Caught exception attempting to use JAXP to create a W3C DOM document");
                System.out.println(new StringBuffer().append("Warning: Exception was: ").append((Object)var1_2).toString());
                var1_2.printStackTrace();
                do {
                    return null;
                    break;
                } while (true);
            }
            System.out.println("Warning: Error occurred using JAXP to create a DOM document.");
            return null;
        }
    }

    /*
     * Enabled aggressive block sorting
     * Enabled unnecessary exception pruning
     * Enabled aggressive exception aggregation
     */
    public Class getDomDocumentClass() throws DocumentException {
        Class class_ = this.domDocumentClass;
        if (class_ != null) {
            return class_;
        }
        int n = DEFAULT_DOM_DOCUMENT_CLASSES.length;
        int n2 = 0;
        while (n2 < n) {
            try {
                Class class_2;
                String string2 = DEFAULT_DOM_DOCUMENT_CLASSES[n2];
                if (class$org$dom4j$io$DOMWriter == null) {
                    class$org$dom4j$io$DOMWriter = class_2 = DOMWriter.class$("org.dom4j.io.DOMWriter");
                } else {
                    class_2 = class$org$dom4j$io$DOMWriter;
                }
                if ((class_ = Class.forName((String)string2, (boolean)true, (ClassLoader)class_2.getClassLoader())) != null) {
                    return class_;
                }
            }
            catch (Exception var4_4) {
                // empty catch block
            }
            ++n2;
        }
        return class_;
    }

    protected boolean isNamespaceDeclaration(Namespace namespace) {
        String string2;
        if (namespace != null && namespace != Namespace.NO_NAMESPACE && namespace != Namespace.XML_NAMESPACE && (string2 = namespace.getURI()) != null && string2.length() > 0 && !this.namespaceStack.contains(namespace)) {
            return true;
        }
        return false;
    }

    protected void resetNamespaceStack() {
        this.namespaceStack.clear();
        this.namespaceStack.push(Namespace.XML_NAMESPACE);
    }

    public void setDomDocumentClass(Class class_) {
        this.domDocumentClass = class_;
    }

    /*
     * Enabled aggressive block sorting
     * Enabled unnecessary exception pruning
     * Enabled aggressive exception aggregation
     */
    public void setDomDocumentClassName(String string2) throws DocumentException {
        try {
            Class class_;
            if (class$org$dom4j$io$DOMWriter == null) {
                class$org$dom4j$io$DOMWriter = class_ = DOMWriter.class$("org.dom4j.io.DOMWriter");
            } else {
                class_ = class$org$dom4j$io$DOMWriter;
            }
            this.domDocumentClass = Class.forName((String)string2, (boolean)true, (ClassLoader)class_.getClassLoader());
            return;
        }
        catch (Exception var2_3) {
            throw new DocumentException(new StringBuffer().append("Could not load the DOM Document class: ").append(string2).toString(), (Throwable)var2_3);
        }
    }

    public org.w3c.dom.Document write(Document document) throws DocumentException {
        if (document instanceof org.w3c.dom.Document) {
            return (org.w3c.dom.Document)document;
        }
        this.resetNamespaceStack();
        org.w3c.dom.Document document2 = this.createDomDocument(document);
        this.appendDOMTree(document2, (Node)document2, document.content());
        this.namespaceStack.clear();
        return document2;
    }

    public org.w3c.dom.Document write(Document document, DOMImplementation dOMImplementation) throws DocumentException {
        if (document instanceof org.w3c.dom.Document) {
            return (org.w3c.dom.Document)document;
        }
        this.resetNamespaceStack();
        org.w3c.dom.Document document2 = this.createDomDocument(document, dOMImplementation);
        this.appendDOMTree(document2, (Node)document2, document.content());
        this.namespaceStack.clear();
        return document2;
    }

    protected void writeNamespace(org.w3c.dom.Element element, Namespace namespace) {
        element.setAttribute(this.attributeNameForNamespace(namespace), namespace.getURI());
    }
}

