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