use of javax.xml.xpath.XPathExpressionException 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 javax.xml.xpath.XPathExpressionException in project honeycomb by altamiracorp.
the class ConfigurationParser method parseAdapters.
private static Map<String, Map<String, String>> parseAdapters(Document doc) {
// Extract adapter names from document
NodeList adapterNameNodes;
try {
adapterNameNodes = (NodeList) xPath.evaluate(QUERY_ADAPTER_NAMES, doc, XPathConstants.NODESET);
} catch (XPathExpressionException e) {
logger.error("Unable to parse adapter names from honeycomb configuration.", e);
throw new RuntimeException("Exception while parsing adapter names from honeycomb configuration.", e);
}
List<String> adapterNames = Lists.newArrayList();
for (int i = 0; i < adapterNameNodes.getLength(); i++) {
Node adapterNameNode = adapterNameNodes.item(i);
if (adapterNameNode.getNodeType() == Node.ATTRIBUTE_NODE) {
adapterNames.add(adapterNameNode.getNodeValue());
}
}
// Extract individual adapter options from the document
ImmutableMap.Builder<String, Map<String, String>> adapters = ImmutableMap.builder();
for (String adapterName : adapterNames) {
adapters.put(adapterName, parseOptions(adapterName, doc));
}
return adapters.build();
}
use of javax.xml.xpath.XPathExpressionException in project atlas by alibaba.
the class ManifestFileUtils method getPackage.
public static String getPackage(File manifestFile) {
String version = manifestMap.get(manifestFile.getAbsolutePath());
if (null != version) {
return version;
}
XPath xpath = AndroidXPathFactory.newXPath();
try {
version = xpath.evaluate("/manifest/@package", new InputSource(new FileInputStream(manifestFile)));
manifestMap.put(manifestFile.getAbsolutePath(), version);
return version;
} catch (XPathExpressionException e) {
// won't happen.
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
}
return null;
}
use of javax.xml.xpath.XPathExpressionException in project robovm by robovm.
the class XPathImpl method evaluate.
/**
* <p>Evaluate an XPath expression in the context of the specified <code>InputSource</code>
* and return the result as the specified type.</p>
*
* <p>This method builds a data model for the {@link InputSource} and calls
* {@link #evaluate(String expression, Object item, QName returnType)} on the resulting document object.</p>
*
* <p>See "Evaluation of XPath Expressions" section of JAXP 1.3 spec
* for context item evaluation,
* variable, function and QName resolution and return type conversion.</p>
*
* <p>If <code>returnType</code> is not one of the types defined in {@link XPathConstants},
* then an <code>IllegalArgumentException</code> is thrown.</p>
*
* <p>If <code>expression</code>, <code>source</code> or <code>returnType</code> is <code>null</code>,
* then a <code>NullPointerException</code> is thrown.</p>
*
* @param expression The XPath expression.
* @param source The input source of the document to evaluate over.
* @param returnType The desired return type.
*
* @return The <code>Object</code> that encapsulates the result of evaluating the expression.
*
* @throws XPathExpressionException If expression cannot be evaluated.
* @throws IllegalArgumentException If <code>returnType</code> is not one of the types defined in {@link XPathConstants}.
* @throws NullPointerException If <code>expression</code>, <code>source</code> or <code>returnType</code>
* is <code>null</code>.
*/
public Object evaluate(String expression, InputSource source, QName returnType) throws XPathExpressionException {
// Checking validity of different parameters
if (source == null) {
String fmsg = XSLMessages.createXPATHMessage(XPATHErrorResources.ER_ARG_CANNOT_BE_NULL, new Object[] { "source" });
throw new NullPointerException(fmsg);
}
if (expression == null) {
String fmsg = XSLMessages.createXPATHMessage(XPATHErrorResources.ER_ARG_CANNOT_BE_NULL, new Object[] { "XPath expression" });
throw new NullPointerException(fmsg);
}
if (returnType == null) {
String fmsg = XSLMessages.createXPATHMessage(XPATHErrorResources.ER_ARG_CANNOT_BE_NULL, new Object[] { "returnType" });
throw new NullPointerException(fmsg);
}
//returnType need to be defined in XPathConstants
if (!isSupported(returnType)) {
String fmsg = XSLMessages.createXPATHMessage(XPATHErrorResources.ER_UNSUPPORTED_RETURN_TYPE, new Object[] { returnType.toString() });
throw new IllegalArgumentException(fmsg);
}
try {
Document document = getParser().parse(source);
XObject resultObject = eval(expression, document);
return getResultAsType(resultObject, returnType);
} catch (SAXException e) {
throw new XPathExpressionException(e);
} catch (IOException e) {
throw new XPathExpressionException(e);
} catch (javax.xml.transform.TransformerException te) {
Throwable nestedException = te.getException();
if (nestedException instanceof javax.xml.xpath.XPathFunctionException) {
throw (javax.xml.xpath.XPathFunctionException) nestedException;
} else {
throw new XPathExpressionException(te);
}
}
}
use of javax.xml.xpath.XPathExpressionException in project robovm by robovm.
the class XPathExpressionImpl method evaluate.
/**
* <p>Evaluate the compiled XPath expression in the context of the
* specified <code>InputSource</code> and return the result as the
* specified type.</p>
*
* <p>This method builds a data model for the {@link InputSource} and calls
* {@link #evaluate(Object item, QName returnType)} on the resulting
* document object.</p>
*
* <p>See "Evaluation of XPath Expressions" section of JAXP 1.3 spec
* for context item evaluation,
* variable, function and QName resolution and return type conversion.</p>
*
* <p>If <code>returnType</code> is not one of the types defined in
* {@link XPathConstants},
* then an <code>IllegalArgumentException</code> is thrown.</p>
*
*<p>If <code>source</code> or <code>returnType</code> is <code>null</code>,
* then a <code>NullPointerException</code> is thrown.</p>
*
* @param source The <code>InputSource</code> of the document to evaluate
* over.
* @param returnType The desired return type.
*
* @return The <code>Object</code> that is the result of evaluating the
* expression and converting the result to
* <code>returnType</code>.
*
* @throws XPathExpressionException If the expression cannot be evaluated.
* @throws IllegalArgumentException If <code>returnType</code> is not one
* of the types defined in {@link XPathConstants}.
* @throws NullPointerException If <code>source</code> or
* <code>returnType</code> is <code>null</code>.
*/
public Object evaluate(InputSource source, QName returnType) throws XPathExpressionException {
if ((source == null) || (returnType == null)) {
String fmsg = XSLMessages.createXPATHMessage(XPATHErrorResources.ER_SOURCE_RETURN_TYPE_CANNOT_BE_NULL, null);
throw new NullPointerException(fmsg);
}
// defined in XPathConstants
if (!isSupported(returnType)) {
String fmsg = XSLMessages.createXPATHMessage(XPATHErrorResources.ER_UNSUPPORTED_RETURN_TYPE, new Object[] { returnType.toString() });
throw new IllegalArgumentException(fmsg);
}
try {
if (dbf == null) {
dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
dbf.setValidating(false);
}
db = dbf.newDocumentBuilder();
Document document = db.parse(source);
return eval(document, returnType);
} catch (Exception e) {
throw new XPathExpressionException(e);
}
}
Aggregations