001/*
002 * (C) Copyright 2011 Nuxeo SAS (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     tdelprat
016 *
017 */
018
019package org.nuxeo.wizard.helpers;
020
021import java.util.regex.Matcher;
022import java.util.regex.Pattern;
023
024/**
025 * RegExp based helper to check IP address format
026 *
027 * @author Tiry (tdelprat@nuxeo.com)
028 * @since 5.4.2
029 */
030public class IPValidator {
031
032    private static Pattern pattern;
033
034    private static Matcher matcher;
035
036    private static final String IPADDRESS_PATTERN = "^([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\."
037            + "([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." + "([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\."
038            + "([01]?\\d\\d?|2[0-4]\\d|25[0-5])$";
039
040    public static synchronized boolean validate(String ip) {
041        if (pattern == null) {
042            pattern = Pattern.compile(IPADDRESS_PATTERN);
043        }
044        matcher = pattern.matcher(ip);
045        return matcher.matches();
046    }
047}