1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.struts2.interceptor;
19
20 import com.opensymphony.xwork2.ActionInvocation;
21 import com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor;
22 import com.opensymphony.xwork2.util.ValueStack;
23
24
25 /***
26 * <!-- START SNIPPET: description -->
27 *
28 * This interceptor extends {@link ConversionErrorInterceptor} but only adds conversion errors from the ActionContext to
29 * the field errors of the action if the field value is not null, "", or {""} (a size 1 String array with only an empty
30 * String). See {@link ConversionErrorInterceptor} for more information, as well as the Type Conversion documentation.
31 *
32 * <!-- END SNIPPET: description -->
33 *
34 * <p/> <u>Interceptor parameters:</u>
35 *
36 * <!-- START SNIPPET: parameters -->
37 *
38 * <ul>
39 *
40 * <li>None</li>
41 *
42 * </ul>
43 *
44 * <!-- END SNIPPET: parameters -->
45 *
46 * <p/> <u>Extending the interceptor:</u>
47 *
48 * <p/>
49 *
50 * <!-- START SNIPPET: extending -->
51 *
52 * There are no known extension points for this interceptor.
53 *
54 * <!-- END SNIPPET: extending -->
55 *
56 * <pre>
57 * <!-- START SNIPPET: example -->
58 * <action name="someAction" class="com.examples.SomeAction">
59 * <interceptor-ref name="params"/>
60 * <interceptor-ref name="conversionError"/>
61 * <result name="success">good_result.ftl</result>
62 * </action>
63 * <!-- END SNIPPET: example -->
64 * </pre>
65 *
66 * @see com.opensymphony.xwork2.ActionContext#getConversionErrors()
67 * @see ConversionErrorInterceptor
68 */
69 public class StrutsConversionErrorInterceptor extends ConversionErrorInterceptor {
70
71 private static final long serialVersionUID = 2759744840082921602L;
72
73 protected Object getOverrideExpr(ActionInvocation invocation, Object value) {
74 ValueStack stack = invocation.getStack();
75
76 try {
77 stack.push(value);
78
79 return "'" + stack.findValue("top", String.class) + "'";
80 } finally {
81 stack.pop();
82 }
83 }
84
85 /***
86 * Returns <tt>false</tt> if the value is null, "", or {""} (array of size 1 with a blank element). Returns
87 * <tt>true</tt> otherwise.
88 *
89 * @param propertyName the name of the property to check.
90 * @param value the value to error check.
91 * @return <tt>false</tt> if the value is null, "", or {""}, <tt>true</tt> otherwise.
92 */
93 protected boolean shouldAddError(String propertyName, Object value) {
94 if (value == null) {
95 return false;
96 }
97
98 if ("".equals(value)) {
99 return false;
100 }
101
102 if (value instanceof String[]) {
103 String[] array = (String[]) value;
104
105 if (array.length == 0) {
106 return false;
107 }
108
109 if (array.length > 1) {
110 return true;
111 }
112
113 String str = array[0];
114
115 if ("".equals(str)) {
116 return false;
117 }
118 }
119
120 return true;
121 }
122 }