changeset 49:b84b6e775783

~ intercept the call to tidy
author Alexandre Bertails <bertails@w3.org>
date Wed, 28 Sep 2011 16:28:16 -0400
parents 0991de4541f4
children c144438f08b0
files src/main/scala/Main.scala src/main/scala/URLResolver.scala
diffstat 2 files changed, 41 insertions(+), 20 deletions(-) [+]
line wrap: on
line diff
--- a/src/main/scala/Main.scala	Wed Sep 28 15:06:28 2011 -0400
+++ b/src/main/scala/Main.scala	Wed Sep 28 16:28:16 2011 -0400
@@ -209,7 +209,7 @@
     val streamSource = new StreamSource(url.openStream())
     streamSource.setSystemId(url.toString)
     Source(streamSource)
-    }
+  }
 
   def apply(url:String):Source = Source(new URL(url))
 
@@ -218,7 +218,7 @@
     * we may have to use a local tidy if jtidy is not enough:
     *   https://lists.w3.org/Archives/Team/w3t-sys/2011SepOct/0162.html
     */
-  def tidy(in:InputStream, charset:String):InputStream = {
+  def jtidy(in:InputStream, charset:String):InputStream = {
     val tidy = new Tidy
     // tidy_options = ["-n", "-asxml", "-q", "--force-output","yes", "--show-warnings", "no"]
     tidy.setXHTML(true)
@@ -247,12 +247,20 @@
   // }
 
   def fromTidy(url:URL):Source = {
-    val is:InputStream = Http(new Request(url.toString) >> (tidy _))
-    val streamSource = new StreamSource(is)
+    val tidyURL = new URL("http://services.w3.org/tidy/tidy?docAddr=" + url.toString)
+    val streamSource = new StreamSource(tidyURL.openStream())
     streamSource.setSystemId(url.toString)
     Source(streamSource)
   }
 
+
+  // def fromTidyOld(url:URL):Source = {
+  //   val is:InputStream = Http(new Request(url.toString) >> (tidy _))
+  //   val streamSource = new StreamSource(is)
+  //   streamSource.setSystemId(url.toString)
+  //   Source(streamSource)
+  // }
+
   def fromTidy(url:String):Source = Source.fromTidy(new URL(url))
 
 }
--- a/src/main/scala/URLResolver.scala	Wed Sep 28 15:06:28 2011 -0400
+++ b/src/main/scala/URLResolver.scala	Wed Sep 28 16:28:16 2011 -0400
@@ -28,26 +28,31 @@
           rqstMethod:String,
           rqstHeaders:JMap[String, JList[String]]):CacheResponse = {
     val uriString = uri.toString
-    URLResolver.prefixes find { uriString startsWith _._1 } match {
-      case _ if uriString startsWith "http://www.w3.org/services/headers?url=" =>
-        URLResolver.headers(uriString.substring(39))
-      case Some((prefix, path)) => {
-        val local = uriString.replaceAll("^"+prefix, path)
-        val file = new File(base, local)
-        // priority to filesystem or to embedded files ?
-        if (file exists) {
-          CachedResponse(file)
-        } else {
-          val urlClasspath = URLResolver.classloader.getResource(local)
-          if (urlClasspath == null) {
-            println("I need to dereference " + uriString)
-            null
+    // println(uriString)
+    if (uriString startsWith "http://www.w3.org/services/headers?url=")
+      URLResolver.headers(uriString.substring(39))
+    else if (uriString startsWith "http://services.w3.org/tidy/tidy?docAddr=")
+      URLResolver.tidy(uriString.substring(41))
+    else {
+      URLResolver.prefixes find { uriString startsWith _._1 } match {
+        case Some((prefix, path)) => {
+          val local = uriString.replaceAll("^"+prefix, path)
+          val file = new File(base, local)
+          // priority to filesystem or to embedded files ?
+          if (file exists) {
+            CachedResponse(file)
           } else {
-            CachedResponse(urlClasspath.openStream())
+            val urlClasspath = URLResolver.classloader.getResource(local)
+            if (urlClasspath == null) {
+              // println("I need to dereference " + uriString)
+              null
+            } else {
+              CachedResponse(urlClasspath.openStream())
+            }
           }
         }
+        case _ => null
       }
-      case _ => null
     }
   }
 
@@ -73,6 +78,14 @@
       case _ => null
     }
 
+  def tidy(url:String):CachedResponse =
+    try {
+      println("### it doesn't make a call to tidy. You should use scala.sys.process.Process to call it from the command line")
+      CachedResponse(new URL(url).openStream())
+    } catch {
+      case _ => null
+    }
+
   val clazz:Class[_] = this.getClass
   val classloader = this.getClass.getClassLoader
 
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.