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

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

Introduction

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

Prototype

public Variables getVariables() 

Source Link

Document

Returns the variable pool associated with the context.

Usage

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

/**
 * The max() Function [7.7.3]./*ww w.  j a va  2 s  . c o m*/
 * <p/>
 * Function max returns the maximum value of the result of converting
 * the string-values of each node in argument node-set to a number.
 * "Maximum" is determined with the &gt; operator. If the parameter is
 * an empty node-set, the return value is NaN.
 *
 * @param context the expression context.
 * @param nodeset the node-set.
 * @return the computed node-set maximum.
 */
public static double max(ExpressionContext context, List nodeset) {
    if ((nodeset == null) || (nodeset.size() == 0)) {
        return Double.NaN;
    }

    JXPathContext rootContext = context.getJXPathContext();
    Object max = nodeset.get(0);

    for (int index = 1; index < nodeset.size(); index++) {
        Object current = nodeset.get(index);
        rootContext.getVariables().declareVariable("max", max);
        rootContext.getVariables().declareVariable("current", current);

        boolean more = ((Boolean) rootContext.getValue("number($current) > number($max)", Boolean.class))
                .booleanValue();

        if (more) {
            max = current;
        }
    }

    return (new Double(max.toString())).doubleValue();
}

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

/**
 * The min() Function [7.7.2].//from  w w w  .  ja  v a 2s .c o  m
 * <p/>
 * Function min returns the minimum value of the result of converting
 * the string-values of each node in argument node-set to a number.
 * "Minimum" is determined with the &lt; operator. If the parameter is
 * an empty node-set, the return value is NaN.
 *
 * @param context the expression context.
 * @param nodeset the node-set.
 * @return the computed node-set minimum.
 */
public static double min(ExpressionContext context, List nodeset) {
    if ((nodeset == null) || (nodeset.size() == 0)) {
        return Double.NaN;
    }

    JXPathContext rootContext = context.getJXPathContext();
    Object min = nodeset.get(0);

    for (int index = 1; index < nodeset.size(); index++) {
        Object current = nodeset.get(index);
        rootContext.getVariables().declareVariable("min", min);
        rootContext.getVariables().declareVariable("current", current);

        boolean less = ((Boolean) rootContext.getValue("number($current) < number($min)", Boolean.class))
                .booleanValue();

        if (less) {
            min = current;
        }
    }

    return (new Double(min.toString())).doubleValue();
}

From source file:org.openconcerto.xml.JXPathXPath.java

private JXPathContext getJXPath(final Object context) {
    final JXPathContext newContext = JXPathContext.newContext(context);
    for (final Namespace ns : this.namespaces)
        newContext.registerNamespace(ns.getPrefix(), ns.getURI());
    for (final Entry<String, Object> e : this.variables.entrySet())
        newContext.getVariables().declareVariable(e.getKey(), e.getValue());
    // otherwise /a/b on an empty document throws an exception
    newContext.setLenient(true);//from ww  w  .j  av a2s.  c  o  m
    return newContext;
}

From source file:org.openvpms.component.business.service.archetype.AbstractIMObjectFactory.java

/**
 * Creates a node in the context, populating any default value.
 *
 * @param context the jxpath context//from   www .  j a va 2  s .  co  m
 * @param node    the node to create
 * @throws ArchetypeServiceException if the create fails
 */
private void create(JXPathContext context, NodeDescriptor node) {
    if (log.isDebugEnabled()) {
        log.debug("Attempting to create path " + node.getPath() + " for node " + node.getName());
    }

    context.getVariables().declareVariable("node", node);
    context.createPath(node.getPath());

    String expression = node.getDefaultValue();
    if (!StringUtils.isEmpty(expression)) {
        if (log.isDebugEnabled()) {
            log.debug("evaluating default value expression for node " + node.getName() + " path "
                    + node.getPath() + " and expression " + expression);
        }
        Object value = context.getValue(expression);
        IMObject object = (IMObject) context.getContextBean();
        if (node.isCollection()) {
            if (value != null) {
                if (Collection.class.isAssignableFrom(value.getClass())) {
                    for (Object v : (Collection) value) {
                        node.addChildToCollection(object, v);
                    }
                } else {
                    node.addChildToCollection(object, value);
                }
            }
        } else {
            node.setValue(object, value);
        }
    }
}

From source file:org.openvpms.component.business.service.archetype.JXPathGenericObjectCreationFactory.java

@Override
public boolean createObject(JXPathContext context, Pointer ptr, Object parent, String name, int index) {
    try {/*from w  ww  . jav a  2s.  c o m*/
        NodeDescriptor node = (NodeDescriptor) context.getVariables().getVariable("node");

        if (logger.isDebugEnabled()) {
            logger.debug("root: " + context.getContextBean().toString() + " parent: " + parent.toString()
                    + " name: " + name + " index: " + index + " type: " + node.getType());
        }

        Class clazz = Thread.currentThread().getContextClassLoader().loadClass(node.getType());
        if (clazz == Boolean.class) {
            ptr.setValue(new Boolean(false));
        } else if (clazz == Integer.class) {
            ptr.setValue(new Integer(0));
        } else if (clazz == Long.class) {
            ptr.setValue(new Long(0L));
        } else if (clazz == Double.class) {
            ptr.setValue(new Double(0.0));
        } else if (clazz == Float.class) {
            ptr.setValue(new Float(0.0));
        } else if (clazz == Short.class) {
            ptr.setValue(new Short((short) 0));
        } else if (clazz == Byte.class) {
            ptr.setValue(new Byte((byte) 0));
        } else if (clazz == Money.class) {
            ptr.setValue(new Money("0.0"));
        } else if (clazz == BigDecimal.class) {
            ptr.setValue(BigDecimal.valueOf(0.0));
        } else {
            ptr.setValue(clazz.newInstance());
        }
    } catch (Exception exception) {
        logger.error("root: " + context.getContextBean().toString() + " parent: " + parent.toString()
                + " name: " + name + " index: " + index, exception);
        return false;
    }

    return true;
}

From source file:org.openvpms.macro.impl.MacroFunctionsTestCase.java

/**
 * Tests the two argument version://from w w w.  jav  a2s .com
 * <pre>
 *   macro:eval('macroname', somecontext)
 * </pre>
 * This evaluates the macro against the specified context object.
 */
@Test
public void testTwoArgEval() {
    Party customer = TestHelper.createCustomer(false);

    JXPathContext ctx = createContext(customer);
    assertEquals("Customer", ctx.getValue("macro:eval('displayName', .)"));

    ctx = createContext(new Object());
    ctx.getVariables().declareVariable("customer", customer);
    assertEquals("Customer", ctx.getValue("macro:eval('displayName', $customer)"));
}

From source file:org.openvpms.web.echo.style.StylePropertyEvaluator.java

/**
 * Returns properties for the specified screen resolution.
 * <p/>//from w  w w .  ja v  a 2 s.  c  o  m
 * The <tt>width</tt> and <tt>height</tt> are used to declare the <em>$width</em> and <em>$height</em> variables
 * respectively.
 * The <em>$font.size</em> variable is obtained from <tt>properties</tt> if set, or the default properties if not.
 *
 * @param width      the screen width
 * @param height     the screen height
 * @param properties properties to override the defaults. May be <tt>null</tt>
 * @return properties for the screen resolution
 */
public Map<String, String> getProperties(int width, int height, Map<String, String> properties) {
    Map<String, String> result = new HashMap<String, String>(defaults);
    if (properties != null) {
        result.putAll(properties);
    }
    JXPathContext context = JXPathContext.newContext(new Object());
    Variables variables = context.getVariables();
    variables.declareVariable("width", width);
    variables.declareVariable("height", height);
    evaluateAndDeclare(FONT_SIZE, result, context);
    evaluateAndDeclare(FONT_H4_SIZE, result, context);
    evaluateAndDeclare(PADDING_LARGE, result, context);
    evaluateAndDeclare(PADDING_MEDIUM, result, context);
    evaluateAndDeclare(PADDING_SMALL, result, context);
    evaluateAndDeclare(PADDING_SMALLER, result, context);
    evaluateAndDeclare(PADDING_TINY, result, context);

    evaluate(result, context);
    return result;
}

From source file:org.openvpms.web.echo.style.StylePropertyEvaluator.java

/**
 * Evaluates an expression, declaring the value as a variable in the context, and replacing the original expression
 * with it./*w w  w.  ja  v a 2 s  .co  m*/
 *
 * @param name       the property name
 * @param properties the properties to get the property expression from, and replace its value with
 * @param context    the context to evaluate the expression
 */
private void evaluateAndDeclare(String name, Map<String, String> properties, JXPathContext context) {
    String expression = properties.get(name);
    if (expression != null) {
        String value = evaluate(expression, context);
        properties.put(name, value);
        context.getVariables().declareVariable(name, value);
    }
}

From source file:org.xchain.example.namespaces.guide.HelloWorldCommand.java

/**
 * This method will be called when an {http://www.xchain.org/guide}hello-world element is encountered.
 *
 * @param context the context in which this command is called.
 *//* w w  w  .ja v a2s . com*/
public boolean execute(JXPathContext context) throws Exception {
    // create a QName for the variable that we are going to set.
    QName name = QName.valueOf("{http://www.xchain.org/guide}hello");

    // declare the variable.  Out of the box, JXPath does not cleanly implement variables with
    // QNames and it does not have a concept of scope, so we will need to cast the variables
    // class to a org.xchain.framework.jxpath.ScopedQNameVariables class.
    ((ScopedQNameVariables) context.getVariables()).declareVariable(name, "Hello World", Scope.chain);

    // return false, so that other chains will execute.
    return false;
}

From source file:org.xchain.example.namespaces.xhtml.OptionCommand.java

public boolean execute(JXPathContext context) throws Exception {
    String value = getValue(context);
    String currentValue = (String) ((ScopedQNameVariables) context.getVariables())
            .getVariable(SelectCommand.SELECTED_VARIABLE_NAME, Scope.execution);

    // get the xhtml prefix and make sure that there is a mapping for it.
    String xhtmlPrefix = context.getPrefix(XHTML_NAMESPACE);
    if (xhtmlPrefix == null) {
        throw new IllegalStateException("There is no mapping defined for " + XHTML_NAMESPACE);
    }/*from   www . j  a v a 2 s  .co  m*/

    AttributesImpl attributes = new AttributesImpl();
    if (value != null) {
        attributes.addAttribute("", VALUE_LOCAL_NAME, VALUE_LOCAL_NAME, "CDATA", value);
    }
    if (value != null && value.equals(currentValue)) {
        attributes.addAttribute("", SELECTED_LOCAL_NAME, SELECTED_LOCAL_NAME, "CDATA", SELECTED_VALUE);
    }

    ContentHandler handler = getContentHandler();
    handler.startElement(XHTML_NAMESPACE, OPTION_LOCAL_NAME, qNameString(xhtmlPrefix, OPTION_LOCAL_NAME),
            attributes);

    boolean result = false;
    Exception exception = null;

    try {
        result = super.execute(context);
    } catch (Exception e) {
        exception = e;
    }
    if (exception == null || !(exception instanceof SAXException)) {
        handler.endElement(XHTML_NAMESPACE, OPTION_LOCAL_NAME, qNameString(xhtmlPrefix, OPTION_LOCAL_NAME));
    }

    if (exception != null) {
        throw exception;
    }
    return result;
}