Author: marino
Date: Tue Feb 22 18:04:46 2005
New Revision: 154929
URL: http://svn.apache.org/viewcvs?view=rev&rev=154929
Log:
ValidatorTool upgraded for Struts 1.2
Modified:
jakarta/velocity-tools/trunk/src/java/org/apache/velocity/tools/struts/FormTool.java
jakarta/velocity-tools/trunk/src/java/org/apache/velocity/tools/struts/SecureLinkTool.java
jakarta/velocity-tools/trunk/src/java/org/apache/velocity/tools/struts/StrutsUtils.java
jakarta/velocity-tools/trunk/src/java/org/apache/velocity/tools/struts/ValidatorTool.java
Modified:
jakarta/velocity-tools/trunk/src/java/org/apache/velocity/tools/struts/FormTool.java
URL:
http://svn.apache.org/viewcvs/jakarta/velocity-tools/trunk/src/java/org/apache/velocity/tools/struts/FormTool.java?view=diff&r1=154928&r2=154929
==============================================================================
---
jakarta/velocity-tools/trunk/src/java/org/apache/velocity/tools/struts/FormTool.java
(original)
+++
jakarta/velocity-tools/trunk/src/java/org/apache/velocity/tools/struts/FormTool.java
Tue Feb 22 18:04:46 2005
@@ -26,10 +26,11 @@
import org.apache.velocity.tools.view.context.ViewContext;
import org.apache.velocity.tools.view.tools.ViewTool;
+import org.apache.struts.util.ModuleUtils;
/**
- * <p>View tool to work with HTML forms in Struts.</p>
+ * <p>View tool to work with HTML forms in Struts.</p>
* <p><pre>
* Template example(s):
* <input type="hidden" name="$form.tokenName" value="$form.token">
@@ -48,27 +49,27 @@
*
* @author <a href="mailto:[EMAIL PROTECTED]">Gabe Sidler</a>
* @since VelocityTools 1.0
- * @version $Id: FormTool.java,v 1.6 2004/02/18 20:09:51 nbubna Exp $
+ * @version $Id$
*/
public class FormTool implements ViewTool
{
// --------------------------------------------- Properties ---------------
-
+
/**
* A reference to the HtttpServletRequest.
- */
+ */
protected HttpServletRequest request;
-
+
/**
* A reference to the HtttpSession.
- */
+ */
protected HttpSession session;
-
+
// --------------------------------------------- Constructors -------------
/**
@@ -77,8 +78,8 @@
public FormTool()
{
}
-
-
+
+
/**
* Initializes this tool.
*
@@ -104,44 +105,56 @@
/**
* <p>Returns the form bean associated with this action mapping.</p>
*
- * <p>This is a convenience method. The form bean is automatically
- * available in the Velocity context under the name defined in the
- * Struts configuration.</p>
- *
- * <p>If the form bean is used repeatedly, it is recommended to create a
+ * <p>This is a convenience method. The form bean is automatically
+ * available in the Velocity context under the name defined in the
+ * Struts configuration.</p>
+ *
+ * <p>If the form bean is used repeatedly, it is recommended to create a
* local variable referencing the bean rather than calling getBean()
* multiple times.</p>
- *
- * <pre>
+ *
+ * <pre>
* Example:
- * #set ($defaults = $form.bean)
+ * #set ($defaults = $form.bean)
* <input type="text" name="username" value="$defaults.username">
* </pre>
*
- * @return the [EMAIL PROTECTED] ActionForm} associated with this request
or
+ * @return the [EMAIL PROTECTED] ActionForm} associated with this request
or
* <code>null</code> if there is no form bean associated with this mapping
*/
public ActionForm getBean()
{
- return StrutsUtils.getActionForm(request, session);
+ return StrutsUtils.getActionForm(request, session);
}
-
/**
- * <p>Returns the query parameter name under which a cancel button press
- * must be reported if form validation is to be skipped.</p>
+ * <p>Returns the form bean name associated with this action mapping.</p>
*
- * <p>This is the value of
+ * @return the name of the ActionForm associated with this request or
+ * <code>null</code> if there is no form bean associated with this mapping
+ */
+ public String getName()
+ {
+ return StrutsUtils.getActionFormName(request, session);
+ }
+
+
+
+ /**
+ * <p>Returns the query parameter name under which a cancel button press
+ * must be reported if form validation is to be skipped.</p>
+ *
+ * <p>This is the value of
* <code>org.apache.struts.taglib.html.Constants.CANCEL_PROPERTY</code></p>
*/
public String getCancelName()
{
return org.apache.struts.taglib.html.Constants.CANCEL_PROPERTY;
}
-
+
/**
- * Returns the transaction control token for this session or
+ * Returns the transaction control token for this session or
* <code>null</code> if no token exists.
*/
public String getToken()
@@ -152,7 +165,7 @@
/**
* <p>Returns the query parameter name under which a transaction token
- * must be reported. This is the value of
+ * must be reported. This is the value of
* <code>org.apache.struts.taglib.html.Constants.TOKEN_KEY</code></p>
*/
public String getTokenName()
Modified:
jakarta/velocity-tools/trunk/src/java/org/apache/velocity/tools/struts/SecureLinkTool.java
URL:
http://svn.apache.org/viewcvs/jakarta/velocity-tools/trunk/src/java/org/apache/velocity/tools/struts/SecureLinkTool.java?view=diff&r1=154928&r2=154929
==============================================================================
---
jakarta/velocity-tools/trunk/src/java/org/apache/velocity/tools/struts/SecureLinkTool.java
(original)
+++
jakarta/velocity-tools/trunk/src/java/org/apache/velocity/tools/struts/SecureLinkTool.java
Tue Feb 22 18:04:46 2005
@@ -53,7 +53,7 @@
* </p>
* @since VelocityTools 1.1
* @author <a href="mailto:[EMAIL PROTECTED]">Marino A. Jonsson</a>
- * @version $Revision: 1.10 $ $Date: 2004/03/12 20:30:31 $
+ * @version $Revision$ $Date$
*/
public class SecureLinkTool extends LinkTool
{
@@ -183,7 +183,7 @@
{
ModuleConfig moduleConfig = StrutsUtils.selectModule(linkString, app);
- // Strip off the subapp path, if any
+ // Strip off the module path, if any
linkString = linkString.substring(moduleConfig.getPrefix().length());
// Use our servlet mapping, if one is specified
Modified:
jakarta/velocity-tools/trunk/src/java/org/apache/velocity/tools/struts/StrutsUtils.java
URL:
http://svn.apache.org/viewcvs/jakarta/velocity-tools/trunk/src/java/org/apache/velocity/tools/struts/StrutsUtils.java?view=diff&r1=154928&r2=154929
==============================================================================
---
jakarta/velocity-tools/trunk/src/java/org/apache/velocity/tools/struts/StrutsUtils.java
(original)
+++
jakarta/velocity-tools/trunk/src/java/org/apache/velocity/tools/struts/StrutsUtils.java
Tue Feb 22 18:04:46 2005
@@ -54,7 +54,7 @@
* @author <a href="mailto:[EMAIL PROTECTED]">Gabe Sidler</a>
* based on code by <a href="mailto:[EMAIL PROTECTED]">Ted Husted</a>
*
- * @version $Id: StrutsUtils.java,v 1.24 2004/11/11 00:52:15 nbubna Exp $
+ * @version $Id$
*/
public class StrutsUtils
{
@@ -241,8 +241,58 @@
return null;
}
+ /**
+ * Returns the ActionForm name associated with
+ * this request of <code>null</code> if none exists.
+ *
+ * @param request the servlet request
+ * @param session the HTTP session
+ */
+ public static String getActionFormName(HttpServletRequest request,
+ HttpSession session)
+ {
+ /* Is there a mapping associated with this request? */
+ ActionConfig mapping =
+ (ActionConfig)request.getAttribute(Globals.MAPPING_KEY);
+ if (mapping == null)
+ {
+ return null;
+ }
+
+ return mapping.getAttribute();
+ }
+
+
/*************************** Utilities *************************/
+
+ /**
+ * Return the form action converted into an action mapping path. The
+ * value of the <code>action</code> property is manipulated as follows in
+ * computing the name of the requested mapping:
+ * <ul>
+ * <li>Any filename extension is removed (on the theory that extension
+ * mapping is being used to select the controller servlet).</li>
+ * <li>If the resulting value does not start with a slash, then a
+ * slash is prepended.</li>
+ * </ul>
+ */
+ public static String getActionMappingName(String action) {
+
+ String value = action;
+ int question = action.indexOf("?");
+ if (question >= 0) {
+ value = value.substring(0, question);
+ }
+
+ int slash = value.lastIndexOf("/");
+ int period = value.lastIndexOf(".");
+ if ((period >= 0) && (period > slash)) {
+ value = value.substring(0, period);
+ }
+
+ return value.startsWith("/") ? value : ("/" + value);
+ }
/**
* Returns the form action converted into a server-relative URI
Modified:
jakarta/velocity-tools/trunk/src/java/org/apache/velocity/tools/struts/ValidatorTool.java
URL:
http://svn.apache.org/viewcvs/jakarta/velocity-tools/trunk/src/java/org/apache/velocity/tools/struts/ValidatorTool.java?view=diff&r1=154928&r2=154929
==============================================================================
---
jakarta/velocity-tools/trunk/src/java/org/apache/velocity/tools/struts/ValidatorTool.java
(original)
+++
jakarta/velocity-tools/trunk/src/java/org/apache/velocity/tools/struts/ValidatorTool.java
Tue Feb 22 18:04:46 2005
@@ -45,6 +45,7 @@
import org.apache.velocity.tools.view.context.ViewContext;
import org.apache.velocity.tools.view.tools.ViewTool;
+import java.util.StringTokenizer;
/**
* <p>View tool that works with Struts Validator to
@@ -71,7 +72,7 @@
* @author <a href="mailto:[EMAIL PROTECTED]">Marino A. Jonsson</a>
* @author <a href="mailto:[EMAIL PROTECTED]">Nathan Bubna</a>
* @since VelocityTools 1.1
- * @version $Revision: 1.10 $ $Date: 2004/04/14 20:08:28 $
+ * @version $Revision$ $Date$
*/
public class ValidatorTool implements ViewTool {
@@ -102,6 +103,46 @@
private String methodName = null;
private String src = null;
private int page = 0;
+ /**
+ * formName is used for both Javascript and non-javascript validations.
+ * For the javascript validations, there is the possibility that we will
+ * be rewriting the formName (if it is a ValidatorActionForm instead of
just
+ * a ValidatorForm) so we need another variable to hold the formName just
for
+ * javascript usage.
+ */
+ protected String jsFormName = null;
+
+
+
+
+ /**
+ * A Comparator to use when sorting ValidatorAction objects.
+ */
+ private static final Comparator actionComparator = new Comparator() {
+ public int compare(Object o1, Object o2) {
+
+ ValidatorAction va1 = (ValidatorAction) o1;
+ ValidatorAction va2 = (ValidatorAction) o2;
+
+ if ((va1.getDepends() == null || va1.getDepends().length() == 0)
+ && (va2.getDepends() == null || va2.getDepends().length() ==
0)) {
+ return 0;
+
+ } else if (
+ (va1.getDepends() != null && va1.getDepends().length() > 0)
+ && (va2.getDepends() == null || va2.getDepends().length()
== 0)) {
+ return 1;
+
+ } else if (
+ (va1.getDepends() == null || va1.getDepends().length() == 0)
+ && (va2.getDepends() != null && va2.getDepends().length()
> 0)) {
+ return -1;
+
+ } else {
+ return va1.getDependencyList().size() -
va2.getDependencyList().size();
+ }
+ }
+ };
/**
@@ -151,7 +192,6 @@
}
-
/****************** get/set accessors ***************/
/**
@@ -410,13 +450,28 @@
List actions = createActionList(resources, form);
- String methods = createMethods(actions);
+ final String methods = createMethods(actions);
+
+ String formName = form.getName();
+
+ jsFormName = formName;
+ if(jsFormName.charAt(0) == '/') {
+ String mappingName = StrutsUtils.getActionMappingName(jsFormName);
+ ModuleConfig mconfig =
ModuleUtils.getInstance().getModuleConfig(request, app);
+
+ ActionConfig mapping = (ActionConfig)
mconfig.findActionConfig(mappingName);
+ if (mapping == null) {
+ throw new NullPointerException("Cannot retrieve mapping for
action " + mappingName);
+ }
+ jsFormName = mapping.getAttribute();
+ }
+
results.append(getJavascriptBegin(methods));
for (Iterator i = actions.iterator(); i.hasNext();)
{
ValidatorAction va = (ValidatorAction)i.next();
- String jscriptVar = null;
+ int jscriptVar = 0;
String functionName = null;
if (va.getJsFunctionName() != null &&
va.getJsFunctionName().length() > 0)
@@ -429,6 +484,8 @@
}
results.append(" function ");
+ results.append(jsFormName);
+ results.append("_");
results.append(functionName);
results.append(" () { \n");
@@ -449,19 +506,16 @@
String message =
Resources.getMessage(messages, locale, va, field);
- if (message == null)
- {
- message = "";
- }
+ message = (message != null) ? message : "";
- jscriptVar = this.getNextVar(jscriptVar);
+ //jscriptVar = this.getNextVar(jscriptVar);
- results.append(" this.");
- results.append(jscriptVar);
+ results.append(" this.a");
+ results.append(jscriptVar++);
results.append(" = new Array(\"");
results.append(field.getKey());
results.append("\", \"");
- results.append(message);
+ results.append(escapeQuotes(message));
results.append("\", ");
results.append("new Function (\"varName\", \"");
@@ -529,6 +583,29 @@
}
+ private String escapeQuotes(String in)
+ {
+ if (in == null || in.indexOf("\"") == -1)
+ {
+ return in;
+ }
+ StringBuffer buffer = new StringBuffer();
+ StringTokenizer tokenizer = new StringTokenizer(in, "\"", true);
+
+ while (tokenizer.hasMoreTokens())
+ {
+ String token = tokenizer.nextToken();
+ if (token.equals("\""))
+ {
+ buffer.append("\\");
+ }
+ buffer.append(token);
+ }
+
+ return buffer.toString();
+ }
+
+
/**
* Creates the JavaScript methods list from the given actions.
* @param actions A List of ValidatorAction objects.
@@ -608,9 +685,7 @@
}
}
- //TODO? make an instance of this class a static member
- Comparator comp = new ValidatorActionComparator();
- Collections.sort(actions, comp);
+ Collections.sort(actions, actionComparator);
return actions;
}
@@ -624,11 +699,11 @@
protected String getJavascriptBegin(String methods)
{
StringBuffer sb = new StringBuffer();
- String name = formName.replace('/', '_'); // remove any '/' characters
- name = name.substring(0, 1).toUpperCase() +
- name.substring(1, name.length());
+ String name = jsFormName.replace('/', '_'); // remove any '/'
characters
+ name = jsFormName.substring(0, 1).toUpperCase() +
+ jsFormName.substring(1, jsFormName.length());
- sb.append(getStartElement());
+ sb.append(this.getStartElement());
if (this.xhtml && this.cdata)
{
@@ -665,9 +740,11 @@
}
else
{
- sb.append(" return ");
- sb.append(methods);
- sb.append("; \n");
+ //Making Sure that Bitwise operator works:
+ sb.append(" var formValidationResult;\n");
+ sb.append(" formValidationResult = " + methods + "; \n");
+ sb.append(" return (formValidationResult == 1);\n");
+
}
sb.append(" } \n\n");
@@ -726,54 +803,6 @@
/**
- * The value <code>null</code> will be returned at the end of the sequence.
- * ex: "zz" will return <code>null</code>
- *
- * @param input the string to process
- * @return the next var
- */
- private String getNextVar(String input)
- {
- if (input == null)
- {
- return "aa";
- }
-
- input = input.toLowerCase();
-
- for (int i = input.length(); i > 0; i--)
- {
- int pos = i - 1;
-
- char c = input.charAt(pos);
- c++;
-
- if (c <= 'z')
- {
- if (i == 0)
- {
- return c + input.substring(pos, input.length());
- }
- else if (i == input.length())
- {
- return input.substring(0, pos) + c;
- }
- else
- {
- return input.substring(0, pos) + c +
- input.substring(pos, input.length() - 1);
- }
- }
- else
- {
- input = replaceChar(input, pos, 'a');
- }
- }
- return null;
- }
-
-
- /**
* Replaces a single character in a <code>String</code>
*
* @param input the string to process
@@ -821,48 +850,6 @@
start.append("> \n");
return start.toString();
- }
-
-
- /**
- * Inner class for use when creating dynamic javascript
- */
- protected class ValidatorActionComparator implements Comparator
- {
- /**
- *
- * @param o1 the first object to compare with regard to depends
- * @param o2 the second object to compare with regard to depends
- * @return -1, 0 or 1
- */
- public int compare(Object o1, Object o2)
- {
- ValidatorAction va1 = (ValidatorAction)o1;
- ValidatorAction va2 = (ValidatorAction)o2;
-
- String vad1 = va1.getDepends();
- String vad2 = va2.getDepends();
-
- if ((vad1 == null || vad1.length() == 0)
- && (vad2 == null || vad2.length() == 0))
- {
- return 0;
- }
- else if ((vad1 != null && vad1.length() > 0)
- && (vad2 == null || vad2.length() == 0))
- {
- return 1;
- }
- else if ((vad1 == null || vad1.length() == 0)
- && (vad2 != null && vad2.length() > 0))
- {
- return -1;
- }
- else
- {
- return va1.getDependencyList().size() -
va2.getDependencyList().size();
- }
- }
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]