Search in sources :

Example 76 with XPathExpressionException

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;
}
Also used : XPath(javax.xml.xpath.XPath) Message(org.apache.cxf.common.i18n.Message) XPathExpressionException(javax.xml.xpath.XPathExpressionException) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) ToolException(org.apache.cxf.tools.common.ToolException)

Example 77 with XPathExpressionException

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;
}
Also used : XPath(javax.xml.xpath.XPath) MalformedURLException(java.net.MalformedURLException) HttpURLConnection(java.net.HttpURLConnection) InputStream(java.io.InputStream) XPathExpressionException(javax.xml.xpath.XPathExpressionException) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) Document(org.w3c.dom.Document) URL(java.net.URL) SAXException(org.xml.sax.SAXException)

Example 78 with XPathExpressionException

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);
    }
}
Also used : XPath(javax.xml.xpath.XPath) XPathExpression(javax.xml.xpath.XPathExpression) XPathFactory(javax.xml.xpath.XPathFactory) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) Document(org.w3c.dom.Document) XPathExpressionException(javax.xml.xpath.XPathExpressionException) IOException(java.io.IOException) ProcessManagerException(com.sun.appserv.test.util.process.ProcessManagerException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException)

Example 79 with XPathExpressionException

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);
        }
    }
}
Also used : XPathExpressionException(javax.xml.xpath.XPathExpressionException) File(java.io.File) Property(com.thoughtworks.go.domain.Property) XPathExpressionException(javax.xml.xpath.XPathExpressionException)

Example 80 with XPathExpressionException

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;
}
Also used : NamedNodeMap(org.w3c.dom.NamedNodeMap) XPathExpressionException(javax.xml.xpath.XPathExpressionException) Node(org.w3c.dom.Node) JsonNode(com.fasterxml.jackson.databind.JsonNode)

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