changeset 44:82ddda254f91

~ better API
author Alexandre Bertails <bertails@w3.org>
date Mon, 15 Aug 2011 16:19:37 -0400
parents 2cb26f40f2df
children 2cd3a150d581
files src/main/scala/Main.scala
diffstat 1 files changed, 32 insertions(+), 26 deletions(-) [+]
line wrap: on
line diff
--- a/src/main/scala/Main.scala	Mon Aug 15 12:07:10 2011 -0400
+++ b/src/main/scala/Main.scala	Mon Aug 15 16:19:37 2011 -0400
@@ -13,7 +13,6 @@
 
 
 /*
- * - refactoriser
  * - tests
  * - corriger url de base
  * - jtidy
@@ -90,6 +89,7 @@
         //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) }
 
         val defaultParams:Map[String, String] =
@@ -111,26 +111,17 @@
         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 paramsForPubrulesSpecialized = defaultParams ++ paramsNoURI + ( "uri" -> spec )
+
+        val pubrulesHTML = pubrulesXSL using paramsForPubrulesSpecialized applyOn pubrulesXML getSource()
 
         val frame = XSL("http://www.w3.org/2005/09/checker/frame.xsl") using ("display" -> "all") using ("recurse_auth" -> "on")
         
-        val pubrulesWrapper = XSL(frame applyOn pubrulesHTML)
+        val pubrulesWrapper = XSL(frame applyOn pubrulesHTML getSource())
 
-        new ResponseStreamer {
-          def stream(os:OutputStream):Unit = {
-            val out:Serializer = new Serializer()
-            out.setOutputStream(os)
+        val paramsW = defaultParams ++ paramsNoURI + ( "doc_uri" -> spec )        
 
-            val params = defaultParams ++ paramsNoURI + ( "doc_uri" -> spec )
-
-            pubrulesWrapper using params applyOn (Source(spec), out)
-
-          }
-        }
+        pubrulesWrapper using paramsW applyOn (Source(spec)) getResponder()
 
       }
     }
@@ -145,6 +136,8 @@
 
 
 case class XSL(transformer:XsltTransformer) {
+
+  private var source:Source = null
   
   def using(kv:(String, String)):XSL = {
     transformer.setParameter(new QName("", "", kv._1), new XdmAtomicValue(kv._2))
@@ -161,21 +154,34 @@
     this
   }
 
-  def applyOn(source: Source, destination:Destination): Unit = {
-    val doc:XdmNode = XSL.proc.newDocumentBuilder().build(source.source)
+  def applyOn(source:Source): XSL = {
+    this.source = source
+    this
+  }
+
+  def getSource(): Source = {
+    val xdm:XdmDestination = new XdmDestination()
+    this.applyOn(xdm)
+    Source(xdm.getXdmNode().asSource())    
+  }
+
+  def getResponder(): ResponseStreamer =
+    new ResponseStreamer {
+      def stream(os:OutputStream):Unit = {
+        val out:Serializer = new Serializer()
+        out.setOutputStream(os)
+        applyOn(out)
+      }
+    }
+
+  def applyOn(destination:Destination): Unit = {
+    val doc:XdmNode = XSL.proc.newDocumentBuilder().build(this.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())
-  }
+
 
 }
 
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.