use of javax.xml.xpath.XPathExpressionException in project cxf by apache.
the class JAXWSBindingParser method queryXPathNode.
private Node queryXPathNode(Node target, String expression) {
NodeList nlst;
try {
ContextImpl contextImpl = new ContextImpl(target);
XPath xpath = XPathFactory.newInstance().newXPath();
xpath.setNamespaceContext(contextImpl);
nlst = (NodeList) xpath.evaluate(expression, target, XPathConstants.NODESET);
} catch (XPathExpressionException e) {
Message msg = new Message("XPATH_ERROR", LOG, new Object[] { expression });
throw new ToolException(msg, e);
}
if (nlst.getLength() != 1) {
Message msg = new Message("ERROR_TARGETNODE_WITH_XPATH", LOG, new Object[] { expression });
throw new ToolException(msg);
}
Node rnode = nlst.item(0);
if (!(rnode instanceof Element)) {
return null;
}
return rnode;
}
use of javax.xml.xpath.XPathExpressionException in project vcell by virtualcell.
the class DataBaseReferenceReader method getGOTerm.
public static String getGOTerm(String goId) {
String name = null;
// URL a GO Term in OBO xml format
URL u;
try {
String url_str = GO_URL_prefix + goId + GO_URL_surfix;
u = new URL(url_str);
// Connect
HttpURLConnection urlConnection;
urlConnection = (HttpURLConnection) u.openConnection();
// Parse an XML document from the connection
InputStream inputStream;
inputStream = urlConnection.getInputStream();
Document xml;
xml = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(inputStream);
inputStream.close();
// XPath is here used to locate parts of an XML document
XPath xpath = XPathFactory.newInstance().newXPath();
// Locate the term name and print it out
name = xpath.compile("/obo/term/name").evaluate(xml);
// System.out.println("Term name:"+xpath.compile("/obo/term/name").evaluate(xml));
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (XPathExpressionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return name;
}
use of javax.xml.xpath.XPathExpressionException in project Payara by payara.
the class BaseDevTest method evalXPath.
/**
* Evaluates the Xpath expression
*
* @param expr The expression to evaluate
* @param f The file to parse
* @param ret The return type of the expression can be
*
* XPathConstants.NODESET XPathConstants.BOOLEAN XPathConstants.NUMBER XPathConstants.STRING XPathConstants.NODE
*
* @return the object after evaluation can be of type number maps to a java.lang.Double string maps to a
* java.lang.String boolean maps to a java.lang.Boolean node-set maps to an org.w3c.dom.NodeList
*
* @throws XPathExpressionException
* @throws IOException
* @throws SAXException
* @throws ParserConfigurationException
*/
public Object evalXPath(String expr, File f, QName ret) {
try {
DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
// never forget this!
domFactory.setNamespaceAware(true);
DocumentBuilder builder = domFactory.newDocumentBuilder();
Document doc = builder.parse(f);
write("Parsing" + f.getAbsolutePath());
XPathFactory factory = XPathFactory.newInstance();
XPath xpath = factory.newXPath();
XPathExpression xexpr = xpath.compile(expr);
Object result = xexpr.evaluate(doc, ret);
write("Evaluating" + f.getAbsolutePath());
return result;
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e.getMessage(), e);
}
}
use of javax.xml.xpath.XPathExpressionException in project gocd by gocd.
the class ArtifactPropertiesGenerator method generate.
public void generate(GoPublisher publisher, File buildWorkingDirectory) {
File file = new File(buildWorkingDirectory, getSrc());
String indent = " ";
if (!file.exists()) {
publisher.consumeLine(format("%sFailed to create property %s. File %s does not exist.", indent, getName(), file.getAbsolutePath()));
} else {
try {
if (!XpathUtils.nodeExists(file, getXpath())) {
publisher.consumeLine(format("%sFailed to create property %s. Nothing matched xpath \"%s\" in the file: %s.", indent, getName(), getXpath(), file.getAbsolutePath()));
} else {
String value = XpathUtils.evaluate(file, getXpath());
publisher.setProperty(new Property(getName(), value));
publisher.consumeLine(format("%sProperty %s = %s created." + "\n", indent, getName(), value));
}
} catch (Exception e) {
String error = (e instanceof XPathExpressionException) ? (format("Illegal xpath: \"%s\"", getXpath())) : ExceptionUtils.messageOf(e);
String message = format("%sFailed to create property %s. %s", indent, getName(), error);
publisher.reportErrorMessage(message, e);
}
}
}
use of javax.xml.xpath.XPathExpressionException in project ORCID-Source by ORCID.
the class TransformFundRefDataIntoCSV method getFundrefOrganization.
/**
* Parse a RDF node and convert it into a FundRefOrganization object
* */
private FundRefOrganization getFundrefOrganization(Document xmlDocument, NamedNodeMap attrs) {
FundRefOrganization organization = new FundRefOrganization();
try {
Node node = attrs.getNamedItem("rdf:resource");
String itemDoi = node.getNodeValue();
LOGGER.info("Processing item {}", itemDoi);
// Get organization name
String orgName = (String) xPath.compile(orgNameExpression.replace("%s", itemDoi)).evaluate(xmlDocument, XPathConstants.STRING);
//Replace "U.S." with "US" to match RingGold info
orgName = orgName.replace("U.S.", "US");
// Get organization alt name
String orgAltName = (String) xPath.compile(orgAltNameExpression.replace("%s", itemDoi)).evaluate(xmlDocument, XPathConstants.STRING);
// Get country geoname id
Node countryNode = (Node) xPath.compile(orgCountryExpression.replace("%s", itemDoi)).evaluate(xmlDocument, XPathConstants.NODE);
NamedNodeMap countryAttrs = countryNode.getAttributes();
String countryGeonameUrl = countryAttrs.getNamedItem("rdf:resource").getNodeValue();
// Get state geoname id
Node stateNode = (Node) xPath.compile(orgStateExpression.replace("%s", itemDoi)).evaluate(xmlDocument, XPathConstants.NODE);
String stateGeoNameCode = null;
if (stateNode != null) {
NamedNodeMap stateAttrs = stateNode.getAttributes();
stateGeoNameCode = stateAttrs.getNamedItem("rdf:resource").getNodeValue();
}
// Get type
String orgType = (String) xPath.compile(orgTypeExpression.replace("%s", itemDoi)).evaluate(xmlDocument, XPathConstants.STRING);
// Get subType
String orgSubType = (String) xPath.compile(orgSubTypeExpression.replace("%s", itemDoi)).evaluate(xmlDocument, XPathConstants.STRING);
// Fill the organization object
organization.type = StringUtils.isBlank(orgType) ? null : orgType;
organization.id = StringUtils.isBlank(itemDoi) ? null : itemDoi;
organization.name = StringUtils.isBlank(orgName) ? null : orgName;
organization.altName = StringUtils.isBlank(orgAltName) ? null : orgAltName;
organization.subtype = StringUtils.isBlank(orgSubType) ? null : orgSubType;
// Fetch country code from geonames
if (StringUtils.isNotBlank(countryGeonameUrl))
organization.country = fetchFromGeoNames(countryGeonameUrl, "countryCode");
// Fetch state from geonames
if (StringUtils.isNotBlank(stateGeoNameCode)) {
organization.state = fetchFromGeoNames(stateGeoNameCode, "STATE");
}
} catch (XPathExpressionException xpe) {
LOGGER.error("XPathExpressionException {}", xpe.getMessage());
}
return organization;
}
Aggregations