use of com.sun.org.apache.xpath.internal.objects.XObject in project Payara by payara.
the class VerifierTest method getXPathValueForNonRuntime.
/*
*getXPathValueForNonRuntime(String xpath)
* return String - is the value of the element specified in the xpath.
*/
public String getXPathValueForNonRuntime(String xpath) {
try {
String value = null;
Document d = getVerifierContext().getDocument();
if (d == null)
return null;
XObject result = XPathAPI.eval(d, xpath, (PrefixResolver) new XpathPrefixResolver(d));
NodeList nl = result.nodelist();
for (int i = 0; i < nl.getLength(); i++) {
Node n = ((Node) nl.item(i)).getFirstChild();
if (n == null)
return null;
value = n.getNodeValue();
}
return value;
} catch (Exception ex) {
ex.printStackTrace();
return null;
}
}
use of com.sun.org.apache.xpath.internal.objects.XObject in project gocd by gocd.
the class XmlXpathContext method tryGetNodeSet.
private IRubyObject tryGetNodeSet(ThreadContext thread_context, String expr) throws XPathExpressionException {
XObject xobj = null;
Node contextNode = context.node;
try {
com.sun.org.apache.xpath.internal.XPath xpathInternal = new com.sun.org.apache.xpath.internal.XPath(expr, null, prefixResolver, com.sun.org.apache.xpath.internal.XPath.SELECT);
if (contextNode == null)
xobj = xpathInternal.execute(xpathSupport, DTM.NULL, prefixResolver);
else
xobj = xpathInternal.execute(xpathSupport, contextNode, prefixResolver);
switch(xobj.getType()) {
case XObject.CLASS_BOOLEAN:
return thread_context.getRuntime().newBoolean(xobj.bool());
case XObject.CLASS_NUMBER:
return thread_context.getRuntime().newFloat(xobj.num());
case XObject.CLASS_NODESET:
NodeList nodeList = xobj.nodelist();
XmlNodeSet xmlNodeSet = (XmlNodeSet) NokogiriService.XML_NODESET_ALLOCATOR.allocate(getRuntime(), getNokogiriClass(getRuntime(), "Nokogiri::XML::NodeSet"));
xmlNodeSet.setNodeList(nodeList);
xmlNodeSet.initialize(thread_context.getRuntime(), context);
return xmlNodeSet;
default:
return thread_context.getRuntime().newString(xobj.str());
}
} catch (TransformerException ex) {
throw new XPathExpressionException(expr);
}
}
use of com.sun.org.apache.xpath.internal.objects.XObject in project Payara by payara.
the class VerifierTest method getNonRuntimeCountNodeSet.
public int getNonRuntimeCountNodeSet(String xpath) {
try {
// int value = -1;
Document d = getVerifierContext().getDocument();
if (d == null)
return -1;
XObject result = XPathAPI.eval(d, xpath, (PrefixResolver) new XpathPrefixResolver(d));
NodeList nl = result.nodelist();
NodeSet ns = new NodeSet(nl);
return ns.getLength();
} catch (Exception ex) {
ex.printStackTrace();
return -1;
}
}
use of com.sun.org.apache.xpath.internal.objects.XObject in project tmdm-studio-se by Talend.
the class Util method getTextNodes.
public static String[] getTextNodes(Node contextNode, String xPath, Node namespaceNode) throws XtentisException {
String[] results = null;
// test for hard-coded values
if (xPath.startsWith("\"") && xPath.endsWith("\"")) {
// $NON-NLS-1$ //$NON-NLS-2$
return new String[] { xPath.substring(1, xPath.length() - 1) };
}
// test for incomplete path (elements missing /text())
if (!xPath.matches(".*@[^/\\]]+")) {
// $NON-NLS-1$
if (!xPath.endsWith(")")) {
// $NON-NLS-1$
// $NON-NLS-1$
xPath += "/text()";
}
}
try {
XObject xo = XPathAPI.eval(contextNode, xPath, namespaceNode);
if (xo.getType() == XObject.CLASS_NODESET) {
NodeList l = xo.nodelist();
int len = l.getLength();
results = new String[len];
for (int i = 0; i < len; i++) {
Node n = l.item(i);
results[i] = n.getNodeValue();
}
} else {
results = new String[] { xo.toString() };
}
} catch (Exception e) {
String err = Messages.Util_18 + xPath + Messages.Util_19 + e.getClass().getName() + Messages.Util_20 + e.getLocalizedMessage();
throw new XtentisException(err);
}
return results;
}
use of com.sun.org.apache.xpath.internal.objects.XObject in project freemarker by apache.
the class SunInternalXalanXPathSupport method executeQuery.
public synchronized TemplateModel executeQuery(Object context, String xpathQuery) throws TemplateModelException {
if (!(context instanceof Node)) {
if (context != null) {
if (isNodeList(context)) {
int cnt = ((List) context).size();
if (cnt != 0) {
throw new TemplateModelException("Cannot perform an XPath query against a node set of " + cnt + " nodes. Expecting a single node." + ERRMSG_RECOMMEND_JAXEN);
} else {
throw new TemplateModelException(ERRMSG_EMPTY_NODE_SET);
}
} else {
throw new TemplateModelException("Cannot perform an XPath query against a " + context.getClass().getName() + ". Expecting a single org.w3c.dom.Node.");
}
} else {
throw new TemplateModelException(ERRMSG_EMPTY_NODE_SET);
}
}
Node node = (Node) context;
try {
XPath xpath = new XPath(xpathQuery, null, customPrefixResolver, XPath.SELECT, null);
int ctxtNode = xpathContext.getDTMHandleFromNode(node);
XObject xresult = xpath.execute(xpathContext, ctxtNode, customPrefixResolver);
if (xresult instanceof XNodeSet) {
NodeListModel result = new NodeListModel(node);
result.xpathSupport = this;
NodeIterator nodeIterator = xresult.nodeset();
Node n;
do {
n = nodeIterator.nextNode();
if (n != null) {
result.add(n);
}
} while (n != null);
return result.size() == 1 ? result.get(0) : result;
}
if (xresult instanceof XBoolean) {
return ((XBoolean) xresult).bool() ? TemplateBooleanModel.TRUE : TemplateBooleanModel.FALSE;
}
if (xresult instanceof XNull) {
return null;
}
if (xresult instanceof XString) {
return new SimpleScalar(xresult.toString());
}
if (xresult instanceof XNumber) {
return new SimpleNumber(Double.valueOf(((XNumber) xresult).num()));
}
throw new TemplateModelException("Cannot deal with type: " + xresult.getClass().getName());
} catch (TransformerException te) {
throw new TemplateModelException(te);
}
}
Aggregations