changeset 15:b8d7acb0f7b2

~ add this, latest and previous uris retrieval new constructor for Date taking YYYYMMDD
author Alexandre Bertails <bertails@w3.org>
date Wed, 03 Mar 2010 11:34:58 -0500
parents dfc3f2700da3
children cc6c7c639fe8
files src/main/scala/Date.scala src/main/scala/Main.scala src/main/scala/Model.scala src/main/scala/XML.scala src/test/scala/DocumentTest.scala src/test/scala/Test.scala
diffstat 6 files changed, 96 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/src/main/scala/Date.scala	Tue Mar 02 15:08:04 2010 -0500
+++ b/src/main/scala/Date.scala	Wed Mar 03 11:34:58 2010 -0500
@@ -1,6 +1,8 @@
+package org.w3.prc.util
+
 import org.joda.time._
 
-case class Date(date:DateTime) extends Ordered[Date] {
+case class Date(date:ReadableDateTime) extends Ordered[Date] {
 
   override def compare(that : Date):Int = this.date compareTo that.date
 
@@ -12,4 +14,5 @@
 
   def apply():Date = Date(new DateTime())
 
+  def apply(year:Int, month:Int, day:Int):Date = Date(new DateMidnight(year, month, day))
 }
--- a/src/main/scala/Main.scala	Tue Mar 02 15:08:04 2010 -0500
+++ b/src/main/scala/Main.scala	Wed Mar 03 11:34:58 2010 -0500
@@ -42,6 +42,25 @@
     // parse and inject the found editors into Editor
     dds map (dd => Editor.parse(dd.text)) toSeq
   }
-  
+
+  def versionUriAux(m:String):Seq[String] = {
+    val versionblock = divHead + "//html:dl"
+    val dtXPath = versionblock + "/html:dt[matches(normalize-space(.),'version','i') and matches(normalize-space(.),'" + m + "','i')][1]"
+    val dt = doc.evaluateSingle(dtXPath).get
+    val ddXPath = "$trd:dt/following-sibling::html:dd[preceding-sibling::html:dt[1] is $trd:dt]"
+    val dd = dt.evaluateSingle(ddXPath, "dt" -> dt).get
+    val vurisXPath = "$trd:dd//html:a/@href"
+    // val htmls = dd flatMap (dd => dd.evaluateAsString(vurisXPath, "dd" -> dd))
+    val htmls = dd.evaluate(vurisXPath, "dd" -> dd) map (_.normalizeSpace)
+    htmls toSeq
+  }
+
+  def thisVersionUri:DatedUri = DatedUri(versionUriAux("this")(0))
+
+  def previousVersionUris:Seq[DatedUri] = versionUriAux("previous") map (DatedUri(_))
+
+  def previousVersionUri:DatedUri = previousVersionUris max
+
+  def latestVersionUri:ShortUri = ShortUri(versionUriAux("latest")(0))
 
 }
--- a/src/main/scala/Model.scala	Tue Mar 02 15:08:04 2010 -0500
+++ b/src/main/scala/Model.scala	Wed Mar 03 11:34:58 2010 -0500
@@ -1,6 +1,28 @@
 package org.w3.prc
 
 import scala.xml.Elem
+import org.w3.prc.util.Date
+
+abstract class Uri {
+  val uri:String
+  override def toString:String = uri.toString
+  final def hasTrailingSlash:Boolean = uri endsWith "/"
+}
+
+case class DatedUri(uri:String) extends Uri with Ordered[DatedUri] {
+
+  def date:Date = {
+    val r = """-(\d{4})(\d{2})(\d{2})""".r
+    val m = r.findFirstMatchIn(uri).get
+    Date(m.group(1).toInt, m.group(2).toInt, m.group(3).toInt)
+  }
+
+  override def compare(that : DatedUri):Int = date compare that.date
+
+}
+
+case class ShortUri(uri:String) extends Uri
+
 
 case class Email(address:String)
 
--- a/src/main/scala/XML.scala	Tue Mar 02 15:08:04 2010 -0500
+++ b/src/main/scala/XML.scala	Wed Mar 03 11:34:58 2010 -0500
@@ -116,6 +116,8 @@
 				    OccurrenceIndicator.ONE_OR_MORE)
     val selector:XPathSelector = xpathCompiler.compile(xpath).load()
     selector.setContextItem(node)
+    for((v, n) <- variables)
+      selector.setVariable(new QName(NAMESPACE, v), n.node)
     selector
   }
 
@@ -130,8 +132,6 @@
    */
   def evaluateSingle(xpath:String, variables:(String, Node)*):Option[Node] = {
     val selector = getXPathSelector(xpath, variables.toList)
-    for((v, n) <- variables)
-      selector.setVariable(new QName(NAMESPACE, v), n.node)
     Option(selector.evaluateSingle()) flatMap ((item:XdmItem) => Some(Node(item.asInstanceOf[XdmNode])))
   }
 
@@ -141,13 +141,27 @@
    */
   def evaluate(xpath:String, variables:(String, Node)*):Iterator[Node] = {
     val selector = getXPathSelector(xpath, variables.toList)
-    for((v, n) <- variables)
-      selector.setVariable(new QName(NAMESPACE, v), n.node)
     JIteratorWrapper(selector.iterator()) map ((item:XdmItem) => Node(item.asInstanceOf[XdmNode]))
   }
 
+  private def evaluateAsXdmAtomicValues(xpath:String, variables:List[(String, Node)]):Iterator[XdmAtomicValue] = {
+    val selector = getXPathSelector(xpath, variables)
+    JIteratorWrapper(selector.iterator()) map (_.asInstanceOf[XdmAtomicValue])
+  }
+
+  private def evaluateSingleAsXdmAtomicValues(xpath:String, variables:List[(String, Node)]):Option[XdmAtomicValue] = {
+    val selector = getXPathSelector(xpath, variables)
+    Option(selector.evaluateSingle()) flatMap ((item:XdmItem) => Some(item.asInstanceOf[XdmAtomicValue]))
+  }
+
+  def evaluateAsString(xpath:String, variables:(String, Node)*):Iterator[String] =
+    evaluateAsXdmAtomicValues(xpath, variables.toList) map (_.toString)
+
+  def evaluateSingleAsString(xpath:String, variables:(String, Node)*):Option[String] =
+    evaluateSingleAsXdmAtomicValues(xpath, variables.toList) map (_.toString)
+
   override def toString = node.toString
 
   def text:String = evaluateSingle("text()").get.node.toString
-
+  def normalizeSpace:String = evaluateSingleAsString("normalize-space()").get
 }
--- a/src/test/scala/DocumentTest.scala	Tue Mar 02 15:08:04 2010 -0500
+++ b/src/test/scala/DocumentTest.scala	Wed Mar 03 11:34:58 2010 -0500
@@ -13,8 +13,25 @@
 			      Editor("Eve", "Maler"),
 			      Editor("David", "Orchard"),
 			      Editor("Norman", "Walsh"))
+
     assert(expectedEditors === document.editors.toList.toSet)
 
+    val thisVersionUri = document.thisVersionUri
+    assert(DatedUri("http://www.w3.org/TR/2010/PR-xlink11-20100225/") === thisVersionUri)
+    assert(thisVersionUri hasTrailingSlash)
+
+    val expectedpreviousVersionUris = Seq(DatedUri("http://www.w3.org/TR/2008/WD-xlink11-20080331/"),
+					  DatedUri("http://www.w3.org/TR/2006/CR-xlink11-20060328/"))
+    assert(expectedpreviousVersionUris === document.previousVersionUris)
+
+    val previousVersionUri = document.previousVersionUri
+    assert(DatedUri("http://www.w3.org/TR/2008/WD-xlink11-20080331/") === previousVersionUri)
+    assert(previousVersionUri hasTrailingSlash)
+
+    val latestVersionUri = document.latestVersionUri
+    assert(ShortUri("http://www.w3.org/TR/xlink11/") === latestVersionUri)
+    assert(latestVersionUri hasTrailingSlash)
+
   }
 
   test("WD-xmldsig-properties-20100204") {
@@ -26,6 +43,15 @@
     val expectedEditors = Set(Editor("Frederick", "Hirsch"))
     assert(expectedEditors === document.editors.toList.toSet)
 
+    assert(DatedUri("http://www.w3.org/TR/2010/WD-xmldsig-properties-20100204/") === document.thisVersionUri)
+
+    val expectedpreviousVersionUris = Seq(DatedUri("http://www.w3.org/TR/2009/WD-xmldsig-properties-20090430/"))
+    assert(expectedpreviousVersionUris === document.previousVersionUris)
+
+    assert(DatedUri("http://www.w3.org/TR/2009/WD-xmldsig-properties-20090430/") === document.previousVersionUri)
+
+    assert(ShortUri("http://www.w3.org/TR/xmldsig-properties/") === document.latestVersionUri)
+
   }
 
 }
--- a/src/test/scala/Test.scala	Tue Mar 02 15:08:04 2010 -0500
+++ b/src/test/scala/Test.scala	Wed Mar 03 11:34:58 2010 -0500
@@ -4,7 +4,11 @@
 class Test extends FunSuite {
 
   test("") {
-
+    // val document = Document("file://src/test/resources/PR-xlink11-20100225.html")
+    // println(document.versionUriAux("latest"))
+    // println(document.previousVersionUri)
+    // println(document.latestVersionUri)
+    // println(document.latestVersionUri.hasTrailingSlash)
   }
 
 }
Set up and maintained by W3C Systems Team, please report bugs to sysreq@w3.org.

W3C would like to thank Microsoft who donated the server that allows us to run this service.