changeset 40:bdffa232c3c1

~ working with denis
author Alexandre Bertails <bertails@w3.org>
date Mon, 15 Aug 2011 11:47:14 -0400
parents 9119a7b3a784
children 5b107b4bc71f
files src/main/scala/Main.scala
diffstat 1 files changed, 88 insertions(+), 68 deletions(-) [+]
line wrap: on
line diff
--- a/src/main/scala/Main.scala	Thu Aug 11 18:53:05 2011 -0400
+++ b/src/main/scala/Main.scala	Mon Aug 15 11:47:14 2011 -0400
@@ -13,10 +13,13 @@
 
 
 /*
- * - isoler/factoriser le template
- * - recuperer l'input en tant que parametre
- * - generer un .jar
- * - integrer le xsl du pubrules checker
+ * - refactoriser
+ * - tests
+ * - corriger url de base
+ * - jtidy
+ * - gestion des exceptions
+ * - customiser xsl
+ * 
  */
 
 
@@ -62,27 +65,6 @@
       case _        => ()
     }
 
-    val resp = new ResponseStreamer {
-      def stream(os:OutputStream):Unit = {
-
-        val s = "http://www.w3.org/TR/2011/CR-WOFF-20110804/"
-        val xsl = "http://www.w3.org/2001/10/trdoc2rdf.xslt"
-        val o = "/tmp/toto2.xml"
-        
-        val proc:Processor = new Processor(false)
-        val comp:XsltCompiler = proc.newXsltCompiler()
-        val xsltStreamSource = new StreamSource(new URL(xsl).openStream())
-        xsltStreamSource.setSystemId(xsl)
-        val exp:XsltExecutable = comp.compile(xsltStreamSource)
-        val source:XdmNode = proc.newDocumentBuilder().build(new StreamSource(new URL(s).openStream()))
-        val out:Serializer = new Serializer()
-        out.setOutputStream(os)
-        val trans:XsltTransformer = exp.load()
-        trans.setInitialContextNode(source)
-        trans.setDestination(out)
-        trans.transform()
-      }
-    }
 
     val echo = unfiltered.filter.Planify {
       case Path(Seg(p :: Nil)) => ResponseString(p)
@@ -104,7 +86,9 @@
     val pubrules = unfiltered.filter.Planify {
       case Path("/2005/07/pubrules") & Params(params) if params contains "uri" => {
 
+
         //val spec = "http://www.w3.org/TR/2004/WD-i18n-html-tech-char-20040509/"
+
         val spec = params("uri").head
         val paramsNoURI = params - "uri" map { case(k, v) => (k, v.head) }
 
@@ -122,61 +106,32 @@
             "display" -> "all",
             "recursive" -> "off",
             "recurse_auth" -> "on")
-        // 13-pubrules-src.html | 13-pubrules.xsl -> xsl1
-        // param: uri
 
-        val proc:Processor = new Processor(false)
-        val comp:XsltCompiler = proc.newXsltCompiler()
+        val pubrulesXSL = XSL("http://www.w3.org/2005/07/13-pubrules.xsl")
+        val pubrulesXML = Source("http://www.w3.org/2005/07/13-pubrules-src.html")
+
+        // because they are Maps, paramsNoURI overrides the default parameters in defaultParams
+        val paramsForPubrulesSpecialized =
+          defaultParams ++ paramsNoURI + ( "uri" -> spec )
+        val pubrulesHTML =
+          pubrulesXSL using paramsForPubrulesSpecialized applyOn pubrulesXML
+
+        val frame = XSL("http://www.w3.org/2005/09/checker/frame.xsl") using ("display" -> "all") using ("recurse_auth" -> "on")
         
-        val xsl1 = "http://www.w3.org/2005/07/13-pubrules.xsl"
-        val xslt1StreamSource = new StreamSource(new URL(xsl1).openStream())
-        xslt1StreamSource.setSystemId(xsl1)
-        val exp1:XsltExecutable = comp.compile(xslt1StreamSource)
-        val source1:XdmNode =
-          proc.newDocumentBuilder().build(new StreamSource(new URL("http://www.w3.org/2005/07/13-pubrules-src.html").openStream()))
-        val trans1:XsltTransformer = exp1.load()
-        trans1.setInitialContextNode(source1)
-        trans1.setParameter(new QName("", "", "uri"), new XdmAtomicValue(spec))
-        (defaultParams ++ paramsNoURI + ( "uri" -> spec )) foreach { case (k, v) => trans1.setParameter(new QName("", "", k), new XdmAtomicValue(v))}
-
-        // xsl1 | frame.xsl -> xsl2
-        val frame = "http://www.w3.org/2005/09/checker/frame.xsl"
-        val xslt2StreamSource = new StreamSource(new URL(frame).openStream())
-        xslt2StreamSource.setSystemId(frame)
-        val exp2:XsltExecutable = comp.compile(xslt2StreamSource)
-        val trans2:XsltTransformer = exp2.load()
-        trans2.setParameter(new QName("", "", "display"), new XdmAtomicValue("all"))
-        trans2.setParameter(new QName("", "", "recurse_auth"), new XdmAtomicValue("on"))
+        val pubrulesWrapper = frame applyOn pubrulesSpecialized
 
         new ResponseStreamer {
           def stream(os:OutputStream):Unit = {
             val out:Serializer = new Serializer()
             out.setOutputStream(os)
-            //out.setOutputProperty(Serializer.Property.METHOD, "xhtml");
-            //out.setOutputProperty(Serializer.Property.MEDIA_TYPE, "text/html");
-            //out.setOutputProperty(Serializer.Property.INDENT, "yes");
-            trans1.setDestination(trans2)
-            val xsl3:XdmDestination = new XdmDestination()
-            trans2.setDestination(xsl3)
-            trans1.transform()
 
-            // xhtml | xsl2 -> result
-            // param: doc_uri
-            val exp3:XsltExecutable = comp.compile(xsl3.getXdmNode().asSource())
-            val trans3:XsltTransformer = exp3.load()
-            val doc:XdmNode =
-              proc.newDocumentBuilder().build(new StreamSource(new URL(spec).openStream()))
-            trans3.setInitialContextNode(doc)
-//            trans3.setParameter(new QName("", "", "doc_uri"), new XdmAtomicValue(spec))
-            (defaultParams ++ paramsNoURI + ( "doc_uri" -> spec )) foreach { case (k, v) => trans3.setParameter(new QName("", "", k), new XdmAtomicValue(v))}
-            trans3.setDestination(out)
-            trans3.transform()
+            val params = defaultParams ++ paramsNoURI + ( "doc_uri" -> spec )
+
+            pubrulesWrapper using params applyOn (Source(spec))
 
           }
         }
 
-        //val url = params("url").head
-//        pubrulesXSLT(new URL("http://www.w3.org/2005/07/13-pubrules-src.html"), xsltParams)
       }
     }
 
@@ -187,3 +142,68 @@
   }
 
 }
+
+
+case class XSL(transformer:XsltTransformer) {
+  
+  def using(kv:(String, String)):XSL = {
+    transformer.setParameter(new QName("", "", kv._1), new XdmAtomicValue(kv._2))
+    this
+  }
+
+  def using(params:Map[String, String]):XSL = {
+    params foreach { case (k, v) => transformer.setParameter(new QName("", "", k), new XdmAtomicValue(v)) }
+    this
+  }
+
+  def applyOn(xsl:XSL):XSL = {
+    xsl.transformer setDestination this.transformer
+    this
+  }
+
+  def applyOn(source: Source, destination:Destination): Unit = {
+    val doc:XdmNode = XSL.proc.newDocumentBuilder().build(source.source)
+    transformer.setInitialContextNode(doc)
+    transformer.setDestination(destination)
+    transformer.transform()
+  }
+
+  def applyOn(source: Source): Source = {
+    val doc:XdmNode = XSL.proc.newDocumentBuilder().build(source.source)
+    transformer.setInitialContextNode(doc)
+    val xdm:XdmDestination = new XdmDestination()
+    transformer.setDestination(xdm)
+    transformer.transform()
+    Source(xdm.getXdmNode().asSource())
+  }
+
+}
+
+object XSL {
+
+  val proc:Processor = new Processor(false)
+  val comp:XsltCompiler = proc.newXsltCompiler()
+  
+  def apply(source:Source):XSL = {
+    val executable:XsltExecutable = comp.compile(source.source)
+    val transformer:XsltTransformer = executable.load()
+    XSL(transformer)
+  }
+
+  def apply(url:URL):XSL = XSL(Source(url))
+
+  def apply(url:String):XSL = XSL(Source(url))
+  
+}
+
+case class Source(source:javax.xml.transform.Source)
+
+object Source {
+  /* see http://www.stylusstudio.com/xsllist/200601/post60480.html */
+  def apply(url:URL):Source = {
+    val streamSource = new StreamSource(url.openStream())
+    streamSource.setSystemId(url.toString)
+    Source(streamSource)
+    }
+  def apply(url:String):Source = Source(new URL(url))
+}
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.