All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.whitesource.utils.ExtensionUtils Maven / Gradle / Ivy

/**
 * The project is licensed under the WhiteSource Software End User License Agreement .
 * Copyright (C) 2015 WhiteSource Ltd.
 * Licensed under the WhiteSource Software End User License Agreement;
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * https://s3.amazonaws.com/unified-agent/LICENSE.txt
 */
package org.whitesource.utils;


import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * This class contains all supported file extensions and creates regex and GLOB patterns for the directory scanner.
 *
 * @author tom.shapira
 */
public class ExtensionUtils {

    /* --- Static members --- */

    public static final List SOURCE_EXTENSIONS = Arrays.asList(
            "c", "cc", "cp", "cpp", "cxx", "c\\+\\+", "h", "hh", "hpp", "hxx", "h\\+\\+", "m", "mm",  "pch", // C and C++
            "c#", "cs", "csharp",  // C#
            "go", "goc",  // GO
            "js", // JavaScript
            "pl", "plx", "pm", "ph", "cgi", "fcgi", "pod", "psgi", "al", "perl", "t", // PERL
            "pl6", "p6m", "p6l", "pm6", "nqp", "6pl", "6pm", "p6", // PERL6
            "php", // PHP
            "py", // Python
            "rb", // Ruby
            "swift", // Swift
            "java", // Java
            "clj", "cljx", "cljs", "cljc"); // Clojure
    public static final List BINARY_EXTENSIONS = Arrays.asList("jar", "egg", "tar.gz", "tgz", "zip", "whl", "gem",
            "apk", "air", "dmg", "exe", "gem", "gzip", "msi", "nupkg", "swc", "swf", "tar.bz2", "pkg.tar.xz", "(u)?deb", "(a)?rpm");
    public static final List ARCHIVE_EXTENSIONS = Arrays.asList("war", "ear", "zip", "whl", "tar.gz", "tgz", "tar", "car");

    public static final String SOURCE_FILE_PATTERN;
    public static final String BINARY_FILE_PATTERN;
    public static final String ARCHIVE_FILE_PATTERN;
    public static final String[] INCLUDES;
    public static final String[] EXCLUDES = new String[] { "**/*sources.jar", "**/*javadoc.jar", "**/tests/**" };
    public static final String[] ARCHIVE_INCLUDES;
    public static final String[] ARCHIVE_EXCLUDES = new String[] { "**/*sources.jar", "**/*javadoc.jar", "**/tests/**" };

    static {
        SOURCE_FILE_PATTERN = initializeRegexPattern(SOURCE_EXTENSIONS);
        BINARY_FILE_PATTERN = initializeRegexPattern(BINARY_EXTENSIONS);
        ARCHIVE_FILE_PATTERN = initializeRegexPattern(ARCHIVE_EXTENSIONS);
        List allExtensions = new ArrayList<>();
        allExtensions.addAll(SOURCE_EXTENSIONS);
        allExtensions.addAll(BINARY_EXTENSIONS);
        INCLUDES = initializeGlobPattern(allExtensions);
        ARCHIVE_INCLUDES = initializeGlobPattern(ARCHIVE_EXTENSIONS);
    }

    private static String initializeRegexPattern(List extensions) {
        StringBuilder sb = new StringBuilder();
        for (String extension : extensions) {
            sb.append(Constants.REGEX_PATTERN_PREFIX);
            sb.append(extension);
            sb.append(Constants.PIPE);
        }
        return sb.toString().substring(0, sb.toString().lastIndexOf(Constants.PIPE));
    }

    private static String[] initializeGlobPattern(List extensions) {
        String[] globPatterns = new String[extensions.size()];
        for (int i = 0; i < extensions.size(); i++) {
            globPatterns[i] = Constants.GLOB_PATTERN_PREFIX + Constants.DOT +  extensions.get(i);
        }
        return globPatterns;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy