001: /*
002: * Copyright (c) 2002-2003 by OpenSymphony
003: * All rights reserved.
004: */
005: package com.opensymphony.webwork.interceptor;
006:
007: import com.opensymphony.xwork.ActionInvocation;
008: import com.opensymphony.xwork.interceptor.ConversionErrorInterceptor;
009: import com.opensymphony.xwork.util.OgnlValueStack;
010:
011: /**
012: * <!-- START SNIPPET: description -->
013: *
014: * This interceptor extends {@link ConversionErrorInterceptor} but only adds conversion errors from the ActionContext to
015: * the field errors of the action if the field value is not null, "", or {""} (a size 1 String array with only an empty
016: * String). See {@link ConversionErrorInterceptor} for more information, as well as the Type Conversion documentation.
017: *
018: * <!-- END SNIPPET: description -->
019: *
020: * <p/> <u>Interceptor parameters:</u>
021: *
022: * <!-- START SNIPPET: parameters -->
023: *
024: * <ul>
025: *
026: * <li>None</li>
027: *
028: * </ul>
029: *
030: * <!-- END SNIPPET: parameters -->
031: *
032: * <p/> <u>Extending the interceptor:</u>
033: *
034: * <p/>
035: *
036: * <!-- START SNIPPET: extending -->
037: *
038: * There are no known extension points for this interceptor.
039: *
040: * <!-- END SNIPPET: extending -->
041: *
042: * <pre>
043: * <!-- START SNIPPET: example -->
044: * <action name="someAction" class="com.examples.SomeAction">
045: * <interceptor-ref name="params"/>
046: * <interceptor-ref name="conversionError"/>
047: * <result name="success">good_result.ftl</result>
048: * </action>
049: * <!-- END SNIPPET: example -->
050: * </pre>
051: *
052: * @author Jason Carreira
053: * @see com.opensymphony.xwork.ActionContext#getConversionErrors()
054: * @see ConversionErrorInterceptor
055: */
056: public class WebWorkConversionErrorInterceptor extends
057: ConversionErrorInterceptor {
058:
059: private static final long serialVersionUID = 7203991102872573481L;
060:
061: protected Object getOverrideExpr(ActionInvocation invocation,
062: Object value) {
063: OgnlValueStack stack = invocation.getStack();
064:
065: try {
066: stack.push(value);
067:
068: return "'" + stack.findValue("top", String.class) + "'";
069: } finally {
070: stack.pop();
071: }
072: }
073:
074: /**
075: * Returns <tt>false</tt> if the value is null, "", or {""} (array of size 1 with a blank element). Returns
076: * <tt>true</tt> otherwise.
077: *
078: * @param propertyName the name of the property to check.
079: * @param value the value to error check.
080: * @return <tt>false</tt> if the value is null, "", or {""}, <tt>true</tt> otherwise.
081: */
082: protected boolean shouldAddError(String propertyName, Object value) {
083: if (value == null) {
084: return false;
085: }
086:
087: if ("".equals(value)) {
088: return false;
089: }
090:
091: if (value instanceof String[]) {
092: String[] array = (String[]) value;
093:
094: if (array.length == 0) {
095: return false;
096: }
097:
098: if (array.length > 1) {
099: return true;
100: }
101:
102: String str = array[0];
103:
104: if ("".equals(str)) {
105: return false;
106: }
107: }
108:
109: return true;
110: }
111: }
|