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);
}
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);
}
}
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>> ---
}
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>> ---
}
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);
}
}
Aggregations