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+
136137 private static final Logger LOG = LoggerFactory .getLogger (RepopulateConversionErrorFieldValidatorSupport .class );
137-
138+
138139 private String repopulateFieldAsString = "false" ;
139140 private boolean repopulateFieldAsBoolean = false ;
140-
141- public String getRepopulateField () {
141+
142+ public String getRepopulateField () {
142143 return repopulateFieldAsString ;
143144 }
144-
145+
145146 public void setRepopulateField (String repopulateField ) {
146147 this .repopulateFieldAsString = repopulateField == null ? repopulateField : repopulateField .trim ();
147148 this .repopulateFieldAsBoolean = "true" .equalsIgnoreCase (this .repopulateFieldAsString ) ? (true ) : (false );
@@ -153,12 +154,12 @@ public void validate(Object object) throws ValidationException {
153154 repopulateField (object );
154155 }
155156 }
156-
157+
157158 public void repopulateField (Object object ) throws ValidationException {
158-
159+
159160 ActionInvocation invocation = ActionContext .getContext ().getActionInvocation ();
160161 Map <String , Object > conversionErrors = ActionContext .getContext ().getConversionErrors ();
161-
162+
162163 String fieldName = getFieldName ();
163164 String fullFieldName = getValidatorContext ().getFullFieldName (fieldName );
164165 if (conversionErrors .containsKey (fullFieldName )) {
@@ -170,19 +171,23 @@ 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 {
177- LOG .warn ("value is an empty array of String or with first element in it as null [" + value + "], will not repopulate conversion error " );
178+ if (LOG .isWarnEnabled ()) {
179+ LOG .warn ("value is an empty array of String or with first element in it as null [" + value + "], will not repopulate conversion error " );
180+ }
178181 }
179182 } else if (value instanceof String ) {
180183 String tmpValue = (String ) value ;
181184 doExprOverride = true ;
182- fakeParams .put (fullFieldName , "'" + tmpValue + "'" );
185+ fakeParams .put (fullFieldName , escape ( tmpValue ) );
183186 } else {
184187 // opps... it should be
185- LOG .warn ("conversion error value is not a String or array of String but instead is [" + value + "], will not repopulate conversion error" );
188+ if (LOG .isWarnEnabled ()) {
189+ LOG .warn ("conversion error value is not a String or array of String but instead is [" + value + "], will not repopulate conversion error" );
190+ }
186191 }
187192
188193 if (doExprOverride ) {
@@ -194,7 +199,11 @@ public void beforeResult(ActionInvocation invocation, String resultCode) {
194199 });
195200 }
196201 }
197- }
198-
199- protected abstract void doValidate (Object object ) throws ValidationException ;
200- }
202+ }
203+
204+ protected String escape (String value ) {
205+ return "\" " + StringEscapeUtils .escapeJava (value ) + "\" " ;
206+ }
207+
208+ protected abstract void doValidate (Object object ) throws ValidationException ;
209+ }
0 commit comments