public static void copyDir(
     @NotNull File fromDir, @NotNull File toDir, @Nullable final FileFilter filter)
     throws IOException {
   ensureExists(toDir);
   if (isAncestor(fromDir, toDir, true)) {
     LOG.error(fromDir.getAbsolutePath() + " is ancestor of " + toDir + ". Can't copy to itself.");
     return;
   }
   File[] files = fromDir.listFiles();
   if (files == null)
     throw new IOException(
         CommonBundle.message("exception.directory.is.invalid", fromDir.getPath()));
   if (!fromDir.canRead())
     throw new IOException(
         CommonBundle.message("exception.directory.is.not.readable", fromDir.getPath()));
   for (File file : files) {
     if (filter != null && !filter.accept(file)) {
       continue;
     }
     if (file.isDirectory()) {
       copyDir(file, new File(toDir, file.getName()), filter);
     } else {
       copy(file, new File(toDir, file.getName()));
     }
   }
 }
 public static boolean isFilePathAcceptable(@NotNull File root, @Nullable FileFilter fileFilter) {
   File file = root;
   do {
     if (fileFilter != null && !fileFilter.accept(file)) return false;
     file = file.getParentFile();
   } while (file != null);
   return true;
 }
Exemple #3
0
 public static boolean selectInDirectory(
     List<String> list, File dir, int limit, FileFilter fileFilter) {
   String[] listDirectory = dir.list();
   Arrays.sort(listDirectory);
   for (int i = 0; i < listDirectory.length; i++) {
     File f = new File(dir, listDirectory[i]);
     if (f.isFile() && fileFilter.accept(f)) {
       list.add(f.getAbsolutePath());
       if (list.size() >= limit) return true;
     }
   }
   for (int i = 0; i < listDirectory.length; i++) {
     File f = new File(dir, listDirectory[i]);
     if (f.isDirectory() && selectInDirectory(list, f, limit, fileFilter)) return true;
   }
   return false;
 }
Exemple #4
0
  public int compileJava() {
    try {
      //    create new bin directory
      boolean createBin = new File(classPath).mkdir();

      //    create new javac ProcessBuilder
      //      ProcessBuilder pb =
      //      new ProcessBuilder("javac", "-d", classPath, "./" + studentPath + "/*.java");

      ProcessBuilder pbDir = new ProcessBuilder("dir");
      //    Determine current working directory
      File srcAbsPath = new File(sourcePath);
      srcAbsPathName = srcAbsPath.getAbsolutePath();
      System.out.println("Compiler.java line 69 source path: " + sourcePath);
      System.out.println("Compiler.java line 69 source absolute path: " + srcAbsPathName);

      File cwd = pbDir.directory();
      //    debug code - to confirm correct directory
      //    TestTools.dir(cwd);
      //    NB - ProcessBuilder default is to return a null
      //    pointer for the abstract path to indicate that it
      //    is using System.Properties "user.dir", i.e., the
      //    current system working directory; hence the
      //    critical need to handle a NullPointerException.
      //    Also returns a null pointer if the directory
      //    doesn't exist.

      // all this is doing is changing the dir, can we approach this in a different way using a
      // value in our Data Object? -mh
      File nwd = TestTools.cd(cwd, studentPath);

      System.out.println("(Compiler.java line 88)new working directory: " + nwd.toString());
      String studentPathName = nwd.getAbsolutePath();
      File nwdPath = new File(studentPath);
      System.out.println("(Compiler.java line 91)new working directory path: " + studentPathName);
      //    debug code to test new working directory
      //    TestTools.dir(nwd);

      FileFilter filter = new FileFilter() {};

      String[] javaFileList = nwdPath.list(filter);
      //    set up output file
      File outputFile = new File(outputFileName);
      //    System.out.println(outputFileName);
      outputFile.delete();

      for (int k = 0; k < javaFileList.length; k++) {
        try {
          if (filter.accept(nwdPath, javaFileList[k]) == true) {
            System.out.println("COMPILER.JAVA (line 111)  Compiling: " + javaFileList[k]);

            String compilePath =
                "javac" + "-d" + classPath + ".\\" + studentPath + "\\" + javaFileList[k];
            System.out.println("Compiler.java 117 compile path: " + compilePath);
            ProcessBuilder pb =
                // new ProcessBuilder("javac ", "-d", classPath, ".\\" + studentPath + "\\" +
                // javaFileList[k]);
                new ProcessBuilder(compilePath);

            // System.out.println(pb.environment().toString());  <-- THIS IS VERY INTERESTING

            //          Create environment map and set environmental variables
            Map<String, String> env = pb.environment();
            env.clear();
            env.put("PATH", path);
            env.put("CLASSPATH", classPath);
            //          env.put("SOURCEPATH", sourcePath);
            //          env.remove("OTHERVAR");

            pb.redirectErrorStream(true);
            pb.redirectOutput(Redirect.appendTo(outputFile));

            //          start javac process
            Process p = pb.start();

            //          need other processes to wait for compilation to finish
            //          basically joins the thread to the javac process to force sequential
            //          execution - need to be careful - if any process hangs, whole run hangs
            success =
                p
                    .waitFor(); // Returns the exit value of the process. By convention, 0 indicates
                                // normal termination.
                                // http://docs.oracle.com/javase/6/docs/api/java/lang/Process.html#waitFor%28%29

            assert pb.redirectInput() == Redirect.PIPE;
            assert pb.redirectOutput().file() == outputFile;
            assert p.getInputStream().read() == -1;
            System.out.println("COMPILER.JAVA (line 138)  end of loop, success = " + success);
          }
        } catch (Exception e) {
          System.out.println(" Compiler.java FOR LOOP Compile Exception: " + javaFileList[k]);
        }
      }
    } catch (Exception e) {
      System.out.println("Compile Exception, PROBABLY DUE TO FILE PATH");
      System.out.println("source absolute path: " + srcAbsPathName);
    }

    return success;
  }
 @Override
 public boolean shouldHonorFileEncodingForCompilation(File file) {
   return JAVA_SOURCES_FILTER.accept(file);
 }