/*
 * Decompiled with CFR 0_110.
 * 
 * Could not load the following classes:
 *  java.io.IOException
 *  java.io.StringWriter
 *  java.io.Writer
 *  java.lang.Object
 *  java.lang.RuntimeException
 *  java.lang.String
 *  java.lang.StringBuffer
 *  java.util.List
 *  java.util.Map
 */
package org.dom4j.tree;

import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.util.List;
import java.util.Map;
import org.dom4j.Branch;
import org.dom4j.Comment;
import org.dom4j.Document;
import org.dom4j.DocumentFactory;
import org.dom4j.DocumentType;
import org.dom4j.Element;
import org.dom4j.IllegalAddException;
import org.dom4j.Node;
import org.dom4j.ProcessingInstruction;
import org.dom4j.QName;
import org.dom4j.Text;
import org.dom4j.Visitor;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
import org.dom4j.tree.AbstractBranch;

public abstract class AbstractDocument
extends AbstractBranch
implements Document {
    protected String encoding;

    @Override
    public void accept(Visitor visitor) {
        List list;
        visitor.visit((Document)this);
        DocumentType documentType = this.getDocType();
        if (documentType != null) {
            visitor.visit(documentType);
        }
        if ((list = this.content()) != null) {
            for (Object object : list) {
                if (object instanceof String) {
                    visitor.visit(this.getDocumentFactory().createText((String)object));
                    continue;
                }
                ((Node)object).accept(visitor);
            }
        }
    }

    @Override
    public void add(Element element) {
        this.checkAddElementAllowed(element);
        super.add(element);
        this.rootElementAdded(element);
    }

    @Override
    public Document addComment(String string2) {
        this.add(this.getDocumentFactory().createComment(string2));
        return this;
    }

    @Override
    public Element addElement(String string2) {
        Element element = this.getDocumentFactory().createElement(string2);
        this.add(element);
        return element;
    }

    @Override
    public Element addElement(String string2, String string3) {
        Element element = this.getDocumentFactory().createElement(string2, string3);
        this.add(element);
        return element;
    }

    @Override
    public Element addElement(QName qName) {
        Element element = this.getDocumentFactory().createElement(qName);
        this.add(element);
        return element;
    }

    @Override
    public Document addProcessingInstruction(String string2, String string3) {
        this.add(this.getDocumentFactory().createProcessingInstruction(string2, string3));
        return this;
    }

    @Override
    public Document addProcessingInstruction(String string2, Map map) {
        this.add(this.getDocumentFactory().createProcessingInstruction(string2, map));
        return this;
    }

    @Override
    public String asXML() {
        OutputFormat outputFormat = new OutputFormat();
        outputFormat.setEncoding(this.encoding);
        try {
            StringWriter stringWriter = new StringWriter();
            XMLWriter xMLWriter = new XMLWriter((Writer)stringWriter, outputFormat);
            xMLWriter.write(this);
            xMLWriter.flush();
            String string2 = stringWriter.toString();
            return string2;
        }
        catch (IOException var4_5) {
            throw new RuntimeException(new StringBuffer().append("IOException while generating textual representation: ").append(var4_5.getMessage()).toString());
        }
    }

    @Override
    public Node asXPathResult(Element element) {
        return this;
    }

    protected void checkAddElementAllowed(Element element) {
        Element element2 = this.getRootElement();
        if (element2 != null) {
            throw new IllegalAddException((Branch)this, (Node)element, new StringBuffer().append("Cannot add another element to this Document as it already has a root element of: ").append(element2.getQualifiedName()).toString());
        }
    }

    @Override
    protected void childAdded(Node node) {
        if (node != null) {
            node.setDocument((Document)this);
        }
    }

    @Override
    protected void childRemoved(Node node) {
        if (node != null) {
            node.setDocument(null);
        }
    }

    @Override
    public Document getDocument() {
        return this;
    }

    @Override
    public short getNodeType() {
        return 9;
    }

    @Override
    public String getPath(Element element) {
        return "/";
    }

    @Override
    public String getStringValue() {
        Element element = this.getRootElement();
        if (element != null) {
            return element.getStringValue();
        }
        return "";
    }

    @Override
    public String getUniquePath(Element element) {
        return "/";
    }

    @Override
    public String getXMLEncoding() {
        return null;
    }

    @Override
    public void normalize() {
        Element element = this.getRootElement();
        if (element != null) {
            element.normalize();
        }
    }

    @Override
    public boolean remove(Element element) {
        boolean bl = super.remove(element);
        if (this.getRootElement() != null && bl) {
            this.setRootElement(null);
        }
        element.setDocument(null);
        return bl;
    }

    protected abstract void rootElementAdded(Element var1);

    @Override
    public void setRootElement(Element element) {
        this.clearContent();
        if (element != null) {
            super.add(element);
            this.rootElementAdded(element);
        }
    }

    @Override
    public void setXMLEncoding(String string2) {
        this.encoding = string2;
    }

    public String toString() {
        return new StringBuffer().append(super.toString()).append(" [Document: name ").append(this.getName()).append("]").toString();
    }

    @Override
    public void write(Writer writer) throws IOException {
        OutputFormat outputFormat = new OutputFormat();
        outputFormat.setEncoding(this.encoding);
        new XMLWriter(writer, outputFormat).write((Document)this);
    }
}

