Search in sources :

Example 11 with XPathExpressionException

use of javax.xml.xpath.XPathExpressionException in project honeycomb by altamiracorp.

the class ConfigurationParser method parseOptions.

private static Map<String, String> parseOptions(String adapterName, Document doc) {
    String optionsQuery = String.format(QUERY_ADAPTER_CONFIG_NODES, adapterName);
    NodeList optionNodes;
    try {
        optionNodes = (NodeList) xPath.evaluate(optionsQuery, doc, XPathConstants.NODESET);
    } catch (XPathExpressionException e) {
        logger.error("Unable to parse options for " + adapterName + " adapter.", e);
        throw new RuntimeException("Exception while parsing options for " + adapterName + " adapter.", e);
    }
    ImmutableMap.Builder<String, String> options = ImmutableMap.builder();
    for (int i = 0; i < optionNodes.getLength(); i++) {
        Node optionNode = optionNodes.item(i);
        if (optionNode.getNodeType() == Node.ELEMENT_NODE) {
            String optionName = optionNode.getNodeName();
            String namespacedOptionName = isNamespaced(optionName) ? optionName : prependNamespace(adapterName, optionName);
            options.put(namespacedOptionName, optionNode.getTextContent());
        }
    }
    return options.build();
}
Also used : XPathExpressionException(javax.xml.xpath.XPathExpressionException) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 12 with XPathExpressionException

use of javax.xml.xpath.XPathExpressionException in project camel by apache.

the class XmlSignerProcessor method getContentReferenceUrisForDetachedCase.

private List<String> getContentReferenceUrisForDetachedCase(Message message, Node messageBodyNode) throws XmlSignatureException, XPathExpressionException {
    List<XPathFilterParameterSpec> xpathsToIdAttributes = getXpathToIdAttributes(message);
    if (xpathsToIdAttributes.isEmpty()) {
        // should not happen, has already been checked earlier
        throw new IllegalStateException("List of XPATHs to ID attributes is empty in detached signature case");
    }
    List<ComparableNode> result = new ArrayList<ComparableNode>(xpathsToIdAttributes.size());
    for (XPathFilterParameterSpec xp : xpathsToIdAttributes) {
        XPathExpression exp;
        try {
            exp = XmlSignatureHelper.getXPathExpression(xp);
        } catch (XPathExpressionException e) {
            throw new XmlSignatureException("The configured xpath expression " + xp.getXPath() + " is invalid.", e);
        }
        NodeList list = (NodeList) exp.evaluate(messageBodyNode, XPathConstants.NODESET);
        if (list == null) {
            //assume optional element, XSD validation has been done before
            LOG.warn("No ID attribute found for xpath expression {}. Therfore this xpath expression will be ignored.", xp.getXPath());
            continue;
        }
        int length = list.getLength();
        for (int i = 0; i < length; i++) {
            Node node = list.item(i);
            if (node.getNodeType() == Node.ATTRIBUTE_NODE) {
                Attr attr = (Attr) node;
                String value = attr.getValue();
                // check that attribute is ID attribute
                Element element = messageBodyNode.getOwnerDocument().getElementById(value);
                if (element == null) {
                    throw new XmlSignatureException("Wrong configured xpath expression for ID attributes: The evaluation of the xpath expression " + xp.getXPath() + " resulted in an attribute which is not of type ID. The attribute value is " + value + ".");
                }
                result.add(new ComparableNode(element, "#" + value));
                LOG.debug("ID attribute with value {} found for xpath {}", value, xp.getXPath());
            } else {
                throw new XmlSignatureException("Wrong configured xpath expression for ID attributes: The evaluation of the xpath expression " + xp.getXPath() + " returned a node which was not of type Attribute.");
            }
        }
    }
    if (result.size() == 0) {
        throw new XmlSignatureException("No element to sign found in the detached case. No node found for the configured xpath expressions " + toString(xpathsToIdAttributes) + ". Either the configuration of the XML signature component is wrong or the incoming message has not the correct structure.");
    }
    // sort so that elements with deeper hierarchy level are treated first
    Collections.sort(result);
    return ComparableNode.getReferenceUris(result);
}
Also used : XPathExpression(javax.xml.xpath.XPathExpression) XPathExpressionException(javax.xml.xpath.XPathExpressionException) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) XPathFilterParameterSpec(javax.xml.crypto.dsig.spec.XPathFilterParameterSpec) ArrayList(java.util.ArrayList) Attr(org.w3c.dom.Attr) XmlSignatureException(org.apache.camel.component.xmlsecurity.api.XmlSignatureException)

Example 13 with XPathExpressionException

use of javax.xml.xpath.XPathExpressionException in project bazel by bazelbuild.

the class AndroidResourceProcessor method processDataBindings.

/**
   * If resources exist and a data binding layout info file is requested: processes data binding
   * declarations over those resources, populates the output file, and creates a new resources
   * directory with data binding expressions stripped out (so aapt, which doesn't understand
   * data binding, can properly read them).
   *
   * <p>Returns the resources directory that aapt should read.
   */
static Path processDataBindings(Path resourceDir, Path dataBindingInfoOut, VariantType variantType, String packagePath, Path androidManifest) throws IOException {
    if (dataBindingInfoOut == null) {
        return resourceDir;
    } else if (!Files.isDirectory(resourceDir)) {
        // No resources: no data binding needed. Create a dummy file to satisfy declared outputs.
        Files.createFile(dataBindingInfoOut);
        return resourceDir;
    }
    // Strip the file name (the data binding library automatically adds it back in).
    // ** The data binding library assumes this file is called "layout-info.zip". **
    dataBindingInfoOut = dataBindingInfoOut.getParent();
    if (Files.notExists(dataBindingInfoOut)) {
        Files.createDirectory(dataBindingInfoOut);
    }
    Path processedResourceDir = resourceDir.resolveSibling("res_without_databindings");
    if (Files.notExists(processedResourceDir)) {
        Files.createDirectory(processedResourceDir);
    }
    ProcessXmlOptions options = new ProcessXmlOptions();
    options.setAppId(packagePath);
    options.setLibrary(variantType == VariantType.LIBRARY);
    options.setResInput(resourceDir.toFile());
    options.setResOutput(processedResourceDir.toFile());
    options.setLayoutInfoOutput(dataBindingInfoOut.toFile());
    // Aggregate data-bound .xml files into a single .zip.
    options.setZipLayoutInfo(true);
    try {
        Object minSdk = AndroidManifest.getMinSdkVersion(new FileWrapper(androidManifest.toFile()));
        if (minSdk instanceof Integer) {
            options.setMinSdk(((Integer) minSdk).intValue());
        } else {
            // TODO(bazel-team): Enforce the minimum SDK check.
            options.setMinSdk(15);
        }
    } catch (XPathExpressionException | StreamException e) {
        // TODO(bazel-team): Enforce the minimum SDK check.
        options.setMinSdk(15);
    }
    try {
        AndroidDataBinding.doRun(options);
    } catch (Throwable t) {
        throw new RuntimeException(t);
    }
    return processedResourceDir;
}
Also used : Path(java.nio.file.Path) XPathExpressionException(javax.xml.xpath.XPathExpressionException) FileWrapper(com.android.io.FileWrapper) ProcessXmlOptions(android.databinding.cli.ProcessXmlOptions) StreamException(com.android.io.StreamException)

Example 14 with XPathExpressionException

use of javax.xml.xpath.XPathExpressionException in project j2objc by google.

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 15 with XPathExpressionException

use of javax.xml.xpath.XPathExpressionException in project j2objc by google.

the class XPathImpl method compile.

/**
     * <p>Compile an XPath expression for later evaluation.</p>
     *
     * <p>If <code>expression</code> contains any {@link XPathFunction}s,
     * they must be available via the {@link XPathFunctionResolver}.
     * An {@link XPathExpressionException} will be thrown if the <code>XPathFunction</code>
     * cannot be resovled with the <code>XPathFunctionResolver</code>.</p>
     * 
     * <p>If <code>expression</code> is <code>null</code>, a <code>NullPointerException</code> is thrown.</p>
     *
     * @param expression The XPath expression.
     *
     * @return Compiled XPath expression.

     * @throws XPathExpressionException If <code>expression</code> cannot be compiled.
     * @throws NullPointerException If <code>expression</code> is <code>null</code>.
     */
public XPathExpression compile(String expression) throws XPathExpressionException {
    if (expression == null) {
        String fmsg = XSLMessages.createXPATHMessage(XPATHErrorResources.ER_ARG_CANNOT_BE_NULL, new Object[] { "XPath expression" });
        throw new NullPointerException(fmsg);
    }
    try {
        org.apache.xpath.XPath xpath = new XPath(expression, null, prefixResolver, org.apache.xpath.XPath.SELECT);
        // Can have errorListener
        XPathExpressionImpl ximpl = new XPathExpressionImpl(xpath, prefixResolver, functionResolver, variableResolver, featureSecureProcessing);
        return ximpl;
    } catch (javax.xml.transform.TransformerException te) {
        throw new XPathExpressionException(te);
    }
}
Also used : org.apache.xpath(org.apache.xpath) XPathExpressionException(javax.xml.xpath.XPathExpressionException)

Aggregations

XPathExpressionException (javax.xml.xpath.XPathExpressionException)139 NodeList (org.w3c.dom.NodeList)65 XPath (javax.xml.xpath.XPath)64 Document (org.w3c.dom.Document)46 Node (org.w3c.dom.Node)46 IOException (java.io.IOException)42 XPathExpression (javax.xml.xpath.XPathExpression)38 SAXException (org.xml.sax.SAXException)27 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)25 XPathFactory (javax.xml.xpath.XPathFactory)23 ArrayList (java.util.ArrayList)22 HashMap (java.util.HashMap)18 Test (org.junit.Test)17 InputSource (org.xml.sax.InputSource)17 Element (org.w3c.dom.Element)16 Response (com.jayway.restassured.response.Response)12 ValidatableResponse (com.jayway.restassured.response.ValidatableResponse)12 DocumentBuilder (javax.xml.parsers.DocumentBuilder)12 AbstractIntegrationTest (org.codice.ddf.itests.common.AbstractIntegrationTest)12 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)11