1 | /*
|
---|
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
|
---|
3 | * contributor license agreements. See the NOTICE file distributed with
|
---|
4 | * this work for additional information regarding copyright ownership.
|
---|
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
|
---|
6 | * (the "License"); you may not use this file except in compliance with
|
---|
7 | * the License. You may obtain a copy of the License at
|
---|
8 | *
|
---|
9 | * http://www.apache.org/licenses/LICENSE-2.0
|
---|
10 | *
|
---|
11 | * Unless required by applicable law or agreed to in writing, software
|
---|
12 | * distributed under the License is distributed on an "AS IS" BASIS,
|
---|
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
14 | * See the License for the specific language governing permissions and
|
---|
15 | * limitations under the License.
|
---|
16 | */
|
---|
17 | package org.openstreetmap.josm.data.validation.routines;
|
---|
18 |
|
---|
19 | /**
|
---|
20 | * <p><b>InetAddress</b> validation and conversion routines (<code>java.net.InetAddress</code>).</p>
|
---|
21 | *
|
---|
22 | * <p>This class provides methods to validate a candidate IP address.
|
---|
23 | *
|
---|
24 | * <p>
|
---|
25 | * This class is a Singleton; you can retrieve the instance via the {@link #getInstance()} method.
|
---|
26 | * </p>
|
---|
27 | *
|
---|
28 | * @version $Revision: 1227719 $
|
---|
29 | * @since Validator 1.4
|
---|
30 | */
|
---|
31 | public class InetAddressValidator extends AbstractValidator {
|
---|
32 |
|
---|
33 | private static final String IPV4_REGEX =
|
---|
34 | "^(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})$";
|
---|
35 |
|
---|
36 | /**
|
---|
37 | * Singleton instance of this class.
|
---|
38 | */
|
---|
39 | private static final InetAddressValidator VALIDATOR = new InetAddressValidator();
|
---|
40 |
|
---|
41 | /** IPv4 RegexValidator */
|
---|
42 | private final RegexValidator ipv4Validator = new RegexValidator(IPV4_REGEX);
|
---|
43 |
|
---|
44 | /**
|
---|
45 | * Returns the singleton instance of this validator.
|
---|
46 | * @return the singleton instance of this validator
|
---|
47 | */
|
---|
48 | public static InetAddressValidator getInstance() {
|
---|
49 | return VALIDATOR;
|
---|
50 | }
|
---|
51 |
|
---|
52 | /**
|
---|
53 | * Checks if the specified string is a valid IP address.
|
---|
54 | * @param inetAddress the string to validate
|
---|
55 | * @return true if the string validates as an IP address
|
---|
56 | */
|
---|
57 | public boolean isValid(String inetAddress) {
|
---|
58 | return isValidInet4Address(inetAddress);
|
---|
59 | }
|
---|
60 |
|
---|
61 | /**
|
---|
62 | * Validates an IPv4 address. Returns true if valid.
|
---|
63 | * @param inet4Address the IPv4 address to validate
|
---|
64 | * @return true if the argument contains a valid IPv4 address
|
---|
65 | */
|
---|
66 | public boolean isValidInet4Address(String inet4Address) {
|
---|
67 | // verify that address conforms to generic IPv4 format
|
---|
68 | String[] groups = ipv4Validator.match(inet4Address);
|
---|
69 |
|
---|
70 | if (groups == null) return false;
|
---|
71 |
|
---|
72 | // verify that address subgroups are legal
|
---|
73 | for (int i = 0; i <= 3; i++) {
|
---|
74 | String ipSegment = groups[i];
|
---|
75 | if (ipSegment == null || ipSegment.length() <= 0) {
|
---|
76 | return false;
|
---|
77 | }
|
---|
78 |
|
---|
79 | int iIpSegment = 0;
|
---|
80 |
|
---|
81 | try {
|
---|
82 | iIpSegment = Integer.parseInt(ipSegment);
|
---|
83 | } catch(NumberFormatException e) {
|
---|
84 | return false;
|
---|
85 | }
|
---|
86 |
|
---|
87 | if (iIpSegment > 255) {
|
---|
88 | return false;
|
---|
89 | }
|
---|
90 |
|
---|
91 | }
|
---|
92 |
|
---|
93 | return true;
|
---|
94 | }
|
---|
95 | }
|
---|