Commit 5bedfc93 by Ranjit Mathew Committed by Tom Tromey

File (getAbsolutePath): Prefix drive specifier on Windows for paths starting with a '\'.

2003-03-01  Ranjit Mathew  <rmathew@hotmail.com>

	* java/io/File (getAbsolutePath): Prefix drive specifier on
	Windows for paths starting with a '\'.
	(toURL): Make URL more consistent with what Sun's JDK returns.

	* java/io/natFileWin32.cc (java::io::File::isAbsolute): Return
	true only if the path is a UNC network path or it starts with a
	drive specifier.

	* java/net/URLStreamHandler.java (parseURL): Correct minor typo.
	Be prepared to handle either '/' or '\\' in the file path for
	Windows if using the "file" protocol.
	Canonicalise the file path if using a relative path in the given
	context and the "file" protocol.

From-SVN: r63635
parent 73058ef6
2003-03-01 Ranjit Mathew <rmathew@hotmail.com>
* java/io/File (getAbsolutePath): Prefix drive specifier on
Windows for paths starting with a '\'.
(toURL): Make URL more consistent with what Sun's JDK returns.
* java/io/natFileWin32.cc (java::io::File::isAbsolute): Return
true only if the path is a UNC network path or it starts with a
drive specifier.
* java/net/URLStreamHandler.java (parseURL): Correct minor typo.
Be prepared to handle either '/' or '\\' in the file path for
Windows if using the "file" protocol.
Canonicalise the file path if using a relative path in the given
context and the "file" protocol.
2003-03-01 Mohan Embar <gnustuff@thisiscool.com> 2003-03-01 Mohan Embar <gnustuff@thisiscool.com>
* java/lang/natWin32Process.cc (startProcess): Double-quote each * java/lang/natWin32Process.cc (startProcess): Double-quote each
......
// File.java - File name // File.java - File name
/* Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation /* Copyright (C) 1998, 1999, 2000, 2001, 2003 Free Software Foundation, Inc.
This file is part of libgcj. This file is part of libgcj.
...@@ -153,12 +153,20 @@ public class File implements Serializable, Comparable ...@@ -153,12 +153,20 @@ public class File implements Serializable, Comparable
this (dir == null ? null : dir.path, name); this (dir == null ? null : dir.path, name);
} }
// FIXME ???
public String getAbsolutePath () public String getAbsolutePath ()
{ {
if (isAbsolute ()) if (isAbsolute ())
return path; return path;
return System.getProperty("user.dir") + separatorChar + path; else if (separatorChar == '\\'
&& path.length () > 0 && path.charAt (0) == '\\')
{
// On Windows, even if the path starts with a '\\' it is not
// really absolute until we prefix the drive specifier from
// the current working directory to it.
return System.getProperty ("user.dir").substring (0, 2) + path;
}
else
return System.getProperty ("user.dir") + separatorChar + path;
} }
/** @since 1.2 */ /** @since 1.2 */
...@@ -289,8 +297,14 @@ public class File implements Serializable, Comparable ...@@ -289,8 +297,14 @@ public class File implements Serializable, Comparable
public URL toURL () throws MalformedURLException public URL toURL () throws MalformedURLException
{ {
return new URL ("file://" + getAbsolutePath () // On Win32, Sun's JDK returns URLs of the form "file:/c:/foo/bar.txt",
+ (isDirectory() ? "/" : "")); // while on UNIX, it returns URLs of the form "file:/foo/bar.txt".
if (separatorChar == '\\')
return new URL ("file:/" + getAbsolutePath ().replace ('\\', '/')
+ (isDirectory() ? "/" : ""));
else
return new URL ("file:" + getAbsolutePath ()
+ (isDirectory() ? "/" : ""));
} }
private final native boolean performMkdir (); private final native boolean performMkdir ();
......
...@@ -119,9 +119,14 @@ java::io::File::getCanonicalPath (void) ...@@ -119,9 +119,14 @@ java::io::File::getCanonicalPath (void)
jboolean jboolean
java::io::File::isAbsolute (void) java::io::File::isAbsolute (void)
{ {
if (path->length() > 0 // See if the path represents a Windows UNC network path.
&& (path->charAt(0) == '/' || path->charAt(0) == '\\')) if (path->length () > 1
&& (path->charAt (0) == '\\') && (path->charAt (1) == '\\'))
return true; return true;
// Note that the path is not an absolute path even if it starts with
// a '/' or a '\' because it lacks a drive specifier.
if (path->length() < 3) if (path->length() < 3)
return false; return false;
// Hard-code A-Za-z because Windows (I think) can't use non-ASCII // Hard-code A-Za-z because Windows (I think) can't use non-ASCII
......
/* URLStreamHandler.java -- Abstract superclass for all protocol handlers /* URLStreamHandler.java -- Abstract superclass for all protocol handlers
Copyright (C) 1998, 1999, 2002 Free Software Foundation, Inc. Copyright (C) 1998, 1999, 2002, 2003 Free Software Foundation, Inc.
This file is part of GNU Classpath. This file is part of GNU Classpath.
...@@ -39,6 +39,7 @@ exception statement from your version. */ ...@@ -39,6 +39,7 @@ exception statement from your version. */
package java.net; package java.net;
import java.io.IOException; import java.io.IOException;
import java.io.File;
/* /*
* Written using on-line Java Platform 1.2 API Specification, as well * Written using on-line Java Platform 1.2 API Specification, as well
...@@ -112,7 +113,7 @@ public abstract class URLStreamHandler ...@@ -112,7 +113,7 @@ public abstract class URLStreamHandler
* subclasses that implement protocols with URL's the follow a different * subclasses that implement protocols with URL's the follow a different
* syntax should override this method. The lone exception is that if * syntax should override this method. The lone exception is that if
* the protocol name set in the URL is "file", this method will accept * the protocol name set in the URL is "file", this method will accept
* a an empty hostname (i.e., "file:///"), which is legal for that protocol * an empty hostname (i.e., "file:///"), which is legal for that protocol
* *
* @param url The URL object in which to store the results * @param url The URL object in which to store the results
* @param spec The String-ized URL to parse * @param spec The String-ized URL to parse
...@@ -176,8 +177,32 @@ public abstract class URLStreamHandler ...@@ -176,8 +177,32 @@ public abstract class URLStreamHandler
else if (start < end) else if (start < end)
{ {
// Context is available, but only override it if there is a new file. // Context is available, but only override it if there is a new file.
file = file.substring(0, file.lastIndexOf('/')) char sepChar = '/';
+ '/' + spec.substring(start, end); int lastSlash = file.lastIndexOf (sepChar);
if (lastSlash < 0 && File.separatorChar != sepChar
&& url.getProtocol ().equals ("file"))
{
// On Windows, even '\' is allowed in a "file" URL.
sepChar = File.separatorChar;
lastSlash = file.lastIndexOf (sepChar);
}
file = file.substring(0, lastSlash)
+ sepChar + spec.substring (start, end);
if (url.getProtocol ().equals ("file"))
{
// For "file" URLs constructed relative to a context, we
// need to canonicalise the file path.
try
{
file = new File (file).getCanonicalPath ();
}
catch (IOException e)
{
}
}
ref = null; ref = null;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment