Search in sources :

Example 1 with Nodes

use of permafrost.tundra.xml.dom.Nodes in project Tundra by Permafrost.

the class xpath method get.

public static final void get(IData pipeline) throws ServiceException {
    // --- <<IS-START(get)>> ---
    // @subtype unknown
    // @sigtype java 3.5
    // [i] object:0:optional $content
    // [i] field:0:optional $encoding
    // [i] field:0:required $expression
    // [i] record:0:optional $namespace
    // [i] - field:0:optional default
    // [i] field:0:optional $recurse? {"false","true"}
    // [o] record:1:optional $nodes
    // [o] - object:0:required node
    // [o] - field:0:required name.qualified
    // [o] - field:0:optional name.local
    // [o] - field:0:optional name.prefix
    // [o] - field:0:optional name.uri
    // [o] - field:0:required type
    // [o] - field:0:optional value
    // [o] - record:1:optional attributes
    // [o] -- object:0:required node
    // [o] -- field:0:required name.qualified
    // [o] -- field:0:optional name.local
    // [o] -- field:0:optional name.prefix
    // [o] -- field:0:optional name.uri
    // [o] -- field:0:required type
    // [o] -- field:0:optional value
    // [o] - record:1:optional elements
    // [o] -- object:0:required node
    // [o] -- field:0:required name.qualified
    // [o] -- field:0:optional name.local
    // [o] -- field:0:optional name.prefix
    // [o] -- field:0:optional name.uri
    // [o] -- field:0:required type
    // [o] -- field:0:optional value
    // [o] field:0:required $nodes.length
    IDataCursor cursor = pipeline.getCursor();
    try {
        Object content = IDataHelper.get(cursor, "$content");
        Charset charset = IDataHelper.get(cursor, "$encoding", Charset.class);
        String expression = IDataHelper.get(cursor, "$expression", String.class);
        NamespaceContext namespace = IDataHelper.get(cursor, "$namespace", IDataNamespaceContext.class);
        boolean recurse = IDataHelper.getOrDefault(cursor, "$recurse?", Boolean.class, false);
        XPathExpression compiledExpression = XPathHelper.compile(expression, namespace);
        Node node = null;
        if (content instanceof Node) {
            node = (Node) content;
        } else if (content instanceof InputSource) {
            node = DocumentHelper.parse((InputSource) content, namespace);
        } else if (content != null) {
            node = DocumentHelper.parse(InputStreamHelper.normalize(content, charset), charset, true, namespace);
        }
        Nodes nodes = XPathHelper.get(node, compiledExpression);
        if (nodes != null) {
            IDataHelper.put(cursor, "$nodes", nodes.reflect(namespace, recurse));
            IDataHelper.put(cursor, "$nodes.length", nodes.size(), String.class);
        } else {
            IDataHelper.put(cursor, "$nodes.length", "0");
        }
    } catch (XPathExpressionException ex) {
        ExceptionHelper.raise(ex);
    } finally {
        cursor.destroy();
    }
// --- <<IS-END>> ---
}
Also used : XPathExpression(javax.xml.xpath.XPathExpression) InputSource(org.xml.sax.InputSource) IDataNamespaceContext(permafrost.tundra.xml.namespace.IDataNamespaceContext) NamespaceContext(javax.xml.namespace.NamespaceContext) XPathExpressionException(javax.xml.xpath.XPathExpressionException) Node(org.w3c.dom.Node) Charset(java.nio.charset.Charset) Nodes(permafrost.tundra.xml.dom.Nodes)

Aggregations

Charset (java.nio.charset.Charset)1 NamespaceContext (javax.xml.namespace.NamespaceContext)1 XPathExpression (javax.xml.xpath.XPathExpression)1 XPathExpressionException (javax.xml.xpath.XPathExpressionException)1 Node (org.w3c.dom.Node)1 InputSource (org.xml.sax.InputSource)1 Nodes (permafrost.tundra.xml.dom.Nodes)1 IDataNamespaceContext (permafrost.tundra.xml.namespace.IDataNamespaceContext)1