Java IP Address Match checkIPv6Match(String ipTemplateList, String ipStr)

Here you can find the source of checkIPv6Match(String ipTemplateList, String ipStr)

Description

Checks if the passed IPv6 complies to a given pattern.

License

Open Source License

Parameter

Parameter Description
ipTemplateList String list of patterns. Wild cards are allowed.
ipStr The IP address to check.

Return

True, if the passed IP address matches at least one of the patterns.

Declaration

public static boolean checkIPv6Match(String ipTemplateList, String ipStr) 

Method Source Code

//package com.java2s;
/*//from w w  w .  j ava 2 s . c  o  m
 * ------------------------------------------------------------------------------
 * Hermes FTP Server
 * Copyright (c) 2005-2014 Lars Behnke
 * ------------------------------------------------------------------------------
 * 
 * This file is part of Hermes FTP Server.
 * 
 * Hermes FTP Server is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * Hermes FTP Server is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Hermes FTP Server; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 * ------------------------------------------------------------------------------
 */

public class Main {
    private static final String REGEX_COLON = ":";
    private static final int OCTUPLE_LEN = 8;

    /**
     * Checks if the passed IPv6 complies to a given pattern.
     * 
     * @param ipTemplateList String list of patterns. Wild cards are allowed.
     * @param ipStr The IP address to check.
     * @return True, if the passed IP address matches at least one of the
     *         patterns.
     */
    public static boolean checkIPv6Match(String ipTemplateList, String ipStr) {
        String[] chk = ipStr.split(REGEX_COLON);
        if (chk.length != OCTUPLE_LEN) {
            throw new IllegalArgumentException("Illegal IPv6 address: " + ipStr);
        }

        boolean inverse = false;
        String[] ipTemplateArr = ipTemplateList.split(",");
        for (String anIpTemplateArr : ipTemplateArr) {
            String t;
            if (anIpTemplateArr.trim().startsWith("!")) {
                t = anIpTemplateArr.substring(1).trim();
                inverse = true;
            } else {
                t = anIpTemplateArr.trim();
            }
            String[] tmpl = t.split(REGEX_COLON);
            boolean match = true;
            for (int j = 0; j < tmpl.length; j++) {
                match &= ("*".equals(tmpl[j].trim())) || (tmpl[j].trim().equals(chk[j].trim())) ^ inverse;
            }
            if (match) {
                return true;
            }
        }

        return false;

    }
}

Related

  1. checkIPMatching(String pattern, String address)