+ Argot library for parsing
authorAlexandre Bertails <bertails@w3.org>
Sat, 08 Oct 2011 16:45:44 -0400
changeset 47 5733487193e1
parent 46 c75484bb9208
child 48 055d27e60d30
child 104 10f12bf8bbbd
+ Argot library for parsing
+ N3 backend available (--language on the command line)
project/build.scala
src/main/scala/Main.scala
src/main/scala/Resource.scala
src/test/scala/Test.scala
--- a/project/build.scala	Thu Oct 06 20:27:12 2011 -0400
+++ b/project/build.scala	Sat Oct 08 16:45:44 2011 -0400
@@ -21,9 +21,7 @@
   val arq = "com.hp.hpl.jena" % "arq" % "2.8.8"
   val grizzled = "org.clapper" %% "grizzled-scala" % "1.0.8" % "test"
   val scalaz = "org.scalaz" %% "scalaz-core" % "6.0.2"
-
-
-
+  val argot =  "org.clapper" %% "argot" % "0.3.5"
 }
 
 // some usefull repositories
@@ -87,7 +85,8 @@
       libraryDependencies += antiXML,
       libraryDependencies += grizzled,
       libraryDependencies += scalaz,
-      jarName in assembly := "read-write-web.jar"
+      jarName in assembly := "read-write-web.jar",
+      libraryDependencies += argot
     )
 
   lazy val project = Project(
--- a/src/main/scala/Main.scala	Thu Oct 06 20:27:12 2011 -0400
+++ b/src/main/scala/Main.scala	Sat Oct 08 16:45:44 2011 -0400
@@ -138,50 +138,67 @@
 }
 
 
+import org.clapper.argot._
+import ArgotConverters._
+
 object ReadWriteWebMain {
 
   val logger:Logger = LoggerFactory.getLogger(this.getClass)
 
-  // regular Java main
-  def main(args: Array[String]) {
-    
-    val argsList = args.toList
-    
-    val (port, baseDirectory, baseURL) = argsList match {
-      case port :: directory :: base :: _ => (port.toInt, new File(directory), base)
-      case _ => {
-        println(
-"""example usage:
-    java -jar read-write-web.jar 8080 ~/WWW/2011/09 /2011/09 [-strict|-relax]
+  val parser = new ArgotParser("read-write-web")
 
-Options:
- -relax all resources potentially exist, meaning you get an empty RDF graph instead of a 404 (still experimental)
- -strict a GET on a resource will fail with a 404 (default mode if you omit it)
-""")
-        System.exit(1)
-        null
+  val mode = parser.option[RWWMode](List("mode"), "m", "wiki mode") {
+    (sValue, opt) =>
+      sValue match {
+        case "wiki" => AllResourcesAlreadyExist
+        case "strict" => ResourcesDontExistByDefault
+        case _ => throw new ArgotConversionException("Option %s: must be either wiki or strict" format (opt.name, sValue))
       }
     }
 
-    val mode =
-      if (argsList contains "-relax") {
-        logger.info("info: using experimental relax mode")
-        AllResourcesAlreadyExist
-      } else {
-        ResourcesDontExistByDefault
+  val rdfLanguage = parser.option[String](List("language"), "l", "RDF language") {
+    (sValue, opt) =>
+      sValue match {
+        case "n3" => "N3"
+        case "turtle" => "N3"
+        case "rdfxml" => "RDF/XML-ABBREV"
+        case _ => throw new ArgotConversionException("Option %s: must be either n3, turtle or rdfxml" format (opt.name, sValue))
       }
-    
-    if (! baseDirectory.exists) {
-      println("%s does not exist" format (baseDirectory.getAbsolutePath))
-      System.exit(2)
+  }
+
+  val port = parser.parameter[Int]("port", "Port to use", false)
+
+  val rootDirectory = parser.parameter[File]("rootDirectory", "root directory", false) {
+    (sValue, opt) => {
+      val file = new File(sValue)
+      if (! file.exists)
+        throw new ArgotConversionException("Option %s: %s must be a valid path" format (opt.name, sValue))
+      else
+        file
+    }
+  }
+
+  val baseURL = parser.parameter[String]("baseURL", "base URL", false)
+
+  // regular Java main
+  def main(args: Array[String]) {
+
+    try {
+      parser.parse(args)
+    } catch {
+      case e: ArgotUsageException => println(e.message); System.exit(1)
     }
 
-    val filesystem = new Filesystem(baseDirectory, baseURL, lang="TURTLE")(mode)
+    val filesystem =
+      new Filesystem(
+        rootDirectory.value.get,
+        baseURL.value.get,
+        lang=rdfLanguage.value getOrElse "N3")(mode.value getOrElse ResourcesDontExistByDefault)
     
     val app = new ReadWriteWeb(filesystem)
 
     // configures and launches a Jetty server
-    unfiltered.jetty.Http(port).filter {
+    unfiltered.jetty.Http(port.value.get).filter {
       // a jee Servlet filter that logs HTTP requests
       new Filter {
         def destroy():Unit = ()
--- a/src/main/scala/Resource.scala	Thu Oct 06 20:27:12 2011 -0400
+++ b/src/main/scala/Resource.scala	Sat Oct 08 16:45:44 2011 -0400
@@ -20,12 +20,16 @@
   def sanityCheck():Boolean
   def resource(url:URL):Resource
 }
+
 trait Resource {
   def get():Validation[Throwable, Model]
   def save(model:Model):Validation[Throwable, Unit]
 }
 
-class Filesystem(baseDirectory:File, val basePath:String, val lang:String = "RDF/XML-ABBREV")(implicit mode:RWWMode) extends ResourceManager {
+class Filesystem(
+  baseDirectory: File,
+  val basePath: String,
+  val lang: String = "RDF/XML-ABBREV")(mode: RWWMode) extends ResourceManager {
   
   val logger:Logger = LoggerFactory.getLogger(this.getClass)
   
@@ -43,22 +47,23 @@
       logger.debug("Create file %s with success: %s" format (fileOnDisk.getAbsolutePath, r.toString))
     }
     
-    def get():Validation[Throwable, Model] = {
-      val m = ModelFactory.createDefaultModel()
+    def get(): Validation[Throwable, Model] = {
+      val model = ModelFactory.createDefaultModel()
       if (fileOnDisk.exists()) {
         val fis = new FileInputStream(fileOnDisk)
         try {
-          m.read(fis, url.toString)
+          val reader = model.getReader(lang)
+          reader.read(model, fis, url.toString)
         } catch {
-          case je:JenaException => error("File %s was either empty or corrupted: considered as empty graph" format fileOnDisk.getAbsolutePath)
+          case je:JenaException => error("@@@")
         }
         fis.close()
-        m.success
+        model.success
       } else {
         mode match {
-          case AllResourcesAlreadyExist => m.success
+          case AllResourcesAlreadyExist => model.success
           case ResourcesDontExistByDefault => new FileNotFoundException().fail
-      }
+        }
       }
     }
     
@@ -66,7 +71,7 @@
       try {
         createFileOnDisk()
         val fos = new FileOutputStream(fileOnDisk)
-        val writer = model.getWriter("RDF/XML-ABBREV")
+        val writer = model.getWriter(lang)
         writer.write(model, fos, url.toString)
         fos.close().success
       } catch {
--- a/src/test/scala/Test.scala	Thu Oct 06 20:27:12 2011 -0400
+++ b/src/test/scala/Test.scala	Sat Oct 08 16:45:44 2011 -0400
@@ -36,7 +36,7 @@
 //    if (joeOnDisk.exists) joeOnDisk.delete()
   }
   
-  val filesystem = new Filesystem(base, "/2007/wiki")(ResourcesDontExistByDefault)
+  val filesystem = new Filesystem(base, "/2007/wiki", "N3")(ResourcesDontExistByDefault)
 
   def setup = { _.filter(new ReadWriteWeb(filesystem).read) }