source: josm/trunk/src/org/openstreetmap/josm/data/validation/routines/InetAddressValidator.java@ 8510

Last change on this file since 8510 was 8510, checked in by Don-vip, 9 years ago

checkstyle: enable relevant whitespace checks and fix them

  • Property svn:eol-style set to native
File size: 3.1 KB
Line 
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 */
17package 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 */
31public 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 @Override
58 public boolean isValid(String inetAddress) {
59 return isValidInet4Address(inetAddress);
60 }
61
62 /**
63 * Validates an IPv4 address. Returns true if valid.
64 * @param inet4Address the IPv4 address to validate
65 * @return true if the argument contains a valid IPv4 address
66 */
67 public boolean isValidInet4Address(String inet4Address) {
68 // verify that address conforms to generic IPv4 format
69 String[] groups = ipv4Validator.match(inet4Address);
70
71 if (groups == null) return false;
72
73 // verify that address subgroups are legal
74 for (int i = 0; i <= 3; i++) {
75 String ipSegment = groups[i];
76 if (ipSegment == null || ipSegment.isEmpty()) {
77 return false;
78 }
79
80 int iIpSegment = 0;
81
82 try {
83 iIpSegment = Integer.parseInt(ipSegment);
84 } catch (NumberFormatException e) {
85 return false;
86 }
87
88 if (iIpSegment > 255) {
89 return false;
90 }
91
92 }
93
94 return true;
95 }
96}
Note: See TracBrowser for help on using the repository browser.