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    @Override
058    public boolean isValid(String inetAddress) {
059        return isValidInet4Address(inetAddress);
060    }
061
062    /**
063     * Validates an IPv4 address. Returns true if valid.
064     * @param inet4Address the IPv4 address to validate
065     * @return true if the argument contains a valid IPv4 address
066     */
067    public boolean isValidInet4Address(String inet4Address) {
068        // verify that address conforms to generic IPv4 format
069        String[] groups = ipv4Validator.match(inet4Address);
070
071        if (groups == null) return false;
072
073        // verify that address subgroups are legal
074        for (int i = 0; i <= 3; i++) {
075            String ipSegment = groups[i];
076            if (ipSegment == null || ipSegment.isEmpty()) {
077                return false;
078            }
079
080            int iIpSegment = 0;
081
082            try {
083                iIpSegment = Integer.parseInt(ipSegment);
084            } catch (NumberFormatException e) {
085                return false;
086            }
087
088            if (iIpSegment > 255) {
089                return false;
090            }
091
092        }
093
094        return true;
095    }
096}