Updated refactored Normalize for strings (not to be confused with

NormalizeRaw)
This commit is contained in:
nathan 2017-02-12 22:49:09 +01:00
parent a7548cad84
commit 6589d6f95b
2 changed files with 29 additions and 16 deletions

View File

@ -212,8 +212,8 @@ class FileUtil {
} }
String normalizedIn = normalize(in.getAbsolutePath()); String normalizedIn = normalize(in).getAbsolutePath();
String normalizedout = normalize(out.getAbsolutePath()); String normalizedout = normalize(out).getAbsolutePath();
if (normalizedIn.equalsIgnoreCase(normalizedout)) { if (normalizedIn.equalsIgnoreCase(normalizedout)) {
logger.warn("Source equals destination! " + normalizedIn); logger.warn("Source equals destination! " + normalizedIn);
@ -277,8 +277,8 @@ class FileUtil {
throw new IllegalArgumentException("two cannot be null."); throw new IllegalArgumentException("two cannot be null.");
} }
String normalizedOne = normalize(one.getAbsolutePath()); String normalizedOne = normalize(one).getAbsolutePath();
String normalizedTwo = normalize(two.getAbsolutePath()); String normalizedTwo = normalize(two).getAbsolutePath();
Logger logger2 = logger; Logger logger2 = logger;
if (logger2.isTraceEnabled()) { if (logger2.isTraceEnabled()) {
@ -511,7 +511,7 @@ class FileUtil {
boolean delete = true; boolean delete = true;
final File file2 = files[i]; final File file2 = files[i];
String name2 = file2.getName(); String name2 = file2.getName();
String name2Full = normalize(file2.getAbsolutePath()); String name2Full = normalize(file2).getAbsolutePath();
if (file2.isDirectory()) { if (file2.isDirectory()) {
for (String name : namesToIgnore) { for (String name : namesToIgnore) {
@ -1175,6 +1175,7 @@ class FileUtil {
//----------------------------------------------------------------------- //-----------------------------------------------------------------------
/* /*
* FilenameUtils.java (normalize + dependencies) - Apache 2.0 License * FilenameUtils.java (normalize + dependencies) - Apache 2.0 License
* http://commons.apache.org/proper/commons-io/ * http://commons.apache.org/proper/commons-io/
@ -1187,6 +1188,8 @@ class FileUtil {
/** /**
* Normalizes a path, removing double and single dot path steps. * Normalizes a path, removing double and single dot path steps.
* <p/> * <p/>
* THIS IS DIFFERENT in that it might not be a path that resolves to anything
* <p/>
* This method normalizes a path to a standard format. * This method normalizes a path to a standard format.
* The input may contain separators in either Unix or Windows format. * The input may contain separators in either Unix or Windows format.
* The output will contain separators in the format of the system. * The output will contain separators in the format of the system.
@ -1222,10 +1225,11 @@ class FileUtil {
* (Note the file separator returned will be correct for Windows/Unix) * (Note the file separator returned will be correct for Windows/Unix)
* *
* @param filename the filename to normalize, null returns null * @param filename the filename to normalize, null returns null
*
* @return the normalized filename, or null if invalid * @return the normalized filename, or null if invalid
*/ */
public static public static
String normalize(String filename) { String normalizeRaw(String filename) {
return doNormalize(filename, SYSTEM_SEPARATOR, true); return doNormalize(filename, SYSTEM_SEPARATOR, true);
} }
@ -1275,12 +1279,21 @@ class FileUtil {
* </pre> * </pre>
* (Note the file separator returned will be correct for Windows/Unix) * (Note the file separator returned will be correct for Windows/Unix)
* *
* @param filename the filename to normalize, null returns null * @param file the file to normalize, null returns null
* @return the normalized filename, or null if invalid * @return the normalized file, or null if invalid
*/ */
public static public static
String normalizeAsFile(String filename) { File normalize(String filename) {
return doNormalize(new File(filename).getAbsolutePath(), SYSTEM_SEPARATOR, true); if (filename == null) {
return null;
}
String asString = doNormalize(new File(filename).getAbsolutePath(), SYSTEM_SEPARATOR, true);
if (asString == null) {
return null;
}
return new File(asString);
} }
/* /*

View File

@ -15,15 +15,15 @@
*/ */
package dorkbox.util.process; package dorkbox.util.process;
import dorkbox.util.FileUtil;
import dorkbox.util.OS;
import java.io.File; import java.io.File;
import java.io.InputStream; import java.io.InputStream;
import java.io.PrintStream; import java.io.PrintStream;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import dorkbox.util.FileUtil;
import dorkbox.util.OS;
/** /**
* This will FORK the java process initially used to start the currently running JVM. Changing the java executable will change this behaviors * This will FORK the java process initially used to start the currently running JVM. Changing the java executable will change this behaviors
*/ */
@ -61,8 +61,8 @@ class JavaProcessBuilder extends ShellProcessBuilder {
// even though the former is a symlink to the latter! To work around this, see if the // even though the former is a symlink to the latter! To work around this, see if the
// desired jvm is in fact pointed to by /usr/bin/java and, if so, use that instead. // desired jvm is in fact pointed to by /usr/bin/java and, if so, use that instead.
if (OS.isMacOsX()) { if (OS.isMacOsX()) {
String localVM = FileUtil.normalize(new File("/usr/bin/java").getAbsolutePath()); String localVM = FileUtil.normalize("/usr/bin/java").getAbsolutePath();
String vmCheck = FileUtil.normalize(new File(vmpath).getAbsolutePath()); String vmCheck = FileUtil.normalize(vmpath).getAbsolutePath();
if (localVM.equals(vmCheck)) { if (localVM.equals(vmCheck)) {
vmpath = "/usr/bin/java"; vmpath = "/usr/bin/java";
} }
@ -171,7 +171,7 @@ class JavaProcessBuilder extends ShellProcessBuilder {
for (String classpathEntry : this.classpathEntries) { for (String classpathEntry : this.classpathEntries) {
try { try {
// make sure the classpath is ABSOLUTE pathname // make sure the classpath is ABSOLUTE pathname
classpathEntry = FileUtil.normalize(new File(classpathEntry).getAbsolutePath()); classpathEntry = FileUtil.normalize(classpathEntry).getAbsolutePath();
// fix a nasty problem when spaces aren't properly escaped! // fix a nasty problem when spaces aren't properly escaped!
classpathEntry = classpathEntry.replaceAll(" ", "\\ "); classpathEntry = classpathEntry.replaceAll(" ", "\\ ");