Author: chris.laprun(a)jboss.com
Date: 2012-01-26 11:05:12 -0500 (Thu, 26 Jan 2012)
New Revision: 8312
Added:
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/form/validator/AbstractValidator.java
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/form/validator/MultipleConditionsValidator.java
Modified:
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/form/validator/DateTimeValidator.java
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/form/validator/EmailAddressValidator.java
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/form/validator/ExpressionValidator.java
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/form/validator/IdentifierValidator.java
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/form/validator/MandatoryValidator.java
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/form/validator/NameValidator.java
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/form/validator/NaturalLanguageValidator.java
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/form/validator/NullFieldValidator.java
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/form/validator/NumberFormatValidator.java
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/form/validator/PasswordStringLengthValidator.java
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/form/validator/PositiveNumberFormatValidator.java
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/form/validator/ResourceValidator.java
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/form/validator/SpecialCharacterValidator.java
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/form/validator/StringLengthValidator.java
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/form/validator/URLValidator.java
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/form/validator/UsernameValidator.java
portal/trunk/webui/portal/src/main/java/org/exoplatform/portal/webui/CaptchaValidator.java
Log:
- GTNPORTAL-1673: Refactor Validator architecture to remove duplicated code and make it
easier to create new ones and/or change behavior for all validators.
Added:
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/form/validator/AbstractValidator.java
===================================================================
---
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/form/validator/AbstractValidator.java
(rev 0)
+++
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/form/validator/AbstractValidator.java 2012-01-26
16:05:12 UTC (rev 8312)
@@ -0,0 +1,91 @@
+/*
+ * JBoss, a division of Red Hat
+ * Copyright 2012, Red Hat Middleware, LLC, and individual
+ * contributors as indicated by the @authors tag. See the
+ * copyright.txt in the distribution for a full listing of
+ * individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+
+package org.exoplatform.webui.form.validator;
+
+import org.exoplatform.web.application.ApplicationMessage;
+import org.exoplatform.webui.core.UIComponent;
+import org.exoplatform.webui.exception.MessageException;
+import org.exoplatform.webui.form.UIForm;
+import org.exoplatform.webui.form.UIFormInput;
+
+import java.io.Serializable;
+
+/** @author <a href="mailto:chris.laprun@jboss.com">Chris
Laprun</a> */
+public abstract class AbstractValidator implements Validator, Serializable
+{
+ protected String getLabelFor(UIFormInput uiInput) throws Exception
+ {
+ UIComponent uiComponent = (UIComponent)uiInput;
+ UIForm uiForm = uiComponent.getAncestorOfType(UIForm.class);
+ String label = uiInput.getName();
+ if (uiForm != null)
+ {
+ label = uiForm.getLabel(label);
+ }
+
+ return label.trim();
+ }
+
+ public void validate(UIFormInput uiInput) throws Exception
+ {
+ String value = trimmedValueOrNullIfBypassed((String)uiInput.getValue(), uiInput);
+ if (value == null)
+ {
+ return;
+ }
+
+ if (!isValid(value, uiInput))
+ {
+ throw createMessageException(value, uiInput);
+ }
+ }
+
+ protected MessageException createMessageException(String value, UIFormInput uiInput)
throws Exception
+ {
+ return new MessageException(new ApplicationMessage(getMessageLocalizationKey(),
getMessageArgs(value, uiInput), ApplicationMessage.WARNING));
+ }
+
+ protected Object[] getMessageArgs(String value, UIFormInput uiInput) throws Exception
+ {
+ return new Object[]{getLabelFor(uiInput)};
+ }
+
+ protected abstract String getMessageLocalizationKey();
+
+ protected abstract boolean isValid(String value, UIFormInput uiInput);
+
+ protected String trimmedValueOrNullIfBypassed(String value, UIFormInput uiInput)
+ {
+ if (value == null)
+ {
+ return null;
+ }
+ else
+ {
+// value = value.trim(); // should values be trimmed before being validated and
saved?
+
+ return value.trim().isEmpty() ? null : value;
+ }
+ }
+}
Modified:
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/form/validator/DateTimeValidator.java
===================================================================
---
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/form/validator/DateTimeValidator.java 2012-01-26
13:21:05 UTC (rev 8311)
+++
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/form/validator/DateTimeValidator.java 2012-01-26
16:05:12 UTC (rev 8312)
@@ -1,16 +1,16 @@
/**
* Copyright (C) 2009 eXo Platform SAS.
- *
+ *
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
- *
+ *
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
- *
+ *
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
@@ -19,58 +19,64 @@
package org.exoplatform.webui.form.validator;
-import java.text.DateFormat;
-import java.text.SimpleDateFormat;
-
-import org.exoplatform.web.application.ApplicationMessage;
-import org.exoplatform.webui.core.UIComponent;
-import org.exoplatform.webui.exception.MessageException;
-import org.exoplatform.webui.form.UIForm;
import org.exoplatform.webui.form.UIFormDateTimeInput;
import org.exoplatform.webui.form.UIFormInput;
+import java.io.Serializable;
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+
/**
* Created by The eXo Platform SARL
* Author : Tran The Trong
* trongtt(a)gmail.com
* May 15, 2007
- *
+ *
* Validates whether a date is in a correct format
*/
-public class DateTimeValidator implements Validator
+public class DateTimeValidator extends AbstractValidator implements Serializable
{
- public void validate(UIFormInput uiInput) throws Exception
+ @Override
+ protected String getMessageLocalizationKey()
{
- if (uiInput.getValue() == null || ((String)uiInput.getValue()).trim().length() ==
0)
- {
- return;
- }
- String s = (String)uiInput.getValue();
+ return "DateTimeValidator.msg.Invalid-input";
+ }
+
+ @Override
+ protected boolean isValid(String value, UIFormInput uiInput)
+ {
UIFormDateTimeInput uiDateInput = (UIFormDateTimeInput)uiInput;
SimpleDateFormat sdf = new SimpleDateFormat(uiDateInput.getDatePattern_().trim());
-
- UIForm uiForm = ((UIComponent)uiInput).getAncestorOfType(UIForm.class);
- String label;
+ // Specify whether or not date/time parsing is to be lenient.
+ sdf.setLenient(false);
try
{
- label = uiForm.getId() + ".label." + uiInput.getName();
+ sdf.parse(value);
+ return true;
}
- catch (Exception e)
+ catch (ParseException e)
{
- label = uiInput.getName();
+ return false;
}
- Object[] args = {label, s};
+ }
- try
+ @Override
+ protected String trimmedValueOrNullIfBypassed(String value, UIFormInput uiInput)
+ {
+ if(!(uiInput instanceof UIFormDateTimeInput))
{
- // Specify whether or not date/time parsing is to be lenient.
- sdf.setLenient(false);
- sdf.parse(s);
+ return null;
}
- catch (Exception e)
+ else
{
- throw new MessageException(new
ApplicationMessage("DateTimeValidator.msg.Invalid-input", args,
ApplicationMessage.WARNING));
+ return super.trimmedValueOrNullIfBypassed(value, uiInput);
}
}
+
+ @Override
+ protected Object[] getMessageArgs(String value, UIFormInput uiInput) throws Exception
+ {
+ return new Object[]{getLabelFor(uiInput), value};
+ }
}
Modified:
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/form/validator/EmailAddressValidator.java
===================================================================
---
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/form/validator/EmailAddressValidator.java 2012-01-26
13:21:05 UTC (rev 8311)
+++
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/form/validator/EmailAddressValidator.java 2012-01-26
16:05:12 UTC (rev 8312)
@@ -19,11 +19,9 @@
package org.exoplatform.webui.form.validator;
-import org.exoplatform.web.application.ApplicationMessage;
import org.exoplatform.commons.serialization.api.annotations.Serialized;
-import org.exoplatform.webui.core.UIComponent;
-import org.exoplatform.webui.exception.MessageException;
-import org.exoplatform.webui.form.UIForm;
+import org.exoplatform.web.application.ApplicationMessage;
+import org.exoplatform.web.application.CompoundApplicationMessage;
import org.exoplatform.webui.form.UIFormInput;
/**
@@ -45,51 +43,31 @@
* _ (underscore) or . (dot) but not as a starting or ending character
*/
@Serialized
-public class EmailAddressValidator implements Validator
+public class EmailAddressValidator extends MultipleConditionsValidator
{
- public void validate(UIFormInput uiInput) throws Exception
+ @Override
+ protected void validate(String value, String label, CompoundApplicationMessage
messages, UIFormInput uiInput)
{
- // modified by Pham Dinh Tan
- UIComponent uiComponent = (UIComponent)uiInput;
- UIForm uiForm = uiComponent.getAncestorOfType(UIForm.class);
- String label;
- try
- {
- label = uiForm.getId() + ".label." + uiInput.getName();
- }
- catch (Exception e)
- {
- label = uiInput.getName();
- }
Object[] args = {label};
-
- if (uiInput.getValue() == null || ((String)uiInput.getValue()).trim().length() ==
0)
- {
- return;
- }
-
- String s = (String)uiInput.getValue();
- int atIndex = s.indexOf('@');
+ int atIndex = value.indexOf('@');
if (atIndex == -1)
{
- throw new MessageException(new
ApplicationMessage("EmailAddressValidator.msg.Invalid-input", args,
- ApplicationMessage.WARNING));
+ messages.addMessage("EmailAddressValidator.msg.Invalid-input", args,
ApplicationMessage.WARNING);
}
-
- String localPart = s.substring(0, atIndex);
- String domainName = s.substring(atIndex + 1);
+ String localPart = value.substring(0, atIndex);
+ String domainName = value.substring(atIndex + 1);
+
if (!validateLocalPart(localPart.toCharArray()) ||
!validateDomainName(domainName.toCharArray()))
{
- throw new MessageException(new
ApplicationMessage("EmailAddressValidator.msg.Invalid-input", args,
- ApplicationMessage.WARNING));
+ messages.addMessage("EmailAddressValidator.msg.Invalid-input", args,
ApplicationMessage.WARNING);
}
}
private boolean validateLocalPart(char[] localPart)
{
- if(!isAlphabet(localPart[0]) || !isAlphabetOrDigit(localPart[localPart.length
-1]))
+ if(!Character.isLetter(localPart[0]) ||
!Character.isLetterOrDigit(localPart[localPart.length - 1]))
{
return false;
}
@@ -99,7 +77,7 @@
char c = localPart[i];
char next = localPart[i+1];
- if(isAlphabetOrDigit(c) || (isLocalPartSymbol(c) &&
isAlphabetOrDigit(next)))
+ if(Character.isLetterOrDigit(c) || (isLocalPartSymbol(c) &&
Character.isLetterOrDigit(next)))
{
continue;
}
@@ -113,7 +91,7 @@
private boolean validateDomainName(char[] domainName)
{
- if(!isAlphabet(domainName[0]) || !isAlphabetOrDigit(domainName[domainName.length
-1]))
+ if(!Character.isLetter(domainName[0]) ||
!Character.isLetterOrDigit(domainName[domainName.length - 1]))
{
return false;
}
@@ -129,12 +107,12 @@
{
foundValidLastDot = true;
}
- else if(!isAlphabet(c))
+ else if(!Character.isLetter(c))
{
foundValidLastDot = false;
}
- if(isAlphabetOrDigit(c) || (isDomainNameSymbol(c) &&
isAlphabetOrDigit(next)))
+ if(Character.isLetterOrDigit(c) || (isDomainNameSymbol(c) &&
Character.isLetterOrDigit(next)))
{
continue;
}
@@ -145,22 +123,7 @@
}
return foundValidLastDot;
}
-
- private boolean isAlphabet(char c)
- {
- return c >= 'a' && c <= 'z';
- }
-
- private boolean isDigit(char c)
- {
- return c >= '0' && c <= '9';
- }
- private boolean isAlphabetOrDigit(char c)
- {
- return isAlphabet(c) || isDigit(c);
- }
-
private boolean isLocalPartSymbol(char c)
{
return c == '_' || c == '.';
Modified:
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/form/validator/ExpressionValidator.java
===================================================================
---
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/form/validator/ExpressionValidator.java 2012-01-26
13:21:05 UTC (rev 8311)
+++
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/form/validator/ExpressionValidator.java 2012-01-26
16:05:12 UTC (rev 8312)
@@ -1,16 +1,16 @@
/**
* Copyright (C) 2009 eXo Platform SAS.
- *
+ *
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
- *
+ *
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
- *
+ *
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
@@ -19,27 +19,24 @@
package org.exoplatform.webui.form.validator;
-import java.util.regex.Pattern;
-
-import org.exoplatform.web.application.ApplicationMessage;
import org.exoplatform.commons.serialization.api.annotations.Serialized;
-import org.exoplatform.webui.core.UIComponent;
-import org.exoplatform.webui.exception.MessageException;
-import org.exoplatform.webui.form.UIForm;
import org.exoplatform.webui.form.UIFormInput;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
/**
* Created by The eXo Platform SARL
* Author : Le Bien Thuy
* lebienthuy(a)gmail.com
* Oct 10, 2007
- *
+ *
* Validates whether this value matches one regular expression.
*/
@Serialized
-public class ExpressionValidator implements Validator
+public class ExpressionValidator extends AbstractValidator
{
- private Pattern pattern;
+ private Matcher matcher;
private String key_;
@@ -50,42 +47,26 @@
public ExpressionValidator(final String regex)
{
- pattern = Pattern.compile(regex);
+ matcher = Pattern.compile(regex).matcher("");
key_ = "ExpressionValidator.msg.value-invalid";
}
public ExpressionValidator(final String regex, final String key)
{
- pattern = Pattern.compile(regex);
+ matcher = Pattern.compile(regex).matcher("");
key_ = key;
}
- public void validate(final UIFormInput uiInput) throws Exception
+ @Override
+ protected String getMessageLocalizationKey()
{
- if (uiInput.getValue() == null || ((String)uiInput.getValue()).trim().length() ==
0)
- {
- return;
- }
-
- String value = ((String)uiInput.getValue()).trim();
- if (pattern.matcher(value).find())
- {
- return;
- }
+ return key_;
+ }
- // modified by Pham Dinh Tan
- UIComponent uiComponent = (UIComponent)uiInput;
- UIForm uiForm = uiComponent.getAncestorOfType(UIForm.class);
- String label;
- try
- {
- label = uiForm.getId() + ".label." + uiInput.getName();
- }
- catch (Exception e)
- {
- label = uiInput.getName();
- }
- Object[] args = {label};
- throw new MessageException(new ApplicationMessage(key_, args,
ApplicationMessage.WARNING));
+ @Override
+ protected boolean isValid(String value, UIFormInput uiInput)
+ {
+ matcher.reset(value);
+ return matcher.matches();
}
}
Modified:
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/form/validator/IdentifierValidator.java
===================================================================
---
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/form/validator/IdentifierValidator.java 2012-01-26
13:21:05 UTC (rev 8311)
+++
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/form/validator/IdentifierValidator.java 2012-01-26
16:05:12 UTC (rev 8312)
@@ -20,9 +20,7 @@
package org.exoplatform.webui.form.validator;
import org.exoplatform.web.application.ApplicationMessage;
-import org.exoplatform.webui.core.UIComponent;
-import org.exoplatform.webui.exception.MessageException;
-import org.exoplatform.webui.form.UIForm;
+import org.exoplatform.web.application.CompoundApplicationMessage;
import org.exoplatform.webui.form.UIFormInput;
import java.io.Serializable;
@@ -36,42 +34,24 @@
* Validates whether the value is composed of letters, digit , '_' or
'-'h
* First character could not be digit or '-'
*/
-public class IdentifierValidator implements Validator, Serializable
+public class IdentifierValidator extends MultipleConditionsValidator implements
Serializable
{
- public void validate(UIFormInput uiInput) throws Exception
+ @Override
+ protected void validate(String value, String label, CompoundApplicationMessage
messages, UIFormInput uiInput)
{
- if (uiInput.getValue() == null || ((String)uiInput.getValue()).trim().length() ==
0)
- return;
- // modified by Pham Dinh Tan
- UIComponent uiComponent = (UIComponent)uiInput;
- UIForm uiForm = uiComponent.getAncestorOfType(UIForm.class);
- String label;
- try
+ if (Character.isDigit(value.charAt(0)) || value.charAt(0) == '-')
{
- label = uiForm.getId() + ".label." + uiInput.getName();
+ messages.addMessage("FirstAndSpecialCharacterNameValidator.msg", new
Object[]{label, uiInput.getBindingField()}, ApplicationMessage.WARNING);
}
- catch (Exception e)
+ for (int i = 0; i < value.length(); i++)
{
- label = uiInput.getName();
- }
- String s = (String)uiInput.getValue();
- if (Character.isDigit(s.charAt(0)) || s.charAt(0) == '-')
- {
- Object[] args = {label, uiInput.getBindingField()};
- throw new MessageException(new
ApplicationMessage("FirstAndSpecialCharacterNameValidator.msg", args,
- ApplicationMessage.WARNING));
- }
- for (int i = 0; i < s.length(); i++)
- {
- char c = s.charAt(i);
+ char c = value.charAt(i);
if (Character.isLetter(c) || Character.isDigit(c) || c == '_' || c ==
'-')
{
continue;
}
- Object[] args = {label};
- throw new MessageException(new
ApplicationMessage("IdentifierValidator.msg.Invalid-char", args,
- ApplicationMessage.WARNING));
+ messages.addMessage("IdentifierValidator.msg.Invalid-char", new
Object[]{label}, ApplicationMessage.WARNING);
}
}
}
Modified:
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/form/validator/MandatoryValidator.java
===================================================================
---
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/form/validator/MandatoryValidator.java 2012-01-26
13:21:05 UTC (rev 8311)
+++
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/form/validator/MandatoryValidator.java 2012-01-26
16:05:12 UTC (rev 8312)
@@ -19,10 +19,6 @@
package org.exoplatform.webui.form.validator;
-import org.exoplatform.web.application.ApplicationMessage;
-import org.exoplatform.webui.core.UIComponent;
-import org.exoplatform.webui.exception.MessageException;
-import org.exoplatform.webui.form.UIForm;
import org.exoplatform.webui.form.UIFormInput;
import java.io.Serializable;
@@ -38,31 +34,27 @@
* is mandatory in your form, add it this validator. A '*' character will be
automatically added
* during the rendering phase to specify the user
*/
-public class MandatoryValidator implements Validator, Serializable
+public class MandatoryValidator extends AbstractValidator implements Serializable
{
public void validate(UIFormInput uiInput) throws Exception
{
- if ((uiInput.getValue() != null) &&
((String)uiInput.getValue()).trim().length() > 0)
+ String value = (String)uiInput.getValue();
+ if (value == null || value.trim().isEmpty())
{
- return;
+ throw createMessageException("EmptyFieldValidator.msg.empty-input",
uiInput);
}
+ }
- //modified by Pham Dinh Tan
- UIComponent uiComponent = (UIComponent)uiInput;
- UIForm uiForm = uiComponent.getAncestorOfType(UIForm.class);
- String label;
- try
- {
- label = uiForm.getId() + ".label." + uiInput.getName();
- }
- catch (Exception e)
- {
- label = uiInput.getName();
- }
- label = label.trim();
- Object[] args = {label};
- throw new MessageException(new
ApplicationMessage("EmptyFieldValidator.msg.empty-input", args,
- ApplicationMessage.WARNING));
+ @Override
+ protected String getMessageLocalizationKey()
+ {
+ return "EmptyFieldValidator.msg.empty-input";
}
+
+ @Override
+ protected boolean isValid(String value, UIFormInput uiInput)
+ {
+ throw new UnsupportedOperationException("Unneeded by this
implementation");
+ }
}
Added:
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/form/validator/MultipleConditionsValidator.java
===================================================================
---
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/form/validator/MultipleConditionsValidator.java
(rev 0)
+++
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/form/validator/MultipleConditionsValidator.java 2012-01-26
16:05:12 UTC (rev 8312)
@@ -0,0 +1,68 @@
+/*
+ * JBoss, a division of Red Hat
+ * Copyright 2012, Red Hat Middleware, LLC, and individual
+ * contributors as indicated by the @authors tag. See the
+ * copyright.txt in the distribution for a full listing of
+ * individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+
+package org.exoplatform.webui.form.validator;
+
+import org.exoplatform.web.application.CompoundApplicationMessage;
+import org.exoplatform.webui.exception.MessageException;
+import org.exoplatform.webui.form.UIFormInput;
+
+import java.io.Serializable;
+
+/** @author <a href="mailto:chris.laprun@jboss.com">Chris
Laprun</a> */
+public abstract class MultipleConditionsValidator extends AbstractValidator implements
Serializable
+{
+ public void validate(UIFormInput uiInput) throws Exception
+ {
+ String value = trimmedValueOrNullIfBypassed((String)uiInput.getValue(), uiInput);
+ if (value == null)
+ {
+ return;
+ }
+
+ String label = getLabelFor(uiInput);
+
+ CompoundApplicationMessage messages = new CompoundApplicationMessage();
+
+ validate(value, label, messages, uiInput);
+
+ if (!messages.isEmpty())
+ {
+ throw new MessageException(messages);
+ }
+ }
+
+ protected abstract void validate(String value, String label,
CompoundApplicationMessage messages, UIFormInput uiInput);
+
+ @Override
+ protected String getMessageLocalizationKey()
+ {
+ throw new UnsupportedOperationException("Unneeded by this
implementation");
+ }
+
+ @Override
+ protected boolean isValid(String value, UIFormInput uiInput)
+ {
+ throw new UnsupportedOperationException("Unneeded by this
implementation");
+ }
+}
Modified:
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/form/validator/NameValidator.java
===================================================================
---
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/form/validator/NameValidator.java 2012-01-26
13:21:05 UTC (rev 8311)
+++
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/form/validator/NameValidator.java 2012-01-26
16:05:12 UTC (rev 8312)
@@ -19,10 +19,6 @@
package org.exoplatform.webui.form.validator;
-import org.exoplatform.web.application.ApplicationMessage;
-import org.exoplatform.webui.core.UIComponent;
-import org.exoplatform.webui.exception.MessageException;
-import org.exoplatform.webui.form.UIForm;
import org.exoplatform.webui.form.UIFormInput;
import java.io.Serializable;
@@ -35,36 +31,29 @@
*
* Validates whether this value is composed of letters, numbers, '_',
'-', '.' or '*'
*/
-public class NameValidator implements Validator, Serializable
+public class NameValidator extends AbstractValidator implements Serializable
{
- public void validate(UIFormInput uiInput) throws Exception
+ @Override
+ protected String getMessageLocalizationKey()
{
- if (uiInput.getValue() == null || ((String)uiInput.getValue()).trim().length() ==
0)
- return;
- // modified by Pham Dinh Tan
- UIComponent uiComponent = (UIComponent)uiInput;
- UIForm uiForm = uiComponent.getAncestorOfType(UIForm.class);
- String label;
- try
+ return "NameValidator.msg.Invalid-char";
+ }
+
+ @Override
+ protected boolean isValid(String value, UIFormInput uiInput)
+ {
+ for (int i = 0; i < value.length(); i++)
{
- label = uiForm.getId() + ".label." + uiInput.getName();
- }
- catch (Exception e)
- {
- label = uiInput.getName();
- }
- String s = (String)uiInput.getValue();
- for (int i = 0; i < s.length(); i++)
- {
- char c = s.charAt(i);
+ char c = value.charAt(i);
if (Character.isLetter(c) || Character.isDigit(c) || c == '_' || c ==
'-' || c == '.' || c == '*')
{
continue;
}
- Object[] args = {label};
- throw new MessageException(new
ApplicationMessage("NameValidator.msg.Invalid-char", args));
+ return false;
}
+
+ return true;
}
}
Modified:
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/form/validator/NaturalLanguageValidator.java
===================================================================
---
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/form/validator/NaturalLanguageValidator.java 2012-01-26
13:21:05 UTC (rev 8311)
+++
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/form/validator/NaturalLanguageValidator.java 2012-01-26
16:05:12 UTC (rev 8312)
@@ -17,52 +17,36 @@
package org.exoplatform.webui.form.validator;
import org.exoplatform.commons.serialization.api.annotations.Serialized;
-import org.exoplatform.web.application.ApplicationMessage;
-import org.exoplatform.webui.core.UIComponent;
-import org.exoplatform.webui.exception.MessageException;
-import org.exoplatform.webui.form.UIForm;
import org.exoplatform.webui.form.UIFormInput;
/**
* @author <a href="mailto:haint@exoplatform.com">Nguyen Thanh
Hai</a>
- *
* @datOct 11, 2011
- *
+ *
* Validates whether this value is composed of letters or spaces
*/
@Serialized
-public class NaturalLanguageValidator implements Validator
+public class NaturalLanguageValidator extends AbstractValidator
{
- public void validate(UIFormInput uiInput) throws Exception
+ @Override
+ protected String getMessageLocalizationKey()
{
- UIComponent uiComponent = (UIComponent)uiInput;
- UIForm uiForm = uiComponent.getAncestorOfType(UIForm.class);
- String label;
- try
+ return "NaturalLanguageValidator.msg.Invalid-char";
+ }
+
+ @Override
+ protected boolean isValid(String value, UIFormInput uiInput)
+ {
+ for (int i = 0; i < value.length(); i++)
{
- label = uiForm.getId() + ".label." + uiInput.getName();
- }
- catch (Exception e)
- {
- label = uiInput.getName();
- }
- Object[] args = {label};
-
- if (uiInput.getValue() == null || ((String)uiInput.getValue()).trim().length() ==
0)
- {
- return;
- }
-
- String s = (String)uiInput.getValue();
- for (int i = 0; i < s.length(); i++)
- {
- char c = s.charAt(i);
+ char c = value.charAt(i);
if (Character.isLetter(c) || Character.isSpaceChar(c))
{
continue;
}
- throw new MessageException(new
ApplicationMessage("NaturalLanguageValidator.msg.Invalid-char", args));
+ return false;
}
+ return true;
}
}
Modified:
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/form/validator/NullFieldValidator.java
===================================================================
---
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/form/validator/NullFieldValidator.java 2012-01-26
13:21:05 UTC (rev 8311)
+++
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/form/validator/NullFieldValidator.java 2012-01-26
16:05:12 UTC (rev 8312)
@@ -19,12 +19,10 @@
package org.exoplatform.webui.form.validator;
-import org.exoplatform.web.application.ApplicationMessage;
-import org.exoplatform.webui.core.UIComponent;
-import org.exoplatform.webui.exception.MessageException;
-import org.exoplatform.webui.form.UIForm;
import org.exoplatform.webui.form.UIFormInput;
+import java.io.Serializable;
+
/**
* Created by The eXo Platform SARL
* Author : Dang Van Minh
@@ -33,26 +31,26 @@
*
* Validates whether this value is null
*/
-public class NullFieldValidator implements Validator
+public class NullFieldValidator extends AbstractValidator implements Serializable
{
public void validate(UIFormInput uiInput) throws Exception
{
- if ((uiInput.getValue() != null))
- return;
- // modified by Pham Dinh Tan
- UIComponent uiComponent = (UIComponent)uiInput;
- UIForm uiForm = uiComponent.getAncestorOfType(UIForm.class);
- String label;
- try
+ if ((uiInput.getValue() == null))
{
- label = uiForm.getId() + ".label." + uiInput.getName();
+ throw createMessageException(null, uiInput);
}
- catch (Exception e)
- {
- label = uiInput.getName();
- }
- Object[] args = {label};
- throw new MessageException(new
ApplicationMessage("EmptyFieldValidator.msg.empty-input", args));
}
+
+ @Override
+ protected String getMessageLocalizationKey()
+ {
+ return "EmptyFieldValidator.msg.empty-input";
+ }
+
+ @Override
+ protected boolean isValid(String value, UIFormInput uiInput)
+ {
+ throw new UnsupportedOperationException("Unneeded by this
implementation");
+ }
}
Modified:
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/form/validator/NumberFormatValidator.java
===================================================================
---
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/form/validator/NumberFormatValidator.java 2012-01-26
13:21:05 UTC (rev 8311)
+++
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/form/validator/NumberFormatValidator.java 2012-01-26
16:05:12 UTC (rev 8312)
@@ -1,16 +1,16 @@
/**
* Copyright (C) 2009 eXo Platform SAS.
- *
+ *
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
- *
+ *
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
- *
+ *
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
@@ -20,59 +20,46 @@
package org.exoplatform.webui.form.validator;
import org.exoplatform.web.application.ApplicationMessage;
-import org.exoplatform.webui.core.UIComponent;
-import org.exoplatform.webui.exception.MessageException;
-import org.exoplatform.webui.form.UIForm;
+import org.exoplatform.web.application.CompoundApplicationMessage;
import org.exoplatform.webui.form.UIFormInput;
+import java.io.Serializable;
+
/**
* Created by The eXo Platform SARL
* Author : Dang Van Minh
* minhdv81(a)yahoo.com
* Jun 7, 2006
- *
+ *
* Validates whether this number is in a correct format
*/
-public class NumberFormatValidator implements Validator
+public class NumberFormatValidator extends MultipleConditionsValidator implements
Serializable
{
+ @Override
+ protected void validate(String value, String label, CompoundApplicationMessage
messages, UIFormInput uiInput)
+ {
+ validateInteger(value, label, messages);
+ }
- public void validate(UIFormInput uiInput) throws Exception
+ protected Integer validateInteger(String value, String label,
CompoundApplicationMessage messages)
{
- if (uiInput.getValue() == null || ((String)uiInput.getValue()).length() == 0)
- {
- return;
- }
-
- // modified by Pham Dinh Tan
- UIComponent uiComponent = (UIComponent)uiInput;
- UIForm uiForm = uiComponent.getAncestorOfType(UIForm.class);
- String label;
- try
- {
- label = uiForm.getId() + ".label." + uiInput.getName();
- }
- catch (Exception e)
- {
- label = uiInput.getName().trim();
- }
Object[] args = {label};
-
- String s = (String)uiInput.getValue();
- if(s.charAt(0) == '0' && s.length() > 1)
+ if (value.charAt(0) == '0' && value.length() > 1)
{
- throw new MessageException(new
ApplicationMessage("NumberFormatValidator.msg.Invalid-number", args));
+ messages.addMessage("NumberFormatValidator.msg.Invalid-number", args,
ApplicationMessage.WARNING);
}
- else if(s.charAt(0) == '-' && s.length() > 1 &&
s.charAt(1) == '0')
+ else if (value.charAt(0) == '-' && value.length() > 1 &&
value.charAt(1) == '0')
{
- throw new MessageException(new
ApplicationMessage("NumberFormatValidator.msg.Invalid-number", args));
+ messages.addMessage("NumberFormatValidator.msg.Invalid-number", args,
ApplicationMessage.WARNING);
}
try
{
- Integer.parseInt(s);
- }
- catch(NumberFormatException e)
+ return Integer.parseInt(value);
+ }
+ catch (NumberFormatException e)
{
- throw new MessageException(new
ApplicationMessage("NumberFormatValidator.msg.Invalid-number", args));
+ messages.addMessage("NumberFormatValidator.msg.Invalid-number", args,
ApplicationMessage.WARNING);
+ return null;
}
}
Modified:
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/form/validator/PasswordStringLengthValidator.java
===================================================================
---
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/form/validator/PasswordStringLengthValidator.java 2012-01-26
13:21:05 UTC (rev 8311)
+++
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/form/validator/PasswordStringLengthValidator.java 2012-01-26
16:05:12 UTC (rev 8312)
@@ -19,12 +19,7 @@
package org.exoplatform.webui.form.validator;
-import org.exoplatform.web.application.ApplicationMessage;
import org.exoplatform.commons.serialization.api.annotations.Serialized;
-import org.exoplatform.webui.core.UIComponent;
-import org.exoplatform.webui.exception.MessageException;
-import org.exoplatform.webui.form.UIForm;
-import org.exoplatform.webui.form.UIFormInput;
/**
* Created by The eXo Platform SARL
@@ -35,18 +30,8 @@
* Validates whether this value has a length between min and max
*/
@Serialized
-public class PasswordStringLengthValidator implements Validator
+public class PasswordStringLengthValidator extends StringLengthValidator
{
- /**
- * The minimum number of characters in this String
- */
- private Integer min_ = 0;
-
- /**
- * The maximum number of characters in this String
- */
- private Integer max_ = 0;
-
// For @Serialized needs
public PasswordStringLengthValidator()
{
@@ -54,40 +39,17 @@
public PasswordStringLengthValidator(Integer max)
{
- max_ = max;
+ super(max);
}
public PasswordStringLengthValidator(Integer min, Integer max)
{
- min_ = min;
- max_ = max;
+ super(min, max);
}
- public void validate(UIFormInput uiInput) throws Exception
+ @Override
+ protected String getValue(String value)
{
- if (uiInput.getValue() == null || ((String)uiInput.getValue()).trim().length() ==
0)
- return;
- if ((uiInput.getValue() != null))
- {
- int length = ((String)uiInput.getValue()).length();
- if (min_ <= length && max_ >= length)
- return;
- }
-
- //modified by Pham Dinh Tan
- UIComponent uiComponent = (UIComponent)uiInput;
- UIForm uiForm = uiComponent.getAncestorOfType(UIForm.class);
- String label;
- try
- {
- label = uiForm.getId() + ".label." + uiInput.getName();
- }
- catch (Exception e)
- {
- label = uiInput.getName();
- }
- Object[] args = {label, min_.toString(), max_.toString()};
- throw new MessageException(new
ApplicationMessage("StringLengthValidator.msg.length-invalid", args,
- ApplicationMessage.WARNING));
+ return value;
}
}
Modified:
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/form/validator/PositiveNumberFormatValidator.java
===================================================================
---
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/form/validator/PositiveNumberFormatValidator.java 2012-01-26
13:21:05 UTC (rev 8311)
+++
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/form/validator/PositiveNumberFormatValidator.java 2012-01-26
16:05:12 UTC (rev 8312)
@@ -20,11 +20,10 @@
package org.exoplatform.webui.form.validator;
import org.exoplatform.web.application.ApplicationMessage;
-import org.exoplatform.webui.core.UIComponent;
-import org.exoplatform.webui.exception.MessageException;
-import org.exoplatform.webui.form.UIForm;
-import org.exoplatform.webui.form.UIFormInput;
+import org.exoplatform.web.application.CompoundApplicationMessage;
+import java.io.Serializable;
+
/**
* Created by The eXo Platform SARL
* Author : Vu Duy Tu
@@ -34,54 +33,26 @@
* Validates whether this number is positive or 0
*/
-public class PositiveNumberFormatValidator implements Validator
+public class PositiveNumberFormatValidator extends NumberFormatValidator implements
Serializable
{
- public void validate(UIFormInput uiInput) throws Exception
+ @Override
+ protected Integer validateInteger(String value, String label,
CompoundApplicationMessage messages)
{
- if (uiInput.getValue() == null || ((String)uiInput.getValue()).length() == 0)
+ Integer integer = super.validateInteger(value, label, messages);
+
+ if(integer == null)
{
- return;
+ return null;
}
- // modified by Pham Dinh Tan
- UIComponent uiComponent = (UIComponent)uiInput;
- UIForm uiForm = uiComponent.getAncestorOfType(UIForm.class);
- String label;
- try
+ else if(integer < 0)
{
- label = uiForm.getId() + ".label." + uiInput.getName();
+
messages.addMessage("PositiveNumberFormatValidator.msg.Invalid-number", new
Object[]{label}, ApplicationMessage.WARNING);
+ return null;
}
- catch (Exception e)
+ else
{
- label = uiInput.getName();
+ return integer;
}
- Object[] args = {label, uiInput.getBindingField()};
-
- String s = (String)uiInput.getValue();
-
- if(s.charAt(0) == '0' && s.length() > 1)
- {
- throw new MessageException(new
ApplicationMessage("NumberFormatValidator.msg.Invalid-number", args));
- }
- else if(s.charAt(0) == '-' && s.length() > 1 &&
s.charAt(1) == '0')
- {
- throw new MessageException(new
ApplicationMessage("NumberFormatValidator.msg.Invalid-number", args));
- }
-
- int value;
- try
- {
- value = Integer.parseInt(s);
- }
- catch(NumberFormatException e)
- {
- throw new MessageException(new
ApplicationMessage("NumberFormatValidator.msg.Invalid-number", args));
- }
-
- if(value >= 0)
- {
- return;
- }
- throw new MessageException(new
ApplicationMessage("PositiveNumberFormatValidator.msg.Invalid-number", args));
}
}
\ No newline at end of file
Modified:
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/form/validator/ResourceValidator.java
===================================================================
---
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/form/validator/ResourceValidator.java 2012-01-26
13:21:05 UTC (rev 8311)
+++
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/form/validator/ResourceValidator.java 2012-01-26
16:05:12 UTC (rev 8312)
@@ -1,16 +1,16 @@
/**
* Copyright (C) 2009 eXo Platform SAS.
- *
+ *
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
- *
+ *
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
- *
+ *
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
@@ -19,11 +19,9 @@
package org.exoplatform.webui.form.validator;
-import org.exoplatform.web.application.ApplicationMessage;
import org.exoplatform.commons.serialization.api.annotations.Serialized;
-import org.exoplatform.webui.core.UIComponent;
-import org.exoplatform.webui.exception.MessageException;
-import org.exoplatform.webui.form.UIForm;
+import org.exoplatform.web.application.ApplicationMessage;
+import org.exoplatform.web.application.CompoundApplicationMessage;
import org.exoplatform.webui.form.UIFormInput;
/**
@@ -31,46 +29,30 @@
* Author : dang.tung
* tungcnw(a)gmail.com
* 14 March, 2006
- *
+ *
* Validates whether the value is composed of letters, numbers or '_'
*/
@Serialized
-public class ResourceValidator implements Validator
+public class ResourceValidator extends MultipleConditionsValidator
{
-
- public void validate(UIFormInput uiInput) throws Exception
+ @Override
+ protected void validate(String value, String label, CompoundApplicationMessage
messages, UIFormInput uiInput)
{
- if (uiInput == null || ((String)uiInput.getValue()).trim().length() == 0)
- return;
- // modified by Pham Dinh Tan
- UIComponent uiComponent = (UIComponent)uiInput;
- UIForm uiForm = uiComponent.getAncestorOfType(UIForm.class);
- String label;
- try
+ char firstChar = value.charAt(0);
+ if (Character.isDigit(firstChar) || firstChar == '-' || firstChar ==
'.' || firstChar == '_')
{
- label = uiForm.getId() + ".label." + uiInput.getName();
- }
- catch (Exception e)
- {
- label = uiInput.getName();
- }
- String s = (String)uiInput.getValue();
- if (Character.isDigit(s.charAt(0)) || s.charAt(0) == '-' || s.charAt(0) ==
'.' || s.charAt(0) == '_')
- {
Object[] args = {label, uiInput.getBindingField()};
- throw new MessageException(new
ApplicationMessage("FirstCharacterNameValidator.msg", args,
- ApplicationMessage.WARNING));
+ messages.addMessage("FirstCharacterNameValidator.msg", args,
ApplicationMessage.WARNING);
}
- for (int i = 0; i < s.length(); i++)
+ for (int i = 0; i < value.length(); i++)
{
- char c = s.charAt(i);
+ char c = value.charAt(i);
if (Character.isLetter(c) || Character.isDigit(c) || c == '_' || c ==
'-' || c == '.')
{
continue;
}
Object[] args = {label};
- throw new MessageException(new
ApplicationMessage("ResourceValidator.msg.Invalid-char", args,
- ApplicationMessage.WARNING));
+ messages.addMessage("ResourceValidator.msg.Invalid-char", args,
ApplicationMessage.WARNING);
}
}
}
Modified:
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/form/validator/SpecialCharacterValidator.java
===================================================================
---
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/form/validator/SpecialCharacterValidator.java 2012-01-26
13:21:05 UTC (rev 8311)
+++
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/form/validator/SpecialCharacterValidator.java 2012-01-26
16:05:12 UTC (rev 8312)
@@ -1,16 +1,16 @@
/**
* Copyright (C) 2009 eXo Platform SAS.
- *
+ *
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
- *
+ *
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
- *
+ *
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
@@ -19,50 +19,37 @@
package org.exoplatform.webui.form.validator;
-import org.exoplatform.web.application.ApplicationMessage;
import org.exoplatform.commons.serialization.api.annotations.Serialized;
-import org.exoplatform.webui.core.UIComponent;
-import org.exoplatform.webui.exception.MessageException;
-import org.exoplatform.webui.form.UIForm;
import org.exoplatform.webui.form.UIFormInput;
/**
* Created by The eXo Platform SARL
* Author : dang.tung
* tungcnw(a)gmail.com
- * Dec 12, 2007
+ * Dec 12, 2007
*/
@Serialized
-public class SpecialCharacterValidator implements Validator
+public class SpecialCharacterValidator extends AbstractValidator
{
- public void validate(UIFormInput uiInput) throws Exception
+ @Override
+ protected String getMessageLocalizationKey()
{
- if (uiInput.getValue() == null || ((String)uiInput.getValue()).trim().length() ==
0)
- return;
- // modified by Pham Dinh Tan
- UIComponent uiComponent = (UIComponent)uiInput;
- UIForm uiForm = uiComponent.getAncestorOfType(UIForm.class);
- String label;
- try
+ return "SpecialCharacterValidator.msg.Invalid-char";
+ }
+
+ @Override
+ protected boolean isValid(String value, UIFormInput uiInput)
+ {
+ for (int i = 0; i < value.length(); i++)
{
- label = uiForm.getId() + ".label." + uiInput.getName();
- }
- catch (Exception e)
- {
- label = uiInput.getName();
- }
- String s = (String)uiInput.getValue();
- for (int i = 0; i < s.length(); i++)
- {
- char c = s.charAt(i);
+ char c = value.charAt(i);
if (Character.isLetter(c) || Character.isDigit(c) || c == '_' || c ==
'-' || Character.isSpaceChar(c))
{
continue;
}
- Object[] args = {label};
- throw new MessageException(new
ApplicationMessage("SpecialCharacterValidator.msg.Invalid-char", args,
- ApplicationMessage.WARNING));
+ return false;
}
+ return true;
}
}
Modified:
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/form/validator/StringLengthValidator.java
===================================================================
---
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/form/validator/StringLengthValidator.java 2012-01-26
13:21:05 UTC (rev 8311)
+++
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/form/validator/StringLengthValidator.java 2012-01-26
16:05:12 UTC (rev 8312)
@@ -1,16 +1,16 @@
/**
* Copyright (C) 2009 eXo Platform SAS.
- *
+ *
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
- *
+ *
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
- *
+ *
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
@@ -19,34 +19,30 @@
package org.exoplatform.webui.form.validator;
-import org.exoplatform.web.application.ApplicationMessage;
-import org.exoplatform.webui.core.UIComponent;
-import org.exoplatform.webui.exception.MessageException;
-import org.exoplatform.webui.form.UIForm;
+import org.exoplatform.commons.serialization.api.annotations.Serialized;
import org.exoplatform.webui.form.UIFormInput;
-import java.io.Serializable;
-
/**
* Created by The eXo Platform SARL
* Author : Dang Van Minh
- * minhdv81(a)yahoo.com
+ * minhdv81(a)yahoo.com
* Jun 7, 2006
- *
+ * <p/>
* Validates whether this value has a length between min and max
*/
-public class StringLengthValidator implements Validator, Serializable
+@Serialized
+public class StringLengthValidator extends AbstractValidator
{
- /**
- * The minimum number of characters in this String
- */
+ /** The minimum number of characters in this String */
private Integer min_ = 0;
- /**
- * The maximum number of characters in this String
- */
+ /** The maximum number of characters in this String */
private Integer max_ = 0;
+ public StringLengthValidator()
+ {
+ }
+
public StringLengthValidator(Integer max)
{
max_ = max;
@@ -58,31 +54,27 @@
max_ = max;
}
- public void validate(UIFormInput uiInput) throws Exception
+ @Override
+ protected String getMessageLocalizationKey()
{
- if (uiInput.getValue() == null || ((String)uiInput.getValue()).trim().length() ==
0)
- return;
- if ((uiInput.getValue() != null))
- {
- int length = ((String)uiInput.getValue()).trim().length();
- if (min_ <= length && max_ >= length)
- return;
- }
+ return "StringLengthValidator.msg.length-invalid";
+ }
- //modified by Pham Dinh Tan
- UIComponent uiComponent = (UIComponent)uiInput;
- UIForm uiForm = uiComponent.getAncestorOfType(UIForm.class);
- String label;
- try
- {
- label = uiForm.getId() + ".label." + uiInput.getName();
- }
- catch (Exception e)
- {
- label = uiInput.getName();
- }
- Object[] args = {label, min_.toString(), max_.toString()};
- throw new MessageException(new
ApplicationMessage("StringLengthValidator.msg.length-invalid", args,
- ApplicationMessage.WARNING));
+ @Override
+ protected boolean isValid(String value, UIFormInput uiInput)
+ {
+ int length = getValue(value).length();
+ return min_ <= length && max_ >= length;
}
+
+ protected String getValue(String value)
+ {
+ return value.trim();
+ }
+
+ @Override
+ protected Object[] getMessageArgs(String value, UIFormInput uiInput) throws Exception
+ {
+ return new Object[]{getLabelFor(uiInput), min_.toString(), max_.toString()};
+ }
}
Modified:
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/form/validator/URLValidator.java
===================================================================
---
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/form/validator/URLValidator.java 2012-01-26
13:21:05 UTC (rev 8311)
+++
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/form/validator/URLValidator.java 2012-01-26
16:05:12 UTC (rev 8312)
@@ -19,21 +19,16 @@
package org.exoplatform.webui.form.validator;
-import org.exoplatform.web.application.ApplicationMessage;
-import org.exoplatform.webui.core.UIComponent;
-import org.exoplatform.webui.exception.MessageException;
-import org.exoplatform.webui.form.UIForm;
-import org.exoplatform.webui.form.UIFormInput;
+import org.exoplatform.commons.serialization.api.annotations.Serialized;
-import java.io.Serializable;
-
/**
* Created by The eXo Platform SAS
* Author : Tan Pham Dinh
* pdtanit(a)gmail.com
* Oct 30, 2008
*/
-public class URLValidator implements Validator, Serializable
+@Serialized
+public class URLValidator extends ExpressionValidator
{
static private final String IP_REGEX =
@@ -46,39 +41,15 @@
+ "|([a-zA-Z][-a-zA-Z0-9]+))" // domain like localhost
+ "(:[0-9]{1,5})?" // port number :8080
+ "((/?)|(/[0-9a-zA-Z_!~*'().;?:@&=+$,%#-]+)+/?)$"; //uri
+ private static final String DEFAULT_KEY = "URLValidator.msg.invalid-url";
- private String key_;
-
public URLValidator()
{
- key_ = "URLValidator.msg.invalid-url";
+ this(DEFAULT_KEY);
}
public URLValidator(String key)
{
- if (key != null)
- key_ = key;
+ super(URL_REGEX, key != null ? key : DEFAULT_KEY);
}
-
- public void validate(UIFormInput uiInput) throws Exception
- {
- if ((uiInput.getValue() == null) || (uiInput.getValue().toString().trim().length()
== 0))
- return;
- String s = uiInput.getValue().toString().trim();
- if (s.matches(URL_REGEX))
- return;
-
- UIForm uiForm = ((UIComponent)uiInput).getAncestorOfType(UIForm.class);
- String label;
- try
- {
- label = uiForm.getId() + ".label." + uiInput.getName();
- }
- catch (Exception e)
- {
- label = uiInput.getName();
- }
- Object[] args = {label};
- throw new MessageException(new ApplicationMessage(key_, args,
ApplicationMessage.WARNING));
- }
}
Modified:
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/form/validator/UsernameValidator.java
===================================================================
---
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/form/validator/UsernameValidator.java 2012-01-26
13:21:05 UTC (rev 8311)
+++
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/form/validator/UsernameValidator.java 2012-01-26
16:05:12 UTC (rev 8312)
@@ -19,71 +19,53 @@
import org.exoplatform.commons.serialization.api.annotations.Serialized;
import org.exoplatform.web.application.ApplicationMessage;
import org.exoplatform.web.application.CompoundApplicationMessage;
-import org.exoplatform.webui.core.UIComponent;
-import org.exoplatform.webui.exception.MessageException;
-import org.exoplatform.webui.form.UIForm;
import org.exoplatform.webui.form.UIFormInput;
+import java.io.Serializable;
+
/**
- * @author <a href="mailto:haint@exoplatform.com">Nguyen Thanh
Hai</a>
+ * Validate username whether the value is only alpha lower, digit, dot and underscore
with first, last character is
+ * alpha lower or digit and cannot contain consecutive underscore, dot or both.
*
- * @datSep 28, 2011
- *
- * Validate username whether the value is only alpha lower, digit, dot and underscore
with first, last character is alpha lower or digit
- * and cannot contain consecutive underscore, dot or both.
+ * @author <a href="mailto:haint@exoplatform.com">Nguyen Thanh
Hai</a>
+ * @date Sep 28, 2011
*/
@Serialized
-public class UsernameValidator implements Validator
+public class UsernameValidator extends MultipleConditionsValidator implements
Serializable
{
private Integer min = 3;
private Integer max = 30;
-
+
public UsernameValidator(Integer min, Integer max)
{
this.min = min;
this.max = max;
}
- public void validate(UIFormInput uiInput) throws Exception
+ protected void validate(String value, String label, CompoundApplicationMessage
messages, UIFormInput uiInput)
{
- String value = (String)uiInput.getValue();
- if (value == null || value.trim().length() == 0)
- {
- return;
- }
-
- UIComponent uiComponent = (UIComponent)uiInput;
- UIForm uiForm = uiComponent.getAncestorOfType(UIForm.class);
- String label = uiInput.getName();
- if(uiForm != null)
- {
- label = uiForm.getLabel(label);
- }
-
- CompoundApplicationMessage messages = new CompoundApplicationMessage();
-
char[] buff = value.toCharArray();
- if(buff.length < min || buff.length > max)
+ if (buff.length < min || buff.length > max)
{
messages.addMessage("StringLengthValidator.msg.length-invalid", new
Object[]{label, min.toString(), max.toString()}, ApplicationMessage.WARNING);
}
-
- if(!isAlphabet(buff[0]))
+
+ if (!Character.isLowerCase(buff[0]))
{
messages.addMessage("FirstCharacterNameValidator.msg", new
Object[]{label}, ApplicationMessage.WARNING);
}
-
- if(!isAlphabetOrDigit(buff[buff.length - 1]))
+
+ if (!Character.isLetterOrDigit(buff[buff.length - 1]))
{
messages.addMessage("LastCharacterUsernameValidator.msg", new
Object[]{label, buff[buff.length - 1]}, ApplicationMessage.WARNING);
}
-
- for(int i = 1; i < buff.length -1; i++)
+
+ for (int i = 1; i < buff.length - 1; i++)
{
char c = buff[i];
- if (isAlphabetOrDigit(c))
+ if (Character.isLetterOrDigit(c))
{
continue;
}
@@ -95,7 +77,7 @@
{
messages.addMessage("ConsecutiveSymbolValidator.msg", new
Object[]{label, buff[i], buff[i + 1]}, ApplicationMessage.WARNING);
}
- else if (!isAlphabetOrDigit(next))
+ else if (!Character.isLetterOrDigit(next))
{
messages.addMessage("UsernameValidator.msg.Invalid-char", new
Object[]{label}, ApplicationMessage.WARNING);
}
@@ -105,31 +87,22 @@
messages.addMessage("UsernameValidator.msg.Invalid-char", new
Object[]{label}, ApplicationMessage.WARNING);
}
}
-
- if(!messages.isEmpty())
- {
- throw new MessageException(messages);
- }
}
-
- private boolean isAlphabet(char c)
+
+ @Override
+ protected String getMessageLocalizationKey()
{
- return c >= 'a' && c <= 'z';
+ throw new UnsupportedOperationException("Unneeded by this
implementation");
}
-
- private boolean isDigit(char c)
+
+ @Override
+ protected boolean isValid(String value, UIFormInput uiInput)
{
- return c >= '0' && c <= '9';
+ throw new UnsupportedOperationException("Unneeded by this
implementation");
}
-
+
private boolean isSymbol(char c)
{
return c == '_' || c == '.';
}
-
- private boolean isAlphabetOrDigit(char c)
- {
- return isAlphabet(c) || isDigit(c);
- }
-
}
Modified:
portal/trunk/webui/portal/src/main/java/org/exoplatform/portal/webui/CaptchaValidator.java
===================================================================
---
portal/trunk/webui/portal/src/main/java/org/exoplatform/portal/webui/CaptchaValidator.java 2012-01-26
13:21:05 UTC (rev 8311)
+++
portal/trunk/webui/portal/src/main/java/org/exoplatform/portal/webui/CaptchaValidator.java 2012-01-26
16:05:12 UTC (rev 8312)
@@ -1,16 +1,16 @@
/**
* Copyright (C) 2009 eXo Platform SAS.
- *
+ *
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
- *
+ *
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
- *
+ *
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
@@ -19,61 +19,49 @@
package org.exoplatform.portal.webui;
-import org.exoplatform.web.application.ApplicationMessage;
+import nl.captcha.Captcha;
import org.exoplatform.webui.application.portlet.PortletRequestContext;
-import org.exoplatform.webui.core.UIComponent;
-import org.exoplatform.webui.exception.MessageException;
-import org.exoplatform.webui.form.UIForm;
import org.exoplatform.webui.form.UIFormInput;
-import org.exoplatform.webui.form.validator.Validator;
+import org.exoplatform.webui.form.validator.AbstractValidator;
-import nl.captcha.Captcha;
-
-import java.io.Serializable;
-
import javax.portlet.PortletRequest;
import javax.portlet.PortletSession;
+import java.io.Serializable;
/**
* @author <a href="mailto:theute@redhat.com">Thomas Heute</a>
- * Validator for Captcha content.
- * Checks that the user input is equals to the content displayed by the
- * distorted image.
+ * Validator for Captcha content.
+ * Checks that the user input is equals to the content displayed by the
+ * distorted image.
*/
-public class CaptchaValidator implements Validator, Serializable
+public class CaptchaValidator extends AbstractValidator implements Serializable
{
- public void validate(UIFormInput uiInput) throws Exception
+ @Override
+ protected boolean isValid(String value, UIFormInput uiInput)
{
PortletRequestContext ctx = PortletRequestContext.getCurrentInstance();
PortletRequest req = ctx.getRequest();
PortletSession session = req.getPortletSession();
- Captcha captcha = (Captcha) session.getAttribute(Captcha.NAME);
+ Captcha captcha = (Captcha)session.getAttribute(Captcha.NAME);
- if ((captcha != null) && (captcha.isCorrect((String) uiInput.getValue())))
- {
- return;
- }
+ return ((captcha != null) && (captcha.isCorrect(value)));
+ }
- //modified by Pham Dinh Tan
- UIComponent uiComponent = (UIComponent) uiInput;
- UIForm uiForm = uiComponent.getAncestorOfType(UIForm.class);
- String label;
- try
- {
- label = uiForm.getLabel(uiInput.getName());
- }
- catch (Exception e)
- {
- label = uiInput.getName();
- }
- label = label.trim();
+ protected String getMessageLocalizationKey()
+ {
+ return "CaptchaValidator.msg.Invalid-input";
+ }
+
+ @Override
+ protected Object[] getMessageArgs(String value, UIFormInput uiInput) throws Exception
+ {
+ String label = getLabelFor(uiInput);
if (label.charAt(label.length() - 1) == ':')
+ {
label = label.substring(0, label.length() - 1);
- Object[] args =
- {label, uiInput.getBindingField()};
- throw new MessageException(new
ApplicationMessage("CaptchaValidator.msg.Invalid-input", args,
- ApplicationMessage.WARNING));
+ }
+ return new Object[]{label, uiInput.getBindingField()};
}
}