All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.crabshue.commons.xpath.validation.XPathValidationUtils Maven / Gradle / Ivy

package com.crabshue.commons.xpath.validation;

import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import org.xml.sax.InputSource;

import com.crabshue.commons.exceptions.ValidationException;
import com.crabshue.commons.xpath.XPathEvaluator;
import com.crabshue.commons.xpath.exceptions.XPathErrorContext;
import com.crabshue.commons.xpath.exceptions.XPathErrorType;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import net.sf.saxon.om.NodeInfo;
import net.sf.saxon.type.Type;

/**
 * XPath validations.
 *
 */
@Slf4j
public class XPathValidationUtils {

    /**
     * Validate a given XPath expression.
     *
     * @param xpathExpression the XPath expression.
     */
    public static void validateXpathExpression(@NonNull final String xpathExpression) {

        try {
            final XPath xpath = XPathFactory.newInstance().newXPath();
            xpath.compile(xpathExpression);
        } catch (XPathExpressionException e) {
            throw new ValidationException(XPathErrorType.XPATH_INVALID, "Xpath is not valid.", e)
                .addContextValue(XPathErrorContext.XPATH, xpathExpression);
        }
    }


    /**
     * Determine whether an XPath expression matches a valid target (tag, attribute) in a given input.
     *
     * @param inputSource the input
     * @param xpathStr    the XPath expression
     * @return the result, as a boolean
     */
    public static boolean xpathHasATarget(@NonNull final InputSource inputSource,
                                          @NonNull final String xpathStr) {

        boolean ret = true;
        final Object res = XPathEvaluator.of(inputSource)
            .withXpathExpression(xpathStr)
            .withReturnType(XPathConstants.NODE)
            .evaluate();

        if (res == null) {
            ret = false;
        }
        return ret;
    }

    /**
     * Determine whether an XPath expression targets a tag/element in a given XML document.
     *
     * @param inputSource the XML document (as {@link InputSource})
     * @param xpathStr    the XPath expression
     * @return the result, as a boolean
     */
    public static boolean isXPathTargetElement(@NonNull final InputSource inputSource,
                                               @NonNull final String xpathStr) {

        boolean ret = false;

        final Object result = XPathEvaluator.of(inputSource)
            .withXpathExpression(xpathStr)
            .withReturnType(XPathConstants.NODE)
            .evaluate();

        if (result != null) {
            if (result instanceof NodeInfo) {
                if (((NodeInfo) result).getNodeKind() == Type.ELEMENT) {
                    ret = true;
                }
            }
        }
        return ret;
    }


}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy