2222import com .opensymphony .xwork2 .util .logging .Logger ;
2323import com .opensymphony .xwork2 .util .logging .LoggerFactory ;
2424import com .opensymphony .xwork2 .validator .ValidationException ;
25+ import org .apache .commons .lang .StringEscapeUtils ;
2526
2627import java .util .LinkedHashMap ;
2728import java .util .Map ;
2829
2930/**
30- *
31- *
31+ *
32+ *
3233 * An abstract base class that adds in the capability to populate the stack with
3334 * a fake parameter map when a conversion error has occurred and the 'repopulateField'
3435 * property is set to "true".
35- *
36+ *
3637 * <p/>
37- *
38+ *
3839 *
3940 * <!-- START SNIPPET: javadoc -->
4041 *
41- * The capability of auto-repopulating the stack with a fake parameter map when
42- * a conversion error has occurred can be done with 'repopulateField' property
43- * set to "true".
42+ * The capability of auto-repopulating the stack with a fake parameter map when
43+ * a conversion error has occurred can be done with 'repopulateField' property
44+ * set to "true".
4445 *
4546 * <p/>
4647 *
47- * This is typically usefull when one wants to repopulate the field with the original value
48- * when a conversion error occurred. Eg. with a textfield that only allows an Integer
48+ * This is typically usefull when one wants to repopulate the field with the original value
49+ * when a conversion error occurred. Eg. with a textfield that only allows an Integer
4950 * (the action class have an Integer field declared), upon conversion error, the incorrectly
5051 * entered integer (maybe a text 'one') will not appear when dispatched back. With 'repopulateField'
51- * porperty set to true, it will, meaning the textfield will have 'one' as its value
52+ * porperty set to true, it will, meaning the textfield will have 'one' as its value
5253 * upon conversion error.
53- *
54+ *
5455 * <!-- END SNIPPET: javadoc -->
55- *
56+ *
5657 * <p/>
57- *
58+ *
5859 * <pre>
5960 * <!-- START SNIPPET: exampleJspPage -->
60- *
61+ *
6162 * <!-- myJspPage.jsp -->
6263 * <ww:form action="someAction" method="POST">
6364 * ....
64- * <ww:textfield
65+ * <ww:textfield
6566 * label="My Integer Field"
6667 * name="myIntegerField" />
6768 * ....
68- * <ww:submit />
69+ * <ww:submit />
6970 * </ww:form>
70- *
71+ *
7172 * <!-- END SNIPPET: exampleJspPage -->
7273 * </pre>
73- *
74+ *
7475 * <pre>
7576 * <!-- START SNIPPET: exampleXwork -->
76- *
77+ *
7778 * <!-- xwork.xml -->
7879 * <xwork>
7980 * <include file="xwork-default.xml" />
8889 * </package>
8990 * ....
9091 * </xwork>
91- *
92+ *
9293 * <!-- END SNIPPET:exampleXwork -->
9394 * </pre>
94- *
95- *
95+ *
96+ *
9697 * <pre>
9798 * <!-- START SNIPPET: exampleJava -->
98- *
99+ *
99100 * <!-- MyActionSupport.java -->
100101 * public class MyActionSupport extends ActionSupport {
101102 * private Integer myIntegerField;
102- *
103+ *
103104 * public Integer getMyIntegerField() { return this.myIntegerField; }
104- * public void setMyIntegerField(Integer myIntegerField) {
105- * this.myIntegerField = myIntegerField;
105+ * public void setMyIntegerField(Integer myIntegerField) {
106+ * this.myIntegerField = myIntegerField;
106107 * }
107108 * }
108- *
109+ *
109110 * <!-- END SNIPPET: exampleJava -->
110111 * </pre>
111- *
112- *
112+ *
113+ *
113114 * <pre>
114115 * <!-- START SNIPPET: exampleValidation -->
115- *
116+ *
116117 * <!-- MyActionSupport-someAction-validation.xml -->
117118 * <validators>
118119 * ...
124125 * </field>
125126 * ...
126127 * </validators>
127- *
128+ *
128129 * <!-- END SNIPPET: exampleValidation -->
129130 * </pre>
130- *
131+ *
131132 * @author tm_jee
132133 * @version $Date$ $Id$
133134 */
134135public abstract class RepopulateConversionErrorFieldValidatorSupport extends FieldValidatorSupport {
135-
136- private static final Logger LOG = LoggerFactory .getLogger (RepopulateConversionErrorFieldValidatorSupport .class );
137-
138- private String repopulateFieldAsString = "false" ;
139- private boolean repopulateFieldAsBoolean = false ;
140-
141- public String getRepopulateField () {
142- return repopulateFieldAsString ;
143- }
144-
145- public void setRepopulateField (String repopulateField ) {
146- this .repopulateFieldAsString = repopulateField == null ? repopulateField : repopulateField .trim ();
147- this .repopulateFieldAsBoolean = "true" .equalsIgnoreCase (this .repopulateFieldAsString ) ? (true ) : (false );
148- }
149-
150- public void validate (Object object ) throws ValidationException {
151- doValidate (object );
152- if (repopulateFieldAsBoolean ) {
153- repopulateField (object );
154- }
155- }
156-
157- public void repopulateField (Object object ) throws ValidationException {
158-
159- ActionInvocation invocation = ActionContext .getContext ().getActionInvocation ();
160- Map <String , Object > conversionErrors = ActionContext .getContext ().getConversionErrors ();
161-
162- String fieldName = getFieldName ();
163- String fullFieldName = getValidatorContext ().getFullFieldName (fieldName );
136+
137+ private static final Logger LOG = LoggerFactory .getLogger (RepopulateConversionErrorFieldValidatorSupport .class );
138+
139+ private String repopulateFieldAsString = "false" ;
140+ private boolean repopulateFieldAsBoolean = false ;
141+
142+ public String getRepopulateField () {
143+ return repopulateFieldAsString ;
144+ }
145+
146+ public void setRepopulateField (String repopulateField ) {
147+ this .repopulateFieldAsString = repopulateField == null ? repopulateField : repopulateField .trim ();
148+ this .repopulateFieldAsBoolean = "true" .equalsIgnoreCase (this .repopulateFieldAsString ) ? (true ) : (false );
149+ }
150+
151+ public void validate (Object object ) throws ValidationException {
152+ doValidate (object );
153+ if (repopulateFieldAsBoolean ) {
154+ repopulateField (object );
155+ }
156+ }
157+
158+ public void repopulateField (Object object ) throws ValidationException {
159+
160+ ActionInvocation invocation = ActionContext .getContext ().getActionInvocation ();
161+ Map <String , Object > conversionErrors = ActionContext .getContext ().getConversionErrors ();
162+
163+ String fieldName = getFieldName ();
164+ String fullFieldName = getValidatorContext ().getFullFieldName (fieldName );
164165 if (conversionErrors .containsKey (fullFieldName )) {
165166 Object value = conversionErrors .get (fullFieldName );
166167
@@ -170,18 +171,18 @@ public void repopulateField(Object object) throws ValidationException {
170171 if (value instanceof String []) {
171172 // take the first element, if possible
172173 String [] tmpValue = (String []) value ;
173- if (tmpValue != null && (tmpValue .length > 0 )) {
174+ if ((tmpValue .length > 0 )) {
174175 doExprOverride = true ;
175- fakeParams .put (fullFieldName , "'" + tmpValue [0 ] + "'" );
176+ fakeParams .put (fullFieldName , escape ( tmpValue [0 ]) );
176177 } else {
177178 if (LOG .isWarnEnabled ()) {
178- LOG .warn ("value is an empty array of String or with first element in it as null [" + value + "], will not repopulate conversion error " );
179+ LOG .warn ("value is an empty array of String or with first element in it as null [" + value + "], will not repopulate conversion error " );
179180 }
180181 }
181182 } else if (value instanceof String ) {
182183 String tmpValue = (String ) value ;
183184 doExprOverride = true ;
184- fakeParams .put (fullFieldName , "'" + tmpValue + "'" );
185+ fakeParams .put (fullFieldName , escape ( tmpValue ) );
185186 } else {
186187 // opps... it should be
187188 if (LOG .isWarnEnabled ()) {
@@ -198,7 +199,11 @@ public void beforeResult(ActionInvocation invocation, String resultCode) {
198199 });
199200 }
200201 }
201- }
202-
203- protected abstract void doValidate (Object object ) throws ValidationException ;
202+ }
203+
204+ protected String escape (String value ) {
205+ return "\" " + StringEscapeUtils .escapeJava (value ) + "\" " ;
206+ }
207+
208+ protected abstract void doValidate (Object object ) throws ValidationException ;
204209}
0 commit comments