Example usage for org.apache.commons.jxpath JXPathContext getParentContext

List of usage examples for org.apache.commons.jxpath JXPathContext getParentContext

Introduction

In this page you can find the example usage for org.apache.commons.jxpath JXPathContext getParentContext.

Prototype

public JXPathContext getParentContext() 

Source Link

Document

Returns the parent context of this context or null.

Usage

From source file:org.chiba.xml.xforms.constraints.DependencyGraph.java

/**
 * determines which nodes are referenced by given xpath expression and returns them as nodes.
 *
 * @param xpath - the xpath expression under examination
 * @return a list with nodes referenced in given xpath
 *//*  ww  w  .  j a v a 2  s . co m*/
public Vector getXPathRefNodes(JXPathContext relativeContext, String xpath) {
    ReferenceFinder referenceFinder = new ReferenceFinder();
    Parser.parseExpression(xpath, referenceFinder);

    List pathes = referenceFinder.getLocationPathes();
    Vector refNodes = new Vector();

    for (int i = 0; i < pathes.size(); i++) {
        String refPath = pathes.get(i).toString();
        Instance instance = (Instance) relativeContext.getParentContext().getContextBean();
        JXPathContext context = relativeContext;

        if (PathUtil.hasInstanceFunction(refPath)) {
            String instanceId = PathUtil.getInstanceId(instance.getModel(), refPath);

            // use container for instance lookup to allow cross-model references
            instance = (Instance) instance.getModel().getContainer().lookup(instanceId);
            context = instance.getInstanceContext();
        }

        // iterate all referenced nodes
        Iterator iterator = context.iteratePointers(refPath);

        while (iterator.hasNext()) {
            Pointer localPointer = (Pointer) iterator.next();
            //                Object node = localPointer.getNode();
            //                if (node instanceof Pointer) {
            //                    localPointer = (Pointer) node;
            //                }

            String realPath = localPointer.asPath();
            LocalValue localValue = instance.getModelItem(realPath);

            if (localValue != null) {
                // add *existing* reference node
                refNodes.add(localValue.getNode());
            }
        }
    }

    return refNodes;
}

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.//  ww w  .jav  a  2s.  c om
 * @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  w  w w . j  a va2  s  . 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//w  w w  .  j av  a  2s.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//w w 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.ExtensionFunctionsHelper.java

/**
 * Extracts the Chiba container from an JXPath expression context.
 *
 * @param expressionContext the JXPath expression context.
 * @return the Chiba container.//from ww w  .  j a  v  a  2 s . com
 */
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 index() Function [7.7.5].// w ww  .j  a  v a  2 s.  com
 * <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].// www .ja v a2  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 + "')");
}

From source file:org.xchain.framework.lifecycle.Execution.java

/**
 * Returns true if this context represents the start of a local scope.  Returns false if the context represents a change of
 * context node inside of a local scope.
 *///w  w  w  .j  a  va 2 s . co  m
private static boolean representsLocalScopeStart(JXPathContext context) {
    return context.getParentContext() != null && context.getParentContext() == executionContextTl.get();
}

From source file:org.xchain.namespaces.core.TestJXPathContext.java

private boolean isDeclaredInCurrentOrParent(JXPathContext pathContext, String variableName) {
    while (pathContext != null) {
        // Check if the variable is declared in the current context.
        if (!pathContext.getVariables().isDeclaredVariable(variableName)) {
            // Variable not declared in this context.  Move to the parent.
            pathContext = pathContext.getParentContext();
        } else {/*from   ww w.j av a  2 s .c  om*/
            // Variable is declared in this context.
            return true;
        }
    }

    // Variable not found in any context.
    return false;
}