You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by bo...@apache.org on 2006/10/17 20:29:04 UTC

svn commit: r464989 - in /myfaces/tobago/trunk/core/src/main/java/org/apache/myfaces/tobago: taglib/component/SubmittedValueLengthValidatorTag.java util/MessageFactory.java validator/SubmittedValueLengthValidator.java

Author: bommel
Date: Tue Oct 17 11:29:03 2006
New Revision: 464989

URL: http://svn.apache.org/viewvc?view=rev&rev=464989
Log:
(TOBAGO-159) SubmittedValueLengthValidator

Added:
    myfaces/tobago/trunk/core/src/main/java/org/apache/myfaces/tobago/taglib/component/SubmittedValueLengthValidatorTag.java
    myfaces/tobago/trunk/core/src/main/java/org/apache/myfaces/tobago/validator/SubmittedValueLengthValidator.java
Modified:
    myfaces/tobago/trunk/core/src/main/java/org/apache/myfaces/tobago/util/MessageFactory.java

Added: myfaces/tobago/trunk/core/src/main/java/org/apache/myfaces/tobago/taglib/component/SubmittedValueLengthValidatorTag.java
URL: http://svn.apache.org/viewvc/myfaces/tobago/trunk/core/src/main/java/org/apache/myfaces/tobago/taglib/component/SubmittedValueLengthValidatorTag.java?view=auto&rev=464989
==============================================================================
--- myfaces/tobago/trunk/core/src/main/java/org/apache/myfaces/tobago/taglib/component/SubmittedValueLengthValidatorTag.java (added)
+++ myfaces/tobago/trunk/core/src/main/java/org/apache/myfaces/tobago/taglib/component/SubmittedValueLengthValidatorTag.java Tue Oct 17 11:29:03 2006
@@ -0,0 +1,86 @@
+package org.apache.myfaces.tobago.taglib.component;
+
+/*
+ * Copyright 2002-2006 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import org.apache.myfaces.tobago.validator.SubmittedValueLengthValidator;
+import org.apache.myfaces.tobago.apt.annotation.Tag;
+import org.apache.myfaces.tobago.apt.annotation.TagAttribute;
+
+import javax.faces.webapp.ValidatorTag;
+import javax.faces.validator.Validator;
+import javax.servlet.jsp.JspException;
+
+/*
+ * Created by IntelliJ IDEA.
+ * User: bommel
+ * Date: Oct 17, 2006
+ * Time: 12:35:01 AM
+ */
+
+/**
+ * Register an SubmittedValueLengthValidator instance on the UIComponent
+ * associated with the closest parent UIComponent custom action.
+ */
+@Tag(name="validateSubmittedValueLength")
+public class SubmittedValueLengthValidatorTag extends ValidatorTag {
+  public String minimum;
+  public String maximum;
+
+
+  public String getMinimum() {
+    return minimum;
+  }
+  @TagAttribute()
+  public void setMinimum(String minimum) {
+    this.minimum = minimum;
+  }
+
+  public String getMaximum() {
+    return maximum;
+  }
+  @TagAttribute()
+  public void setMaximum(String maximum) {
+    this.maximum = maximum;
+  }
+
+  protected Validator createValidator() throws JspException {
+    setValidatorId(SubmittedValueLengthValidator.VALIDATOR_ID);
+    SubmittedValueLengthValidator validator = (SubmittedValueLengthValidator) super.createValidator();
+    if (minimum != null) {
+      try {
+        validator.setMinimum(Integer.parseInt(minimum));
+      } catch(NumberFormatException e) {
+        // ignore
+      }
+    }
+    if (maximum != null) {
+      try {
+        validator.setMaximum(Integer.parseInt(maximum));
+      } catch(NumberFormatException e) {
+        // ignore
+      }
+    }
+    return validator;
+  }
+
+
+  public void release() {
+    super.release();
+    minimum = null;
+    maximum = null;
+  }
+}

Modified: myfaces/tobago/trunk/core/src/main/java/org/apache/myfaces/tobago/util/MessageFactory.java
URL: http://svn.apache.org/viewvc/myfaces/tobago/trunk/core/src/main/java/org/apache/myfaces/tobago/util/MessageFactory.java?view=diff&rev=464989&r1=464988&r2=464989
==============================================================================
--- myfaces/tobago/trunk/core/src/main/java/org/apache/myfaces/tobago/util/MessageFactory.java (original)
+++ myfaces/tobago/trunk/core/src/main/java/org/apache/myfaces/tobago/util/MessageFactory.java Tue Oct 17 11:29:03 2006
@@ -1,7 +1,7 @@
 package org.apache.myfaces.tobago.util;
 
 /*
- * Copyright 2002-2005 The Apache Software Foundation.
+ * Copyright 2002-2006 The Apache Software Foundation.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -26,8 +26,9 @@
 import java.util.Locale;
 import java.util.Map;
 import java.util.ResourceBundle;
+import java.text.MessageFormat;
 
-/**
+/*
  * User: weber
  * Date: Jun 14, 2005
  * Time: 5:40:04 PM
@@ -40,15 +41,36 @@
       = new HashMap<Locale, ResourceBundle>();
 
   public static FacesMessage createFacesMessage(FacesContext facesContext,
-      String key, FacesMessage.Severity severity) {
-    return createFacesMessage(facesContext, "tobago", key, severity);
+      String key, FacesMessage.Severity severity,  Object args[]) {
+    return createFacesMessage(facesContext, "tobago", key, severity, args);
   }
+
   public static FacesMessage createFacesMessage(FacesContext facesContext,
-      String bundle, String key, FacesMessage.Severity severity) {
+        String key, FacesMessage.Severity severity) {
+    return createFacesMessage(facesContext, key, severity, new Object[0]);
+  }
+
+  public static FacesMessage createFacesMessage(FacesContext facesContext,
+      String bundle, String key, FacesMessage.Severity severity, Object args[]) {
     String summary = getMessageText(facesContext, bundle, key);
     String detail = getMessageText(facesContext, bundle, key + "_detail");
+    if (args != null && args.length > 0) {
+      if (summary != null) {
+        MessageFormat format = new MessageFormat(summary, facesContext.getViewRoot().getLocale());
+        summary = format.format(args);
+      }
+
+      if (detail != null) {
+        MessageFormat format = new MessageFormat(detail, facesContext.getViewRoot().getLocale());
+        detail = format.format(args);
+      }
+    }
     return new FacesMessage(severity, summary != null ? summary : key, detail);
+  }
 
+  public static FacesMessage createFacesMessage(FacesContext facesContext,
+      String bundle, String key, FacesMessage.Severity severity) {
+    return createFacesMessage(facesContext, bundle, key, severity, new Object[0]);
   }
 
   private static String getMessageText(

Added: myfaces/tobago/trunk/core/src/main/java/org/apache/myfaces/tobago/validator/SubmittedValueLengthValidator.java
URL: http://svn.apache.org/viewvc/myfaces/tobago/trunk/core/src/main/java/org/apache/myfaces/tobago/validator/SubmittedValueLengthValidator.java?view=auto&rev=464989
==============================================================================
--- myfaces/tobago/trunk/core/src/main/java/org/apache/myfaces/tobago/validator/SubmittedValueLengthValidator.java (added)
+++ myfaces/tobago/trunk/core/src/main/java/org/apache/myfaces/tobago/validator/SubmittedValueLengthValidator.java Tue Oct 17 11:29:03 2006
@@ -0,0 +1,142 @@
+package org.apache.myfaces.tobago.validator;
+
+/*
+ * Copyright 2002-2006 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import org.apache.myfaces.tobago.util.MessageFactory;
+import org.apache.myfaces.tobago.apt.annotation.Validator;
+
+import javax.faces.validator.LengthValidator;
+import javax.faces.validator.ValidatorException;
+import javax.faces.context.FacesContext;
+import javax.faces.component.UIComponent;
+import javax.faces.component.EditableValueHolder;
+import javax.faces.application.FacesMessage;
+
+/*
+ * Created by IntelliJ IDEA.
+ * User: bommel
+ * Date: Oct 16, 2006
+ * Time: 11:58:47 PM
+ */
+/**
+ * <p><strong>SubmittedLengthValidator</strong> is a {@link Validator} that checks
+ * the number of characters in the submitted value of the
+ * associated component.
+ */
+
+@Validator(id = SubmittedValueLengthValidator.VALIDATOR_ID)
+public class SubmittedValueLengthValidator extends LengthValidator {
+  public static final String VALIDATOR_ID = "org.apache.myfaces.tobago.SubmittedValueLength";
+  private Integer minimum;
+  private Integer maximum;
+
+  public SubmittedValueLengthValidator() {
+  }
+
+  public SubmittedValueLengthValidator(int maximum) {
+    setMaximum(maximum);
+  }
+
+  public SubmittedValueLengthValidator(int maximum, int minimum) {
+    setMaximum(maximum);
+    setMinimum(minimum);
+  }
+
+  public int getMinimum() {
+    return minimum != null ? minimum : 0;
+  }
+
+  public void setMinimum(int minimum) {
+    if (minimum > 0) {
+      this.minimum = minimum;
+    }
+  }
+
+  public int getMaximum() {
+    return maximum != null ? maximum : 0;
+  }
+
+  public void setMaximum(int maximum) {
+    if (maximum > 0) {
+      this.maximum = maximum;
+    }
+  }
+
+  public void validate(FacesContext facesContext, UIComponent uiComponent, Object value) throws ValidatorException {
+    if (value != null) {
+      String submittedValue = ((EditableValueHolder) uiComponent).getSubmittedValue().toString();
+      if (maximum != null && submittedValue.length() > maximum) {
+        Object[] args = { maximum, uiComponent.getId() };
+        FacesMessage facesMessage =
+            MessageFactory.createFacesMessage(facesContext, MAXIMUM_MESSAGE_ID, FacesMessage.SEVERITY_ERROR, args);
+        throw new ValidatorException(facesMessage);
+      }
+      if (minimum != null && submittedValue.length() < minimum) {
+        Object[] args = { minimum, uiComponent.getId() };
+        FacesMessage facesMessage = 
+            MessageFactory.createFacesMessage(facesContext, MINIMUM_MESSAGE_ID, FacesMessage.SEVERITY_ERROR, args);
+        throw new ValidatorException(facesMessage);
+      }
+    }
+  }
+
+  public Object saveState(FacesContext context) {
+    Object values[] = new Object[2];
+    values[0] = maximum;
+    values[1] = minimum;
+    return values;
+
+  }
+
+
+  public void restoreState(FacesContext context, Object state) {
+    Object values[] = (Object[]) state;
+    maximum = (Integer) values[0];
+    minimum = (Integer) values[1];
+  }
+
+
+  public boolean equals(Object o) {
+    if (this == o) {
+      return true;
+    }
+    if (o == null || getClass() != o.getClass()) {
+      return false;
+    }
+    if (!super.equals(o)) {
+      return false;
+    }
+
+    SubmittedValueLengthValidator validator = (SubmittedValueLengthValidator) o;
+
+    if (maximum != null ? !maximum.equals(validator.maximum) : validator.maximum != null) {
+      return false;
+    }
+    if (minimum != null ? !minimum.equals(validator.minimum) : validator.minimum != null) {
+      return false;
+    }
+
+    return true;
+  }
+
+  public int hashCode() {
+    int result;
+    result = (minimum != null ? minimum.hashCode() : 0);
+    result = 31 * result + (maximum != null ? maximum.hashCode() : 0);
+    return result;
+  }
+}