incubator-callback-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From macdo...@apache.org
Subject android commit: Refactoring the checks for file:// into a convenience method
Date Wed, 22 Feb 2012 20:56:14 GMT
Updated Branches:
  refs/heads/master cc5cfad58 -> e85394dd9


Refactoring the checks for file:// into a convenience method


Project: http://git-wip-us.apache.org/repos/asf/incubator-cordova-android/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-cordova-android/commit/e85394dd
Tree: http://git-wip-us.apache.org/repos/asf/incubator-cordova-android/tree/e85394dd
Diff: http://git-wip-us.apache.org/repos/asf/incubator-cordova-android/diff/e85394dd

Branch: refs/heads/master
Commit: e85394dd99fcaccbb9c674d05f66d163952bf3c6
Parents: cc5cfad
Author: macdonst <simon.macdonald@gmail.com>
Authored: Wed Feb 22 15:56:03 2012 -0500
Committer: macdonst <simon.macdonald@gmail.com>
Committed: Wed Feb 22 15:56:03 2012 -0500

----------------------------------------------------------------------
 framework/src/org/apache/cordova/FileUtils.java |   88 ++++++++----------
 1 files changed, 39 insertions(+), 49 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-cordova-android/blob/e85394dd/framework/src/org/apache/cordova/FileUtils.java
----------------------------------------------------------------------
diff --git a/framework/src/org/apache/cordova/FileUtils.java b/framework/src/org/apache/cordova/FileUtils.java
index 7dbf905..f2cc980 100755
--- a/framework/src/org/apache/cordova/FileUtils.java
+++ b/framework/src/org/apache/cordova/FileUtils.java
@@ -281,11 +281,7 @@ public class FileUtils extends Plugin {
      * @throws JSONException
      */
     private JSONArray readEntries(String fileName) throws FileNotFoundException, JSONException
{
-        if (fileName.startsWith("file://")) {
-            fileName = fileName.substring(7);
-        }
-
-        File fp = new File(fileName);
+        File fp = createFileObject(fileName);
 
         if (!fp.exists()) {
             // The directory we are listing doesn't exist so we should fail.
@@ -319,12 +315,8 @@ public class FileUtils extends Plugin {
      * @throws JSONException
      */
     private JSONObject transferTo(String fileName, String newParent, String newName, boolean
move) throws JSONException, NoModificationAllowedException, IOException, InvalidModificationException,
EncodingException {
-        if (fileName.startsWith("file://")) {
-            fileName = fileName.substring(7);
-        }
-        if (newParent.startsWith("file://")) {
-            newParent = newParent.substring(7);
-        }
+        fileName = stripFileProtocol(fileName);
+        newParent = stripFileProtocol(newParent);
 
 
         // Check for invalid file name
@@ -567,11 +559,7 @@ public class FileUtils extends Plugin {
      * @throws FileExistsException
      */
     private boolean removeRecursively(String filePath) throws FileExistsException {
-        if (filePath.startsWith("file://")) {
-            filePath = filePath.substring(7);
-        }
-
-        File fp = new File(filePath);
+        File fp = createFileObject(filePath);
 
         // You can't delete the root directory.
         if (atRootDirectory(filePath)) {
@@ -612,11 +600,7 @@ public class FileUtils extends Plugin {
      * @throws InvalidModificationException
      */
     private boolean remove(String filePath) throws NoModificationAllowedException, InvalidModificationException
{
-        if (filePath.startsWith("file://")) {
-            filePath = filePath.substring(7);
-        }
-
-        File fp = new File(filePath);
+        File fp = createFileObject(filePath);
 
         // You can't delete the root directory.
         if (atRootDirectory(filePath)) {
@@ -707,9 +691,7 @@ public class FileUtils extends Plugin {
         if (fileName.startsWith("/")) {
             fp = new File(fileName);
         } else {
-            if (dirPath.startsWith("file://")) {
-                dirPath = dirPath.substring(7);
-            }
+            dirPath = stripFileProtocol(dirPath);
             fp = new File(dirPath + File.separator + fileName);
         }
         return fp;
@@ -724,9 +706,7 @@ public class FileUtils extends Plugin {
      * @throws JSONException
      */
     private JSONObject getParent(String filePath) throws JSONException {
-        if (filePath.startsWith("file://")) {
-            filePath = filePath.substring(7);
-        }
+        filePath = stripFileProtocol(filePath);
 
         if (atRootDirectory(filePath)) {
             return getEntry(filePath);
@@ -742,9 +722,7 @@ public class FileUtils extends Plugin {
      * @return true if we are at the root, false otherwise.
      */
     private boolean atRootDirectory(String filePath) {
-        if (filePath.startsWith("file://")) {
-            filePath = filePath.substring(7);
-        }
+        filePath = stripFileProtocol(filePath);
 
         if (filePath.equals(Environment.getExternalStorageDirectory().getAbsolutePath() +
"/Android/data/" + ctx.getPackageName() + "/cache") ||
                 filePath.equals(Environment.getExternalStorageDirectory().getAbsolutePath())
|| 
@@ -755,6 +733,32 @@ public class FileUtils extends Plugin {
     }
 
     /**
+     * This method removes the "file://" from the passed in filePath
+     * 
+     * @param filePath to be checked.
+     * @return
+     */
+    private String stripFileProtocol(String filePath) {
+        if (filePath.startsWith("file://")) {
+            filePath = filePath.substring(7);
+        }
+        return filePath;
+    }
+    
+    /**
+     * Create a File object from the passed in path
+     * 
+     * @param filePath
+     * @return
+     */
+    private File createFileObject(String filePath) {
+        filePath = stripFileProtocol(filePath);
+
+        File file = new File(filePath);
+        return file;
+    }
+
+    /**
      * Look up metadata about this entry.
      *
      * @param filePath to entry
@@ -763,11 +767,7 @@ public class FileUtils extends Plugin {
      * @throws JSONException
      */
     private JSONObject getMetadata(String filePath) throws FileNotFoundException, JSONException
{
-        if (filePath.startsWith("file://")) {
-            filePath = filePath.substring(7);
-        }
-
-        File file = new File(filePath);
+        File file = createFileObject(filePath);
 
         if (!file.exists()) {
             throw new FileNotFoundException("Failed to find file in getMetadata");
@@ -788,11 +788,7 @@ public class FileUtils extends Plugin {
      * @throws JSONException
      */
     private JSONObject getFileMetadata(String filePath) throws FileNotFoundException, JSONException
{
-        if (filePath.startsWith("file://")) {
-            filePath = filePath.substring(7);
-        }
-
-        File file = new File(filePath);
+        File file = createFileObject(filePath);
 
         if (!file.exists()) {
             throw new FileNotFoundException("File: " + filePath + " does not exist.");
@@ -975,9 +971,7 @@ public class FileUtils extends Plugin {
      */
     /**/
     public long write(String filename, String data, int offset) throws FileNotFoundException,
IOException {
-        if (filename.startsWith("file://")) {
-            filename = filename.substring(7);
-        }
+        filename = stripFileProtocol(filename);
 
         boolean append = false;
         if (offset > 0) {
@@ -1005,9 +999,7 @@ public class FileUtils extends Plugin {
      * @throws FileNotFoundException, IOException
      */
     private long truncateFile(String filename, long size) throws FileNotFoundException, IOException
{
-        if (filename.startsWith("file://")) {
-            filename = filename.substring(7);
-        }
+        filename = stripFileProtocol(filename);
 
         RandomAccessFile raf = new RandomAccessFile(filename, "rw");
 
@@ -1033,9 +1025,7 @@ public class FileUtils extends Plugin {
             return ctx.getContentResolver().openInputStream(uri);
         }
         else {
-            if (path.startsWith("file://")) {
-                path = path.substring(7);
-            }
+            path = stripFileProtocol(path);
             return new FileInputStream(path);
         }
     }


Mime
View raw message