KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > webwork > interceptor > WebWorkConversionErrorInterceptor


1 /*
2  * Copyright (c) 2002-2003 by OpenSymphony
3  * All rights reserved.
4  */

5 package com.opensymphony.webwork.interceptor;
6
7 import com.opensymphony.xwork.ActionInvocation;
8 import com.opensymphony.xwork.interceptor.ConversionErrorInterceptor;
9 import com.opensymphony.xwork.util.OgnlValueStack;
10
11
12 /**
13  * This interceptor adds the conversion errors from the ActionContext to the field errors of the Action
14  * if the field value is not null, "", or {""} (a size 1 String array with only an empty String).
15  *
16  * @author Jason Carreira
17  * @see com.opensymphony.xwork.ActionContext#getConversionErrors()
18  * @see ConversionErrorInterceptor
19  */

20 public class WebWorkConversionErrorInterceptor extends ConversionErrorInterceptor {
21     //~ Methods ////////////////////////////////////////////////////////////////
22

23     protected Object JavaDoc getOverrideExpr(ActionInvocation invocation, Object JavaDoc value) {
24         OgnlValueStack stack = invocation.getStack();
25
26         try {
27             stack.push(value);
28
29             return "'" + stack.findValue("top", String JavaDoc.class) + "'";
30         } finally {
31             stack.pop();
32         }
33     }
34
35     /**
36      * Returns <tt>false</tt> if the value is null, "", or {""} (array of size 1 with
37      * a blank element). Returns <tt>true</tt> otherwise.
38      *
39      * @param propertyName the name of the property to check.
40      * @param value the value to error check.
41      * @return <tt>false</tt> if the value is null, "", or {""}, <tt>true</tt> otherwise.
42      */

43     protected boolean shouldAddError(String JavaDoc propertyName, Object JavaDoc value) {
44         if (value == null) {
45             return false;
46         }
47
48         if ("".equals(value)) {
49             return false;
50         }
51
52         if (value instanceof String JavaDoc[]) {
53             String JavaDoc[] array = (String JavaDoc[]) value;
54
55             if (array.length == 0) {
56                 return false;
57             }
58
59             if (array.length > 1) {
60                 return true;
61             }
62
63             String JavaDoc str = array[0];
64
65             if ("".equals(str)) {
66                 return false;
67             }
68         }
69
70         return true;
71     }
72 }
73
Popular Tags