Search in sources :

Example 31 with DirectoryScanner

use of org.codehaus.plexus.util.DirectoryScanner in project ph-schematron by phax.

the class SchematronValidationMojo method _performValidation.

private void _performValidation(@Nonnull final ISchematronResource aSch, @Nonnull final File aXMLDirectory, @Nullable final String sXMLIncludes, @Nullable final String sXMLExcludes, @Nullable final File aSVRLDirectory, final boolean bExpectSuccess) throws MojoExecutionException, MojoFailureException {
    final DirectoryScanner aScanner = new DirectoryScanner();
    aScanner.setBasedir(aXMLDirectory);
    if (StringHelper.hasText(sXMLIncludes))
        aScanner.setIncludes(new String[] { sXMLIncludes });
    if (StringHelper.hasText(sXMLExcludes))
        aScanner.setExcludes(new String[] { sXMLExcludes });
    aScanner.setCaseSensitive(true);
    aScanner.scan();
    final String[] aXMLFilenames = aScanner.getIncludedFiles();
    if (aXMLFilenames != null) {
        for (final String sXMLFilename : aXMLFilenames) {
            final File aXMLFile = new File(aXMLDirectory, sXMLFilename);
            // Validate XML file
            getLog().info("Validating XML file '" + aXMLFile.getPath() + "' against Schematron rules from '" + m_aSchematronFile + "' expecting " + (bExpectSuccess ? "success" : "failure"));
            try {
                final SchematronOutputType aSOT = aSch.applySchematronValidationToSVRL(TransformSourceFactory.create(aXMLFile));
                if (aSVRLDirectory != null) {
                    // Save SVRL
                    final File aSVRLFile = new File(aSVRLDirectory, sXMLFilename + ".svrl");
                    if (!aSVRLFile.getParentFile().mkdirs())
                        getLog().error("Failed to create parent directory of '" + aSVRLFile.getAbsolutePath() + "'!");
                    if (new SVRLMarshaller().write(aSOT, aSVRLFile).isSuccess())
                        getLog().info("Successfully saved SVRL file '" + aSVRLFile.getPath() + "'");
                    else
                        getLog().error("Error saving SVRL file '" + aSVRLFile.getPath() + "'");
                }
                final ICommonsList<SVRLFailedAssert> aFailedAsserts = SVRLHelper.getAllFailedAssertions(aSOT);
                if (bExpectSuccess) {
                    // No failed assertions expected
                    if (aFailedAsserts.isNotEmpty()) {
                        final String sMessage = aFailedAsserts.size() + " failed Schematron assertions for XML file '" + aXMLFile.getPath() + "'";
                        getLog().error(sMessage);
                        aFailedAsserts.forEach(x -> getLog().error(x.getAsResourceError(aXMLFile.getPath()).getAsString(Locale.US)));
                        throw new MojoFailureException(sMessage);
                    }
                } else {
                    // At least one failed assertions expected
                    if (aFailedAsserts.isEmpty()) {
                        final String sMessage = "No failed Schematron assertions for erroneous XML file '" + aXMLFile.getPath() + "'";
                        getLog().error(sMessage);
                        throw new MojoFailureException(sMessage);
                    }
                }
            } catch (final MojoFailureException | MojoExecutionException up) {
                throw up;
            } catch (final Exception ex) {
                final String sMessage = "Exception validating XML '" + aXMLFile.getPath() + "' against Schematron rules from '" + m_aSchematronFile + "'";
                getLog().error(sMessage, ex);
                throw new MojoExecutionException(sMessage, ex);
            }
        }
    }
}
Also used : SchematronOutputType(org.oclc.purl.dsdl.svrl.SchematronOutputType) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) SVRLFailedAssert(com.helger.schematron.svrl.SVRLFailedAssert) DirectoryScanner(org.codehaus.plexus.util.DirectoryScanner) MojoFailureException(org.apache.maven.plugin.MojoFailureException) SVRLMarshaller(com.helger.schematron.svrl.SVRLMarshaller) File(java.io.File) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MojoFailureException(org.apache.maven.plugin.MojoFailureException)

Example 32 with DirectoryScanner

use of org.codehaus.plexus.util.DirectoryScanner in project st-js by st-js.

the class AbstractSTJSMojo method accumulatePackages.

/**
 * @return the list of Java source files to processed (those which are older than the corresponding Javascript
 *         file). The returned files are relative to the given source directory.
 */
private Collection<String> accumulatePackages(File sourceDir) throws MojoExecutionException {
    final Collection<String> result = new HashSet<String>();
    if (sourceDir == null || !sourceDir.exists()) {
        return result;
    }
    DirectoryScanner ds = new DirectoryScanner();
    ds.setFollowSymlinks(true);
    ds.addDefaultExcludes();
    ds.setBasedir(sourceDir);
    ds.setIncludes(new String[] { "**/*.java" });
    ds.scan();
    for (String fileName : ds.getIncludedFiles()) {
        File file = new File(fileName);
        // Supports classes without packages
        result.add(file.getParent() == null ? "" : file.getParent().replace(File.separatorChar, '.'));
    }
    /*
		 * // Trim root path from file paths for (File file : staleFiles) { String filePath = file.getPath(); String
		 * basePath = sourceDir.getAbsoluteFile().toString(); result.add(new File(filePath.substring(basePath.length() +
		 * 1))); }
		 */
    return result;
}
Also used : DirectoryScanner(org.codehaus.plexus.util.DirectoryScanner) File(java.io.File) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 33 with DirectoryScanner

use of org.codehaus.plexus.util.DirectoryScanner in project scala-maven-plugin by davidB.

the class MainHelper method findFiles.

public static String[] findFiles(File dir, String[] includes, String[] excludes) {
    DirectoryScanner scanner = new DirectoryScanner();
    scanner.setBasedir(dir);
    scanner.setIncludes(includes);
    scanner.setExcludes(excludes);
    scanner.addDefaultExcludes();
    scanner.scan();
    return scanner.getIncludedFiles();
}
Also used : DirectoryScanner(org.codehaus.plexus.util.DirectoryScanner)

Example 34 with DirectoryScanner

use of org.codehaus.plexus.util.DirectoryScanner in project evosuite by EvoSuite.

the class FileUtils method scan.

/**
 * Scans a single directory
 *
 * @param root Directory to scan
 * @param includes
 * @param excludes
 * @return
 */
public static List<File> scan(File root, String[] includes, String[] excludes) {
    List<File> files = new ArrayList<File>();
    if (!root.exists()) {
        return files;
    }
    final DirectoryScanner directoryScanner = new DirectoryScanner();
    directoryScanner.setIncludes(includes);
    directoryScanner.setExcludes(excludes);
    directoryScanner.setBasedir(root);
    directoryScanner.scan();
    for (final String fileName : directoryScanner.getIncludedFiles()) {
        files.add(new File(root, fileName));
    }
    return files;
}
Also used : DirectoryScanner(org.codehaus.plexus.util.DirectoryScanner) ArrayList(java.util.ArrayList) File(java.io.File)

Example 35 with DirectoryScanner

use of org.codehaus.plexus.util.DirectoryScanner in project sts4 by spring-projects.

the class MavenProjectClasspath method resolveClasspathResources.

private ImmutableList<String> resolveClasspathResources(MavenProject project) {
    if (project == null) {
        return ImmutableList.of();
    }
    return ImmutableList.copyOf(project.getBuild().getResources().stream().filter(resource -> new File(resource.getDirectory()).exists()).flatMap(resource -> {
        DirectoryScanner scanner = new DirectoryScanner();
        scanner.setBasedir(resource.getDirectory());
        if (resource.getIncludes() != null && !resource.getIncludes().isEmpty()) {
            scanner.setIncludes(resource.getIncludes().toArray(new String[resource.getIncludes().size()]));
        }
        if (resource.getExcludes() != null && !resource.getExcludes().isEmpty()) {
            scanner.setExcludes(resource.getExcludes().toArray(new String[resource.getExcludes().size()]));
        }
        scanner.setCaseSensitive(false);
        scanner.scan();
        return Arrays.stream(scanner.getIncludedFiles());
    }).toArray(String[]::new));
}
Also used : Arrays(java.util.Arrays) HtmlJavadocProvider(org.springframework.ide.vscode.commons.javadoc.HtmlJavadocProvider) URL(java.net.URL) IJavadocProvider(org.springframework.ide.vscode.commons.java.IJavadocProvider) JandexClasspath(org.springframework.ide.vscode.commons.jandex.JandexClasspath) ImmutableList(com.google.common.collect.ImmutableList) MavenProject(org.apache.maven.project.MavenProject) ArtifactRepository(org.apache.maven.artifact.repository.ArtifactRepository) Artifact(org.apache.maven.artifact.Artifact) JandexIndex(org.springframework.ide.vscode.commons.jandex.JandexIndex) Objects(com.google.common.base.Objects) Path(java.nio.file.Path) LinkedHashSet(java.util.LinkedHashSet) MavenException(org.springframework.ide.vscode.commons.maven.MavenException) MavenCore(org.springframework.ide.vscode.commons.maven.MavenCore) Log(org.springframework.ide.vscode.commons.util.Log) MalformedURLException(java.net.MalformedURLException) Set(java.util.Set) SourceUrlProviderFromSourceContainer(org.springframework.ide.vscode.commons.javadoc.SourceUrlProviderFromSourceContainer) Collectors(java.util.stream.Collectors) File(java.io.File) List(java.util.List) Stream(java.util.stream.Stream) Optional(java.util.Optional) DirectoryScanner(org.codehaus.plexus.util.DirectoryScanner) Collections(java.util.Collections) ClasspathData(org.springframework.ide.vscode.commons.java.ClasspathData) DirectoryScanner(org.codehaus.plexus.util.DirectoryScanner) File(java.io.File)

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