Search in sources :

Example 1 with ParentNode

use of nu.xom.ParentNode in project teiid by teiid.

the class BinaryXMLCodec method readElementF.

/**
 * Iterative pull parser reading an entire element subtree (using NodeFactory).
 */
private void readElementF(ArrayByteList src, Element current, InputStream input) throws BinaryParsingException, IOException {
    // final ArrayList stack = new ArrayList();
    final FastStack stack = new FastStack();
    stack.push(current);
    boolean addAttributesAndNamespaces = true;
    while (true) {
        Nodes nodes = null;
        // look ahead
        int type = src.get();
        switch(// three low bits indicate node type
        type & 0x07) {
            case TEXT:
                {
                    nodes = readTextF(src, type);
                    break;
                }
            case ATTRIBUTE:
                {
                    Element elem = addAttributesAndNamespaces ? current : null;
                    nodes = readAttributeF(src, elem, type);
                    break;
                }
            case BEGIN_ELEMENT:
                {
                    Element elem = readStartTagF(src, type, false);
                    // even if it's null
                    stack.push(elem);
                    if (elem != null) {
                        current.insertChild(elem, current.getChildCount());
                        // recurse down
                        current = elem;
                    }
                    addAttributesAndNamespaces = elem != null;
                    continue;
                }
            case END_ELEMENT:
                {
                    Element elem = stack.pop();
                    if (elem == null) {
                        // skip element
                        continue;
                    }
                    ParentNode parent = elem.getParent();
                    if (parent == null)
                        throwTamperedWithParent();
                    if (parent instanceof Document) {
                        // we're done with the root element
                        return;
                    }
                    // recurse up
                    current = (Element) parent;
                    nodes = factory.finishMakingElement(elem);
                    if (nodes.size() == 1 && nodes.get(0) == elem) {
                        // optimization: no need to remove and then readd same element
                        continue;
                    }
                    if (current.getChildCount() - 1 < 0)
                        throwTamperedWithParent();
                    current.removeChild(current.getChildCount() - 1);
                    break;
                }
            case COMMENT:
                {
                    nodes = readCommentF(src, type);
                    break;
                }
            case NAMESPACE_DECLARATION:
                {
                    Element elem = addAttributesAndNamespaces ? current : null;
                    readNamespaceDeclaration(src, elem, type);
                    continue;
                }
            case PROCESSING_INSTRUCTION:
                {
                    nodes = readProcessingInstructionF(src);
                    break;
                }
            case DOC_TYPE:
                {
                    // surrogate hack for BEGIN_PAGE
                    readPage(src, input);
                    continue;
                }
        }
        appendNodes(current, nodes);
    }
}
Also used : ParentNode(nu.xom.ParentNode) Element(nu.xom.Element) Document(nu.xom.Document) Nodes(nu.xom.Nodes)

Aggregations

Document (nu.xom.Document)1 Element (nu.xom.Element)1 Nodes (nu.xom.Nodes)1 ParentNode (nu.xom.ParentNode)1