Search in sources :

Example 1 with WellformednessException

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

the class BinaryXMLCodec method readDocument.

/**
 * Parses document from encoded src buffer; tokens appear in document order.
 */
private Document readDocument(ArrayByteList src, InputStream input) throws BinaryParsingException, IOException {
    if (DEBUG)
        System.err.println("reading document");
    readPage(src, input);
    Document doc = factory.startMakingDocument();
    // doc.setBaseURI(symbols[src.getInt()]);
    doc.setBaseURI(getInternedName(src.getInt()));
    boolean hasRootElement = false;
    int i = 0;
    // add children of document, retaining the exact same order found in input
    while (src.remaining() > 0) {
        Nodes nodes;
        // look ahead
        int type = src.get();
        if (DEBUG)
            System.err.println("reading type = " + toString(type));
        switch(// three low bits indicate node type
        type & 0x07) {
            case TEXT:
                {
                    throw new BinaryParsingException("Unreachable text");
                }
            case ATTRIBUTE:
                {
                    throw new BinaryParsingException("Unreachable attribute");
                }
            case BEGIN_ELEMENT:
                {
                    if (factory.getClass() == NodeFactory.class) {
                        // fast path
                        Element root = readStartTag(src, type);
                        // reads entire subtree
                        readElement(src, root, input);
                        nodes = new Nodes(root);
                    } else {
                        // slow path
                        Element root = readStartTagF(src, type, true);
                        if (root == null) {
                            throw new NullPointerException("Factory failed to create root element.");
                        }
                        doc.setRootElement(root);
                        readElementF(src, root, input);
                        nodes = factory.finishMakingElement(root);
                    }
                    break;
                }
            case END_ELEMENT:
                {
                    throw new BinaryParsingException("Unreachable end of element");
                }
            case COMMENT:
                {
                    nodes = readCommentF(src, type);
                    break;
                }
            case NAMESPACE_DECLARATION:
                {
                    throw new BinaryParsingException("Unreachable namespace declaration");
                }
            case PROCESSING_INSTRUCTION:
                {
                    nodes = readProcessingInstructionF(src);
                    break;
                }
            case DOC_TYPE:
                {
                    nodes = readDocTypeF(src);
                    break;
                }
            default:
                {
                    throw new BinaryParsingException("Illegal node type code=" + type);
                }
        }
        // append nodes:
        for (int j = 0; j < nodes.size(); j++) {
            Node node = nodes.get(j);
            if (node instanceof Element) {
                // replace fake root with real root
                if (hasRootElement) {
                    throw new IllegalAddException("Factory returned multiple root elements");
                }
                doc.setRootElement((Element) node);
                hasRootElement = true;
            } else {
                doc.insertChild(node, i);
            }
            i++;
        }
    }
    if (!hasRootElement)
        throw new WellformednessException("Factory attempted to remove the root element");
    factory.finishMakingDocument(doc);
    if (DEBUG)
        System.err.println("finished reading document");
    return doc;
}
Also used : NodeFactory(nu.xom.NodeFactory) WellformednessException(nu.xom.WellformednessException) IllegalAddException(nu.xom.IllegalAddException) Element(nu.xom.Element) ParentNode(nu.xom.ParentNode) Node(nu.xom.Node) Document(nu.xom.Document) Nodes(nu.xom.Nodes)

Aggregations

Document (nu.xom.Document)1 Element (nu.xom.Element)1 IllegalAddException (nu.xom.IllegalAddException)1 Node (nu.xom.Node)1 NodeFactory (nu.xom.NodeFactory)1 Nodes (nu.xom.Nodes)1 ParentNode (nu.xom.ParentNode)1 WellformednessException (nu.xom.WellformednessException)1