📄 valueutils.java
字号:
/* * Copyright (c) 2005, John Mettraux, OpenWFE.org * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * . Redistributions of source code must retain the above copyright notice, this * list of conditions and the following disclaimer. * * . Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * . Neither the name of the "OpenWFE" nor the names of its contributors may be * used to endorse or promote products derived from this software without * specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. * * $Id: ValueUtils.java,v 1.20 2005/06/30 14:47:16 jmettraux Exp $ *///// ValueUtils.java//// jmettraux@openwfe.org//// generated with // jtmpl 1.1.00 16.08.2003 John Mettraux (jmettraux@openwfe.org)//package openwfe.org.engine.expressions;import openwfe.org.misc.Text;import openwfe.org.misc.ArgParser;import openwfe.org.engine.Definitions;import openwfe.org.engine.expool.ExpressionPool;import openwfe.org.engine.workitem.Attribute;import openwfe.org.engine.workitem.LongAttribute;import openwfe.org.engine.workitem.StringAttribute;import openwfe.org.engine.workitem.DoubleAttribute;import openwfe.org.engine.workitem.BooleanAttribute;import openwfe.org.engine.workitem.IntegerAttribute;import openwfe.org.engine.workitem.InFlowWorkItem;import openwfe.org.engine.functions.FunctionMap;/** * Some static methods for extracting values from fields and variables and * fetching common values in expression attributes. * This class doesn't need to be instantiated. * * <p><font size=2>CVS Info : * <br>$Author: jmettraux $ * <br>$Id: ValueUtils.java,v 1.20 2005/06/30 14:47:16 jmettraux Exp $ </font> * * @author jmettraux@openwfe.org */public abstract class ValueUtils{ private final static org.apache.log4j.Logger log = org.apache.log4j.Logger .getLogger(ValueUtils.class.getName()); // // CONSTANTS private final static String VALUE = "value"; private final static String FIELD_VALUE = "field-value"; private final static String VARIABLE_VALUE = "variable-value"; private final static String FUNCTION_VALUE = "function-value"; private final static String FIELD_TARGET = "field"; private final static String VARIABLE_TARGET = "variable"; private final static String OVERRIDE = "override"; private final static String TYPE = "type"; /** * This variable name is used by expressions like 'if' and 'not' * to determine the outcome of their action (they usually set this * variable in their parent scope... */ public final static String BOOLEAN_RESULT = "__boolean_result__"; private final static String T_LONG = "long"; private final static String T_DOUBLE = "double"; private final static String T_BOOLEAN = "boolean"; private final static String T_INTEGER = "integer"; /* * Not in use * public final static String T_STRING = "string"; */ private final static ArgParser argParser = new ArgParser(); // // STATIC METHODS /* private static String resolveVariables (final FlowExpression expression, final String arg) { if (arg.indexOf("${") < 0) return arg; return Text.substitute(arg, new VariableMap(expression)); } */ /** * This method is normally used by determineValue() to call * a function; but it's also used from VariableMap for the same * purpose, that's why it's public. */ public static String executeFunction (final FlowExpression expression, final InFlowWorkItem wi, final String functionCall) { int i = functionCall.indexOf("("); final String functionName = functionCall.substring(0, i); log.debug("executeFunction() functionName >"+functionName+"<"); int j = functionCall.lastIndexOf(")"); final String rawArgs = functionCall.substring(i+1, j); log.debug("executeFunction() rawArgs >"+rawArgs+"<"); //final String[] args = parseArgs(expression, rawArgs); final String[] args = argParser.parse(rawArgs); final FunctionMap functionMap = Definitions .getFunctionMap(expression.context()); return functionMap.eval(functionName, expression, wi, args); } /** * Given a constant name like * "openwfe.org.engine.Definitions.OPENWFE_VERSION", will return the * value attached to this constant as a String. */ public static String lookupConstant (final FlowExpression expression, final InFlowWorkItem wi, final String constantName) { final int i = constantName.lastIndexOf("."); if (i > -1) { final String className = constantName.substring(0, i); final String constName = constantName.substring(i+1); try { final Class clazz = Class.forName(className); final java.lang.reflect.Field field = clazz.getDeclaredField(constName); final Object o = field.get(null); if (o != null) return o.toString(); log.debug("lookupConstant() '"+constantName+"' found null."); } catch (final Throwable t) { log.debug("lookupConstant() '"+constantName+"' failure", t); } } return "cannot lookup constant \""+constantName+"\""; } /** * This method looks in the expression params to determine which value * to return, the expression params dictate whether the value comes * from a workitem field or an expression variable. */ public static Object determineValue (final FlowExpression expression, final InFlowWorkItem workItem) throws ValueException { return determineValue("", expression, workItem); } /** * This method looks in the expression params to determine which value * to return, the expression params dictate whether the value comes * from a workitem field or an expression variable, a prefix is given for * the key lookup in the params of the expression. */ public static Object determineValue (final String prefix, final FlowExpression expression, final InFlowWorkItem workItem) throws ValueException { log.debug("determineValue() prefix >"+prefix+"<"); // // where should the value be taken from ? String value = expression .lookupAttribute(prefix+VALUE, workItem); String valueFieldName = expression .lookupAttribute(prefix+FIELD_VALUE, workItem); String valueVariableName = expression .lookupAttribute(prefix+VARIABLE_VALUE, workItem); if (value == null && prefix.equals("")) // // nested XML value // value = expression.lookupAttribute(Definitions.A_VALUE, workItem); //String functionValue = (String)expression.getAttributes() // .get(prefix+FUNCTION_VALUE); String functionValue = expression .lookupAttribute(prefix+FUNCTION_VALUE, workItem); if (functionValue != null) { functionValue = executeFunction (expression, workItem, functionValue); } if (value == null && valueFieldName == null && valueVariableName == null && functionValue == null) { throw new ValueException ("There is no attribute '"+prefix+VALUE+ "', '"+prefix+FIELD_VALUE+ "' or '"+prefix+VARIABLE_VALUE+ "' or '"+prefix+FUNCTION_VALUE+ "'. Cannot determine value"); } log.debug("value >"+value+"<"); log.debug("value of field >"+valueFieldName+"<"); log.debug("value of variable >"+valueVariableName+"<"); log.debug("functionValue >"+functionValue+"<"); // // extract value Object variableValue = null; if (value != null) { variableValue = value; String type = expression.lookupAttribute(TYPE, workItem); if (type != null) // // enforce a certain type { log.debug("type >"+type+"<"); if (type.equals(T_BOOLEAN)) variableValue = new BooleanAttribute(value); else if (type.equals(T_INTEGER)) variableValue = new IntegerAttribute(value); else if (type.equals(T_LONG)) variableValue = new LongAttribute(value); else if (type.equals(T_DOUBLE)) variableValue = new DoubleAttribute(value); } } else if (valueVariableName != null) { variableValue = expression.lookupVariable(valueVariableName); } else if (valueFieldName != null) { variableValue = workItem.getAttribute(valueFieldName); } else if (functionValue != null) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -