support for CAPTCHAs
[mir.git] / source / mir / util / FileRoutines.java
index ac1d4c1..06de92a 100755 (executable)
  */
 package mir.util;
 
-import gnu.regexp.RE;
-
-import java.io.*;
+import org.apache.oro.text.regex.MalformedPatternException;
+import org.apache.oro.text.regex.Pattern;
+import org.apache.oro.text.regex.Perl5Compiler;
+import org.apache.oro.text.regex.Perl5Matcher;
+
+import java.io.BufferedInputStream;
+import java.io.BufferedOutputStream;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.FilenameFilter;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
 import java.util.Arrays;
 import java.util.Collections;
 import java.util.List;
@@ -51,16 +64,15 @@ public class FileRoutines {
 
     inputStream = new BufferedInputStream(new FileInputStream(aSourceFile));
     try {
-      File directory = new File(aDestinationFile.getParent());
-      if (directory != null && !directory.exists()) {
-        directory.mkdirs();
-      }
+      createParentDirectories(aDestinationFile);
+
       outputStream = new BufferedOutputStream(new FileOutputStream(aDestinationFile), 8192);
       try {
         do {
           nrBytesRead = inputStream.read(buffer);
-          if (nrBytesRead > 0)
+          if (nrBytesRead > 0) {
             outputStream.write(buffer, 0, nrBytesRead);
+          }
         }
         while (nrBytesRead >= 0);
       }
@@ -162,19 +174,18 @@ public class FileRoutines {
   }
 
   public static class RegExpFileFilter implements FilenameFilter {
-    private RE expression;
+    private Pattern expression;
 
-    public RegExpFileFilter(String anExpression) {
-      try {
-        expression = new RE(anExpression);
-      }
-      catch (Throwable t) {
-        throw new RuntimeException(t.getMessage());
-      }
+    public RegExpFileFilter(String anExpression) throws MalformedPatternException {
+      Perl5Compiler compiler = new Perl5Compiler();
+
+      expression = compiler.compile(anExpression, Perl5Compiler.READ_ONLY_MASK);
     }
 
     public boolean accept(File aDir, String aName) {
-      return expression.isMatch(aName) && !new File(aDir, aName).isDirectory();
+      Perl5Matcher matcher = new Perl5Matcher();
+
+      return !new File(aDir, aName).isDirectory() && matcher.matches(aName, expression);
     }
   }
 
@@ -205,17 +216,22 @@ public class FileRoutines {
   }
 
   /**
-   * Return the extension of a path. (e.g. <code>getExtension("example.txt")</code> will
+   * Returns the extension of a path. (e.g. <code>getExtension("example.txt")</code> will
    * return <code>"txt"</code>
    */
   public static String getExtension(String aPath) {
     int position = aPath.lastIndexOf('.');
+
     if (position >= 0) {
       return aPath.substring(position + 1);
     }
+
     return "";
   }
 
+  /**
+   * Returns <code>true</code> if the given path is absolute
+   */
   public static boolean isAbsolutePath(String aPath) {
     return new File(aPath).isAbsolute();
   }
@@ -248,27 +264,24 @@ public class FileRoutines {
     return result.toByteArray();
   }
 
-       /**
-        * Reads the content of a file into an array of bytes
-        */
-       public static void writeByteArrayIntoFile(byte[] anArray, File aFile) throws IOException {
-         OutputStream output = new FileOutputStream(aFile);
-
-         try {
-               ByteArrayInputStream input = new ByteArrayInputStream(anArray);
-               try {
-                 IORoutines.copyStream(input, output);
-               }
-               finally {
-                       input.close();
-               }
-         }
-         finally {
-               output.close();
-         }
-       }
-
-
+  /**
+   * Reads the content of a file into an array of bytes
+   */
+  public static void writeByteArrayIntoFile(byte[] anArray, File aFile) throws IOException {
+    OutputStream output = new FileOutputStream(aFile);
+    try {
+      ByteArrayInputStream input = new ByteArrayInputStream(anArray);
+      try {
+        IORoutines.copyStream(input, output);
+      }
+      finally {
+        input.close();
+      }
+    }
+    finally {
+      output.close();
+    }
+  }
 
   /**
    * Creates all parent directories of a file if they do not exist