View Javadoc

1   /*
2    * $Id: StrutsConversionErrorInterceptor.java 651946 2008-04-27 13:41:38Z apetrelli $
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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   * &lt;action name="someAction" class="com.examples.SomeAction"&gt;
63   *     &lt;interceptor-ref name="params"/&gt;
64   *     &lt;interceptor-ref name="conversionError"/&gt;
65   *     &lt;result name="success"&gt;good_result.ftl&lt;/result&gt;
66   * &lt;/action&gt;
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 }