Search in sources :

Example 26 with Result

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

the class XmlConfigurationProvider method buildExceptionMappings.

/**
 * Build a list of exception mapping objects from below a given XML element.
 *
 * @param element the given XML element
 * @param packageContext the package context
 *
 * @return list of exception mapping config objects
 */
protected List<ExceptionMappingConfig> buildExceptionMappings(Element element, PackageConfig.Builder packageContext) {
    NodeList exceptionMappingEls = element.getElementsByTagName("exception-mapping");
    List<ExceptionMappingConfig> exceptionMappings = new ArrayList<>();
    for (int i = 0; i < exceptionMappingEls.getLength(); i++) {
        Element ehElement = (Element) exceptionMappingEls.item(i);
        if (ehElement.getParentNode().equals(element) || ehElement.getParentNode().getNodeName().equals(element.getNodeName())) {
            String emName = ehElement.getAttribute("name");
            String exceptionClassName = ehElement.getAttribute("exception");
            String exceptionResult = ehElement.getAttribute("result");
            Map<String, String> params = XmlHelper.getParams(ehElement);
            if (StringUtils.isEmpty(emName)) {
                emName = exceptionResult;
            }
            ExceptionMappingConfig ehConfig = new ExceptionMappingConfig.Builder(emName, exceptionClassName, exceptionResult).addParams(params).location(DomHelper.getLocationObject(ehElement)).build();
            exceptionMappings.add(ehConfig);
        }
    }
    return exceptionMappings;
}
Also used : NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) ExceptionMappingConfig(com.opensymphony.xwork2.config.entities.ExceptionMappingConfig)

Example 27 with Result

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

the class XmlConfigurationProvider method addPackage.

/**
 * Create a PackageConfig from an XML element representing it.
 *
 * @param packageElement the given XML element
 * @return the package config
 * @throws ConfigurationException in case of configuration errors
 */
protected PackageConfig addPackage(Element packageElement) throws ConfigurationException {
    String packageName = packageElement.getAttribute("name");
    PackageConfig packageConfig = configuration.getPackageConfig(packageName);
    if (packageConfig != null) {
        LOG.debug("Package [{}] already loaded, skipping re-loading it and using existing PackageConfig [{}]", packageName, packageConfig);
        return packageConfig;
    }
    PackageConfig.Builder newPackage = buildPackageContext(packageElement);
    if (newPackage.isNeedsRefresh()) {
        return newPackage.build();
    }
    LOG.debug("Loaded {}", newPackage);
    // add result types (and default result) to this package
    addResultTypes(newPackage, packageElement);
    // load the interceptors and interceptor stacks for this package
    loadInterceptors(newPackage, packageElement);
    // load the default interceptor reference for this package
    loadDefaultInterceptorRef(newPackage, packageElement);
    // load the default class ref for this package
    loadDefaultClassRef(newPackage, packageElement);
    // load the global result list for this package
    loadGlobalResults(newPackage, packageElement);
    loadGlobalAllowedMethods(newPackage, packageElement);
    // load the global exception handler list for this package
    loadGlobalExceptionMappings(newPackage, packageElement);
    // get actions
    NodeList actionList = packageElement.getElementsByTagName("action");
    for (int i = 0; i < actionList.getLength(); i++) {
        Element actionElement = (Element) actionList.item(i);
        addAction(actionElement, newPackage);
    }
    // load the default action reference for this package
    loadDefaultActionRef(newPackage, packageElement);
    PackageConfig cfg = newPackage.build();
    configuration.addPackageConfig(cfg.getName(), cfg);
    return cfg;
}
Also used : NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element) PackageConfig(com.opensymphony.xwork2.config.entities.PackageConfig)

Example 28 with Result

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

the class XmlConfigurationProvider method addResultTypes.

protected void addResultTypes(PackageConfig.Builder packageContext, Element element) {
    NodeList resultTypeList = element.getElementsByTagName("result-type");
    for (int i = 0; i < resultTypeList.getLength(); i++) {
        Element resultTypeElement = (Element) resultTypeList.item(i);
        String name = resultTypeElement.getAttribute("name");
        String className = resultTypeElement.getAttribute("class");
        String def = resultTypeElement.getAttribute("default");
        Location loc = DomHelper.getLocationObject(resultTypeElement);
        Class clazz = verifyResultType(className, loc);
        if (clazz != null) {
            String paramName = null;
            try {
                paramName = (String) clazz.getField("DEFAULT_PARAM").get(null);
            } catch (Throwable t) {
                LOG.debug("The result type [{}] doesn't have a default param [DEFAULT_PARAM] defined!", className, t);
            }
            ResultTypeConfig.Builder resultType = new ResultTypeConfig.Builder(name, className).defaultResultParam(paramName).location(DomHelper.getLocationObject(resultTypeElement));
            Map<String, String> params = XmlHelper.getParams(resultTypeElement);
            if (!params.isEmpty()) {
                resultType.addParams(params);
            }
            packageContext.addResultTypeConfig(resultType.build());
            // set the default result type
            if (BooleanUtils.toBoolean(def)) {
                packageContext.defaultResultType(name);
            }
        }
    }
}
Also used : ResultTypeConfig(com.opensymphony.xwork2.config.entities.ResultTypeConfig) NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element) ContainerBuilder(com.opensymphony.xwork2.inject.ContainerBuilder) Location(com.opensymphony.xwork2.util.location.Location)

Example 29 with Result

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

the class CollectionConverter method convertValue.

public Object convertValue(Map<String, Object> context, Object target, Member member, String propertyName, Object value, Class toType) {
    Collection result;
    Class memberType = String.class;
    if (target != null) {
        memberType = objectTypeDeterminer.getElementClass(target.getClass(), propertyName, null);
        if (memberType == null) {
            memberType = String.class;
        }
    }
    if (toType.isAssignableFrom(value.getClass())) {
        // no need to do anything
        result = (Collection) value;
    } else if (value.getClass().isArray()) {
        Object[] objArray = (Object[]) value;
        TypeConverter converter = getTypeConverter(context);
        result = createCollection(toType, memberType, objArray.length);
        for (Object anObjArray : objArray) {
            Object convertedValue = converter.convertValue(context, target, member, propertyName, anObjArray, memberType);
            if (!TypeConverter.NO_CONVERSION_POSSIBLE.equals(convertedValue)) {
                result.add(convertedValue);
            }
        }
    } else if (Collection.class.isAssignableFrom(value.getClass())) {
        Collection col = (Collection) value;
        TypeConverter converter = getTypeConverter(context);
        result = createCollection(toType, memberType, col.size());
        for (Object aCol : col) {
            Object convertedValue = converter.convertValue(context, target, member, propertyName, aCol, memberType);
            if (!TypeConverter.NO_CONVERSION_POSSIBLE.equals(convertedValue)) {
                result.add(convertedValue);
            }
        }
    } else {
        result = createCollection(toType, memberType, -1);
        TypeConverter converter = getTypeConverter(context);
        Object convertedValue = converter.convertValue(context, target, member, propertyName, value, memberType);
        if (!TypeConverter.NO_CONVERSION_POSSIBLE.equals(convertedValue)) {
            result.add(convertedValue);
        }
    }
    return result;
}
Also used : TypeConverter(com.opensymphony.xwork2.conversion.TypeConverter) Collection(java.util.Collection)

Example 30 with Result

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

the class AbstractMatcher method convertParam.

/**
 * <p> Inserts into a value wildcard-matched strings where specified
 * with the {x} syntax.  If a wildcard-matched value isn't found, the
 * replacement token is turned into an empty string.
 * </p>
 *
 * @param val  The value to convert
 * @param vars A Map of wildcard-matched strings
 * @return The new value
 */
protected String convertParam(String val, Map<String, String> vars) {
    if (val == null) {
        return null;
    }
    Matcher wildcardMatcher = WILDCARD_PATTERN.matcher(val);
    StringBuffer result = new StringBuffer();
    while (wildcardMatcher.find()) {
        wildcardMatcher.appendReplacement(result, vars.getOrDefault(wildcardMatcher.group(1), ""));
    }
    wildcardMatcher.appendTail(result);
    return result.toString();
}
Also used : Matcher(java.util.regex.Matcher) PatternMatcher(com.opensymphony.xwork2.util.PatternMatcher)

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