Example usage for org.apache.commons.jxpath Variables declareVariable

List of usage examples for org.apache.commons.jxpath Variables declareVariable

Introduction

In this page you can find the example usage for org.apache.commons.jxpath Variables declareVariable.

Prototype

void declareVariable(String varName, Object value);

Source Link

Document

Defines a new variable with the specified value or modifies the value of an existing variable.

Usage

From source file:org.apache.cocoon.woody.transformation.WoodyPipelineConfig.java

/**
 * Creates and initializes a WoodyPipelineConfig object based on the passed
 * arguments of the setup() of the specific Pipeline-component.
 * /*w  w w  . j a v a2s.  c  o m*/
 * @param objectModel the objectmodel as passed in the setup()
 * @param parameters the parameters as passed in the setup()
 * @return an instance of WoodyPipelineConfig initialized according to the 
 * settings in the sitemap.
 */
public static WoodyPipelineConfig createConfig(Map objectModel, Parameters parameters) {
    // create and set the jxpathContext...
    Object flowContext = FlowHelper.getContextObject(objectModel);
    WebContinuation wk = FlowHelper.getWebContinuation(objectModel);
    JXPathContext jxpc = JXPathContext.newContext(flowContext);
    Variables vars = jxpc.getVariables();
    vars.declareVariable("continuation", wk);
    Request request = ObjectModelHelper.getRequest(objectModel);
    vars.declareVariable("request", request);
    Session session = request.getSession(false);
    vars.declareVariable("session", session);
    vars.declareVariable("parameters", parameters);

    Locale localeParameter = null;
    String localeStr = parameters.getParameter("locale", null);
    if (localeStr != null) {
        localeParameter = I18nUtils.parseLocale(localeStr);
    }

    String attributeName = parameters.getParameter("attribute-name", null);
    String actionExpression = parameters.getParameter("form-action", null);
    String formMethod = parameters.getParameter("form-method", "POST");
    //TODO (20031223 mpo)think about adding form-encoding for the Generator.
    // Note generator will also need some text to go on the submit-button? 
    // Alternative to adding more here is to apply xinclude ?

    return new WoodyPipelineConfig(jxpc, request, localeParameter, attributeName, actionExpression, formMethod);
}

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. c om
 */
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.firesoa.common.jxpath.XMLModelTestCase.java

public void setUp() {
    if (context == null) {
        DocumentContainer docCtr = createDocumentContainer();

        context = createContext(docCtr);

        Variables vars = context.getVariables();
        vars.declareVariable("document", docCtr.getValue());
        vars.declareVariable("container", docCtr);
        vars.declareVariable("element", context.getPointer("vendor/location/address/street").getNode());
    }// w ww. j  av a  2  s  .  c om
}

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

/**
 * Returns properties for the specified screen resolution.
 * <p/>//from   w  w  w . jav a2  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.paxml.core.Context.java

/**
 * Select objects with xpath.//from  w w  w.  j  a  v  a 2s .  c  o m
 * 
 * @param from
 *            the object to select properties from, null to select from
 *            entire context.
 * @param xpath
 *            the xpath
 * @param alwaysList
 *            true to return a list with one item inside if the xpath
 *            results in one object to be selected.
 * @return either a list of objects that satisfies the xpath, or the object
 *         itself if the xpath results in one object to be selected and the
 *         "alwaysList" parameter is false.
 */
public Object xpathSelect(Object from, String xpath, boolean alwaysList) {
    Variables vars = new BasicVariables();

    if (from == null) {
        ObjectTree nameGlobal = getRootContext().getNameMap(false, true);
        ObjectTree nameLocal = getNameMap(true, false);

        vars.declareVariable(XPATH_NAME_GLOBAL_VAR, nameGlobal);
        vars.declareVariable(XPATH_NAME_LOCAL_VAR, nameLocal);

        ObjectTree nameAuto = new ObjectTree(null, nameGlobal);
        nameAuto.addValues(nameLocal);

        from = nameAuto;
    }

    JXPathContext xpathContext = JXPathContext.newContext(from);
    xpathContext.setVariables(vars);
    xpathContext.setIdentityManager(this);

    setXpathFunctions(xpathContext);

    try {
        Object selected = xpathContext.iterate(xpath);

        List<Object> list = new ArrayList<Object>(1);
        if (selected instanceof Iterator) {
            final Iterator<?> it = (Iterator<?>) selected;
            while (it.hasNext()) {
                Object obj = it.next();
                list.add(getXpathResultObject(obj));
            }
            if (list.size() == 1) {
                selected = list.get(0);
            } else {
                selected = list;
            }
        } else {

            selected = getXpathResultObject(selected);

            if (selected != null && alwaysList) {
                list.add(selected);
            }
        }
        if (alwaysList) {
            return list;
        } else {
            if (selected instanceof List) {
                list = (List) selected;
                final int size = list.size();
                if (size == 0) {
                    return null;
                } else if (size == 1) {
                    return list.get(0);
                }
            }
            return selected;
        }
    } catch (NullPointerException e) {
        // when jxpath throws null pointer exception, it has problem
        // searching non-existing paths
        return null;
    }
}