EXTVAL-144 allow to override constraints with meta-data provided by the component (deactivated by default) - also thx to Thomas Andraschko

git-svn-id: https://svn.apache.org/repos/asf/myfaces/extensions/validator/trunk@1364111 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/core/src/main/java/org/apache/myfaces/extensions/validator/core/DefaultExtValCoreConfiguration.java b/core/src/main/java/org/apache/myfaces/extensions/validator/core/DefaultExtValCoreConfiguration.java
index 4713246..8d860db 100644
--- a/core/src/main/java/org/apache/myfaces/extensions/validator/core/DefaultExtValCoreConfiguration.java
+++ b/core/src/main/java/org/apache/myfaces/extensions/validator/core/DefaultExtValCoreConfiguration.java
@@ -495,4 +495,14 @@
     {

         return !"false".equalsIgnoreCase(WebXmlParameter.VALIDATE_EMPTY_FIELDS);

     }

+

+    /**

+     * {@inheritDoc}

+     * Value taken from the Web.xml initialization parameter ACTIVATE_MARKUP_META_DATA.

+     */

+    @Override

+    public boolean activateMarkupMetaData()

+    {

+        return "true".equalsIgnoreCase(WebXmlParameter.ACTIVATE_MARKUP_META_DATA);

+    }

 }

diff --git a/core/src/main/java/org/apache/myfaces/extensions/validator/core/ExtValCoreConfiguration.java b/core/src/main/java/org/apache/myfaces/extensions/validator/core/ExtValCoreConfiguration.java
index cbbc0cd..44ced97 100644
--- a/core/src/main/java/org/apache/myfaces/extensions/validator/core/ExtValCoreConfiguration.java
+++ b/core/src/main/java/org/apache/myfaces/extensions/validator/core/ExtValCoreConfiguration.java
@@ -628,4 +628,16 @@
      * @return false for using the default behavior of JSF 2.0

      */

     public abstract boolean validateEmptyFields();

+

+    /**

+     * @since r6

+     */

+

+    /**

+     * Per default component initialization overrules properties of the component.

+     * With activating markup meta-data it's possible to overrule the meta-data of the constraints with the meta-data

+     * of the component.

+     * @return true to overrule constraint meta-data with meta-data provided by the component, false otherwise

+     */

+    public abstract boolean activateMarkupMetaData();

 }

diff --git a/core/src/main/java/org/apache/myfaces/extensions/validator/core/WebXmlParameter.java b/core/src/main/java/org/apache/myfaces/extensions/validator/core/WebXmlParameter.java
index 20d24af..118a804 100644
--- a/core/src/main/java/org/apache/myfaces/extensions/validator/core/WebXmlParameter.java
+++ b/core/src/main/java/org/apache/myfaces/extensions/validator/core/WebXmlParameter.java
@@ -118,10 +118,20 @@
     static final String ACTIVATE_REQUIRED_INITIALIZATION = WebXmlUtils

         .getInitParameter("ACTIVATE_REQUIRED_INITIALIZATION");

 

+    /**

+     * Per default component initialization overrules properties of the component.

+     * With activating markup meta-data it's possible to overrule the meta-data of the constraints with the meta-data

+     * of the component.

+     * 

+     * @since r6

+     */

+    static final String ACTIVATE_MARKUP_META_DATA = WebXmlUtils

+        .getInitParameter("ACTIVATE_MARKUP_META_DATA");

+

     /*

      * deactivate

      */

-    /**

+    /*

      * @since r4

      */

     static final String DEACTIVATE_REQUIRED_ATTRIBUTE_SUPPORT = WebXmlUtils

diff --git a/core/src/main/java/org/apache/myfaces/extensions/validator/core/initializer/component/AbstractHtmlCoreComponentsComponentInitializer.java b/core/src/main/java/org/apache/myfaces/extensions/validator/core/initializer/component/AbstractHtmlCoreComponentsComponentInitializer.java
index 3816d29..609c945 100644
--- a/core/src/main/java/org/apache/myfaces/extensions/validator/core/initializer/component/AbstractHtmlCoreComponentsComponentInitializer.java
+++ b/core/src/main/java/org/apache/myfaces/extensions/validator/core/initializer/component/AbstractHtmlCoreComponentsComponentInitializer.java
@@ -18,6 +18,8 @@
  */

 package org.apache.myfaces.extensions.validator.core.initializer.component;

 

+import org.apache.myfaces.extensions.validator.core.ExtValContext;

+import org.apache.myfaces.extensions.validator.core.ExtValCoreConfiguration;

 import org.apache.myfaces.extensions.validator.internal.UsageInformation;

 import org.apache.myfaces.extensions.validator.internal.UsageCategory;

 import org.apache.myfaces.extensions.validator.internal.ToDo;

@@ -50,6 +52,11 @@
 @UsageInformation(UsageCategory.REUSE)

 public abstract class AbstractHtmlCoreComponentsComponentInitializer implements ComponentInitializer

 {

+    //short because it influences the state

+    protected static final String INITIAL_MARKUP_META_DATA_KEY = "OAM_EV_MARKUP_METADATA";

+

+    protected Boolean forceComponentInitialization;

+

     /**

      * If the component is one of the standard input components, the max length attribute is configured and the

      * required attribute is configured (if empty field validation and required initialization is activated)

@@ -165,16 +172,70 @@
             {

                 return;

             }

+

+            init(); //lazy init

+

             if(uiComponent instanceof HtmlInputText)

             {

                 HtmlInputText htmlInputText = (HtmlInputText)uiComponent;

-                htmlInputText.setMaxlength((Integer)maxLength);

+

+                if (this.forceComponentInitialization)

+                {

+                    htmlInputText.setMaxlength((Integer) maxLength);

+                }

+                else

+                {

+                    Integer initialMaxLength = (Integer)

+                        htmlInputText.getAttributes().get(INITIAL_MARKUP_META_DATA_KEY);

+

+                    if (initialMaxLength == null)

+                    {

+                        initialMaxLength = htmlInputText.getMaxlength(); //value overriden by the component

+                        htmlInputText.getAttributes().put(INITIAL_MARKUP_META_DATA_KEY, initialMaxLength);

+                    }

+

+                    // only override maxlength if not already set by xhtml definition

+                    if (initialMaxLength <= 0)

+                    {

+                        htmlInputText.setMaxlength((Integer) maxLength);

+                    }

+                }

             }

             else if(uiComponent instanceof HtmlInputSecret)

             {

                 HtmlInputSecret htmlInputSecret = (HtmlInputSecret)uiComponent;

-                htmlInputSecret.setMaxlength((Integer)maxLength);

+

+                if (this.forceComponentInitialization)

+                {

+                    htmlInputSecret.setMaxlength((Integer)maxLength);

+                }

+                else

+                {

+                    Integer initialMaxLength = (Integer)

+                        htmlInputSecret.getAttributes().get(INITIAL_MARKUP_META_DATA_KEY);

+

+                    if (initialMaxLength == null)

+                    {

+                        initialMaxLength = htmlInputSecret.getMaxlength(); //value overriden by the component

+                        htmlInputSecret.getAttributes().put(INITIAL_MARKUP_META_DATA_KEY, initialMaxLength);

+                    }

+

+                    // only override maxlength if not already set by xhtml definition

+                    if (initialMaxLength <= 0)

+                    {

+                        htmlInputSecret.setMaxlength((Integer) maxLength);

+                    }

+                }

             }

         }

     }

+

+    protected void init()

+    {

+        if (this.forceComponentInitialization == null)

+        {

+            this.forceComponentInitialization = !ExtValContext.getContext()

+                .getModuleConfiguration(ExtValCoreConfiguration.class).activateMarkupMetaData();

+        }

+    }

 }