Author: schultz
Date: Thu Sep  6 20:25:16 2012
New Revision: 1381744

URL: http://svn.apache.org/viewvc?rev=1381744&view=rev
Log:
Fixed VELTOOLS-152: ValidatorTool generates invalid XHTML even when in XHTML 
mode
- Added new XMLMode setting to use & instead of & in generated Javascript
- Adjusted logic added in r1297754 to use XMLMode instead of XHTML setting

Modified:
    
velocity/tools/branches/2.0.x/src/main/java/org/apache/velocity/tools/struts/ValidatorTool.java

Modified: 
velocity/tools/branches/2.0.x/src/main/java/org/apache/velocity/tools/struts/ValidatorTool.java
URL: 
http://svn.apache.org/viewvc/velocity/tools/branches/2.0.x/src/main/java/org/apache/velocity/tools/struts/ValidatorTool.java?rev=1381744&r1=1381743&r2=1381744&view=diff
==============================================================================
--- 
velocity/tools/branches/2.0.x/src/main/java/org/apache/velocity/tools/struts/ValidatorTool.java
 (original)
+++ 
velocity/tools/branches/2.0.x/src/main/java/org/apache/velocity/tools/struts/ValidatorTool.java
 Thu Sep  6 20:25:16 2012
@@ -103,6 +103,11 @@ public class ValidatorTool
 
     private boolean xhtml = false;
 
+    /**
+     * Whether this ValidatorTool should generate strict XHTML in XML mode.
+     */
+    private boolean xmlMode = false;
+
     private boolean htmlComment = true;
     private boolean cdata = true;
     private String formName = null;
@@ -174,9 +179,11 @@ public class ValidatorTool
 
         Boolean b = (Boolean)params.get("XHTML");
         if (b != null)
-        {
             this.xhtml = b.booleanValue();
-        }
+
+        b = (Boolean)params.get("XMLMode");
+        if (b != null)
+            this.xmlMode = b.booleanValue();
 
         /* Is there a mapping associated with this request? */
         ActionConfig config =
@@ -314,6 +321,51 @@ public class ValidatorTool
         this.cdata = cdata;
     }
 
+    /**
+     * Returns the XHTML setting.
+     *
+     * @return boolean - "true" if JavaScript will use & operators
+     * and suppress HTML comment wrappers.
+     */
+    public boolean getXhtml()
+    {
+        return this.xhtml;
+    }
+
+    /**
+     * Sets the XML mode status.
+     * @param xhtml The XHTML mode to set
+     */
+    public void setXhtml(boolean xhtml)
+    {
+        this.xhtml = xhtml;
+    }
+
+    /**
+     * Returns the XMLMode setting.
+     *
+     * @return boolean - "true" if JavaScript will use & operators
+     * and suppress HTML comment wrappers.
+     */
+    public boolean getXMLMode()
+    {
+        return this.xmlMode;
+    }
+
+    /**
+     * Sets the XML mode status. If <code>true</code>, also sets
+     * XHTML to <code>true</code>. Setting this to <code>true</code>
+     * will force the use of &amp;amp; for logical AND operations
+     * and will alwasy suppress the use of HTML comments around
+     * Javascript code.
+     * @param xmlMode The XML mode to set
+     */
+    public void setXMLMode(boolean xmlMode)
+    {
+        if(xmlMode)
+            setXhtml(true);
+        this.xmlMode = xmlMode;
+    }
 
     /****************** methods that aren't just accessors ***************/
 
@@ -385,7 +437,7 @@ public class ValidatorTool
     /**
      * Render just the dynamic JavaScript to perform validations based
      * on the supplied form name. Useful i.e. if the static
-     * parts are located in a seperate .js file.
+     * parts are located in a separate .js file.
      *
      * @param formName the key (form name)
      * @return the dynamic Javascript for the specified form
@@ -518,8 +570,8 @@ public class ValidatorTool
 
                 message = escapeJavascript(message);
 
-                // Escape required XML entities if we are in XHTML mode 
without CDATA
-                if(this.xhtml && !this.cdata)
+                // Escape required XML entities if we are in XML mode without 
CDATA
+                if(this.xmlMode && !this.cdata)
                     message = StringEscapeUtils.escapeXml(message);
 
                 results.append(message);
@@ -548,8 +600,8 @@ public class ValidatorTool
 
                     String escapedVarValue = escapeJavascript(varValue);
 
-                    // Escape required XML entities if we are in XHTML mode 
without CDATA
-                    if(this.xhtml && !this.cdata)
+                    // Escape required XML entities if we are in XML mode 
without CDATA
+                    if(this.xmlMode && !this.cdata)
                         escapedVarValue = 
StringEscapeUtils.escapeXml(escapedVarValue);
 
                     if (Var.JSTYPE_INT.equalsIgnoreCase(jsType))
@@ -646,8 +698,9 @@ public class ValidatorTool
     protected String createMethods(List<ValidatorAction> actions)
     {
         String methodOperator;
-        // Escape required XML entities if we are in XHTML mode without CDATA
-        if(this.xhtml && !this.cdata)
+        // Escape required XML entities if we are in XML XHTML mode
+        // without a CDATA wrapper
+        if(this.xmlMode && !this.cdata)
             methodOperator = " &amp;&amp; ";
         else
             methodOperator = " && ";
@@ -749,11 +802,13 @@ public class ValidatorTool
             sb.append("<![CDATA[\r\n");
         }
 
-        if (!this.xhtml && this.htmlComment)
+        // Do not use HTML comments when in XML mode: the browser will
+        // ignore the contents
+        if (!this.xmlMode && !this.xhtml && this.htmlComment)
         {
             sb.append(HTML_BEGIN_COMMENT);
         }
-        sb.append("\n     var bCancel = false; \n\n");
+        sb.append("\n    var bCancel = false;\n\n");
 
         if (methodName == null || methodName.length() == 0)
         {
@@ -766,23 +821,23 @@ public class ValidatorTool
             sb.append(methodName);
         }
         sb.append("(form) {\n");
-        sb.append("      if (bCancel) \n");
-        sb.append("          return true; \n");
-        sb.append("      else \n");
+        sb.append("      if (bCancel)\n");
+        sb.append("          return true;\n");
+        sb.append("      else\n");
 
         // Always return true if there aren't any Javascript validation methods
         if (methods == null || methods.length() == 0)
         {
-            sb.append("       return true; \n");
+            sb.append("       return true;\n");
         }
         else
         {
-            //Making Sure that Bitwise operator works:
-            sb.append(" var formValidationResult;\n");
-            sb.append("       formValidationResult = " + methods + "; \n");
-            sb.append("     return (formValidationResult == 1);\n");
+            // Making Sure that Bitwise operator works:
+            sb.append("      {\n        var formValidationResult;\n        
formValidationResult = ");
+            sb.append(methods);
+            sb.append(";\n        return (formValidationResult == 1);\n      
}\n");
         }
-        sb.append("   } \n\n");
+        sb.append("    }\n");
 
         return sb.toString();
     }
@@ -824,7 +879,7 @@ public class ValidatorTool
         StringBuilder sb = new StringBuilder();
         sb.append("\n");
 
-        if (!this.xhtml && this.htmlComment)
+        if (!this.xmlMode && !this.xhtml && this.htmlComment)
         {
             sb.append(HTML_END_COMMENT);
         }
@@ -859,8 +914,7 @@ public class ValidatorTool
             start.append(" src=\"" + src + "\"");
         }
 
-        start.append("> \n");
+        start.append(">\n");
         return start.toString();
     }
-
 }


Reply via email to