- Fix java.lang.NullPointerException,at java.util.regex.Matcher.getTextLength(Matcher.java:1140) exception.

This commit is contained in:
Paulo Gustavo Veiga 2012-09-11 20:30:00 -03:00
parent d811c8f011
commit 1e53ba827f

View File

@ -1,55 +1,59 @@
/* /*
* Copyright [2011] [wisemapping] * Copyright [2011] [wisemapping]
* *
* Licensed under WiseMapping Public License, Version 1.0 (the "License"). * Licensed under WiseMapping Public License, Version 1.0 (the "License").
* It is basically the Apache License, Version 2.0 (the "License") plus the * It is basically the Apache License, Version 2.0 (the "License") plus the
* "powered by wisemapping" text requirement on every single page; * "powered by wisemapping" text requirement on every single page;
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the license at * You may obtain a copy of the license at
* *
* http://www.wisemapping.org/license * http://www.wisemapping.org/license
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package com.wisemapping.validator; package com.wisemapping.validator;
import org.springframework.validation.Errors; import org.jetbrains.annotations.Nullable;
import org.springframework.validation.ValidationUtils; import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import java.util.regex.Pattern;
import java.util.regex.Matcher; import java.util.regex.Pattern;
import java.util.regex.Matcher;
final public class Utils {
//Set the email emailPattern string final public class Utils {
//Set the email emailPattern string
static private Pattern emailPattern = Pattern.compile(".+@.+\\.[a-z]+");
static private Pattern emailPattern = Pattern.compile(".+@.+\\.[a-z]+");
private Utils() {
private Utils() {
}
}
static void validateEmailAddress(final String email, final Errors errors) {
if (email == null || email.trim().length() == 0) { static void validateEmailAddress(final String email, final Errors errors) {
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "email", Messages.FIELD_REQUIRED); if (email == null || email.trim().length() == 0) {
} else { ValidationUtils.rejectIfEmptyOrWhitespace(errors, "email", Messages.FIELD_REQUIRED);
boolean isValid = Utils.isValidateEmailAddress(email); } else {
if (!isValid) { boolean isValid = Utils.isValidateEmailAddress(email);
errors.rejectValue("email", Messages.NO_VALID_EMAIL_ADDRESS); if (!isValid) {
} errors.rejectValue("email", Messages.NO_VALID_EMAIL_ADDRESS);
} }
} }
}
static boolean isValidateEmailAddress(final String email) {
static boolean isValidateEmailAddress(@Nullable final String email) {
//Match the given string with the emailPattern boolean result = false;
final Matcher m = emailPattern.matcher(email); if (email != null) {
//Match the given string with the emailPattern
//check whether match is found final Matcher m = emailPattern.matcher(email);
return m.matches();
} //check whether match is found
} result = m.matches();
}
return result;
}
}