Example usage for org.apache.commons.jxpath ExpressionContext getJXPathContext

List of usage examples for org.apache.commons.jxpath ExpressionContext getJXPathContext

Introduction

In this page you can find the example usage for org.apache.commons.jxpath ExpressionContext getJXPathContext.

Prototype

JXPathContext getJXPathContext();

Source Link

Document

Get the JXPathContext in which this function is being evaluated.

Usage

From source file:org.chiba.xml.xforms.xpath.ChibaExtensionFunctions.java

/**
 * Returns the calculation result of an external calculator.
 *
 * @param expressionContext the expression context.
 * @param uri the calculator uri.// w  w w  . j  a v  a  2s .co m
 * @return the calculation result of an external calculator.
 * @throws XFormsException if any error occurred during the calculation.
 * @deprecated use custom extension functions instead
 */
public static String calculate(ExpressionContext expressionContext, String uri) throws XFormsException {
    JXPathContext context = expressionContext.getJXPathContext();

    while (context != null) {
        Object contextBean = context.getContextBean();

        if (contextBean instanceof XFormsElement) {
            // get hook from jxpath to chiba
            XFormsElement xFormsElement = (XFormsElement) contextBean;
            Container container = xFormsElement.getModel().getContainer();
            Element contextElement = xFormsElement.getElement();
            Node instanceNode = (Node) expressionContext.getContextNodePointer().getNode();

            ModelItemCalculator calculator = container.getConnectorFactory().createModelItemCalculator(uri,
                    contextElement);
            return calculator.calculate(instanceNode);
        }

        context = context.getParentContext();
    }

    throw new XFormsException("invalid expression context when evaluating calculate('" + uri + "')");
}

From source file:org.chiba.xml.xforms.xpath.ChibaExtensionFunctions.java

/**
 * Returns the validation result of an external validator.
 *
 * @param expressionContext the expression context.
 * @param uri the calculator uri./*from   www. ja va  2s.c  o m*/
 * @return the validation result of an external validator.
 * @throws XFormsException if any error occurred during the validation.
 * @deprecated use custom extension functions instead
 */
public static boolean validate(ExpressionContext expressionContext, String uri) throws XFormsException {
    JXPathContext context = expressionContext.getJXPathContext();

    while (context != null) {
        Object contextBean = context.getContextBean();

        if (contextBean instanceof XFormsElement) {
            // get hook from jxpath to chiba
            XFormsElement xFormsElement = (XFormsElement) contextBean;
            Container container = xFormsElement.getModel().getContainer();
            Element contextElement = xFormsElement.getElement();
            Node instanceNode = (Node) expressionContext.getContextNodePointer().getNode();

            ModelItemValidator validator = container.getConnectorFactory().createModelItemValidator(uri,
                    contextElement);
            return validator.validate(instanceNode);
        }

        context = context.getParentContext();
    }

    throw new XFormsException("invalid expression context when evaluating validate('" + uri + "')");
}

From source file:org.chiba.xml.xforms.xpath.ChibaExtensionFunctions.java

/**
 * custom extension function to get the size of a local file.
 *
 * @param expressionContext//from www.j a va2  s .c  o m
 * @param nodeset nodeset must contain a single node that has a filename or
 * path as value. The value will be resolved against the baseURI of the
 * processor to find the file.
 * @return the size of the file as String
 *         <p/>
 *         todo: revisit code structure - fileSize and fileDate functions
 *         only differ in one line of code
 */
public static String fileSize(ExpressionContext expressionContext, List nodeset) {
    if ((nodeset == null) || (nodeset.size() == 0)) {
        return "Error: Nodeset does not exist";
    }
    JXPathContext rootContext = expressionContext.getJXPathContext();

    while (rootContext != null) {
        Object rootNode = rootContext.getContextBean();

        if (rootNode instanceof Instance) {
            //get the Context
            Instance instance = (Instance) rootNode;
            String baseUri = instance.getModel().getContainer().getProcessor().getBaseURI();
            String path;
            try {
                //                    uri = new URI(baseUri).getPath();
                path = new URI(baseUri).getPath().substring(1);
            } catch (URISyntaxException e) {
                return "Error: base URI not valid: " + baseUri;
            }

            File file = new File(path, (String) nodeset.get(0));
            if (!file.exists() || file.isDirectory()) {
                LOGGER.info("File " + file.toString() + " does not exist or is directory");
                return "";
            }

            return "" + file.length();
        }
        rootContext = rootContext.getParentContext();
    }
    return "";
}

From source file:org.chiba.xml.xforms.xpath.ChibaExtensionFunctions.java

/**
 * custom extension function to get the lastModified Date of a local file.
 *
 * @param expressionContext//from ww w.j  a  va  2  s  . c o m
 * @param nodeset must contain a single node that has a filename or path as
 * value. The value will be resolved against the baseURI of the processor to
 * find the file.
 * @param format a format pattern conformant with to
 * java.text.SimpleDateFormat. If an empty string is passed the format
 * defaults to "dd.MM.yyyy H:m:s".
 * @return the formatted lastModified Date of the file
 * @see java.text.SimpleDateFormat
 */
public static String fileDate(ExpressionContext expressionContext, List nodeset, String format) {
    if ((nodeset == null) || (nodeset.size() == 0)) {
        return "Error: Nodeset does not exist";
    }
    JXPathContext rootContext = expressionContext.getJXPathContext();

    while (rootContext != null) {
        Object rootNode = rootContext.getContextBean();

        if (rootNode instanceof Instance) {
            //                //get the Context
            //                Instance instance = (Instance) rootNode;
            //                String baseUri = instance.getModel().getContainer().getProcessor().getBaseURI();
            //
            //                File file = new File(baseUri,(String) nodeset.get(0));
            //                if(!file.exists()){
            //                    LOGGER.info("File " + file.toString() + " does not exist");
            //                    return "";
            //                }
            //get the Context
            Instance instance = (Instance) rootNode;
            String baseUri = instance.getModel().getContainer().getProcessor().getBaseURI();
            String path;
            try {
                //                    uri = new URI(baseUri).getPath();
                path = new URI(baseUri).getPath().substring(1);
            } catch (URISyntaxException e) {
                return "Error: base URI not valid: " + baseUri;
            }

            File file = new File(path, (String) nodeset.get(0));
            if (!file.exists() || file.isDirectory()) {
                LOGGER.info("File " + file.toString() + " does not exist or is directory");
                return "";
            }

            return formatDateString(file, format);
        }
        rootContext = rootContext.getParentContext();
    }
    return "Error: Calculation failed";
}

From source file:org.chiba.xml.xforms.xpath.CoreExtensionFunctions.java

/**
 * Declares the specified variable an returns its value.
 *
 * @param expressionContext the expression context.
 * @param name the name of the variable.
 * @param value the value of the variable.
 * @return the value of the variable.//from w w w.  j  a  v  a2  s  . co  m
 */
public static Object declare(ExpressionContext expressionContext, String name, Object value) {
    Object variable;
    if (value instanceof List) {
        // hell, jxpath passes nodesets as list here
        List list = (List) value;
        variable = list.size() > 0 ? list.get(0) : null;
    } else {
        variable = value;
    }

    Variables variables = expressionContext.getJXPathContext().getVariables();
    variables.declareVariable(name, variable);

    return value;
}

From source file:org.chiba.xml.xforms.xpath.CoreExtensionFunctions.java

/**
 * Undeclares the specified variable an returns its value.
 *
 * @param expressionContext the expression context.
 * @param name the name of the variable.
 * @return the value of the variable./*from  w  w  w . j  a v a2  s . c  o m*/
 */
public static Object undeclare(ExpressionContext expressionContext, String name) {
    Variables variables = expressionContext.getJXPathContext().getVariables();
    Object value = variables.getVariable(name);
    variables.undeclareVariable(name);

    return value;
}

From source file:org.chiba.xml.xforms.xpath.ExtensionFunctionsHelper.java

/**
 * Extracts the Chiba container from an JXPath expression context.
 *
 * @param expressionContext the JXPath expression context.
 * @return the Chiba container./*from w w w . j av  a2 s . c  o m*/
 */
public static Container getChibaContainer(ExpressionContext expressionContext) {
    if (expressionContext == null) {
        return null;
    }

    Object rootNode;
    JXPathContext rootContext = expressionContext.getJXPathContext();

    while (rootContext != null) {
        rootNode = rootContext.getContextBean();
        if (rootNode instanceof XFormsElement) {
            return ((XFormsElement) rootNode).getModel().getContainer();
        }

        rootContext = rootContext.getParentContext();
    }

    return null;
}

From source file:org.chiba.xml.xforms.xpath.XFormsExtensionFunctions.java

/**
 * The avg() Function [7.7.1].//from ww  w.  j a va 2  s .com
 * <p/>
 * Function avg returns the arithmetic average of the result of
 * converting the string-values of each node in the argument node-set
 * to a number. The sum is computed with sum(), and divided with div
 * by the value computed with count().
 *
 * @param context the expression context.
 * @param nodeset the node-set.
 * @return the computed node-set average.
 */
public static double avg(ExpressionContext context, List nodeset) {
    if ((nodeset == null) || (nodeset.size() == 0)) {
        return Double.NaN;
    }

    JXPathContext rootContext = context.getJXPathContext();
    rootContext.getVariables().declareVariable("nodeset", nodeset);

    Double value = (Double) rootContext.getValue("sum($nodeset) div count($nodeset)", Double.class);

    return value.doubleValue();
}

From source file:org.chiba.xml.xforms.xpath.XFormsExtensionFunctions.java

/**
 * The index() Function [7.7.5].//w w w .j  a  va2s  . c o  m
 * <p/>
 * Function index takes a string argument that is the idref of a repeat
 * and returns the current 1-based position of the repeat index for the
 * identified repeat see 9.3.1 The repeat Element for details on repeat
 * and its associated repeat index. If the specified argument does not
 * identify a repeat, processing stops with an exception.
 *
 * @param context the expression context.
 * @param idref   the repeat id.
 * @return the specified repeat index.
 * @throws XFormsException if any error occurred during repeat index lookup.
 */
public static int index(ExpressionContext context, String idref) throws XFormsException {
    JXPathContext rootContext = context.getJXPathContext();

    while (rootContext != null) {
        Object rootNode = rootContext.getContextBean();

        if (rootNode instanceof XFormsElement) {
            XFormsElement element = (XFormsElement) rootNode;
            Repeat repeat = (Repeat) element.getModel().getContainer().lookup(idref);

            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug("index for Element: " + element.getId() + " evaluated to " + repeat.getIndex());
            }
            return repeat.getIndex();
        }

        rootContext = rootContext.getParentContext();
    }

    throw new XFormsException("invalid expression context when evaluating index('" + idref + "')");
}

From source file:org.chiba.xml.xforms.xpath.XFormsExtensionFunctions.java

/**
 * The instance() Function [7.10.1].// ww  w  .  j  a  va2  s .  c  o  m
 * <p/>
 * An XForms Model can contain more that one instance. This function allows
 * access to instance data, within the same XForms Model, but outside the
 * instance data containing the context node.
 * <p/>
 * The argument is converted to a string as if by a call to the string function.
 * This string is treated as an IDREF, which is matched against instance elements
 * in the containing document. If a match is located, and the matching instance
 * data is associated with the same XForms Model as the current context node,
 * this function returns a node-set containing just the root element node (also
 * called the document element node) of the referenced instance data. In all
 * other cases, an empty node-set is returned.
 *
 * @param context the expression context.
 * @param idref   the instance id.
 * @return the specified instance.
 * @throws XFormsException if any error occurred during instance lookup.
 */
public static Object instance(ExpressionContext context, String idref) throws XFormsException {
    JXPathContext rootContext = context.getJXPathContext();

    while (rootContext != null) {
        Object rootNode = rootContext.getContextBean();

        //does not work cause rootnode is no XFormsElement
        if (rootNode instanceof XFormsElement) {
            Object instance = ((XFormsElement) rootNode).getModel().getContainer().lookup(idref);

            if (instance != null && instance instanceof Instance) {
                Pointer pointer = ((Instance) instance).getPointer(BindingResolver.OUTERMOST_CONTEXT);
                return pointer;
            }
        }

        rootContext = rootContext.getParentContext();
    }

    throw new XFormsException("invalid expression context when evaluating instance('" + idref + "')");
}