KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > input > validators > IPAddressPatternValidator


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.sslexplorer.input.validators;
21  
22 /**
23  * IP Address validator that uses easy to understand when you read
24  * them regular expressions.
25  * <p>
26  * It matches IP address patterns, where a string may contain either
27  * a specific address, CIDR network string or wildcard IP address.
28  *
29  * @author Brett Smith <a HREF="mailto: brett@3sp.com">&lt;brett@3sp.com&gt;</a>
30  */

31 public class IPAddressPatternValidator extends StringValidator {
32     
33     /**
34      * Regular expression for specific IP address in the format [n].[n].[n].[n]
35      * where 'n' is a number between 1 and 3 characters in length.
36      */

37     final static String JavaDoc IP_ADDRESS_PATTERN_1 = "^[\\d]{1,3}+\\.[\\d]{1,3}+\\.[\\d]{1,3}+\\.[\\d]{1,3}+$";
38     
39     /**
40      * Regular expression for IP address in the CIDR format [n].[n].[n].[n]/[X]
41      * where 'n' is a number between 1 and 3 characters in length and 'X'
42      * is a number between between 1 and 3 characters in length
43      */

44     final static String JavaDoc IP_ADDRESS_PATTERN_2 = "^[\\d]{1,3}+\\.[\\d]{1,3}+\\.[\\d]{1,3}+\\.[\\d]{1,3}+/[\\d]{1,3}";
45     
46     /**
47      * Regular expression for wildcard IP address in CIDR format [n].[n].[n].[n]
48      * where 'n' is a number between 1 and 3 characters OR a '*' character.
49      */

50     final static String JavaDoc IP_ADDRESS_PATTERN_3 = "^[\\d\\*]{1,3}+\\.([\\d\\*]|\\*){1,3}+\\.([\\d\\*]|\\*){1,3}+\\.([\\d\\*]|\\*){1,3}+";
51     
52     /**
53      * Compound regular expression that matches if ANY of the IP address
54      * patterns match
55      */

56     final static String JavaDoc IP_ADDRESS_PATTERN_REGEXP =
57         "(" + IP_ADDRESS_PATTERN_1 + ")|" +
58         "(" + IP_ADDRESS_PATTERN_2 + ")|" +
59         "(" + IP_ADDRESS_PATTERN_3 + ")";
60
61     /**
62      * Constructor.
63      */

64     public IPAddressPatternValidator() {
65         super(0, 99, IP_ADDRESS_PATTERN_REGEXP, null, true);
66         this.regExpErrCode = ErrorConstants.ERR_STRING_ISNT_IP_ADDRESS_PATTERN;
67     }
68
69     
70 }
71
Popular Tags