01: package com.sun.portal.admin.console.sra.validators;
02:
03: import javax.faces.validator.Validator;
04: import javax.faces.validator.ValidatorException;
05: import javax.faces.context.FacesContext;
06: import javax.faces.component.UIComponent;
07: import javax.faces.application.FacesMessage;
08: import java.util.regex.Pattern;
09: import java.util.regex.Matcher;
10:
11: /**
12: * author: Noble Paul
13: * Date: Jul 27, 2005, 12:11:17 PM
14: */
15: public class IPAddressValidator implements Validator {
16: static final Pattern IP_PATTERN = Pattern
17: .compile("\\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.)"
18: + "{3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\b");
19:
20: public void validate(FacesContext facesContext,
21: UIComponent uiComponent, Object value)
22: throws ValidatorException {
23: if (value == null || value.toString().trim().equals(""))
24: return;
25: String ip = value.toString();
26: Matcher matcher = IP_PATTERN.matcher(ip);
27: if (!matcher.matches()) {
28: FacesMessage message = new FacesMessage();
29: message.setDetail("Invalid IP Address");
30: message.setSummary("Invalid IP Address");
31: message.setSeverity(FacesMessage.SEVERITY_ERROR);
32: throw new ValidatorException(message);
33: }
34: }
35: }
|