Java XPath Expression createXPathExpression(String xpathString)

Here you can find the source of createXPathExpression(String xpathString)

Description

create X Path Expression

License

Open Source License

Declaration

private static XPathExpression createXPathExpression(String xpathString) 

Method Source Code

//package com.java2s;
/*//from   ww  w . ja  v  a  2s . co m
 * DSS - Digital Signature Services
 *
 * Copyright (C) 2011 European Commission, Directorate-General Internal Market and Services (DG MARKT), B-1049 Bruxelles/Brussel
 *
 * Developed by: 2011 ARHS Developments S.A. (rue Nicolas Bov? 2B, L-1253 Luxembourg) http://www.arhs-developments.com
 *
 * This file is part of the "DSS - Digital Signature Services" project.
 *
 * "DSS - Digital Signature Services" is free software: you can redistribute it and/or modify it under the terms of
 * the GNU Lesser General Public License as published by the Free Software Foundation, either version 2.1 of the
 * License, or (at your option) any later version.
 *
 * DSS is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License along with
 * "DSS - Digital Signature Services".  If not, see <http://www.gnu.org/licenses/>.
 */

import java.util.Iterator;
import javax.xml.crypto.dsig.XMLSignature;

import javax.xml.namespace.NamespaceContext;
import javax.xml.xpath.XPath;

import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

public class Main {
    private static XPathExpression createXPathExpression(String xpathString) {
        /* XPath */
        XPathFactory factory = XPathFactory.newInstance();
        XPath xpath = factory.newXPath();
        xpath.setNamespaceContext(new NamespaceContext() {

            @Override
            public Iterator<?> getPrefixes(String namespaceURI) {
                throw new RuntimeException();
            }

            @Override
            public String getPrefix(String namespaceURI) {
                throw new RuntimeException();
            }

            @Override
            public String getNamespaceURI(String prefix) {
                if ("ds".equals(prefix)) {
                    return XMLSignature.XMLNS;
                } else if ("xades".equals(prefix)) {
                    return "http://uri.etsi.org/01903/v1.3.2#";
                } else if ("xades141".equals(prefix)) {
                    return "http://uri.etsi.org/01903/v1.4.1#";
                } else if ("xades111".equals(prefix)) {
                    return "http://uri.etsi.org/01903/v1.1.1#";
                }
                throw new RuntimeException("Prefix not recognized : " + prefix);
            }
        });
        try {
            XPathExpression expr = xpath.compile(xpathString);
            return expr;
        } catch (XPathExpressionException ex) {
            throw new RuntimeException(ex);
        }

    }
}

Related

  1. createXPath()
  2. createXPath(NamespaceContext namespaceContext, XPathFunctionResolver functionResolver)
  3. createXPathExpression(NamespaceContext context, String xPathQuery)
  4. createXPathExpression(String expression, NamespaceContext namespaceContext)
  5. createXPathExpression(String xpath)
  6. deleteXMLElement(String xml, String xpath)
  7. dumpXpath(Node node, PrintStream printer)
  8. executePath(final Node node, final XPathExpression expression)
  9. expr(String xpathStr)