View Javadoc

1   /*
2    * $Id: StrutsConversionErrorInterceptor.java 451544 2006-09-30 05:38:02Z mrdon $
3    *
4    * Copyright 2006 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
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   * &lt;action name="someAction" class="com.examples.SomeAction"&gt;
59   *     &lt;interceptor-ref name="params"/&gt;
60   *     &lt;interceptor-ref name="conversionError"/&gt;
61   *     &lt;result name="success"&gt;good_result.ftl&lt;/result&gt;
62   * &lt;/action&gt;
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 }