Search in sources :

Example 31 with Result

use of com.opensymphony.xwork2.Result in project struts by apache.

the class ArrayConverter method convertValue.

@Override
public Object convertValue(Map<String, Object> context, Object target, Member member, String propertyName, Object value, Class toType) {
    Object result = null;
    Class componentType = toType.getComponentType();
    if (componentType != null) {
        TypeConverter converter = getTypeConverter(context);
        if (value.getClass().isArray()) {
            int length = Array.getLength(value);
            result = Array.newInstance(componentType, length);
            for (int i = 0; i < length; i++) {
                Object valueItem = Array.get(value, i);
                Array.set(result, i, converter.convertValue(context, target, member, propertyName, valueItem, componentType));
            }
        } else {
            result = Array.newInstance(componentType, 1);
            Array.set(result, 0, converter.convertValue(context, target, member, propertyName, value, componentType));
        }
    }
    return result;
}
Also used : TypeConverter(com.opensymphony.xwork2.conversion.TypeConverter)

Example 32 with Result

use of com.opensymphony.xwork2.Result in project struts by apache.

the class DefaultWorkflowInterceptor method processInputConfig.

/**
 * Process {@link InputConfig} annotation applied to method
 * @param action action object
 * @param method method
 * @param currentResultName current result name
 *
 * @return result name
 *
 * @throws Exception in case of any errors
 */
protected String processInputConfig(final Object action, final String method, final String currentResultName) throws Exception {
    String resultName = currentResultName;
    InputConfig annotation = MethodUtils.getAnnotation(action.getClass().getMethod(method, EMPTY_CLASS_ARRAY), InputConfig.class, true, true);
    if (annotation != null) {
        if (StringUtils.isNotEmpty(annotation.methodName())) {
            resultName = (String) MethodUtils.invokeMethod(action, true, annotation.methodName());
        } else {
            resultName = annotation.resultName();
        }
        LOG.debug("Changing result name from [{}] to [{}] because of processing annotation [{}] on action [{}]", currentResultName, resultName, InputConfig.class.getSimpleName(), action);
    }
    return resultName;
}
Also used : InputConfig(com.opensymphony.xwork2.interceptor.annotations.InputConfig)

Example 33 with Result

use of com.opensymphony.xwork2.Result in project struts by apache.

the class OgnlValueStackFactory method createValueStack.

public ValueStack createValueStack(ValueStack stack) {
    ValueStack result = new OgnlValueStack(stack, xworkConverter, compoundRootAccessor, containerAllowsStaticMethodAccess(), containerAllowsStaticFieldAccess());
    container.inject(result);
    return result.getActionContext().withContainer(container).withValueStack(result).getValueStack();
}
Also used : ValueStack(com.opensymphony.xwork2.util.ValueStack)

Example 34 with Result

use of com.opensymphony.xwork2.Result in project struts by apache.

the class TextParseUtil method translateVariablesCollection.

/**
 * Resolves given expression on given ValueStack. If found element is a
 * collection each element will be converted to String. If just a single
 * object is found it is converted to String and wrapped in a collection.
 *
 * @param openChars open character array
 * @param expression expression string
 * @param stack value stack
 * @param evaluator value evaluator
 * @param excludeEmptyElements Whether empty elements shall be excluded.
 * @param maxLoopCount max loop count
 * @return converted objects
 */
public static Collection<String> translateVariablesCollection(char[] openChars, String expression, final ValueStack stack, boolean excludeEmptyElements, final ParsedValueEvaluator evaluator, int maxLoopCount) {
    ParsedValueEvaluator ognlEval = new ParsedValueEvaluator() {

        public Object evaluate(String parsedValue) {
            // no asType !!!
            return stack.findValue(parsedValue);
        }
    };
    ActionContext actionContext = stack.getActionContext();
    TextParser parser = actionContext.getContainer().getInstance(TextParser.class);
    Object result = parser.evaluate(openChars, expression, ognlEval, maxLoopCount);
    Collection<String> resultCol;
    if (result instanceof Collection) {
        @SuppressWarnings("unchecked") Collection<Object> casted = (Collection<Object>) result;
        resultCol = new ArrayList<>();
        XWorkConverter conv = actionContext.getContainer().getInstance(XWorkConverter.class);
        for (Object element : casted) {
            String stringElement = (String) conv.convertValue(actionContext.getContextMap(), element, String.class);
            if (shallBeIncluded(stringElement, excludeEmptyElements)) {
                if (evaluator != null) {
                    stringElement = evaluator.evaluate(stringElement).toString();
                }
                resultCol.add(stringElement);
            }
        }
    } else {
        resultCol = new ArrayList<>();
        String resultStr = translateVariables(expression, stack, evaluator);
        if (shallBeIncluded(resultStr, excludeEmptyElements)) {
            resultCol.add(resultStr);
        }
    }
    return resultCol;
}
Also used : XWorkConverter(com.opensymphony.xwork2.conversion.impl.XWorkConverter) ActionContext(com.opensymphony.xwork2.ActionContext)

Example 35 with Result

use of com.opensymphony.xwork2.Result in project struts by apache.

the class DefaultActionInvocation method createResult.

public Result createResult() throws Exception {
    LOG.trace("Creating result related to resultCode [{}]", resultCode);
    if (explicitResult != null) {
        Result ret = explicitResult;
        explicitResult = null;
        return ret;
    }
    ActionConfig config = proxy.getConfig();
    Map<String, ResultConfig> results = config.getResults();
    ResultConfig resultConfig = null;
    try {
        resultConfig = results.get(resultCode);
    } catch (NullPointerException e) {
        LOG.debug("Got NPE trying to read result configuration for resultCode [{}]", resultCode);
    }
    if (resultConfig == null) {
        // If no result is found for the given resultCode, try to get a wildcard '*' match.
        resultConfig = results.get("*");
    }
    if (resultConfig != null) {
        try {
            return objectFactory.buildResult(resultConfig, invocationContext.getContextMap());
        } catch (Exception e) {
            LOG.error("There was an exception while instantiating the result of type {}", resultConfig.getClassName(), e);
            throw new StrutsException(e, resultConfig);
        }
    } else if (resultCode != null && !Action.NONE.equals(resultCode) && unknownHandlerManager.hasUnknownHandlers()) {
        return unknownHandlerManager.handleUnknownResult(invocationContext, proxy.getActionName(), proxy.getConfig(), resultCode);
    }
    return null;
}
Also used : ActionConfig(com.opensymphony.xwork2.config.entities.ActionConfig) StrutsException(org.apache.struts2.StrutsException) ResultConfig(com.opensymphony.xwork2.config.entities.ResultConfig) ConfigurationException(com.opensymphony.xwork2.config.ConfigurationException) NoSuchPropertyException(ognl.NoSuchPropertyException) StrutsException(org.apache.struts2.StrutsException) MethodFailedException(ognl.MethodFailedException)

Aggregations

ActionSupport (com.opensymphony.xwork2.ActionSupport)63 Test (org.junit.Test)52 ActionProxy (com.opensymphony.xwork2.ActionProxy)51 List (java.util.List)49 ValueStack (com.opensymphony.xwork2.util.ValueStack)38 Result (edu.stanford.CVC4.Result)35 ResultConfig (com.opensymphony.xwork2.config.entities.ResultConfig)34 ActionContext (com.opensymphony.xwork2.ActionContext)33 Expr (edu.stanford.CVC4.Expr)32 SExpr (edu.stanford.CVC4.SExpr)32 CVC4.vectorExpr (edu.stanford.CVC4.vectorExpr)31 ArrayList (java.util.ArrayList)31 ActionInvocation (com.opensymphony.xwork2.ActionInvocation)30 HashMap (java.util.HashMap)29 Rational (edu.stanford.CVC4.Rational)25 ActionConfig (com.opensymphony.xwork2.config.entities.ActionConfig)22 Map (java.util.Map)21 ServletActionContext (org.apache.struts2.ServletActionContext)21 Result (com.opensymphony.xwork2.Result)18 PackageConfig (com.opensymphony.xwork2.config.entities.PackageConfig)18