~ refactoring webacl
authorAlexandre Bertails <bertails@w3.org>
Mon, 21 Nov 2011 15:26:58 -0500
branchwebacl
changeset 136 0012041ebba9
parent 135 071c0931f322
child 137 771587831866
~ refactoring
src/main/scala/rdf/ConcreteRDFModel.scala
src/main/scala/rdf/ConcreteTurtleSerializer.scala
src/main/scala/rdf/JenaModel.scala
src/main/scala/rdf/JenaTurtleSerializer.scala
src/main/scala/rdf/TurtleParser.scala
src/main/scala/rdf/main.scala
src/main/scala/rdf/turtle.scala
--- a/src/main/scala/rdf/ConcreteRDFModel.scala	Mon Nov 21 13:31:35 2011 -0500
+++ b/src/main/scala/rdf/ConcreteRDFModel.scala	Mon Nov 21 15:26:58 2011 -0500
@@ -1,5 +1,6 @@
-package org.w3.rdf
+package org.w3.rdf.concrete
 
+import org.w3.rdf.RDFModel
 import org.w3.isomorphic._
 
 trait ConcreteRDFModel extends RDFModel {
@@ -80,4 +81,3 @@
 
 object ConcreteRDFModel extends ConcreteRDFModel
 
-object ConcreteModelWithImplicits extends ConcreteRDFModel with Implicits
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/scala/rdf/ConcreteTurtleSerializer.scala	Mon Nov 21 15:26:58 2011 -0500
@@ -0,0 +1,55 @@
+package org.w3.rdf.concrete
+
+import org.w3.rdf.turtle.TurtleSerializer
+import java.net.URL
+
+object ConcreteTurtleSerializer extends TurtleSerializer[ConcreteRDFModel] {
+  
+  val rdf = ConcreteRDFModel
+  
+  def asString(g: rdf.Graph, base: URL): String = {
+    g map {
+      t =>
+        val rdf.Triple(rdf.SubjectNode(s), rdf.PredicateIRI(p), o) = t
+        try {
+          "%s %s %s" format (nodeStr(s), iriStr(p), objectStr(o))
+        } catch {
+          case e => {
+            println("=== "+t)
+            println("s: "+s)
+            println("p: "+p)
+            println("o: "+o)
+            throw e
+          }
+        }
+    } mkString "\n"
+  }
+
+  def objectStr(n: rdf.Object): String = {
+    n match {
+//       case l:rdf.ObjectLiteral => {
+//         val x:rdf.ObjectLiteral = l
+//         "**ObjectLiteral(" + x + ")**"
+//       }
+      case rdf.ObjectNode(n) => nodeStr(n)
+      case rdf.ObjectLiteral(l) => literalStr(l)
+      case x => { sys.error(x.toString) }
+    }
+  }
+
+  private def nodeStr(n: rdf.Node): String = {
+    n match {
+      case rdf.NodeIRI(i) => iriStr(i)
+      case rdf.NodeBNode(b) => bnodeStr(b)
+    }
+  }
+
+  private def iriStr(i: rdf.IRI): String =
+    "<%s>" format { val rdf.IRI(s) = i; s }
+
+  private def bnodeStr(b: rdf.BNode): String =
+    "_:" + { val rdf.BNode(l) = b; l }
+ 
+  private def literalStr(l: rdf.Literal): String = l.toString
+
+}
--- a/src/main/scala/rdf/JenaModel.scala	Mon Nov 21 13:31:35 2011 -0500
+++ b/src/main/scala/rdf/JenaModel.scala	Mon Nov 21 15:26:58 2011 -0500
@@ -1,12 +1,12 @@
 package org.w3.rdf.jena
 
-import org.w3.rdf._
+import org.w3.rdf.RDFModel
+import org.w3.readwriteweb.util.trySome
+import org.w3.isomorphic._
+
 import com.hp.hpl.jena.graph.{Graph => JenaGraph, Triple => JenaTriple, Node => JenaNode, _}
-import com.hp.hpl.jena.rdf.model.{AnonId}
+import com.hp.hpl.jena.rdf.model.AnonId
 import com.hp.hpl.jena.datatypes.{RDFDatatype, TypeMapper}
-import org.w3.readwriteweb.util.trySome
-
-import org.w3.isomorphic._
 
 trait JenaModel extends RDFModel {
 
@@ -132,4 +132,4 @@
 
 object JenaModel extends JenaModel
 
-object JenaModelWithImplicits extends JenaModel with Implicits
\ No newline at end of file
+//object JenaModelWithImplicits extends JenaModel with Implicits
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/scala/rdf/JenaTurtleSerializer.scala	Mon Nov 21 15:26:58 2011 -0500
@@ -0,0 +1,22 @@
+package org.w3.rdf.jena
+
+import org.w3.rdf.turtle.TurtleSerializer
+import org.w3.readwriteweb.TURTLE
+
+import scala.util.parsing.combinator._
+import java.net.URL
+import com.hp.hpl.jena.rdf.model.{Model, ModelFactory}
+
+object JenaTurtleSerializer extends TurtleSerializer[JenaModel] {
+  
+  val rdf = JenaModel
+  
+  def asString(g: rdf.Graph, base: URL): String = {
+    val model = ModelFactory.createModelForGraph(g.jenaGraph)
+    val writer = model.getWriter(TURTLE.jenaLang)
+    val sw = new java.io.StringWriter
+    writer.write(model, sw, base.toString)
+    sw.toString
+  }
+  
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/scala/rdf/TurtleParser.scala	Mon Nov 21 15:26:58 2011 -0500
@@ -0,0 +1,96 @@
+package org.w3.rdf.turtle
+
+import org.w3.rdf._
+import scala.util.parsing.combinator._
+
+object MyParsers extends RegexParsers {
+  val uri = """[a-zA-Z0-9:/#_\.\-\+]+""".r
+  val integer = """[0-9]+""".r
+  val float = """([0-9]+)?\.([0-9]+)?""".r
+  val name = """[a-zA-Z][a-zA-Z0-9_-]*|[a-zA-Z_][a-zA-Z0-9_]+""".r
+  var prefixes:Map[String, String] = Map()
+  var nextBNode = 1
+}
+
+import MyParsers._
+
+class TurtleParser extends JavaTokenParsers {
+  
+  def toGraph(t:String)(implicit rdf: RDFModel): rdf.Graph = parseAll(turtle, t).get
+
+  def toGraph(file:java.io.File)(implicit rdf: RDFModel): rdf.Graph = {
+    val t = scala.io.Source.fromFile(file).getLines.reduceLeft(_+_)
+    parseAll(turtle, t).get
+  }
+ 
+  def turtle(implicit rdf: RDFModel): Parser[rdf.Graph] =
+    opt(triplesblock) ^^ { case tbOPT => tbOPT.getOrElse(rdf.Graph.empty) }
+    
+  def prefixdecl(implicit rdf: RDFModel): Parser[Unit] =
+    "@prefix" ~ name ~ ":" ~ qnameORuri ~ "." ^^ { case "@prefix"~pre~":"~u~"." => prefixes += (pre -> { val rdf.IRI(i: String) = u ; i }) }
+    
+  def triplesblock(implicit rdf: RDFModel): Parser[rdf.Graph] =
+    rep(triplepatternOrPrefixOrBase) ^^ {
+      case pats => rdf.Graph(pats.flatten(_.toTraversable))
+    }
+    
+    def triplepatternOrPrefixOrBase(implicit rdf: RDFModel): Parser[Option[rdf.Triple]] = (
+        triplepattern ^^ { case p => Some(p) }
+      | prefixdecl ^^ { case _ => None }
+    )
+      
+    def triplepattern(implicit rdf: RDFModel): Parser[rdf.Triple] =
+      subject ~ predicate ~ objectt ~ "." ^^ { case s~p~o~"." => rdf.Triple(s, p, o) }
+  
+    def bnode(implicit rdf: RDFModel): Parser[rdf.BNode] =
+      "_:"~name ^^ { case "_:"~name => rdf.BNode(name) }
+
+    def literal(implicit rdf: RDFModel): Parser[rdf.Literal] = (
+        stringLiteral~"^^"~qnameORuri ^^ {
+        case lit~"^^"~dt => rdf.TypedLiteral(lit.substring(1,lit.size - 1), dt match {
+          case rdf.IRI("http://www.w3.org/2001/XMLSchema#string") => rdf.StringDatatype
+          case rdf.IRI("http://www.w3.org/2001/XMLSchema#integer") => rdf.IntegerDatatype
+          case rdf.IRI("http://www.w3.org/2001/XMLSchema#float") => rdf.FloatDatatype
+          case rdf.IRI("http://www.w3.org/2001/XMLSchema#date") => rdf.DateDatatype
+          case rdf.IRI("http://www.w3.org/2001/XMLSchema#dateTime") => rdf.DateTimeDatatype
+          case x => sys.error("only programed to deal with string and integer, not " + x)
+        })
+      } |
+        stringLiteral ^^ {
+        case lit => rdf.PlainLiteral(lit.substring(1,lit.size - 1), None)
+      } |
+        float ^^ { l => rdf.TypedLiteral(l, rdf.FloatDatatype) } |
+        integer ^^ { l => rdf.TypedLiteral(l, rdf.IntegerDatatype) }
+    )
+
+    def subject(implicit rdf: RDFModel): Parser[rdf.Subject] = {
+        qnameORuri ^^ { case x => rdf.SubjectNode(rdf.NodeIRI(x)) } |
+        bnode ^^ { case x => rdf.SubjectNode(rdf.NodeBNode(x)) }
+    }
+      
+    def predicate(implicit rdf: RDFModel): Parser[rdf.Predicate] = (
+        qnameORuri ^^ { case x => rdf.PredicateIRI(x) }
+      | "a" ^^ { x => rdf.PredicateIRI(rdf.IRI("http://www.w3.org/1999/02/22-rdf-syntax-ns#type")) }
+    )
+      
+    def objectt(implicit rdf: RDFModel): Parser[rdf.Object] = {
+        qnameORuri ^^ { case x => rdf.ObjectNode(rdf.NodeIRI(x)) } |
+        bnode ^^ { case x => rdf.ObjectNode(rdf.NodeBNode(x)) } |
+        literal ^^ { case x => rdf.ObjectLiteral(x) }
+    }
+  
+    def qnameORuri(implicit rdf: RDFModel): Parser[rdf.IRI] = (
+        "<"~uri~">" ^^ { case "<"~x~">" => rdf.IRI(x) } |
+        name~":"~name ^^ {
+        case prefix~":"~localName => try {
+          rdf.IRI(prefixes(prefix) + localName)
+        } catch {
+          case e:java.util.NoSuchElementException =>
+            throw new Exception("unknown prefix " + prefix)
+        }
+      }
+    )
+  
+}
+
+
--- a/src/main/scala/rdf/main.scala	Mon Nov 21 13:31:35 2011 -0500
+++ b/src/main/scala/rdf/main.scala	Mon Nov 21 15:26:58 2011 -0500
@@ -82,11 +82,9 @@
     val rdfxmlParser = org.w3.rdf.rdfxml.RDFXMLParser(rdf)
     
     val g: rdf.Graph = rdfxmlParser.parse(new File("src/test/resources/card.rdf"))
-
-//    val m:Graph = g.jenaGraph
     
     //val s = org.w3.rdf.turtle.ConcreteTurtleSerializer
-    val s = org.w3.rdf.turtle.JenaTurtleSerializer
+    val s = org.w3.rdf.jena.JenaTurtleSerializer
     
     println(s.asString(g, new java.net.URL("http://www.w3.org/People/Berners-Lee/card")))
     
--- a/src/main/scala/rdf/turtle.scala	Mon Nov 21 13:31:35 2011 -0500
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,173 +0,0 @@
-package org.w3.rdf.turtle
-
-import org.w3.rdf._
-import scala.util.parsing.combinator._
-import java.net.URI
-
-object MyParsers extends RegexParsers {
-  val uri = """[a-zA-Z0-9:/#_\.\-\+]+""".r
-  val integer = """[0-9]+""".r
-  val float = """([0-9]+)?\.([0-9]+)?""".r
-  val name = """[a-zA-Z][a-zA-Z0-9_-]*|[a-zA-Z_][a-zA-Z0-9_]+""".r
-  var prefixes:Map[String, String] = Map()
-  var nextBNode = 1
-}
-
-import MyParsers._
-
-class TurtleParser extends JavaTokenParsers {
-  
-  def toGraph(t:String)(implicit rdf: RDFModel): rdf.Graph = parseAll(turtle, t).get
-
-  def toGraph(file:java.io.File)(implicit rdf: RDFModel): rdf.Graph = {
-    val t = scala.io.Source.fromFile(file).getLines.reduceLeft(_+_)
-    parseAll(turtle, t).get
-  }
- 
-  def turtle(implicit rdf: RDFModel): Parser[rdf.Graph] =
-    opt(triplesblock) ^^ { case tbOPT => tbOPT.getOrElse(rdf.Graph.empty) }
-    
-  def prefixdecl(implicit rdf: RDFModel): Parser[Unit] =
-    "@prefix" ~ name ~ ":" ~ qnameORuri ~ "." ^^ { case "@prefix"~pre~":"~u~"." => prefixes += (pre -> { val rdf.IRI(i: String) = u ; i }) }
-    
-  def triplesblock(implicit rdf: RDFModel): Parser[rdf.Graph] =
-    rep(triplepatternOrPrefixOrBase) ^^ {
-      case pats => rdf.Graph(pats.flatten(_.toTraversable))
-    }
-    
-    def triplepatternOrPrefixOrBase(implicit rdf: RDFModel): Parser[Option[rdf.Triple]] = (
-        triplepattern ^^ { case p => Some(p) }
-      | prefixdecl ^^ { case _ => None }
-    )
-      
-    def triplepattern(implicit rdf: RDFModel): Parser[rdf.Triple] =
-      subject ~ predicate ~ objectt ~ "." ^^ { case s~p~o~"." => rdf.Triple(s, p, o) }
-  
-    def bnode(implicit rdf: RDFModel): Parser[rdf.BNode] =
-      "_:"~name ^^ { case "_:"~name => rdf.BNode(name) }
-
-    def literal(implicit rdf: RDFModel): Parser[rdf.Literal] = (
-        stringLiteral~"^^"~qnameORuri ^^ {
-        case lit~"^^"~dt => rdf.TypedLiteral(lit.substring(1,lit.size - 1), dt match {
-          case rdf.IRI("http://www.w3.org/2001/XMLSchema#string") => rdf.StringDatatype
-          case rdf.IRI("http://www.w3.org/2001/XMLSchema#integer") => rdf.IntegerDatatype
-          case rdf.IRI("http://www.w3.org/2001/XMLSchema#float") => rdf.FloatDatatype
-          case rdf.IRI("http://www.w3.org/2001/XMLSchema#date") => rdf.DateDatatype
-          case rdf.IRI("http://www.w3.org/2001/XMLSchema#dateTime") => rdf.DateTimeDatatype
-          case x => sys.error("only programed to deal with string and integer, not " + x)
-        })
-      } |
-        stringLiteral ^^ {
-        case lit => rdf.PlainLiteral(lit.substring(1,lit.size - 1), None)
-      } |
-        float ^^ { l => rdf.TypedLiteral(l, rdf.FloatDatatype) } |
-        integer ^^ { l => rdf.TypedLiteral(l, rdf.IntegerDatatype) }
-    )
-
-    def subject(implicit rdf: RDFModel): Parser[rdf.Subject] = {
-        qnameORuri ^^ { case x => rdf.SubjectNode(rdf.NodeIRI(x)) } |
-        bnode ^^ { case x => rdf.SubjectNode(rdf.NodeBNode(x)) }
-    }
-      
-    def predicate(implicit rdf: RDFModel): Parser[rdf.Predicate] = (
-        qnameORuri ^^ { case x => rdf.PredicateIRI(x) }
-      | "a" ^^ { x => rdf.PredicateIRI(rdf.IRI("http://www.w3.org/1999/02/22-rdf-syntax-ns#type")) }
-    )
-      
-    def objectt(implicit rdf: RDFModel): Parser[rdf.Object] = {
-        qnameORuri ^^ { case x => rdf.ObjectNode(rdf.NodeIRI(x)) } |
-        bnode ^^ { case x => rdf.ObjectNode(rdf.NodeBNode(x)) } |
-        literal ^^ { case x => rdf.ObjectLiteral(x) }
-    }
-  
-    def qnameORuri(implicit rdf: RDFModel): Parser[rdf.IRI] = (
-        "<"~uri~">" ^^ { case "<"~x~">" => rdf.IRI(x) } |
-        name~":"~name ^^ {
-        case prefix~":"~localName => try {
-          rdf.IRI(prefixes(prefix) + localName)
-        } catch {
-          case e:java.util.NoSuchElementException =>
-            throw new Exception("unknown prefix " + prefix)
-        }
-      }
-    )
-  
-}
-
-import java.net.URL
-trait TurtleSerializer[RDF <: RDFModel] {
-  
-  val rdf: RDF
-  
-  def asString(g: rdf.Graph, base: URL): String
-  
-}
-
-import org.w3.rdf.jena.JenaModel
-import com.hp.hpl.jena.rdf.model.{Model, ModelFactory}
-import org.w3.readwriteweb.TURTLE
-
-object JenaTurtleSerializer extends TurtleSerializer[JenaModel] {
-  
-  val rdf = JenaModel
-  
-  def asString(g: rdf.Graph, base: URL): String = {
-    val model = ModelFactory.createModelForGraph(g.jenaGraph)
-    val writer = model.getWriter(TURTLE.jenaLang)
-    val sw = new java.io.StringWriter
-    writer.write(model, sw, base.toString)
-    sw.toString
-  }
-  
-}
-
-object ConcreteTurtleSerializer extends TurtleSerializer[ConcreteRDFModel] {
-  
-  val rdf = ConcreteRDFModel
-  
-  def asString(g: rdf.Graph, base: URL): String = {
-    g map {
-      t =>
-        val rdf.Triple(rdf.SubjectNode(s), rdf.PredicateIRI(p), o) = t
-        try {
-          "%s %s %s" format (nodeStr(s), iriStr(p), objectStr(o))
-        } catch {
-          case e => {
-            println("=== "+t)
-            println("s: "+s)
-            println("p: "+p)
-            println("o: "+o)
-            throw e
-          }
-        }
-    } mkString "\n"
-  }
-
-  def objectStr(n: rdf.Object): String = {
-    n match {
-//       case l:rdf.ObjectLiteral => {
-//         val x:rdf.ObjectLiteral = l
-//         "**ObjectLiteral(" + x + ")**"
-//       }
-      case rdf.ObjectNode(n) => nodeStr(n)
-      case rdf.ObjectLiteral(l) => literalStr(l)
-      case x => { sys.error(x.toString) }
-    }
-  }
-
-  private def nodeStr(n: rdf.Node): String = {
-    n match {
-      case rdf.NodeIRI(i) => iriStr(i)
-      case rdf.NodeBNode(b) => bnodeStr(b)
-    }
-  }
-
-  private def iriStr(i: rdf.IRI): String =
-    "<%s>" format { val rdf.IRI(s) = i; s }
-
-  private def bnodeStr(b: rdf.BNode): String =
-    "_:" + { val rdf.BNode(l) = b; l }
- 
-  private def literalStr(l: rdf.Literal): String = l.toString
-
-}