Skip to content

Commit

Permalink
Move file path normalizing method to FileUtils class.
Browse files Browse the repository at this point in the history
Originally in FileManager plugin, it is useful for other plugins as well.
  • Loading branch information
Justin Tyberg committed Mar 24, 2011
1 parent b79e055 commit 754e39d
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 20 deletions.
25 changes: 6 additions & 19 deletions framework/ext/src/com/phonegap/file/FileManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -737,9 +737,10 @@ else if (!parent.endsWith(FileUtils.FILE_SEPARATOR)) {
String srcDir = srcURL.substring(0, srcURL.length() - srcName.length());
if (srcDir.equals(parent)) {
// rename to itself is an error
if (formatPath(srcName).equals(formatPath(newName))) {
return new PluginResult(
PluginResult.Status.IOEXCEPTION, INVALID_MODIFICATION_ERR);
if (FileUtils.stripSeparator(srcName).equals(
FileUtils.stripSeparator(newName))) {
return new PluginResult(PluginResult.Status.IOEXCEPTION,
INVALID_MODIFICATION_ERR);
}

// file replace file || directory replace directory ==> OK
Expand Down Expand Up @@ -839,8 +840,8 @@ protected static Entry getEntryFromURI(String filePath)
// create a new Entry
entry = new Entry();
entry.setDirectory(fconn.isDirectory());
entry.setName(formatPath(fconn.getName()));
entry.setFullPath(formatPath(path));
entry.setName(FileUtils.stripSeparator(fconn.getName()));
entry.setFullPath(FileUtils.stripSeparator(path));
}
}
catch (IOException e) {
Expand Down Expand Up @@ -913,20 +914,6 @@ protected static String getFileSystemName(int type) {
return name;
}

/**
* Strips the trailing slash from path names.
*
* @param path
* full or relative path name
* @return formatted path (without trailing slash)
*/
protected static String formatPath(String path) {
while (path.endsWith(FileUtils.FILE_SEPARATOR)) {
path = path.substring(0, path.length() - 1);
}
return path;
}

/**
* Returns full path from the directory and name specified.
*
Expand Down
47 changes: 46 additions & 1 deletion framework/ext/src/com/phonegap/file/FileUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import net.rim.device.api.io.FileNotFoundException;
import net.rim.device.api.io.IOUtilities;
import net.rim.device.api.io.MIMETypeAssociations;
import net.rim.device.api.system.Application;

import com.phonegap.PhoneGapExtension;
Expand Down Expand Up @@ -610,5 +611,49 @@ public static Enumeration listDirectory(String path)
}
}
return listing;
}
}

public static File getFileProperties(String filePath) throws FileNotFoundException {
File file = new File(stripSeparator(filePath));
FileConnection fconn = null;
try {
fconn = (FileConnection)Connector.open(filePath);
if (!fconn.exists()) {
throw new FileNotFoundException();
}
file.setLastModifiedDate(fconn.lastModified());
file.setName(stripSeparator(fconn.getName()));
file.setType(MIMETypeAssociations.getMIMEType(filePath));
file.setSize(fconn.fileSize());
}
catch (IllegalArgumentException e) {
Logger.log(FileUtils.class.getName() + ": " + e);
}
catch (IOException e) {
Logger.log(FileUtils.class.getName() + ": " + e);
}
finally {
try {
if (fconn != null) fconn.close();
}
catch (IOException ignored) {
}
}
return file;
}

/**
* Strips the trailing slash from path names.
*
* @param path
* full or relative path name
* @return formatted path (without trailing slash)
*/
public static String stripSeparator(String path) {
int len = FILE_SEPARATOR.length();
while (path.endsWith(FILE_SEPARATOR)) {
path = path.substring(0, path.length() - len);
}
return path;
}
}

0 comments on commit 754e39d

Please sign in to comment.