Search in sources :

Example 1 with DirectoryScanner

use of org.codehaus.plexus.util.DirectoryScanner in project sass-maven-plugin by Jasig.

the class Resource method getDirectoriesAndDestinations.

public Map<String, String> getDirectoriesAndDestinations() {
    final File sourceDirectory = new File(source.getDirectory());
    // Scan for directories
    final DirectoryScanner scanner = new DirectoryScanner();
    scanner.setBasedir(sourceDirectory);
    scanner.setIncludes(source.getIncludes().toArray(new String[source.getIncludes().size()]));
    scanner.setExcludes(source.getExcludes().toArray(new String[source.getExcludes().size()]));
    // Add default excludes to the list of excludes (see http://plexus.codehaus.org/plexus-utils/apidocs/org/codehaus/plexus/util/AbstractScanner.html#DEFAULTEXCLUDES
    // or http://plexus.codehaus.org/plexus-utils/apidocs/org/codehaus/plexus/util/AbstractScanner.html#addDefaultExcludes() )
    scanner.addDefaultExcludes();
    scanner.scan();
    final Map<String, String> result = new LinkedHashMap<String, String>();
    result.put(FilenameUtils.separatorsToUnix(sourceDirectory.toString()), FilenameUtils.separatorsToUnix(destination.toString()));
    for (String included : scanner.getIncludedDirectories()) {
        if (!included.isEmpty()) {
            final String subdir = StringUtils.difference(sourceDirectory.toString(), included);
            final File sourceDir = new File(sourceDirectory, included);
            File destDir = new File(this.destination, subdir);
            if (this.relativeOutputDirectory != null && !this.relativeOutputDirectory.isEmpty()) {
                destDir = new File(destDir, this.relativeOutputDirectory);
            }
            result.put(FilenameUtils.separatorsToUnix(sourceDir.toString()), FilenameUtils.separatorsToUnix(destDir.toString()));
        }
    }
    return result;
}
Also used : DirectoryScanner(org.codehaus.plexus.util.DirectoryScanner) File(java.io.File) LinkedHashMap(java.util.LinkedHashMap)

Example 2 with DirectoryScanner

use of org.codehaus.plexus.util.DirectoryScanner in project maven-plugins by apache.

the class AbstractJavadocMojo method fixFrameInjectionBug.

/**
     * Patches the given Javadoc output directory to work around CVE-2013-1571
     * (see http://www.kb.cert.org/vuls/id/225657).
     *
     * @param javadocOutputDirectory directory to scan for vulnerabilities
     * @param outputEncoding         encoding used by the javadoc tool (-docencoding parameter).
     *                               If {@code null}, the platform's default encoding is used (like javadoc does).
     * @return the number of patched files
     */
private int fixFrameInjectionBug(File javadocOutputDirectory, String outputEncoding) throws IOException {
    final String fixData;
    InputStream in = null;
    try {
        in = this.getClass().getResourceAsStream("frame-injection-fix.txt");
        if (in == null) {
            throw new FileNotFoundException("Missing resource 'frame-injection-fix.txt' in classpath.");
        }
        fixData = StringUtils.unifyLineSeparators(IOUtil.toString(in, "US-ASCII")).trim();
        in.close();
        in = null;
    } finally {
        IOUtil.close(in);
    }
    final DirectoryScanner ds = new DirectoryScanner();
    ds.setBasedir(javadocOutputDirectory);
    ds.setCaseSensitive(false);
    ds.setIncludes(new String[] { "**/index.html", "**/index.htm", "**/toc.html", "**/toc.htm" });
    ds.addDefaultExcludes();
    ds.scan();
    int patched = 0;
    for (String f : ds.getIncludedFiles()) {
        final File file = new File(javadocOutputDirectory, f);
        // we load the whole file as one String (toc/index files are
        // generally small, because they only contain frameset declaration):
        final String fileContents = FileUtils.fileRead(file, outputEncoding);
        // check if file may be vulnerable because it was not patched with "validURL(url)":
        if (!StringUtils.contains(fileContents, "function validURL(url) {")) {
            // we need to patch the file!
            final String patchedFileContents = StringUtils.replaceOnce(fileContents, "function loadFrames() {", fixData);
            if (!patchedFileContents.equals(fileContents)) {
                FileUtils.fileWrite(file, outputEncoding, patchedFileContents);
                patched++;
            }
        }
    }
    return patched;
}
Also used : InputStream(java.io.InputStream) DirectoryScanner(org.codehaus.plexus.util.DirectoryScanner) FileNotFoundException(java.io.FileNotFoundException) File(java.io.File)

Example 3 with DirectoryScanner

use of org.codehaus.plexus.util.DirectoryScanner in project felix by apache.

the class BundlePlugin method addLocalPackages.

private static void addLocalPackages(File outputDirectory, Analyzer analyzer) throws IOException {
    Packages packages = new Packages();
    if (outputDirectory != null && outputDirectory.isDirectory()) {
        // scan classes directory for potential packages
        DirectoryScanner scanner = new DirectoryScanner();
        scanner.setBasedir(outputDirectory);
        scanner.setIncludes(new String[] { "**/*.class" });
        scanner.addDefaultExcludes();
        scanner.scan();
        String[] paths = scanner.getIncludedFiles();
        for (int i = 0; i < paths.length; i++) {
            packages.put(analyzer.getPackageRef(getPackageName(paths[i])));
        }
    }
    Packages exportedPkgs = new Packages();
    Packages privatePkgs = new Packages();
    boolean noprivatePackages = "!*".equals(analyzer.getProperty(Analyzer.PRIVATE_PACKAGE));
    for (PackageRef pkg : packages.keySet()) {
        // mark all source packages as private by default (can be overridden by export list)
        privatePkgs.put(pkg);
        // we can't export the default package (".") and we shouldn't export internal packages
        String fqn = pkg.getFQN();
        if (noprivatePackages || !(".".equals(fqn) || fqn.contains(".internal") || fqn.contains(".impl"))) {
            exportedPkgs.put(pkg);
        }
    }
    Properties properties = analyzer.getProperties();
    String exported = properties.getProperty(Analyzer.EXPORT_PACKAGE);
    if (exported == null) {
        if (!properties.containsKey(Analyzer.EXPORT_CONTENTS)) {
            // no -exportcontents overriding the exports, so use our computed list
            for (Attrs attrs : exportedPkgs.values()) {
                attrs.put(Constants.SPLIT_PACKAGE_DIRECTIVE, "merge-first");
            }
            properties.setProperty(Analyzer.EXPORT_PACKAGE, Processor.printClauses(exportedPkgs));
        } else {
            // leave Export-Package empty (but non-null) as we have -exportcontents
            properties.setProperty(Analyzer.EXPORT_PACKAGE, "");
        }
    } else if (exported.indexOf(LOCAL_PACKAGES) >= 0) {
        String newExported = StringUtils.replace(exported, LOCAL_PACKAGES, Processor.printClauses(exportedPkgs));
        properties.setProperty(Analyzer.EXPORT_PACKAGE, newExported);
    }
    String internal = properties.getProperty(Analyzer.PRIVATE_PACKAGE);
    if (internal == null) {
        if (!privatePkgs.isEmpty()) {
            for (Attrs attrs : privatePkgs.values()) {
                attrs.put(Constants.SPLIT_PACKAGE_DIRECTIVE, "merge-first");
            }
            properties.setProperty(Analyzer.PRIVATE_PACKAGE, Processor.printClauses(privatePkgs));
        } else {
            // if there are really no private packages then use "!*" as this will keep the Bnd Tool happy
            properties.setProperty(Analyzer.PRIVATE_PACKAGE, "!*");
        }
    } else if (internal.indexOf(LOCAL_PACKAGES) >= 0) {
        String newInternal = StringUtils.replace(internal, LOCAL_PACKAGES, Processor.printClauses(privatePkgs));
        properties.setProperty(Analyzer.PRIVATE_PACKAGE, newInternal);
    }
}
Also used : Packages(aQute.bnd.osgi.Packages) DirectoryScanner(org.codehaus.plexus.util.DirectoryScanner) Attrs(aQute.bnd.header.Attrs) PackageRef(aQute.bnd.osgi.Descriptors.PackageRef) Properties(java.util.Properties)

Example 4 with DirectoryScanner

use of org.codehaus.plexus.util.DirectoryScanner in project felix by apache.

the class MavenProjectScanner method getSources.

/**
 * Return all sources.
 */
public Collection<Source> getSources() {
    if (scanClasses) {
        final ArrayList<Source> files = new ArrayList<Source>();
        final DirectoryScanner scanner = new DirectoryScanner();
        scanner.setBasedir(this.project.getBuild().getOutputDirectory());
        if (this.includeString != null) {
            scanner.setIncludes(this.includeString.split(","));
        } else {
            scanner.setIncludes(new String[] { "**/*.class" });
        }
        if (this.excludeString != null) {
            scanner.setExcludes(this.excludeString.split(","));
        }
        scanner.addDefaultExcludes();
        scanner.scan();
        for (final String fileName : scanner.getIncludedFiles()) {
            files.add(new Source() {

                public File getFile() {
                    return new File(project.getBuild().getOutputDirectory(), fileName);
                }

                public String getClassName() {
                    // remove ".class"
                    String name = fileName.substring(0, fileName.length() - 6);
                    return name.replace(File.separatorChar, '/').replace('/', '.');
                }
            });
        }
        return files;
    }
    return getSourcesForScanKind(ScanKind.ADDED_OR_UPDATED);
}
Also used : DirectoryScanner(org.codehaus.plexus.util.DirectoryScanner) ArrayList(java.util.ArrayList) File(java.io.File) Source(org.apache.felix.scrplugin.Source)

Example 5 with DirectoryScanner

use of org.codehaus.plexus.util.DirectoryScanner in project intellij-plugins by JetBrains.

the class LocalPackageCollector method addLocalPackages.

private static void addLocalPackages(File outputDirectory, Analyzer analyzer) {
    Collection<String> packages = new LinkedHashSet<String>();
    if (outputDirectory != null && outputDirectory.isDirectory()) {
        // scan classes directory for potential packages
        DirectoryScanner scanner = new DirectoryScanner();
        scanner.setBasedir(outputDirectory);
        scanner.setIncludes(new String[] { "**/*.class" });
        scanner.addDefaultExcludes();
        scanner.scan();
        String[] paths = scanner.getIncludedFiles();
        for (String path : paths) {
            packages.add(getPackageName(path));
        }
    }
    StringBuilder exportedPackages = new StringBuilder();
    StringBuilder privatePackages = new StringBuilder();
    boolean noPrivatePackages = "!*".equals(analyzer.getProperty(Constants.PRIVATE_PACKAGE));
    for (Object aPackage : packages) {
        String pkg = (String) aPackage;
        // mark all source packages as private by default (can be overridden by export list)
        if (privatePackages.length() > 0) {
            privatePackages.append(';');
        }
        privatePackages.append(pkg);
        // we can't export the default package (".") and we shouldn't export internal packages
        if (noPrivatePackages || !(".".equals(pkg) || pkg.contains(".internal") || pkg.contains(".impl"))) {
            if (exportedPackages.length() > 0) {
                exportedPackages.append(';');
            }
            exportedPackages.append(pkg);
        }
    }
    if (analyzer.getProperty(Constants.EXPORT_PACKAGE) == null) {
        if (analyzer.getProperty(Constants.EXPORT_CONTENTS) == null) {
            // no -exportcontents overriding the exports, so use our computed list
            analyzer.setProperty(Constants.EXPORT_PACKAGE, exportedPackages + ";-split-package:=merge-first");
        } else {
            // leave Export-Package empty (but non-null) as we have -exportcontents
            analyzer.setProperty(Constants.EXPORT_PACKAGE, "");
        }
    } else {
        String exported = analyzer.getProperty(Constants.EXPORT_PACKAGE);
        if (exported.contains(LOCAL_PACKAGES)) {
            String newExported = StringUtil.replace(exported, LOCAL_PACKAGES, exportedPackages.toString());
            analyzer.setProperty(Constants.EXPORT_PACKAGE, newExported);
        }
    }
    String internal = analyzer.getProperty(Constants.PRIVATE_PACKAGE);
    if (internal == null) {
        if (privatePackages.length() > 0) {
            analyzer.setProperty(Constants.PRIVATE_PACKAGE, privatePackages + ";-split-package:=merge-first");
        } else {
            // if there are really no private packages then use "!*" as this will keep the Bnd Tool happy
            analyzer.setProperty(Constants.PRIVATE_PACKAGE, "!*");
        }
    } else if (internal.contains(LOCAL_PACKAGES)) {
        String newInternal = StringUtil.replace(internal, LOCAL_PACKAGES, privatePackages.toString());
        analyzer.setProperty(Constants.PRIVATE_PACKAGE, newInternal);
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) DirectoryScanner(org.codehaus.plexus.util.DirectoryScanner)

Aggregations

DirectoryScanner (org.codehaus.plexus.util.DirectoryScanner)46 File (java.io.File)31 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)12 IOException (java.io.IOException)9 ArrayList (java.util.ArrayList)7 FileInputStream (java.io.FileInputStream)4 FileOutputStream (java.io.FileOutputStream)4 InputStream (java.io.InputStream)4 Path (java.nio.file.Path)4 LinkedHashSet (java.util.LinkedHashSet)4 HashSet (java.util.HashSet)3 BufferedInputStream (java.io.BufferedInputStream)2 BufferedOutputStream (java.io.BufferedOutputStream)2 FileNotFoundException (java.io.FileNotFoundException)2 OutputStream (java.io.OutputStream)2 UncheckedIOException (java.io.UncheckedIOException)2 Arrays (java.util.Arrays)2 LinkedHashMap (java.util.LinkedHashMap)2 List (java.util.List)2 Properties (java.util.Properties)2