001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2014  Oliver Burn
004//
005// This library is free software; you can redistribute it and/or
006// modify it under the terms of the GNU Lesser General Public
007// License as published by the Free Software Foundation; either
008// version 2.1 of the License, or (at your option) any later version.
009//
010// This library is distributed in the hope that it will be useful,
011// but WITHOUT ANY WARRANTY; without even the implied warranty of
012// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013// Lesser General Public License for more details.
014//
015// You should have received a copy of the GNU Lesser General Public
016// License along with this library; if not, write to the Free Software
017// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
018////////////////////////////////////////////////////////////////////////////////
019package com.puppycrawl.tools.checkstyle;
020
021import java.io.File;
022
023/**
024 * Contains utility methods.
025 *
026 * @author <a href="mailto:nesterenko-aleksey@list.ru">Aleksey Nesterenko</a>
027 */
028public final class Utils
029{
030
031    /** stop instances being created **/
032    private Utils()
033    {
034    }
035
036    /**
037     * Returns whether the file extension matches what we are meant to
038     * process.
039     * @param file the file to be checked.
040     * @param fileExtensions files extensions, empty property in config makes it matches to all.
041     * @return whether there is a match.
042     */
043    public static boolean fileExtensionMatches(File file, String[] fileExtensions)
044    {
045        boolean result = false;
046        if ((fileExtensions == null) || (fileExtensions.length == 0)) {
047            result = true;
048        }
049        else {
050            // normalize extensions so all of them have a leading dot
051            final String[] withDotExtensions = new String[fileExtensions.length];
052            for (int i = 0; i < fileExtensions.length; i++) {
053                final String extension = fileExtensions[i];
054                if (extension.startsWith(".")) {
055                    withDotExtensions[i] = extension;
056                }
057                else {
058                    withDotExtensions[i] = "." + extension;
059                }
060            }
061
062            final String fileName = file.getName();
063            for (final String fileExtension : withDotExtensions) {
064                if (fileName.endsWith(fileExtension)) {
065                    result = true;
066                }
067            }
068        }
069
070        return result;
071    }
072}