001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.openstreetmap.josm.data.validation.routines;
018
019/**
020 * <p><b>InetAddress</b> validation and conversion routines (<code>java.net.InetAddress</code>).</p>
021 *
022 * <p>This class provides methods to validate a candidate IP address.
023 *
024 * <p>
025 * This class is a Singleton; you can retrieve the instance via the {@link #getInstance()} method.
026 * </p>
027 *
028 * @version $Revision: 1227719 $
029 * @since Validator 1.4
030 */
031public class InetAddressValidator extends AbstractValidator {
032
033    private static final String IPV4_REGEX =
034            "^(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})$";
035
036    /**
037     * Singleton instance of this class.
038     */
039    private static final InetAddressValidator VALIDATOR = new InetAddressValidator();
040
041    /** IPv4 RegexValidator */
042    private final RegexValidator ipv4Validator = new RegexValidator(IPV4_REGEX);
043
044    /**
045     * Returns the singleton instance of this validator.
046     * @return the singleton instance of this validator
047     */
048    public static InetAddressValidator getInstance() {
049        return VALIDATOR;
050    }
051
052    /**
053     * Checks if the specified string is a valid IP address.
054     * @param inetAddress the string to validate
055     * @return true if the string validates as an IP address
056     */
057    public boolean isValid(String inetAddress) {
058        return isValidInet4Address(inetAddress);
059    }
060
061    /**
062     * Validates an IPv4 address. Returns true if valid.
063     * @param inet4Address the IPv4 address to validate
064     * @return true if the argument contains a valid IPv4 address
065     */
066    public boolean isValidInet4Address(String inet4Address) {
067        // verify that address conforms to generic IPv4 format
068        String[] groups = ipv4Validator.match(inet4Address);
069
070        if (groups == null) return false;
071
072        // verify that address subgroups are legal
073        for (int i = 0; i <= 3; i++) {
074            String ipSegment = groups[i];
075            if (ipSegment == null || ipSegment.length() <= 0) {
076                return false;
077            }
078
079            int iIpSegment = 0;
080
081            try {
082                iIpSegment = Integer.parseInt(ipSegment);
083            } catch(NumberFormatException e) {
084                return false;
085            }
086
087            if (iIpSegment > 255) {
088                return false;
089            }
090
091        }
092
093        return true;
094    }
095}