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