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

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.StringTokenizer;
import org.dom4j.Branch;
import org.dom4j.Comment;
import org.dom4j.DocumentFactory;
import org.dom4j.Element;
import org.dom4j.IllegalAddException;
import org.dom4j.Namespace;
import org.dom4j.Node;
import org.dom4j.ProcessingInstruction;
import org.dom4j.QName;
import org.dom4j.Text;
import org.dom4j.tree.AbstractNode;
import org.dom4j.tree.BackedList;
import org.dom4j.tree.ContentListFacade;

public abstract class AbstractBranch
extends AbstractNode
implements Branch {
    protected static final int DEFAULT_CONTENT_LIST_SIZE = 5;

    @Override
    public void add(Comment comment) {
        this.addNode(comment);
    }

    @Override
    public void add(Element element) {
        this.addNode(element);
    }

    @Override
    public void add(Node node) {
        switch (node.getNodeType()) {
            default: {
                this.invalidNodeTypeAddException(node);
                return;
            }
            case 1: {
                this.add((Element)node);
                return;
            }
            case 8: {
                this.add((Comment)node);
                return;
            }
            case 7: 
        }
        this.add((ProcessingInstruction)node);
    }

    @Override
    public void add(ProcessingInstruction processingInstruction) {
        this.addNode(processingInstruction);
    }

    @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;
    }

    public Element addElement(String string2, String string3, String string4) {
        Namespace namespace = Namespace.get(string3, string4);
        return this.addElement(this.getDocumentFactory().createQName(string2, namespace));
    }

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

    protected abstract void addNode(int var1, Node var2);

    protected abstract void addNode(Node var1);

    @Override
    public void appendContent(Branch branch) {
        int n = branch.nodeCount();
        for (int i = 0; i < n; ++i) {
            this.add((Node)branch.node(i).clone());
        }
    }

    protected abstract void childAdded(Node var1);

    protected abstract void childRemoved(Node var1);

    @Override
    public List content() {
        return new ContentListFacade(this, this.contentList());
    }

    protected abstract List contentList();

    protected void contentRemoved() {
        List list = this.contentList();
        int n = list.size();
        for (int i = 0; i < n; ++i) {
            Object object = list.get(i);
            if (!(object instanceof Node)) continue;
            this.childRemoved((Node)object);
        }
    }

    protected List createContentList() {
        return new ArrayList(5);
    }

    protected List createContentList(int n) {
        return new ArrayList(n);
    }

    protected List createEmptyList() {
        return new BackedList(this, this.contentList(), 0);
    }

    protected BackedList createResultList() {
        return new BackedList(this, this.contentList());
    }

    protected List createSingleResultList(Object object) {
        BackedList backedList = new BackedList((AbstractBranch)this, this.contentList(), 1);
        backedList.addLocal(object);
        return backedList;
    }

    /*
     * Enabled aggressive block sorting
     */
    @Override
    public Element elementByID(String string2) {
        int n = 0;
        int n2 = this.nodeCount();
        while (n < n2) {
            Element element;
            String string3;
            Node node = this.node(n);
            if (node instanceof Element && ((string3 = this.elementID(element = (Element)node)) != null && string3.equals((Object)string2) || (element = element.elementByID(string2)) != null)) {
                return element;
            }
            ++n;
        }
        return null;
    }

    protected String elementID(Element element) {
        return element.attributeValue("ID");
    }

    /*
     * Enabled aggressive block sorting
     */
    protected String getContentAsStringValue(Object object) {
        if (object instanceof Node) {
            Node node = (Node)object;
            switch (node.getNodeType()) {
                default: {
                    return "";
                }
                case 1: 
                case 3: 
                case 4: 
                case 5: {
                    return node.getStringValue();
                }
            }
        }
        if (!(object instanceof String)) return "";
        return (String)object;
    }

    /*
     * Enabled aggressive block sorting
     */
    protected String getContentAsText(Object object) {
        if (object instanceof Node) {
            Node node = (Node)object;
            switch (node.getNodeType()) {
                default: {
                    return "";
                }
                case 3: 
                case 4: 
                case 5: {
                    return node.getText();
                }
            }
        }
        if (!(object instanceof String)) return "";
        return (String)object;
    }

    @Override
    public String getText() {
        int n;
        List list = this.contentList();
        if (list != null && (n = list.size()) >= 1) {
            String string2 = this.getContentAsText(list.get(0));
            if (n == 1) {
                return string2;
            }
            StringBuffer stringBuffer = new StringBuffer(string2);
            for (int i = 1; i < n; ++i) {
                stringBuffer.append(this.getContentAsText(list.get(i)));
            }
            return stringBuffer.toString();
        }
        return "";
    }

    public String getTextTrim() {
        String string2 = this.getText();
        StringBuffer stringBuffer = new StringBuffer();
        StringTokenizer stringTokenizer = new StringTokenizer(string2);
        while (stringTokenizer.hasMoreTokens()) {
            stringBuffer.append(stringTokenizer.nextToken());
            if (!stringTokenizer.hasMoreTokens()) continue;
            stringBuffer.append(" ");
        }
        return stringBuffer.toString();
    }

    @Override
    public boolean hasContent() {
        if (this.nodeCount() > 0) {
            return true;
        }
        return false;
    }

    @Override
    public int indexOf(Node node) {
        return this.contentList().indexOf((Object)node);
    }

    protected void invalidNodeTypeAddException(Node node) {
        throw new IllegalAddException(new StringBuffer().append("Invalid node type. Cannot add node: ").append((Object)node).append(" to this branch: ").append((Object)this).toString());
    }

    @Override
    public boolean isReadOnly() {
        return false;
    }

    @Override
    public Node node(int n) {
        Object object = this.contentList().get(n);
        if (object instanceof Node) {
            return (Node)object;
        }
        if (object instanceof String) {
            return this.getDocumentFactory().createText(object.toString());
        }
        return null;
    }

    @Override
    public int nodeCount() {
        return this.contentList().size();
    }

    @Override
    public Iterator nodeIterator() {
        return this.contentList().iterator();
    }

    @Override
    public boolean remove(Comment comment) {
        return this.removeNode(comment);
    }

    @Override
    public boolean remove(Element element) {
        return this.removeNode(element);
    }

    @Override
    public boolean remove(Node node) {
        switch (node.getNodeType()) {
            default: {
                this.invalidNodeTypeAddException(node);
                return false;
            }
            case 1: {
                return this.remove((Element)node);
            }
            case 8: {
                return this.remove((Comment)node);
            }
            case 7: 
        }
        return this.remove((ProcessingInstruction)node);
    }

    @Override
    public boolean remove(ProcessingInstruction processingInstruction) {
        return this.removeNode(processingInstruction);
    }

    protected abstract boolean removeNode(Node var1);

    @Override
    public void setProcessingInstructions(List list) {
        Iterator iterator = list.iterator();
        while (iterator.hasNext()) {
            this.addNode((ProcessingInstruction)iterator.next());
        }
    }
}

