Search in sources :

Example 81 with XPathExpressionException

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;
}
Also used : DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) XPathExpressionException(javax.xml.xpath.XPathExpressionException) NodeList(org.w3c.dom.NodeList) ArrayList(java.util.ArrayList) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) Document(org.w3c.dom.Document) FileInputStream(java.io.FileInputStream) SAXException(org.xml.sax.SAXException) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Example 82 with XPathExpressionException

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;
}
Also used : Node(org.w3c.dom.Node) OnmsNode(org.opennms.netmgt.model.OnmsNode) DateTimeFormatter(org.joda.time.format.DateTimeFormatter) Date(java.util.Date) DateTime(org.joda.time.DateTime) XPathExpressionException(javax.xml.xpath.XPathExpressionException) CollectionException(org.opennms.netmgt.collection.api.CollectionException) ParseException(java.text.ParseException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException)

Example 83 with XPathExpressionException

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;
}
Also used : XPath(javax.xml.xpath.XPath) InputSource(org.xml.sax.InputSource) DOMSource(javax.xml.transform.dom.DOMSource) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) XPathExpressionException(javax.xml.xpath.XPathExpressionException) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) IOException(java.io.IOException) Document(org.w3c.dom.Document) SAXException(org.xml.sax.SAXException) StringWriter(java.io.StringWriter) ByteArrayInputStream(java.io.ByteArrayInputStream) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) TransformerException(javax.xml.transform.TransformerException)

Example 84 with XPathExpressionException

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);
    }
}
Also used : XPathExpressionException(javax.xml.xpath.XPathExpressionException) Document(org.w3c.dom.Document) XPathExpressionException(javax.xml.xpath.XPathExpressionException) TransformerException(javax.xml.transform.TransformerException)

Example 85 with XPathExpressionException

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);
    }
}
Also used : XPathExpressionException(javax.xml.xpath.XPathExpressionException) NodeList(org.w3c.dom.NodeList)

Aggregations

XPathExpressionException (javax.xml.xpath.XPathExpressionException)157 XPath (javax.xml.xpath.XPath)79 NodeList (org.w3c.dom.NodeList)74 Document (org.w3c.dom.Document)51 Node (org.w3c.dom.Node)50 IOException (java.io.IOException)47 XPathExpression (javax.xml.xpath.XPathExpression)42 SAXException (org.xml.sax.SAXException)32 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)29 XPathFactory (javax.xml.xpath.XPathFactory)29 ArrayList (java.util.ArrayList)23 Element (org.w3c.dom.Element)22 InputSource (org.xml.sax.InputSource)20 HashMap (java.util.HashMap)19 Test (org.junit.Test)17 DocumentBuilder (javax.xml.parsers.DocumentBuilder)14 Response (com.jayway.restassured.response.Response)12 ValidatableResponse (com.jayway.restassured.response.ValidatableResponse)12 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)12 AbstractIntegrationTest (org.codice.ddf.itests.common.AbstractIntegrationTest)12