Search in sources :

Example 1 with NamespaceContext

use of javax.xml.namespace.NamespaceContext in project openhab1-addons by openhab.

the class IhcResourceInteractionService method getValue.

private String getValue(Node n, String expr) throws XPathExpressionException {
    XPath xpath = XPathFactory.newInstance().newXPath();
    xpath.setNamespaceContext(new NamespaceContext() {

        @Override
        public String getNamespaceURI(String prefix) {
            if (prefix == null) {
                throw new NullPointerException("Null prefix");
            } else if ("SOAP-ENV".equals(prefix)) {
                return "http://schemas.xmlsoap.org/soap/envelope/";
            } else if ("ns1".equals(prefix)) {
                return "utcs";
            }
            // else if ("ns2".equals(prefix)) return "utcs.values";
            return "utcs.values";
        // return null;
        }

        @Override
        public String getPrefix(String uri) {
            return null;
        }

        @Override
        @SuppressWarnings("rawtypes")
        public Iterator getPrefixes(String uri) {
            throw new UnsupportedOperationException();
        }
    });
    XPathExpression pathExpr = xpath.compile(expr);
    return (String) pathExpr.evaluate(n, XPathConstants.STRING);
}
Also used : XPath(javax.xml.xpath.XPath) XPathExpression(javax.xml.xpath.XPathExpression) NamespaceContext(javax.xml.namespace.NamespaceContext) Iterator(java.util.Iterator)

Example 2 with NamespaceContext

use of javax.xml.namespace.NamespaceContext in project openhab1-addons by openhab.

the class WSBaseDataType method parseValue.

public static String parseValue(String xml, String xpathExpression) throws IhcExecption {
    InputStream is;
    try {
        is = new ByteArrayInputStream(xml.getBytes("UTF8"));
    } catch (UnsupportedEncodingException e) {
        throw new IhcExecption(e);
    }
    XPath xpath = XPathFactory.newInstance().newXPath();
    InputSource inputSource = new InputSource(is);
    xpath.setNamespaceContext(new NamespaceContext() {

        @Override
        public String getNamespaceURI(String prefix) {
            if (prefix == null) {
                throw new NullPointerException("Null prefix");
            } else if ("SOAP-ENV".equals(prefix)) {
                return "http://schemas.xmlsoap.org/soap/envelope/";
            } else if ("ns1".equals(prefix)) {
                return "utcs";
            } else if ("ns2".equals(prefix)) {
                return "utcs.values";
            }
            return null;
        }

        @Override
        public String getPrefix(String uri) {
            return null;
        }

        @Override
        @SuppressWarnings("rawtypes")
        public Iterator getPrefixes(String uri) {
            throw new UnsupportedOperationException();
        }
    });
    try {
        return (String) xpath.evaluate(xpathExpression, inputSource, XPathConstants.STRING);
    } catch (XPathExpressionException e) {
        throw new IhcExecption(e);
    }
}
Also used : XPath(javax.xml.xpath.XPath) InputSource(org.xml.sax.InputSource) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) XPathExpressionException(javax.xml.xpath.XPathExpressionException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IhcExecption(org.openhab.binding.ihc.ws.IhcExecption) ByteArrayInputStream(java.io.ByteArrayInputStream) NamespaceContext(javax.xml.namespace.NamespaceContext) Iterator(java.util.Iterator)

Example 3 with NamespaceContext

use of javax.xml.namespace.NamespaceContext in project Tundra by Permafrost.

the class xpath method exists.

// ---( server methods )---
public static final void exists(IData pipeline) throws ServiceException {
    // --- <<IS-START(exists)>> ---
    // @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
    // [o] field:0:required $exists?
    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);
        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);
        }
        IDataHelper.put(cursor, "$exists?", XPathHelper.exists(node, compiledExpression), String.class);
    } 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)

Example 4 with NamespaceContext

use of javax.xml.namespace.NamespaceContext 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)

Example 5 with NamespaceContext

use of javax.xml.namespace.NamespaceContext in project webservices-axiom by apache.

the class XMLStreamWriterNamespaceContextProvider method isBound.

/**
     * @param prefix 
     * @param namespace
     * @return true if the prefix is associated with the namespace in the current context
     */
public boolean isBound(String prefix, String namespace) throws StreamException {
    try {
        // of this issue.
        if ("xml".equals(prefix)) {
            return true;
        }
        // NOTE: Calling getNamespaceContext() on many XMLStreamWriter implementations is expensive.
        // Please use other writer methods first.
        // For consistency, convert null arguments.
        // This helps get around the parser implementation differences.
        // In addition, the getPrefix/getNamespace methods cannot be called with null parameters.
        prefix = (prefix == null) ? "" : prefix;
        namespace = (namespace == null) ? "" : namespace;
        if (namespace.length() > 0) {
            // QUALIFIED NAMESPACE
            // Get the namespace associated with the prefix
            String writerPrefix = writer.getPrefix(namespace);
            if (prefix.equals(writerPrefix)) {
                return true;
            }
            // So try getting the namespace as a second step.
            if (writerPrefix != null) {
                NamespaceContext nsContext = writer.getNamespaceContext();
                if (nsContext != null) {
                    String writerNS = nsContext.getNamespaceURI(prefix);
                    return namespace.equals(writerNS);
                }
            }
            return false;
        } else {
            // Neither XML 1.0 nor XML 1.1 allow to associate a prefix with an unqualified name (see also AXIOM-372).
            if (prefix.length() > 0) {
                throw new StreamException("Invalid namespace declaration: Prefixed namespace bindings may not be empty.");
            }
            // protected
            try {
                String writerPrefix = writer.getPrefix("");
                if (writerPrefix != null && writerPrefix.length() == 0) {
                    return true;
                }
            } catch (Throwable t) {
                if (log.isDebugEnabled()) {
                    log.debug("Caught exception from getPrefix(\"\"). Processing continues: " + t);
                }
            }
            // Fallback to using the namespace context
            NamespaceContext nsContext = writer.getNamespaceContext();
            if (nsContext != null) {
                String writerNS = nsContext.getNamespaceURI("");
                if (writerNS != null && writerNS.length() > 0) {
                    return false;
                }
            }
            return true;
        }
    } catch (XMLStreamException ex) {
        throw new StreamException(ex);
    }
}
Also used : XMLStreamException(javax.xml.stream.XMLStreamException) NamespaceContext(javax.xml.namespace.NamespaceContext) StreamException(org.apache.axiom.core.stream.StreamException) XMLStreamException(javax.xml.stream.XMLStreamException)

Aggregations

NamespaceContext (javax.xml.namespace.NamespaceContext)23 XPath (javax.xml.xpath.XPath)7 InputStream (java.io.InputStream)5 Iterator (java.util.Iterator)5 XPathExpression (javax.xml.xpath.XPathExpression)5 InputSource (org.xml.sax.InputSource)5 OMElement (org.apache.axiom.om.OMElement)4 Node (org.w3c.dom.Node)4 Charset (java.nio.charset.Charset)3 XMLStreamReader (javax.xml.stream.XMLStreamReader)3 XPathExpressionException (javax.xml.xpath.XPathExpressionException)3 XPathFactory (javax.xml.xpath.XPathFactory)3 IDataNamespaceContext (permafrost.tundra.xml.namespace.IDataNamespaceContext)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 HashSet (java.util.HashSet)2 OMXMLStreamReaderConfiguration (org.apache.axiom.om.OMXMLStreamReaderConfiguration)2 NodeList (org.w3c.dom.NodeList)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 Date (java.util.Date)1 QName (javax.xml.namespace.QName)1