public boolean accept(File dir, String name) {
   for (FilenameFilter filter : filters) {
     boolean accept = filter.accept(dir, name);
     if (accept) {
       return true;
     }
   }
   return false;
 }
 /** Test the filter. */
 public static void main(String[] args) {
   File d = null;
   FilenameFilter joe = new FilenameFilterByEnding(".dsc");
   if (joe.accept(d, "eric.dsc")) System.out.println("OK");
   else System.out.println("FAIL");
   if (joe.accept(d, ".dscasd")) System.out.println("FAIL");
   else System.out.println("OK");
   if (joe.accept(d, "dsc")) System.out.println("FAIL");
   else System.out.println("OK");
 }
示例#3
0
 /**
  * Get a string representing the script stack of this exception. If optimization is enabled, this
  * corresponds to all java stack elements with a source name matching the <code>filter</code>.
  *
  * @param filter the file name filter to determine whether a file is a script file
  * @return a script stack dump
  * @since 1.6R6
  */
 public String getScriptStackTrace(FilenameFilter filter) {
   List<String> interpreterStack = null;
   Evaluator interpreter = Context.createInterpreter();
   if (interpreter != null) {
     interpreterStack = interpreter.getScriptStack(this);
   }
   int interpreterStackIndex = 0;
   StringBuffer buffer = new StringBuffer();
   String lineSeparator = SecurityUtilities.getSystemProperty("line.separator");
   StackTraceElement[] stack = getStackTrace();
   for (int i = 0; i < stack.length; i++) {
     StackTraceElement e = stack[i];
     String name = e.getFileName();
     if (e.getLineNumber() > -1 && name != null && filter.accept(null, name)) {
       buffer.append("\tat ");
       buffer.append(e.getFileName());
       buffer.append(':');
       buffer.append(e.getLineNumber());
       buffer.append(lineSeparator);
     } else if (interpreterStack != null
         && interpreterStack.size() > interpreterStackIndex
         && "org.mozilla.javascript.Interpreter".equals(e.getClassName())
         && "interpretLoop".equals(e.getMethodName())) {
       buffer.append(interpreterStack.get(interpreterStackIndex++));
     }
   }
   return buffer.toString();
 }
示例#4
0
    @Override
    public List<AbsFile> listFiles(AbsFile dir, FilenameFilter filter) {
      String path = dir.getPath();

      boolean absolute = path.startsWith("/");

      FSTree crt = absolute ? tree.getDir("absolute") : tree.getDir("relative");

      StringTokenizer st = new StringTokenizer(path, "/", false);
      while (st.hasMoreTokens() && crt != null) {
        String token = st.nextToken();

        if (token.equals("..")) {
          crt = crt.getParent();
        } else if (token.equals(".")) {
          // skip
        } else {
          crt = crt.findDir(token);
        }
      }
      if (crt == null) {
        throw new RuntimeException("No such directory: " + path);
      }

      List<AbsFile> l = new ArrayList<AbsFile>();
      for (String name : crt.listFiles()) {
        if (filter.accept(null, name)) {
          l.add(new AbsFile(dir, name));
        }
      }

      return l;
    }
示例#5
0
 /**
  * Checks the filter.
  *
  * @param dir the directory
  * @param name the filename in the directory
  * @return true if the filter matches
  */
 @Override
 public boolean accept(File dir, String name) {
   if (filenameFilter != null) {
     return filenameFilter.accept(dir, name);
   } else {
     return super.accept(dir, name);
   }
 }
 public static URLClassLoader newClassLoader(
     final ClassLoader parentClassLoader, final File file) {
   final Collection<URL> urls = new LinkedHashSet<>();
   if (file.isDirectory()) {
     addJars(urls, file);
   } else if (JAR_FILTER.accept(file.getParentFile(), FileUtil.getFileName(file))) {
     urls.add(FileUtil.toUrl(file));
   }
   return newClassLoader(parentClassLoader, urls);
 }
  public File[] listFiles(File file, FilenameFilter filter) {
    File[] unfiltered = this.listFiles(file);
    ArrayList retList = new ArrayList();
    for (int i = 0; i < unfiltered.length; i++) {
      if (filter.accept(unfiltered[i].getParentFile(), unfiltered[i].getName())) {
        retList.add(unfiltered[i]);
      }
    }

    return (File[]) retList.toArray(new File[0]);
  }
示例#8
0
 private void collectFiles(FilenameFilter filter, String relDirPath, ArrayList<String> entries) {
   File dir = new File(baseDir, relDirPath);
   String[] fileNames = dir.list();
   if (fileNames != null) {
     for (String fileName : fileNames) {
       String relFilePath = relDirPath + fileName;
       File file = new File(baseDir, relFilePath);
       if (file.isDirectory()) {
         if (!filesOnly && filter.accept(dir, fileName)) {
           entries.add(relFilePath);
         }
         if (recursive) {
           collectFiles(filter, relFilePath + '/', entries);
         }
       } else if (filter.accept(dir, fileName)) {
         entries.add(relFilePath);
       }
     }
   }
 }
示例#9
0
  @Override
  public File[] listFiles(FilenameFilter filter) {
    LinkedList<ServerFile> filtered = new LinkedList<ServerFile>();

    for (ServerFile child : children) {
      if (filter.accept(this, child.getName())) {
        filtered.add(child);
      }
    }
    return filtered.toArray(new File[0]);
  }
示例#10
0
  @Override
  public String[] list(java.io.FilenameFilter filter) {

    LinkedList<String> names = new LinkedList<String>();
    for (ServerFile child : children) {
      if (filter == null || filter.accept(this, child.getName())) {
        names.add(child.getName());
      }
    }

    return names.toArray(new String[0]);
  };
 private void visitDirectory(
     File dir, List<File> output, String[] suffixes, FilenameFilter fileNameFilter) {
   for (File child : dir.listFiles()) {
     if (child.isFile()) {
       if (fileNameFilter.accept(dir, child.getName())) {
         output.add(child);
       }
     } else if (child.isDirectory()) {
       visitDirectory(child, output, suffixes, fileNameFilter);
     }
   }
 }
示例#12
0
  public Collection<File> listFiles(File directory, FilenameFilter[] filter, int recurse) {

    Vector<File> files = new Vector<File>();

    File[] entries = directory.listFiles();

    if (entries != null) {
      for (File entry : entries) {
        for (FilenameFilter filefilter : filter) {
          if (filter == null || filefilter.accept(directory, entry.getName())) {
            files.add(entry);
            Log.v("ImageViewFlipper", "Added: " + entry.getName());
          }
        }
        if ((recurse <= -1) || (recurse > 0 && entry.isDirectory())) {
          recurse--;
          files.addAll(listFiles(entry, filter, recurse));
          recurse++;
        }
      }
    }
    return files;
  }
示例#13
0
 /**
  * Helper method to find all files to post compile in a given directory and all its
  * subdirectories.
  *
  * @param classesDir the directory that contains all compiled classes
  * @param filter the filter to apply on the file names, or null
  * @return an array list of file names
  */
 protected ArrayList findFiles(File classesDir, FilenameFilter filter) {
   ArrayList list = new ArrayList();
   String[] files = FileUtil.filterDirectory(classesDir, ".class", true);
   if (filter != null) {
     for (int i = 0; i < files.length; i++) {
       if (filter.accept(classesDir, files[i])) {
         list.add(files[i]);
       }
     }
   } else {
     for (int i = 0; i < files.length; i++) {
       list.add(files[i]);
     }
   }
   return list;
 }
示例#14
0
 public FileTreeReader(File dir, FilenameFilter filter) throws IOException {
   if (filter == null) {
     filter =
         new FilenameFilter() {
           @Override
           public boolean accept(File dir, String name) {
             return true;
           }
         };
   }
   for (File f : FileUtils.listFilesRecursively(dir)) {
     if (filter.accept(f.getParentFile(), f.getName())) {
       files.add(f.getAbsolutePath());
     }
   }
 }
  public static boolean unzip(File zip, File out, FilenameFilter filter) {
    byte[] buffer = new byte[1024];

    try {

      if (!out.exists()) {
        out.mkdir();
      }

      ZipInputStream zis = new ZipInputStream(new FileInputStream(zip));
      ZipEntry ze;

      while ((ze = zis.getNextEntry()) != null) {

        if (!filter.accept(zip, ze.getName())) continue;

        String fileName = ze.getName();
        File newFile = new File(out + File.separator + fileName);

        new File(newFile.getParent()).mkdirs();

        if (fileName.endsWith("/")) continue;

        FileOutputStream fos = new FileOutputStream(newFile);

        int len;

        while ((len = zis.read(buffer)) > 0) {
          fos.write(buffer, 0, len);
        }

        fos.close();
      }

      zis.closeEntry();
      zis.close();

      return true;

    } catch (IOException ex) {
      ex.printStackTrace();
      return false;
    }
  }
  @Override
  public void recursiveDelete(String keyPrefix, FilenameFilter except) throws IOException {

    if (keyPrefix.charAt(keyPrefix.length() - 1) == '/') {
      // Need to delete the dir too, and to list it, can't specify its trailing slash
      keyPrefix = keyPrefix.substring(0, keyPrefix.length() - 1);
    }

    boolean truncated = true;
    String marker = null;

    String bucket = Namespaces.get().getBucket();
    while (truncated) {

      ListObjectsRequest listRequest =
          new ListObjectsRequest(bucket, keyPrefix, marker, null, null);
      ObjectListing listing;
      try {
        listing = s3Client.listObjects(listRequest);
        Collection<S3ObjectSummary> summaries = listing.getObjectSummaries();
        if (summaries.isEmpty()) {
          return;
        }

        List<DeleteObjectsRequest.KeyVersion> keysToDelete =
            Lists.newArrayListWithCapacity(summaries.size());
        for (S3ObjectSummary summary : summaries) {
          String key = summary.getKey();
          if (except == null || !except.accept(null, key)) {
            keysToDelete.add(new DeleteObjectsRequest.KeyVersion(key));
          }
        }

        DeleteObjectsRequest deleteObjectsRequest = new DeleteObjectsRequest(bucket);
        deleteObjectsRequest.setKeys(keysToDelete);
        s3Client.deleteObjects(deleteObjectsRequest);
      } catch (AmazonClientException ace) {
        throw new IOException(ace);
      }

      truncated = listing.isTruncated();
      marker = listing.getNextMarker();
    }
  }
示例#17
0
 /** Locate a file or directory */
 public static File findFile(String startAt, FilenameFilter fnf) {
   File root = new File(startAt);
   String[] files = root.list();
   if (files == null) {
     return null;
   }
   for (String file : files) {
     File f = new File(startAt + File.separator + file);
     if (f.isDirectory()) {
       File r = findFile(f.getAbsolutePath(), fnf);
       if (r != null) {
         return r;
       }
     } else if (fnf.accept(f.getParentFile(), f.getName())) {
       return f;
     }
   }
   return null;
 }
 private void blossomDirectory(File dir) throws IOException {
   // System.out.println(">> blossomDirectory() on " + dir.getPath());
   String canonicalPath = dir.getCanonicalPath();
   String[] listing = (filter == null ? dir.list() : dir.list(filter));
   for (int i = listing.length; --i >= 0; ) {
     // System.out.println(">> listing: " + listing[i]);
     if (filter == null || filter.accept(dir, listing[i])) {
       String name = (canonical ? canonicalPath : dir.getPath()) + File.separator + listing[i];
       File file = new File(name);
       // System.out.println(">> parent: " + dir.getName());
       // System.out.println(">> created file: " + file.getPath());
       if (file.isFile()) files.push(file);
       else // dir
       {
         if (!markedDirex.containsKey(file.getCanonicalPath())) direx.push(file);
       }
     }
   }
   markedDirex.put(canonicalPath, dummy);
 }
  @Test
  @SuppressFBWarnings(value = "NP_NONNULL_PARAM_VIOLATION")
  public void testAll() {
    try {
      // null not allowed
      new FilenameFilterStartsWith(null);
      fail();
    } catch (final NullPointerException ex) {
    }

    final FilenameFilter ff = new FilenameFilterStartsWith("file");
    assertNotNull(ff);
    assertTrue(ff.accept(null, "file.htm"));
    assertTrue(ff.accept(new File("dir"), "file.htm"));
    assertFalse(ff.accept(null, "hello.html"));
    assertFalse(ff.accept(new File("dir"), "hello.html"));
    assertFalse(ff.accept(null, null));
    assertFalse(ff.accept(null, ""));
  }
示例#20
0
 public FileHandle[] list(FilenameFilter filter) {
   if (type == FileType.Classpath)
     throw new RuntimeException("Cannot list a classpath directory: " + file);
   File file = file();
   String[] relativePaths = file.list();
   if (relativePaths == null) return new FileHandle[0];
   FileHandle[] handles = new FileHandle[relativePaths.length];
   int count = 0;
   for (int i = 0, n = relativePaths.length; i < n; i++) {
     String path = relativePaths[i];
     if (!filter.accept(file, path)) continue;
     handles[count] = child(path);
     count++;
   }
   if (count < relativePaths.length) {
     FileHandle[] newHandles = new FileHandle[count];
     System.arraycopy(handles, 0, newHandles, 0, count);
     handles = newHandles;
   }
   return handles;
 }
示例#21
0
 /**
  * Recursively search from the rootDir, filtering files based on fileFilter, and store a reference
  * to every file seen.
  *
  * @param rootDir Starting directory to recurse from
  * @param fileFilter Only add references to image files that are allowed by the filter
  */
 public static void searchForImageReferences(File rootDir, FilenameFilter fileFilter) {
   for (File file : rootDir.listFiles()) {
     if (file.isDirectory()) {
       searchForImageReferences(file, fileFilter);
       continue;
     }
     try {
       if (fileFilter.accept(rootDir, file.getName())) {
         if (MapTool.getFrame() != null) {
           MapTool.getFrame().setStatusMessage("Caching image reference: " + file.getName());
         }
         rememberLocalImageReference(file);
       }
     } catch (IOException ioe) {
       ioe.printStackTrace();
     }
   }
   // Done
   if (MapTool.getFrame() != null) {
     MapTool.getFrame().setStatusMessage("");
   }
 }
示例#22
0
  protected String[] listJarResources(URL dirURL, FilenameFilter filter)
      throws IOException, URISyntaxException {
    String[] files = new String[0];
    String spec = dirURL.getFile();
    int seperator = spec.indexOf("!/");

    if (seperator == -1) {
      return files;
    }

    URL jarFileURL = new URL(spec.substring(0, seperator));
    Set<String> filesSet = new HashSet<>();

    try (JarFile jarFile = new JarFile(jarFileURL.toURI().getPath())) {
      Enumeration<JarEntry> entries = jarFile.entries();

      while (entries.hasMoreElements()) {
        JarEntry entry = entries.nextElement();

        if (entry.isDirectory()) {
          continue;
        }

        String entryName = entry.getName();

        if (entryName.indexOf(schemaPath) > -1 && filter.accept(null, entryName)) {
          filesSet.add(entryName);
        }
      }
    }

    if (!filesSet.isEmpty()) {
      files = new String[filesSet.size()];
      files = filesSet.toArray(files);
    }

    return files;
  }
  /*
   * Tests if the filesystem contains the specified Item. Don't add a JavaDoc
   * comment here, we use the default documentation from implemented
   * interface.
   */
  @Override
  public boolean containsId(Object itemId) {
    if (logger.isTraceEnabled()) {
      logger.trace("containsId: " + ((File) itemId).hashCode() + " " + ((File) itemId).getName());
    }

    if (!(itemId instanceof File)) {
      return false;
    }
    boolean val = false;

    // Try to match all roots
    for (int i = 0; i < roots.length; i++) {
      try {
        val |= ((File) itemId).getCanonicalPath().startsWith(roots[i].getCanonicalPath());
      } catch (final IOException e) { // NOPMD
        // Exception ignored
      }
    }
    if (val && filter != null) {
      val &= filter.accept(((File) itemId).getParentFile(), ((File) itemId).getName());
    }
    return val;
  }
 public boolean requiresWriteLock(String resourceName) {
   FilenameFilter unlocked = strategy.getUnlockedFilter();
   if (unlocked == null) return true;
   else return !unlocked.accept(null, resourceName);
 }
示例#25
0
 /**
  * Provide a String representaion of this file filter.
  *
  * @return a String representaion
  */
 @Override
 public String toString() {
   String delegate = (fileFilter != null ? fileFilter.toString() : filenameFilter.toString());
   return super.toString() + "(" + delegate + ")";
 }
示例#26
0
 public boolean accept(File f, String s) {
   return lhs.accept(f, s) || rhs.accept(f, s);
 }