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

com.crabshue.commons.xpath.XPathVariableUtils Maven / Gradle / Ivy

package com.crabshue.commons.xpath;

import java.util.ArrayList;
import java.util.Collection;

import javax.xml.namespace.QName;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import javax.xml.xpath.XPathVariableResolver;

import org.apache.commons.lang3.Validate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.crabshue.commons.exceptions.ApplicationException;
import com.crabshue.commons.exceptions.SystemException;
import com.crabshue.commons.exceptions.context.CommonErrorContext;
import com.crabshue.commons.xml.inputsource.InputSourceBuilder;
import com.crabshue.commons.xpath.exceptions.XPathErrorContext;
import com.crabshue.commons.xpath.exceptions.XPathErrorType;

/**
 * Utility class for XPath variables operations.
 *
 * @author vinh
 */
public class XPathVariableUtils {

    private static Logger logger = LoggerFactory.getLogger(XPathVariableUtils.class);

    /**
     * Extract the variables from an XPath expression.
     *
     * @param xpathExpression the XPath expression
     * @return the collection of variables used in the XPath expression
     */
    public static Collection extractVariablesFromXpathExpression(final String xpathExpression) {
        Validate.notNull(xpathExpression);

        final Collection ret = new ArrayList<>();
        try {
            XPathFactory xPathFactory = XPathFactoryUtils.provideXPathFactory();
            XPath xpath = xPathFactory.newXPath();
            xpath.setXPathVariableResolver(new XPathVariableResolver() {
                @Override
                public Object resolveVariable(final QName qName) {
                    final String variableName = qName.getLocalPart();
                    ret.add(variableName);
                    return null;
                }
            });
            XPathExpression xPathExpression = xpath.compile(xpathExpression);

            final String xml = "";
            xPathExpression.evaluate(InputSourceBuilder.newInputSource(xml), XPathConstants.NODESET);

        } catch (ApplicationException e) {
            throw e;
        } catch (XPathExpressionException e) {
            // saxon exception is not serializable (JSON)
            // take only the message of the exception because issue serialization saxon...
            throw new SystemException(XPathErrorType.ERROR_EXTRACTION_XPATH_VARIABLES, "Error extracting variables from XPath expression")
                .addContextValue(XPathErrorContext.XPATH, xpathExpression)
                .addContextValue(CommonErrorContext.CAUSE, e.getMessage());
        } catch (Exception e) {
            throw new SystemException(XPathErrorType.ERROR_EXTRACTION_XPATH_VARIABLES, e)
                .addContextValue(XPathErrorContext.XPATH, xpathExpression);
        }

        logger.info("Extracted variables [{}] from xpath expression [{}]", ret, xpathExpression);
        return ret;

    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy