Java Code Examples for org.apache.tools.ant.DirectoryScanner#setBasedir()

The following examples show how to use org.apache.tools.ant.DirectoryScanner#setBasedir() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: OperatorDiscoveryTest.java    From attic-apex-core with Apache License 2.0 6 votes vote down vote up
public static String[] getClassFileInClasspath()
{
  String classpath = System.getProperty("java.class.path");
  String[] paths = classpath.split(":");
  List<String> fnames = new LinkedList<>();
  for (String cp : paths) {
    File f = new File(cp);
    if (!f.isDirectory()) {
      continue;
    }
    DirectoryScanner ds = new DirectoryScanner();
    ds.setBasedir(f);
    ds.setIncludes(new String[]{"**\\*.class"});
    ds.scan();
    for (String name : ds.getIncludedFiles()) {
      fnames.add(new File(f, name).getAbsolutePath());
    }

  }
  return fnames.toArray(new String[]{});
}
 
Example 2
Source File: AntFile.java    From antiplag with Apache License 2.0 6 votes vote down vote up
public static String[] scanFiles(File f,String[] filter){
	String[] includeFiles = null;
	try {
		DirectoryScanner ds=new DirectoryScanner(); 
		
		ds.setBasedir(f); 
		ds.setIncludes(filter);  //**/*.java
		ds.scan(); 
		if(ds.getIncludedFilesCount()>0) { 	 
		 includeFiles=ds.getIncludedFiles(); 
		}
	} catch (IllegalStateException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	return includeFiles;
	 
}
 
Example 3
Source File: WildcardScanner.java    From gocd with Apache License 2.0 6 votes vote down vote up
public File[] getFiles() {
    DirectoryScanner scanner = new DirectoryScanner();
    scanner.setBasedir(rootPath);
    scanner.setIncludes(new String[]{pattern});
    scanner.scan();
    String[] allPaths = scanner.getIncludedFiles();
    List<File> allFiles = new ArrayList<>();
    String[] directories = scanner.getIncludedDirectories();
    for (String directory : directories) {
        allFiles.add(new File(rootPath, directory));
    }

    for (int i = 0; i < allPaths.length; i++) {
        File file = new File(rootPath, allPaths[i]);
        if (!allFiles.contains(file.getParentFile())) {
            allFiles.add(file);
        }
    }
    return allFiles.toArray(new File[]{});
}
 
Example 4
Source File: FileUtil.java    From ambari-logsearch with Apache License 2.0 5 votes vote down vote up
public static File[] getInputFilesByPattern(String searchPath) {
  File searchFile = new File(searchPath);
  if (searchFile.isFile()) {
    return new File[]{searchFile};
  } else {
    if (searchPath.contains("*")) {
      try {
        String folderBeforeRegex = getLogDirNameBeforeWildCard(searchPath);
        String fileNameAfterLastFolder = searchPath.substring(folderBeforeRegex.length());

        DirectoryScanner scanner = new DirectoryScanner();
        scanner.setIncludes(new String[]{fileNameAfterLastFolder});
        scanner.setBasedir(folderBeforeRegex);
        scanner.setCaseSensitive(true);
        scanner.scan();
        String[] fileNames = scanner.getIncludedFiles();

        if (fileNames != null && fileNames.length > 0) {
          File[] files = new File[fileNames.length];
          for (int i = 0; i < fileNames.length; i++) {
            files[i] = new File(folderBeforeRegex + fileNames[i]);
          }
          return files;
        }
      } catch (Exception e) {
        fileMonitorLogger.info("Input file was not found by pattern (exception thrown); {}, message: {}", searchPath, e.getMessage());
      }

    } else {
      fileMonitorLogger.debug("Input file config was not found by pattern; {}", searchPath);
    }
    return new File[]{};
  }
}
 
Example 5
Source File: PatternSet.java    From onedev with MIT License 5 votes vote down vote up
public Collection<File> listFiles(File dir) {
   	Collection<File> files = new ArrayList<File>();
   	
   	DirectoryScanner scanner = new DirectoryScanner();
   	scanner.setBasedir(dir);
   	scanner.setIncludes(includes.toArray(new String[0]));
   	scanner.setExcludes(excludes.toArray(new String[0]));
   	scanner.scan();
   	
   	for (String path: scanner.getIncludedFiles()) 
   		files.add(new File(dir, path));
   	
	return files;
}
 
Example 6
Source File: CodeGenerator.java    From avro-util with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private void findAllIncludedFiles(Map<File, File> fileToParent, Set<File> sharedSchemas, Set<File> nonSharedSchemas) {
    String[] includesArray = includes.toArray(new String[0]);

    for (File input : inputs) {
        if (!input.exists()) {
            LOG.warn("path {} does not exist", input);
        }

        if (input.isFile()) {
            //individual files are always considered included, so no test against includes
            includeFile(input, input, fileToParent, sharedSchemas, nonSharedSchemas);
        } else if (input.isDirectory()) {
            DirectoryScanner scanner = new DirectoryScanner();
            scanner.setBasedir(input);
            scanner.setIncludes(includesArray);
            scanner.scan();
            //results are paths relative (==under) input
            String[] includedFiles = scanner.getIncludedFiles();
            for (String included : includedFiles) {
                File file = new File(input, included);
                includeFile(file, input, fileToParent, sharedSchemas, nonSharedSchemas);
            }
        } else {
            LOG.warn("path {} is neither a file nor a directory", input);
        }
    }
}
 
Example 7
Source File: AntDirectoryScanner.java    From gocd-yaml-config-plugin with Apache License 2.0 5 votes vote down vote up
@Override
public String[] getFilesMatchingPattern(File directory, String pattern) {
    DirectoryScanner scanner = new DirectoryScanner();
    scanner.setBasedir(directory);
    scanner.setIncludes(pattern.trim().split(" *, *"));
    scanner.scan();
    return scanner.getIncludedFiles();
}
 
Example 8
Source File: Files.java    From development with Apache License 2.0 5 votes vote down vote up
public Collection<IFile> getFiles() {
    final DirectoryScanner ds = new DirectoryScanner();
    ds.setBasedir(basedir);
    ds.setIncludes(new String[] { pattern });
    ds.addDefaultExcludes();
    ds.scan();
    final String[] filenames = ds.getIncludedFiles();
    final Collection<IFile> files = new ArrayList<IFile>(
            filenames.length);
    for (final String name : filenames) {
        final String normName = name.replace(File.separatorChar, '/');
        files.add(new PhysicalFile(normName, new File(basedir, name)));
    }
    return files;
}
 
Example 9
Source File: Helper.java    From bootstraped-multi-test-results-report with MIT License 5 votes vote down vote up
public static String[] findFiles(File targetDirectory, String fileIncludePattern, String fileExcludePattern,
    final String DEFAULT_FILE_INCLUDE_PATTERN) {
    DirectoryScanner scanner = new DirectoryScanner();
    if (fileIncludePattern == null || fileIncludePattern.isEmpty()) {
        scanner.setIncludes(new String[] {DEFAULT_FILE_INCLUDE_PATTERN});
    } else {
        scanner.setIncludes(new String[] {fileIncludePattern});
    }
    if (fileExcludePattern != null) {
        scanner.setExcludes(new String[] {fileExcludePattern});
    }
    scanner.setBasedir(targetDirectory);
    scanner.scan();
    return scanner.getIncludedFiles();
}
 
Example 10
Source File: PublishExecutor.java    From gocd-s3-artifacts with Apache License 2.0 5 votes vote down vote up
protected String[] parseSourcePath(String source, String workingDir) {
    DirectoryScanner directoryScanner = new DirectoryScanner();
    directoryScanner.setBasedir(workingDir);
    directoryScanner.setIncludes(new String[]{source});
    directoryScanner.scan();
    return ArrayUtils.addAll(directoryScanner.getIncludedFiles(), directoryScanner.getIncludedDirectories());
}
 
Example 11
Source File: FileSelector.java    From maven-replacer-plugin with MIT License 5 votes vote down vote up
public List<String> listIncludes(String basedir, List<String> includes, List<String> excludes) {
	if (includes == null || includes.isEmpty()) {
		return Collections.emptyList();
	}

	DirectoryScanner directoryScanner = new DirectoryScanner();
	directoryScanner.addDefaultExcludes();
	directoryScanner.setBasedir(new File(basedir));
	directoryScanner.setIncludes(stringListToArray(includes));
	directoryScanner.setExcludes(stringListToArray(excludes));

	directoryScanner.scan();
	return Arrays.asList(directoryScanner.getIncludedFiles());
}
 
Example 12
Source File: PublishExecutor.java    From gocd-s3-artifacts with Apache License 2.0 5 votes vote down vote up
protected String[] parseSourcePath(String source, String workingDir) {
    DirectoryScanner directoryScanner = new DirectoryScanner();
    directoryScanner.setBasedir(workingDir);
    directoryScanner.setIncludes(new String[]{source});
    directoryScanner.scan();
    return ArrayUtils.addAll(directoryScanner.getIncludedFiles(), directoryScanner.getIncludedDirectories());
}
 
Example 13
Source File: CompleterHelper.java    From yiistorm with MIT License 5 votes vote down vote up
public static String[] searchFiles(String searchPath, String searchString) {

        DirectoryScanner scanner = new DirectoryScanner();
        scanner.setIncludes(new String[]{searchString + "*.php"});

        scanner.setBasedir(searchPath);
        scanner.setCaseSensitive(false);
        scanner.scan();
        return scanner.getIncludedFiles();

    }
 
Example 14
Source File: CompleterHelper.java    From yiistorm with MIT License 5 votes vote down vote up
public static String[] searchFolders(String searchPath, String searchString) {
    DirectoryScanner scanner = new DirectoryScanner();
    scanner.setIncludes(new String[]{searchString + "*"});
    scanner.setExcludes(new String[]{searchString + "*.php"});

    scanner.setBasedir(searchPath);
    scanner.setCaseSensitive(false);
    scanner.scan();
    return scanner.getIncludedDirectories();
}
 
Example 15
Source File: CopyIcons.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private void processProjectDir(File f) {
    String prjPath = null;
    //if (f.getAbsolutePath().equals(baseDir.getAbsolutePath())) {
    //    prjPath = "";
    //} else {
        prjPath = f.getAbsolutePath().substring(baseDir.getAbsolutePath().length() + 1);
    //}
    log("Processing project dir: " + prjPath);
    DirectoryScanner ds = new DirectoryScanner();
    ds.setBasedir(f);
    ds.setIncludes(getAsArray(iconIncludes));
    ds.setExcludes(getAsArray(iconExcludes));
    ds.setCaseSensitive(false);
    ds.scan();
    String[] files = ds.getIncludedFiles();
    log("    Found " + files.length + " files in " + f);
    List<ImageInfo> goodIcons = new ArrayList<>();
    List<ImageInfo> badIcons = new ArrayList<>();
    for (String file : files) {
        String ext = file.substring(file.lastIndexOf('.') + 1);
        if (ext.equalsIgnoreCase("gif") || ext.equalsIgnoreCase("png")) {
            File iconFile = new File(f, file);
            try {
                ImageInfo imageInfo = null;
                imageInfo = readImageInfo(iconFile);
                if (imageInfo != null) {
                    imageInfo.setPath(file);
                    imageInfo.setExt(ext);
                    int w = imageInfo.getWidth();
                    int h = imageInfo.getHeight();
                    if ((w == 8 && h == 8) || (w == 16 && h == 16) ||
                        (w == 24 && h == 24) || (w == 32 && h == 32)) {
                        goodIcons.add(imageInfo);
                    } else {
                        badIcons.add(imageInfo);
                    }
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }
    ProjectIconInfo prjIconInfo = new ProjectIconInfo(prjPath, goodIcons, badIcons);
    prjIconInfoList.add(prjIconInfo);
}
 
Example 16
Source File: Branding.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private void packBrandingJar(File srcDir, File destJarBase, String locale) throws IOException {
    DirectoryScanner scanner = new DirectoryScanner();
    scanner.setBasedir(srcDir);
    String localeToken = "";
    if(locale != null) {
        String [] includes = {"**/*_" + locale.toString() + ".*"};
        scanner.setIncludes(includes);
        localeToken = "_" + locale.toString();
    } else {
        String [] excludes = {"**/*_??_??.*", "**/*_??.*"};
        scanner.setExcludes(excludes);
    }
    scanner.addDefaultExcludes(); // #68929
    scanner.scan();
    String[] files = scanner.getIncludedFiles();
    if(files.length > 0) {
        Jar zip = (Jar) getProject().createTask("jar");
        String name = destJarBase.getName();
        String nameBase = name.substring(0, name.length() - ".jar".length());
        File destFolder = new File(destJarBase.getParentFile(), "locale");
        if (!destFolder.isDirectory() && !destFolder.mkdirs()) {
            throw new IOException("Could not create directory " + destFolder);
        }
        File destJar = new File(destFolder, nameBase + "_" + token + localeToken + ".jar");
        zip.setDestFile(destJar);
        zip.setCompress(true);
        for (int i = 0; i < files.length; i++) {
            ZipFileSet entry = new ZipFileSet();
            entry.setFile(new File(srcDir, files[i]));
            String basePath = files[i].replace(File.separatorChar, '/');
            int slash = basePath.lastIndexOf('/');
            int dot = basePath.lastIndexOf('.');
            String infix = "_" + token + localeToken;
            String brandedPath;
            if (dot == -1 || dot < slash) {
                brandedPath = basePath + infix;
            } else {
                brandedPath = basePath.substring(0, dot - localeToken.length()) + infix + basePath.substring(dot);
            }
            entry.setFullpath(brandedPath);
            zip.addZipfileset(entry);
        }
        zip.setLocation(getLocation());
        zip.init();
        zip.execute();
    }
}