summaryrefslogtreecommitdiff
path: root/src/compiler/scala/tools/nsc/io/AbstractFile.scala
diff options
context:
space:
mode:
Diffstat (limited to 'src/compiler/scala/tools/nsc/io/AbstractFile.scala')
-rw-r--r--src/compiler/scala/tools/nsc/io/AbstractFile.scala67
1 files changed, 22 insertions, 45 deletions
diff --git a/src/compiler/scala/tools/nsc/io/AbstractFile.scala b/src/compiler/scala/tools/nsc/io/AbstractFile.scala
index eaa3091eee..f7d73ed200 100644
--- a/src/compiler/scala/tools/nsc/io/AbstractFile.scala
+++ b/src/compiler/scala/tools/nsc/io/AbstractFile.scala
@@ -8,8 +8,9 @@
package scala.tools.nsc
package io
-import java.io.{File, FileOutputStream, IOException, InputStream, OutputStream}
+import java.io.{ File => JFile, FileOutputStream, IOException, InputStream, OutputStream }
import java.net.URL
+import PartialFunction._
import scala.collection.mutable.ArrayBuffer
@@ -17,21 +18,23 @@ import scala.collection.mutable.ArrayBuffer
* @author Philippe Altherr
* @version 1.0, 23/03/2004
*/
-object AbstractFile {
+object AbstractFile
+{
+ def isJarOrZip(f: Path) = cond(f.extension) { case Some("zip" | "jar") => true }
/** Returns "getFile(new File(path))". */
- def getFile(path: String): AbstractFile = getFile(new File(path))
+ def getFile(path: String): AbstractFile = getFile(Path(path))
+ def getFile(path: Path): AbstractFile = getFile(path.toFile)
/**
* If the specified File exists and is a regular file, returns an
* abstract regular file backed by it. Otherwise, returns <code>null</code>.
*/
def getFile(file: File): AbstractFile =
- if (file.isFile() && file.exists()) new PlainFile(file) else null
-
+ if (file.isFile) new PlainFile(file) else null
/** Returns "getDirectory(new File(path))". */
- def getDirectory(path: String): AbstractFile = getDirectory(new File(path))
+ def getDirectory(path: Path): AbstractFile = getDirectory(path.toFile)
/**
* If the specified File exists and is either a directory or a
@@ -41,15 +44,10 @@ object AbstractFile {
* @param file ...
* @return ...
*/
- def getDirectory(file: File): AbstractFile = {
- if (file.isDirectory() && file.exists()) return new PlainFile(file)
- if (file.isFile() && file.exists()) {
- val path = file.getPath()
- if (path.endsWith(".jar") || path.endsWith(".zip"))
- return ZipArchive.fromFile(file);
- }
- null
- }
+ def getDirectory(file: File): AbstractFile =
+ if (file.isDirectory) new PlainFile(file)
+ else if (file.isFile && isJarOrZip(file)) ZipArchive fromFile file
+ else null
/**
* If the specified URL exists and is a readable zip or jar archive,
@@ -60,15 +58,7 @@ object AbstractFile {
* @return ...
*/
def getURL(url: URL): AbstractFile =
- if (url ne null) {
- val path = url.getPath()
- if (path.endsWith(".jar") || path.endsWith(".zip"))
- ZipArchive.fromURL(url)
- else
- null
- }
- else
- null
+ Option(url) filterMap { case url: URL if isJarOrZip(url.getPath) => ZipArchive fromURL url } orNull
}
/**
@@ -110,11 +100,12 @@ abstract class AbstractFile extends AnyRef with Iterable[AbstractFile] {
def container : AbstractFile
/** Returns the underlying File if any and null otherwise. */
- def file: File
+ def file: JFile
+ def sfile = File(file) // XXX
/** Does this abstract file denote an existing file? */
def exists: Boolean =
- if (file ne null) file.exists()
+ if (file ne null) file.exists
else true
/** Create a file on disk, if one does not exist already. */
@@ -207,11 +198,11 @@ abstract class AbstractFile extends AnyRef with Iterable[AbstractFile] {
private def lookup(getFile: (AbstractFile, String, Boolean) => AbstractFile,
path0: String,
directory: Boolean): AbstractFile = {
- val separator = File.separatorChar
+ val separator = JFile.separatorChar
// trim trailing '/'s
- val path = if (path0.charAt(path0.length - 1) == separator) path0.substring(0, path0.length - 1) else path0
+ val path: String = if (path0.last == separator) path0 dropRight 1 else path0
val length = path.length()
- assert(0 < length && path.lastIndexOf(separator) < length - 1, path)
+ assert(length > 0 && !(path.last == separator), path)
var file = this
var start = 0
while (true) {
@@ -231,14 +222,7 @@ abstract class AbstractFile extends AnyRef with Iterable[AbstractFile] {
*/
def fileNamed(name: String): AbstractFile = {
assert(isDirectory)
- val existing = lookupName(name, false)
- if (existing == null) {
- val newFile = new File(file, name)
- newFile.createNewFile()
- new PlainFile(newFile)
- } else {
- existing
- }
+ Option(lookupName(name, false)) getOrElse new PlainFile((sfile / name).createFile())
}
/**
@@ -247,14 +231,7 @@ abstract class AbstractFile extends AnyRef with Iterable[AbstractFile] {
*/
def subdirectoryNamed(name: String): AbstractFile = {
assert (isDirectory)
- val existing = lookupName(name, true)
- if (existing == null) {
- val dir = new File(file, name)
- dir.mkdir()
- new PlainFile(dir)
- } else {
- existing
- }
+ Option(lookupName(name, true)) getOrElse new PlainFile((sfile / name).createDirectory())
}
/** Returns the path of this abstract file. */