use of javax.xml.xpath.XPathExpressionException in project ORCID-Source by ORCID.
the class TransformFundRefDataIntoCSV method loadFundRefOrgs.
/*****************************************************************************
******************************* FUNDREF FUNCTIONS ***************************
***************************************************************************** */
/**
* Load data from FundRef
* */
private List<FundRefOrganization> loadFundRefOrgs() {
List<FundRefOrganization> fundRefOrgs = new ArrayList<FundRefOrganization>();
System.out.println("Begin loading FundRef orgs");
try {
FileInputStream file = new FileInputStream(fundRefFile);
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = builderFactory.newDocumentBuilder();
Document xmlDocument = builder.parse(file);
// Parent node
NodeList nodeList = (NodeList) xPath.compile(conceptsExpression).evaluate(xmlDocument, XPathConstants.NODESET);
for (int i = 0; i < nodeList.getLength(); i++) {
FundRefOrganization fOrg = getFundrefOrganization(xmlDocument, nodeList.item(i).getAttributes());
fundRefOrgs.add(fOrg);
}
} catch (FileNotFoundException fne) {
LOGGER.error("Unable to read file {}", fundRefFile);
} catch (ParserConfigurationException pce) {
LOGGER.error("Unable to initialize the DocumentBuilder");
} catch (IOException ioe) {
LOGGER.error("Unable to parse document {}", fundRefFile);
} catch (SAXException se) {
LOGGER.error("Unable to parse document {}", fundRefFile);
} catch (XPathExpressionException xpe) {
LOGGER.error("XPathExpressionException {}", xpe.getMessage());
}
return fundRefOrgs;
}
use of javax.xml.xpath.XPathExpressionException in project opennms by OpenNMS.
the class AbstractXmlCollectionHandler method getTimeStamp.
/**
* Gets the time stamp.
*
* @param doc the doc
* @param xpath the xpath
* @param group the group
* @return the time stamp
* @throws XPathExpressionException the x path expression exception
*/
protected Date getTimeStamp(Document doc, XPath xpath, XmlGroup group) throws XPathExpressionException {
if (group.getTimestampXpath() == null) {
return null;
}
String pattern = group.getTimestampFormat() == null ? "yyyy-MM-dd HH:mm:ss" : group.getTimestampFormat();
LOG.debug("getTimeStamp: retrieving custom timestamp to be used when updating RRDs using XPATH {} and pattern {}", group.getTimestampXpath(), pattern);
Node tsNode = (Node) xpath.evaluate(group.getTimestampXpath(), doc, XPathConstants.NODE);
if (tsNode == null) {
LOG.warn("getTimeStamp: can't find the custom timestamp using XPATH {}", group.getTimestampXpath());
return null;
}
Date date = null;
String value = tsNode.getNodeValue() == null ? tsNode.getTextContent() : tsNode.getNodeValue();
LOG.debug("getTimeStamp: time stamp value is {}", value);
try {
DateTimeFormatter dtf = DateTimeFormat.forPattern(pattern);
DateTime dateTime = dtf.parseDateTime(value);
date = dateTime.toDate();
} catch (Exception e) {
LOG.warn("getTimeStamp: can't convert custom timetime {} using pattern {}", value, pattern);
}
return date;
}
use of javax.xml.xpath.XPathExpressionException in project amos-ss17-alexa by c-i-ber.
the class XMLParser method getPrettyXML.
public static String getPrettyXML(String xmlString) {
try {
Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new InputSource(new ByteArrayInputStream(xmlString.getBytes("utf-8"))));
XPath xPath = XPathFactory.newInstance().newXPath();
NodeList nodeList = (NodeList) xPath.evaluate("//text()[normalize-space()='']", document, XPathConstants.NODESET);
for (int i = 0; i < nodeList.getLength(); ++i) {
Node node = nodeList.item(i);
node.getParentNode().removeChild(node);
}
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
StringWriter stringWriter = new StringWriter();
StreamResult streamResult = new StreamResult(stringWriter);
transformer.transform(new DOMSource(document), streamResult);
return stringWriter.toString();
} catch (ParserConfigurationException | XPathExpressionException | TransformerException | IOException | SAXException e) {
log.error(e.getMessage());
}
return null;
}
use of javax.xml.xpath.XPathExpressionException in project j2objc by google.
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);
}
}
use of javax.xml.xpath.XPathExpressionException in project buck by facebook.
the class DefaultAndroidManifestReader method getLauncherActivities.
@Override
public List<String> getLauncherActivities() {
try {
NodeList nodes;
nodes = (NodeList) launchableActivitiesExpression.evaluate(doc, XPathConstants.NODESET);
List<String> activities = Lists.newArrayList();
for (int i = 0; i < nodes.getLength(); i++) {
activities.add(nodes.item(i).getTextContent());
}
return activities;
} catch (XPathExpressionException e) {
throw new RuntimeException(e);
}
}
Aggregations