Permalink
Browse files

Upgraded to Saxon 9.3, convert it's XdmNode output to Scala native XML

  • Loading branch information...
1 parent 3ce2579 commit e39a86e80ebff0f2ffe89259d97cbf4bea3cea64 @LeifW committed Nov 22, 2010
@@ -1,9 +1,9 @@
package org.musicpath
-import net.sf.saxon.functions.ExtensionFunctionCall
+import net.sf.saxon.lib.ExtensionFunctionCall
import net.sf.saxon.om.SequenceIterator
-import net.sf.saxon.om.SingletonIterator.makeIterator
-import net.sf.saxon.om.SingleNodeIterator
+import net.sf.saxon.tree.iter.SingletonIterator.makeIterator
+//import net.sf.saxon.om.SingleNodeIterator
import net.sf.saxon.expr.XPathContext
import net.sf.saxon.om.StructuredQName
import net.sf.saxon.value.SequenceType.{SINGLE_STRING, SINGLE_ELEMENT_NODE}
@@ -1,17 +1,17 @@
package org.musicpath
-import net.sf.saxon.functions.ExtensionFunctionDefinition
+import net.sf.saxon.lib.ExtensionFunctionDefinition
import net.sf.saxon.om.StructuredQName
import net.sf.saxon.value.SequenceType
import SequenceType.{SINGLE_STRING, SINGLE_ELEMENT_NODE, OPTIONAL_DOCUMENT_NODE}
object ExtFunDef extends ExtensionFunctionDefinition {
val getFunctionQName = new StructuredQName("mp", "http://musicpath.org/ns/xsparql-fn#", "sparql")
- val getMinimumNumberOfArguments = 1
- //val getMaximumNumberOfArguments = 1
+ override val getMinimumNumberOfArguments = 1
+ override val getMaximumNumberOfArguments = 1
val getArgumentTypes = List(SINGLE_STRING).toArray
def getResultType(suppliedArgumentTypes:Array[SequenceType]) = OPTIONAL_DOCUMENT_NODE
- def makeCallExpression = ExtFunCall
+ val makeCallExpression = ExtFunCall
}
// vim: set ts=4 sw=4 et:
@@ -8,7 +8,9 @@ import scala.xml.{ProcInstr,NodeSeq,Text}
import com.thinkminimo.step._ // Web framework
import net.croz.scardf._ // Jena wrapper
import com.hp.hpl.jena.rdf.model.ResourceFactory //
+import saxon2scala.Saxon2Scala.convert
import Scene._ // Predicates in musicpath ontology
+import net.sf.saxon.s9api.{Serializer,XdmNode}
//import com.tristanhunt._
// This class mostly defines routes. A couple view helpers are factored out into the "View" object.
@@ -157,8 +159,15 @@ class MusicPath extends Step {
val req = Res(res.plural+"/"+params(":id"))
if (req/RDF.Type isEmpty)
redirect(params(":id")+"/edit")
- else
- XQueryCall.run(new File(res.plural, res.plural+".xquery"), baseUrl+res.plural+'/'+params(":id"))
+ else {
+ val templated = convert(XQueryCall.run(new File(res.plural, res.plural+".xquery"), baseUrl+res.plural+'/'+params(":id")))
+ <html xmlns="http://www.w3.org/1999/xhtml">
+ <head>
+ <title>{templated\"@title"}</title>
+ </head>
+ <body>{templated}</body>
+ </html>
+ }
}
// GET /resourcetype/id/xml
@@ -2,6 +2,7 @@ package org.musicpath
import net.sf.saxon.s9api._
import java.io.File
+import java.io.OutputStream
import java.net.URI
object XQueryCall {
@@ -14,8 +15,11 @@ object XQueryCall {
val evaluator = query.load
//evaluator.setExternalVariable(new QName("this"), new XdmAtomicValue(new URI(self)))
evaluator.setExternalVariable(new QName("this"), new XdmAtomicValue('<'+self+'>'))
- val result = evaluator.evaluate
- result.toString
+ //val destination = new Serializer
+ //destination.setOutputStream(ostream)
+ //evaluator.setDestination(destination)
+ //evaluator.run(destination)
+ evaluator.evaluateSingle
}
}
@@ -0,0 +1,39 @@
+package org.musicpath.saxon2scala
+
+import net.sf.saxon.s9api._
+import XdmNodeKind._
+import scala.collection.jcl.MutableIterator.Wrapper
+import scala.xml._
+object Saxon2Scala {
+ implicit def toScalaCollection(it:java.util.Iterator[XdmItem]) = new Wrapper(it)
+ implicit def convert(node:XdmItem):Node = traverse(node, TopScope, Set())
+ implicit def item2Node(i:XdmItem):XdmNode = i.asInstanceOf[XdmNode]
+ protected def saxon2scalaAttr(scalaAtts:MetaData, saxonAtt:XdmItem) = {
+ // not dealing withi namespaced attributed yet.
+ new UnprefixedAttribute(saxonAtt.getNodeName.getLocalName, saxonAtt.getStringValue, scalaAtts)
+ }
+ protected def traverse(node:XdmNode, scope:NamespaceBinding, defaultNSs:Set[String]):Node = node.getNodeKind match {
+ case TEXT => Text(node.getStringValue)
+ case ELEMENT => val attributes = node.axisIterator(Axis.ATTRIBUTE).foldLeft(Null.asInstanceOf[MetaData])(saxon2scalaAttr)
+ val qname = node.getNodeName
+ val namespace = qname.getNamespaceURI
+ val prefix = qname.getPrefix
+ val (newScope, newDefaults) =
+ if (namespace != "")
+ if (prefix == "")
+ if (defaultNSs.contains(namespace))
+ (scope, defaultNSs)
+ else
+ (new NamespaceBinding(null, namespace, scope), defaultNSs + namespace)
+ else
+ if (scope.getPrefix(namespace) == prefix)
+ (scope, defaultNSs)
+ else
+ (new NamespaceBinding(prefix, namespace, scope), defaultNSs)
+ else
+ (scope, defaultNSs)
+ Elem(if (prefix == "") null else prefix, qname.getLocalName, attributes, newScope, node.axisIterator(Axis.CHILD).map((i)=>traverse(i, newScope, newDefaults)).toList: _*)
+ }
+}
+
+// vim: set ts=4 sw=4 et:

0 comments on commit e39a86e

Please sign in to comment.