~ fixed directory creation bug
authorAlexandre Bertails <bertails@w3.org>
Mon, 29 Aug 2011 13:11:43 -0400
changeset 23 a6e5204c0f07
parent 22 209fb5cf5c2a
child 24 01905dac172e
~ fixed directory creation bug
project/build.scala
src/main/scala/Resource.scala
src/test/scala/Test.scala
--- a/project/build.scala	Mon Aug 29 00:17:02 2011 -0400
+++ b/project/build.scala	Mon Aug 29 13:11:43 2011 -0400
@@ -14,9 +14,7 @@
   val antiXML = "com.codecommit" %% "anti-xml" % "0.3-SNAPSHOT" % "test"
   val jena = "com.hp.hpl.jena" % "jena" % "2.6.4"
   val arq = "com.hp.hpl.jena" % "arq" % "2.8.8"
-//  val jenaIri = "com.hp.hpl.jena" % "iri" % "0.8" from "http://openjena.org/repo/com/hp/hpl/jena/iri/0.8/iri-0.8.jar"
-
-
+  val grizzled = "org.clapper" %% "grizzled-scala" % "1.0.7" % "test"
 }
 
 // some usefull repositories
@@ -73,7 +71,8 @@
 //      libraryDependencies += slf4jSimple,
       libraryDependencies += jena,
       libraryDependencies += arq,
-      libraryDependencies += antiXML
+      libraryDependencies += antiXML,
+      libraryDependencies += grizzled
     )
 
   lazy val yourProject = Project(
--- a/src/main/scala/Resource.scala	Mon Aug 29 00:17:02 2011 -0400
+++ b/src/main/scala/Resource.scala	Mon Aug 29 13:11:43 2011 -0400
@@ -16,7 +16,6 @@
 }
 trait Resource {
   def get():Model
-  def getAndCreateIfDoesNotExist():Model
   def save(model:Model):Unit
 }
 
@@ -30,6 +29,14 @@
     val relativePath:String = url.getPath.replaceAll("^"+basePath.toString, "")
     val fileOnDisk = new File(baseDirectory, relativePath)
     
+    private def createFileOnDisk():Unit = {
+      // create parent directory if needed
+      val parent = fileOnDisk.getParentFile
+      if (! parent.exists) println(parent.mkdirs)
+      val r = fileOnDisk.createNewFile()
+      logger.debug("Create file %s with success: %s" format (fileOnDisk.getAbsolutePath, r.toString))
+    }
+    
     def get():Model = {
       val fis = new FileInputStream(fileOnDisk)
       val m = ModelFactory.createDefaultModel()
@@ -42,26 +49,8 @@
       m
     }
     
-    def getAndCreateIfDoesNotExist():Model = {
-      val model = ModelFactory.createDefaultModel()
-      if (fileOnDisk exists) {
-        val fis = new FileInputStream(fileOnDisk)
-        model.read(fis, url.toString, lang)
-        fis.close()
-      }
-      // if file does not exist, create it
-      if (! fileOnDisk.exists) {
-        // create parent directory if needed
-        val parent = fileOnDisk.getParentFile
-        if (! parent.exists) println(parent.mkdirs)
-        val r = fileOnDisk.createNewFile()
-        logger.debug("Create file %s with success: %s" format 
-            (fileOnDisk.getAbsolutePath, r.toString))
-      }
-      model
-    }
-    
     def save(model:Model):Unit = {
+      createFileOnDisk()
       val fos = new FileOutputStream(fileOnDisk)
       val writer = model.getWriter("RDF/XML-ABBREV")
       writer.write(model, fos, url.toString)
--- a/src/test/scala/Test.scala	Mon Aug 29 00:17:02 2011 -0400
+++ b/src/test/scala/Test.scala	Mon Aug 29 13:11:43 2011 -0400
@@ -8,6 +8,7 @@
 import java.io._
 
 import com.codecommit.antixml._
+import grizzled.file.GrizzledFile._
 
 import com.hp.hpl.jena.rdf.model._
 import com.hp.hpl.jena.query._
@@ -24,8 +25,8 @@
   val joeOnDisk = new File(base, "2007/wiki/people/JoeLambda")
   
   doBeforeSpec {
-    if (joeOnDisk.exists) joeOnDisk.delete()   
-    if (! base.exists) base.mkdir()
+    base.deleteRecursively()
+    base.mkdir()
   }
   
   doAfterSpec {