Java XPath Validate validateXPath(String xml, String... tests)

Here you can find the source of validateXPath(String xml, String... tests)

Description

A helper method which validates a String against an array of XPath test strings.

License

Apache License

Parameter

Parameter Description
xml The xml String to validate
tests Array of XPath strings to test (in boolean mode) on the xml

Return

null if all good, otherwise the first test that fails.

Declaration

public static String validateXPath(String xml, String... tests) throws XPathExpressionException, SAXException 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.io.ByteArrayInputStream;
import java.io.IOException;

import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;

import org.xml.sax.SAXException;

public class Main {
    private static final ThreadLocal<DocumentBuilder> builderTL = new ThreadLocal<DocumentBuilder>();
    private static final ThreadLocal<XPath> xpathTL = new ThreadLocal<XPath>();

    /**//w w  w. j  a v a2 s  .  c o  m
     * A helper method which validates a String against an array of XPath test
     * strings.
     *
     * @param xml The xml String to validate
     * @param tests Array of XPath strings to test (in boolean mode) on the xml
     * @return null if all good, otherwise the first test that fails.
     */
    public static String validateXPath(String xml, String... tests) throws XPathExpressionException, SAXException {

        if (tests == null || tests.length == 0)
            return null;

        Document document = null;
        try {
            document = getXmlDocumentBuilder()
                    .parse(new ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8)));
        } catch (UnsupportedEncodingException e1) {
            throw new RuntimeException("Totally weird UTF-8 exception", e1);
        } catch (IOException e2) {
            throw new RuntimeException("Totally weird io exception", e2);
        }

        for (String xp : tests) {
            xp = xp.trim();
            Boolean bool = (Boolean) getXpath().evaluate(xp, document, XPathConstants.BOOLEAN);

            if (!bool) {
                return xp;
            }
        }
        return null;
    }

    public static DocumentBuilder getXmlDocumentBuilder() {
        try {
            DocumentBuilder builder = builderTL.get();
            if (builder == null) {
                builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
                builderTL.set(builder);
            }
            return builder;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static XPath getXpath() {
        try {
            XPath xpath = xpathTL.get();
            if (xpath == null) {
                xpath = XPathFactory.newInstance().newXPath();
                xpathTL.set(xpath);
            }
            return xpath;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}

Related

  1. validateXPath(final Document doc, final String xpathExpression)
  2. validateXPath(String path)
  3. validateXPathExpression(String xpathExpression)