Search in sources :

Example 1 with Node

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

the class BinaryXMLCodec method writeDocument.

private void writeDocument(Document doc) throws IOException {
    if (DEBUG)
        System.err.println("writing document");
    writeXMLDeclaration(doc.getBaseURI());
    for (int i = 0; i < doc.getChildCount(); i++) {
        Node node = doc.getChild(i);
        if (node instanceof Element) {
            writeElement((Element) node);
        } else if (node instanceof Comment) {
            writeComment((Comment) node);
        } else if (node instanceof ProcessingInstruction) {
            writeProcessingInstruction((ProcessingInstruction) node);
        } else if (node instanceof DocType) {
            writeDocType((DocType) node);
        } else {
            throw new IllegalAddException("Cannot write node type: " + node);
        }
    }
    writeEndDocument();
    if (DEBUG)
        System.err.println("finished writing document");
}
Also used : Comment(nu.xom.Comment) IllegalAddException(nu.xom.IllegalAddException) ParentNode(nu.xom.ParentNode) Node(nu.xom.Node) Element(nu.xom.Element) ProcessingInstruction(nu.xom.ProcessingInstruction) DocType(nu.xom.DocType)

Example 2 with Node

use of nu.xom.Node 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)

Example 3 with Node

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

the class XQueryEvaluator method wrap.

/**
 * Converts a xom node into something readable by Saxon
 * @param node
 * @param config
 * @return
 */
static NodeInfo wrap(Node node, Configuration config) {
    if (node == null)
        // $NON-NLS-1$
        throw new IllegalArgumentException("node must not be null");
    if (node instanceof DocType)
        // $NON-NLS-1$
        throw new IllegalArgumentException("DocType can't be queried by XQuery/XPath");
    Node root = node;
    while (root.getParent() != null) {
        root = root.getParent();
    }
    XOMDocumentWrapper docWrapper = new XOMDocumentWrapper(root, config);
    return docWrapper.wrap(node);
}
Also used : XMLTableNode(org.teiid.query.processor.relational.XMLTableNode) Node(nu.xom.Node) RelationalNode(org.teiid.query.processor.relational.RelationalNode) XOMDocumentWrapper(net.sf.saxon.option.xom.XOMDocumentWrapper) DocType(nu.xom.DocType)

Example 4 with Node

use of nu.xom.Node in project ma-core-public by infiniteautomation.

the class XOMConverter method convertOutbound.

/* (non-Javadoc)
     * @see org.directwebremoting.Converter#convertOutbound(java.lang.Object, org.directwebremoting.OutboundContext)
     */
public OutboundVariable convertOutbound(Object data, OutboundContext outctx) throws MarshallException {
    try {
        // Using XSLT to convert to a stream. Setup the source
        if (!(data instanceof Node)) {
            throw new MarshallException(data.getClass());
        }
        Node node = (Node) data;
        String script = EnginePrivate.xmlStringToJavascriptDom(node.toXML());
        OutboundVariable ov = new SimpleOutboundVariable(script, outctx, false);
        outctx.put(data, ov);
        return ov;
    } catch (MarshallException ex) {
        throw ex;
    } catch (Exception ex) {
        throw new MarshallException(data.getClass(), ex);
    }
}
Also used : OutboundVariable(org.directwebremoting.extend.OutboundVariable) SimpleOutboundVariable(org.directwebremoting.dwrp.SimpleOutboundVariable) MarshallException(org.directwebremoting.extend.MarshallException) Node(nu.xom.Node) SimpleOutboundVariable(org.directwebremoting.dwrp.SimpleOutboundVariable) MarshallException(org.directwebremoting.extend.MarshallException)

Example 5 with Node

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

the class BinaryXMLCodec method readElement.

/**
 * Iterative pull parser reading an entire element subtree.
 */
private void readElement(ArrayByteList src, Element current, InputStream input) throws BinaryParsingException, IOException {
    while (true) {
        Node node = null;
        Element down = null;
        // 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:
                {
                    node = readText(src, type);
                    break;
                }
            case ATTRIBUTE:
                {
                    readAttribute(src, current, type);
                    continue;
                }
            case BEGIN_ELEMENT:
                {
                    down = readStartTag(src, type);
                    node = down;
                    break;
                }
            case END_ELEMENT:
                {
                    current = (Element) current.getParent();
                    // we're done with the root element
                    if (current == null)
                        return;
                    continue;
                }
            case COMMENT:
                {
                    node = readComment(src, type);
                    break;
                }
            case NAMESPACE_DECLARATION:
                {
                    readNamespaceDeclaration(src, current, type);
                    continue;
                }
            case PROCESSING_INSTRUCTION:
                {
                    node = readProcessingInstruction(src);
                    break;
                }
            case DOC_TYPE:
                {
                    // surrogate hack to identify BEGIN_PAGE
                    readPage(src, input);
                    continue;
                }
        }
        // if (DEBUG) System.err.println("read node=" + node.toXML());
        current.insertChild(node, current.getChildCount());
        // recurse down
        if (down != null)
            current = down;
    }
}
Also used : ParentNode(nu.xom.ParentNode) Node(nu.xom.Node) Element(nu.xom.Element)

Aggregations

Node (nu.xom.Node)5 Element (nu.xom.Element)3 ParentNode (nu.xom.ParentNode)3 DocType (nu.xom.DocType)2 IllegalAddException (nu.xom.IllegalAddException)2 XOMDocumentWrapper (net.sf.saxon.option.xom.XOMDocumentWrapper)1 Comment (nu.xom.Comment)1 Document (nu.xom.Document)1 NodeFactory (nu.xom.NodeFactory)1 Nodes (nu.xom.Nodes)1 ProcessingInstruction (nu.xom.ProcessingInstruction)1 WellformednessException (nu.xom.WellformednessException)1 SimpleOutboundVariable (org.directwebremoting.dwrp.SimpleOutboundVariable)1 MarshallException (org.directwebremoting.extend.MarshallException)1 OutboundVariable (org.directwebremoting.extend.OutboundVariable)1 RelationalNode (org.teiid.query.processor.relational.RelationalNode)1 XMLTableNode (org.teiid.query.processor.relational.XMLTableNode)1