changeset 3:1133234bce9a

~ can query a list a list of items
author Alexandre Bertails <bertails@w3.org>
date Wed, 24 Feb 2010 15:24:25 -0500
parents 3990c89ea03b
children e70e06b19b1a
files src/main/scala/Main.scala src/test/scala/Test.scala
diffstat 2 files changed, 22 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/src/main/scala/Main.scala	Wed Feb 24 14:23:51 2010 -0500
+++ b/src/main/scala/Main.scala	Wed Feb 24 15:24:25 2010 -0500
@@ -4,6 +4,7 @@
 import Http._
 import net.sf.saxon.s9api._
 import javax.xml.transform.stream.StreamSource
+import scala.collection.JavaConversions.JIteratorWrapper
 
 object Document {
 
@@ -78,27 +79,38 @@
   }
 
   /**
-   * evaluates an xpath expression against a document
+   * prepare an XPath selector against the document, ready to be used
    * xhtml namespace is assumed
    */
-  def evaluateSingle(doc:XdmNode, xpath:String):XdmItem = {
-
+  def getXPathSelector(doc:XdmNode, xpath:String):XPathSelector = {
     val proc:Processor = doc.getProcessor
     val xpathCompiler:XPathCompiler = proc.newXPathCompiler();
     xpathCompiler.declareNamespace("xhtml", "http://www.w3.org/1999/xhtml")
     val selector:XPathSelector = xpathCompiler.compile(xpath).load()
     selector.setContextItem(doc)
-    selector.evaluateSingle()
+    selector
+  }
 
-  }
+  /**
+   * evaluates an xpath expression against a document
+   * TODO
+   */
+  def evaluateSingle(doc:XdmNode, xpath:String):Option[XdmItem] = Option(getXPathSelector(doc, xpath).evaluateSingle())
+
+  /**
+   * evaluates an xpath expression against a document
+   * TODO
+   */
+  def evaluate(doc:XdmNode, xpath:String):Iterator[XdmItem] = JIteratorWrapper(getXPathSelector(doc, xpath).iterator())
 
 }
 
 case class Document(url:String) {
 
-  val doc:XdmNode = Document.convertUrlToXdmNode(url)
+  private val doc:XdmNode = Document.convertUrlToXdmNode(url)
 
-  def evaluateSingle(xpath:String):XdmItem = Document.evaluateSingle(doc, xpath)
+  def evaluateSingle(xpath:String):Option[XdmItem] = Document.evaluateSingle(doc, xpath)
 
+  def evaluate(xpath:String):Iterator[XdmItem] = Document.evaluate(doc, xpath)
 
 }
--- a/src/test/scala/Test.scala	Wed Feb 24 14:23:51 2010 -0500
+++ b/src/test/scala/Test.scala	Wed Feb 24 15:24:25 2010 -0500
@@ -9,7 +9,9 @@
 
     println(document.evaluateSingle("//xhtml:h2"))
 
-    assert(1 === 1)
+    for(item <- document.evaluate("//xhtml:h2"))
+      println("-- " + item)
+
   }
 
 }
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.