Search in sources :

Example 91 with XPathExpressionException

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);
    }
}
Also used : XPathExpressionException(javax.xml.xpath.XPathExpressionException) Node(org.w3c.dom.Node) NodeList(org.w3c.dom.NodeList) XObject(com.sun.org.apache.xpath.internal.objects.XObject) TransformerException(javax.xml.transform.TransformerException)

Example 92 with XPathExpressionException

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();
}
Also used : XPathExpressionException(javax.xml.xpath.XPathExpressionException) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) ImmutableMap(com.google.common.collect.ImmutableMap) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 93 with XPathExpressionException

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;
}
Also used : XPath(javax.xml.xpath.XPath) InputSource(org.xml.sax.InputSource) XPathExpressionException(javax.xml.xpath.XPathExpressionException) FileNotFoundException(java.io.FileNotFoundException) FileInputStream(java.io.FileInputStream)

Example 94 with XPathExpressionException

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);
        }
    }
}
Also used : XPathExpressionException(javax.xml.xpath.XPathExpressionException) IOException(java.io.IOException) Document(org.w3c.dom.Document) SAXException(org.xml.sax.SAXException) org.apache.xpath(org.apache.xpath) XObject(org.apache.xpath.objects.XObject)

Example 95 with XPathExpressionException

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

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