+ ObjectURI parser
authorEric Prud'hommeaux <bertails@w3.org>
Fri, 11 Dec 2009 16:00:59 -0500
changeset 18 43d31777516b
parent 17 f08e5d0a1540
child 19 70407126e7b1
+ ObjectURI parser
src/main/scala/SPARQL.scala
src/test/scala/SparqlTest.scala
--- a/src/main/scala/SPARQL.scala	Fri Dec 11 14:28:11 2009 -0500
+++ b/src/main/scala/SPARQL.scala	Fri Dec 11 16:00:59 2009 -0500
@@ -11,12 +11,16 @@
 case class TriplePatterns(triplepatterns:List[TriplePattern])
 case class TriplePattern(s:S, p:P, o:O)
 
+case class ObjUri(stem:Stem, rel:Rel, attr:Attr, v:CellValue)
+
+case class CellValue(s:String)
+
 sealed abstract class S
-case class SUri(uri:URI) extends S
+case class SUri(obj:ObjUri) extends S
 case class SVar(v:Var) extends S
 
 sealed abstract class O
-case class OUri(uri:URI) extends O
+case class OUri(obj:ObjUri) extends O
 case class OVar(v:Var) extends O
 case class OLit(lit:SparqlLiteral) extends O
 
@@ -47,7 +51,7 @@
     subject ~ predicate ~ objectt ^^ { case s~p~o => TriplePattern(s, p, o) }
 
   def subject:Parser[S] = (
-      uri ^^ { x => SUri(new URI(x)) }
+      "<"~uri~">" ^^ { case "<"~x~">" => SUri(Sparql.parseObjectURI(x)) }
     | varr ^^ { x => SVar(x) }
   )
 
@@ -55,7 +59,7 @@
     "<"~uri~">" ^^ { case "<"~x~">" => Sparql.parsePredicateURI(x) }
 
   def objectt:Parser[O] = (
-      uri ^^ { x => OUri(new URI(x)) }
+      "<"~uri~">" ^^ { case "<"~x~">" => OUri(Sparql.parseObjectURI(x)) }
     | varr ^^ { x => OVar(x) }
     | literal ^^ { x => OLit(x) }
   )
@@ -73,12 +77,26 @@
 
 object Sparql {
 
+  /* stemURI + '/' + (\w+) + '#' (\w+) */
   def parsePredicateURI(x:String):PUri = {
     val uri = new URI(x)
-    val path = uri.getPath().split("/")
-    val subPath = path.toList.remove(_ == "").slice(0, path.size - 2).mkString("/")
+    val path = uri.getPath().split("/").toList.remove(_ == "")
+    val subPath = path.slice(0, path.size - 1).mkString("/")
     val stem = uri.getScheme() + "://" + uri.getAuthority + "/" + subPath
     PUri(Stem(stem), Rel(path.last), Attr(uri.getFragment))
   }
 
+  /* stemURI + '/' (\w+) '/' (\w+) '.' (\w+) '#record' */
+  def parseObjectURI(x:String):ObjUri = {
+    val uri = new URI(x)
+    val path = uri.getPath().split("/").toList.remove(_ == "")
+    val subPath = path.slice(0, path.size - 2).mkString("/")
+    val rel = path(path.size - 2)
+    println("attrPair:" + path(path.size-1))
+    val attrPair = path(path.size-1).split("\\.")
+    val stem = uri.getScheme() + "://" + uri.getAuthority + "/" + subPath
+    assert("record" == uri.getFragment)
+    ObjUri(Stem(stem), Rel(rel), Attr(attrPair(0)), CellValue(attrPair(1)))
+  }
+
 }
--- a/src/test/scala/SparqlTest.scala	Fri Dec 11 14:28:11 2009 -0500
+++ b/src/test/scala/SparqlTest.scala	Fri Dec 11 16:00:59 2009 -0500
@@ -78,11 +78,20 @@
   }
 
   test("decompose a predicate uri in stem, rel and attr") {
-    val uri = "http://hr.example/DB/Employee#lastName"
+    val uri = "http://hr.example/our/favorite/DB/Employee#lastName"
     val puri:PUri = Sparql.parsePredicateURI(uri)
-    assert(puri === PUri(Stem("http://hr.example/DB"),
+    assert(puri === PUri(Stem("http://hr.example/our/favorite/DB"),
 			 Rel("Employee"),
 			 Attr("lastName")))
   }
 
+  test("decompose a object uri in stem, rel and attr") {
+    val uri = "http://hr.example/our/favorite/DB/Employee/id.18#record"
+    val objuri:ObjUri = Sparql.parseObjectURI(uri)
+    assert(objuri === ObjUri(Stem("http://hr.example/our/favorite/DB"),
+			 Rel("Employee"),
+			 Attr("id"),
+			 CellValue("18")))
+  }
+
 }