View Javadoc

1   /*
2    * $Id: StrutsConversionErrorInterceptor.java 471756 2006-11-06 15:01:43Z husted $
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  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   * &lt;action name="someAction" class="com.examples.SomeAction"&gt;
62   *     &lt;interceptor-ref name="params"/&gt;
63   *     &lt;interceptor-ref name="conversionError"/&gt;
64   *     &lt;result name="success"&gt;good_result.ftl&lt;/result&gt;
65   * &lt;/action&gt;
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 }