001: /*
002: * $Id: StrutsConversionErrorInterceptor.java 471756 2006-11-06 15:01:43Z husted $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021: package org.apache.struts2.interceptor;
022:
023: import com.opensymphony.xwork2.ActionInvocation;
024: import com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor;
025: import com.opensymphony.xwork2.util.ValueStack;
026:
027: /**
028: * <!-- START SNIPPET: description -->
029: *
030: * This interceptor extends {@link ConversionErrorInterceptor} but only adds conversion errors from the ActionContext to
031: * the field errors of the action if the field value is not null, "", or {""} (a size 1 String array with only an empty
032: * String). See {@link ConversionErrorInterceptor} for more information, as well as the Type Conversion documentation.
033: *
034: * <!-- END SNIPPET: description -->
035: *
036: * <p/> <u>Interceptor parameters:</u>
037: *
038: * <!-- START SNIPPET: parameters -->
039: *
040: * <ul>
041: *
042: * <li>None</li>
043: *
044: * </ul>
045: *
046: * <!-- END SNIPPET: parameters -->
047: *
048: * <p/> <u>Extending the interceptor:</u>
049: *
050: * <p/>
051: *
052: * <!-- START SNIPPET: extending -->
053: *
054: * There are no known extension points for this interceptor.
055: *
056: * <!-- END SNIPPET: extending -->
057: *
058: * <pre>
059: * <!-- START SNIPPET: example -->
060: * <action name="someAction" class="com.examples.SomeAction">
061: * <interceptor-ref name="params"/>
062: * <interceptor-ref name="conversionError"/>
063: * <result name="success">good_result.ftl</result>
064: * </action>
065: * <!-- END SNIPPET: example -->
066: * </pre>
067: *
068: * @see com.opensymphony.xwork2.ActionContext#getConversionErrors()
069: * @see ConversionErrorInterceptor
070: */
071: public class StrutsConversionErrorInterceptor extends
072: ConversionErrorInterceptor {
073:
074: private static final long serialVersionUID = 2759744840082921602L;
075:
076: protected Object getOverrideExpr(ActionInvocation invocation,
077: Object value) {
078: ValueStack stack = invocation.getStack();
079:
080: try {
081: stack.push(value);
082:
083: return "'" + stack.findValue("top", String.class) + "'";
084: } finally {
085: stack.pop();
086: }
087: }
088:
089: /**
090: * Returns <tt>false</tt> if the value is null, "", or {""} (array of size 1 with a blank element). Returns
091: * <tt>true</tt> otherwise.
092: *
093: * @param propertyName the name of the property to check.
094: * @param value the value to error check.
095: * @return <tt>false</tt> if the value is null, "", or {""}, <tt>true</tt> otherwise.
096: */
097: protected boolean shouldAddError(String propertyName, Object value) {
098: if (value == null) {
099: 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: }
|