/*
 * Decompiled with CFR 0_110.
 * 
 * Could not load the following classes:
 *  java.lang.Object
 *  java.lang.String
 *  java.lang.StringBuffer
 *  java.util.Collections
 *  java.util.Iterator
 *  java.util.List
 *  org.xml.sax.EntityResolver
 */
package org.dom4j.tree;

import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import org.dom4j.Branch;
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.tree.AbstractDocument;
import org.dom4j.tree.BackedList;
import org.dom4j.tree.ContentListFacade;
import org.xml.sax.EntityResolver;

public class DefaultDocument
extends AbstractDocument {
    protected static final Iterator EMPTY_ITERATOR;
    protected static final List EMPTY_LIST;
    private List content;
    private DocumentType docType;
    private DocumentFactory documentFactory;
    private transient EntityResolver entityResolver;
    private String name;
    private Element rootElement;

    static {
        EMPTY_LIST = Collections.EMPTY_LIST;
        EMPTY_ITERATOR = EMPTY_LIST.iterator();
    }

    public DefaultDocument() {
        this.documentFactory = DocumentFactory.getInstance();
    }

    public DefaultDocument(String string2) {
        this.documentFactory = DocumentFactory.getInstance();
        this.name = string2;
    }

    public DefaultDocument(String string2, Element element, DocumentType documentType) {
        this.documentFactory = DocumentFactory.getInstance();
        this.name = string2;
        this.rootElement = element;
        this.docType = documentType;
    }

    public DefaultDocument(DocumentType documentType) {
        this.documentFactory = DocumentFactory.getInstance();
        this.docType = documentType;
    }

    public DefaultDocument(Element element) {
        this.documentFactory = DocumentFactory.getInstance();
        this.rootElement = element;
    }

    public DefaultDocument(Element element, DocumentType documentType) {
        this.documentFactory = DocumentFactory.getInstance();
        this.rootElement = element;
        this.docType = documentType;
    }

    @Override
    public Document addDocType(String string2, String string3, String string4) {
        this.setDocType(this.getDocumentFactory().createDocType(string2, string3, string4));
        return this;
    }

    @Override
    protected void addNode(int n, Node node) {
        if (node != null) {
            Document document = node.getDocument();
            if (document != null && document != this) {
                throw new IllegalAddException((Branch)this, node, new StringBuffer().append("The Node already has an existing document: ").append((Object)document).toString());
            }
            this.contentList().add(n, (Object)node);
            this.childAdded(node);
        }
    }

    @Override
    protected void addNode(Node node) {
        if (node != null) {
            Document document = node.getDocument();
            if (document != null && document != this) {
                throw new IllegalAddException((Branch)this, node, new StringBuffer().append("The Node already has an existing document: ").append((Object)document).toString());
            }
            this.contentList().add((Object)node);
            this.childAdded(node);
        }
    }

    @Override
    public void clearContent() {
        this.contentRemoved();
        this.content = null;
        this.rootElement = null;
    }

    @Override
    public Object clone() {
        DefaultDocument defaultDocument = (DefaultDocument)super.clone();
        defaultDocument.rootElement = null;
        defaultDocument.content = null;
        defaultDocument.appendContent(this);
        return defaultDocument;
    }

    @Override
    protected List contentList() {
        if (this.content == null) {
            this.content = this.createContentList();
            if (this.rootElement != null) {
                this.content.add((Object)this.rootElement);
            }
        }
        return this.content;
    }

    @Override
    public DocumentType getDocType() {
        return this.docType;
    }

    @Override
    protected DocumentFactory getDocumentFactory() {
        return this.documentFactory;
    }

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

    @Override
    public String getName() {
        return this.name;
    }

    @Override
    public Element getRootElement() {
        return this.rootElement;
    }

    @Override
    public String getXMLEncoding() {
        return this.encoding;
    }

    @Override
    public ProcessingInstruction processingInstruction(String string2) {
        List list = this.contentList();
        int n = list.size();
        for (int i = 0; i < n; ++i) {
            ProcessingInstruction processingInstruction;
            Object object = list.get(i);
            if (!(object instanceof ProcessingInstruction) || !string2.equals((Object)(processingInstruction = (ProcessingInstruction)object).getName())) continue;
            return processingInstruction;
        }
        return null;
    }

    @Override
    public List processingInstructions() {
        List list = this.contentList();
        BackedList backedList = this.createResultList();
        int n = list.size();
        for (int i = 0; i < n; ++i) {
            Object object = list.get(i);
            if (!(object instanceof ProcessingInstruction)) continue;
            backedList.add(object);
        }
        return backedList;
    }

    @Override
    public List processingInstructions(String string2) {
        List list = this.contentList();
        BackedList backedList = this.createResultList();
        int n = list.size();
        for (int i = 0; i < n; ++i) {
            ProcessingInstruction processingInstruction;
            Object object = list.get(i);
            if (!(object instanceof ProcessingInstruction) || !string2.equals((Object)(processingInstruction = (ProcessingInstruction)object).getName())) continue;
            backedList.add((Object)processingInstruction);
        }
        return backedList;
    }

    @Override
    protected boolean removeNode(Node node) {
        if (node == this.rootElement) {
            this.rootElement = null;
        }
        if (this.contentList().remove((Object)node)) {
            this.childRemoved(node);
            return true;
        }
        return false;
    }

    @Override
    public boolean removeProcessingInstruction(String string2) {
        Iterator iterator = this.contentList().iterator();
        while (iterator.hasNext()) {
            Object object = iterator.next();
            if (!(object instanceof ProcessingInstruction) || !string2.equals((Object)((ProcessingInstruction)object).getName())) continue;
            iterator.remove();
            return true;
        }
        return false;
    }

    @Override
    protected void rootElementAdded(Element element) {
        this.rootElement = element;
        element.setDocument((Document)this);
    }

    /*
     * Enabled force condition propagation
     * Lifted jumps to return sites
     */
    @Override
    public void setContent(List list) {
        this.rootElement = null;
        this.contentRemoved();
        if (list instanceof ContentListFacade) {
            list = ((ContentListFacade)list).getBackingList();
        }
        if (list == null) {
            this.content = null;
            return;
        }
        int n = list.size();
        List list2 = this.createContentList(n);
        for (int i = 0; i < n; ++i) {
            Object object = list.get(i);
            if (!(object instanceof Node)) continue;
            Node node = (Node)object;
            Document document = node.getDocument();
            if (document != null && document != this) {
                node = (Node)node.clone();
            }
            if (node instanceof Element) {
                if (this.rootElement != null) throw new IllegalAddException(new StringBuffer().append("A document may only contain one root element: ").append((Object)list).toString());
                this.rootElement = (Element)node;
            }
            list2.add((Object)node);
            this.childAdded(node);
        }
        this.content = list2;
    }

    @Override
    public void setDocType(DocumentType documentType) {
        this.docType = documentType;
    }

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

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

    @Override
    public void setName(String string2) {
        this.name = string2;
    }
}

