changeset 24:943aa66639b2

~ deal with files accessible through classpath
author Alexandre Bertails <bertails@w3.org>
date Mon, 08 Aug 2011 14:19:30 -0400
parents f348c0cc2fa7
children 41bb4358478e
files src/main/scala/URLResolver.scala
diffstat 1 files changed, 28 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/src/main/scala/URLResolver.scala	Mon Aug 08 14:18:34 2011 -0400
+++ b/src/main/scala/URLResolver.scala	Mon Aug 08 14:19:30 2011 -0400
@@ -4,6 +4,14 @@
 import java.io.{File, InputStream, FileInputStream}
 import java.util.{Map => JMap, List => JList}
 
+
+import java.net._
+import java.io._
+import java.util.jar._
+import scala.collection.JavaConversions._
+import scala.io.Source
+
+
 case class CachedResponse(in:InputStream) extends CacheResponse {
   def getBody() = in
   def getHeaders() = null
@@ -22,12 +30,25 @@
           rqstMethod:String,
           rqstHeaders:JMap[String, JList[String]]):CacheResponse = {
     val uriString = uri.toString
-    // println(uriString)
+    println("Trying to resolve: "+uriString)
     URLResolver.prefixes find { uriString startsWith _._1 } match {
       case Some((prefix, path)) => {
         val local = uriString.replaceAll("^"+prefix, path)
         val file = new File(base, local)
-        if (file exists) CachedResponse(file) else { println("not found: "+uriString); null }
+        // priority to filesystem or to embedded files ?
+
+        if (file exists) {
+          CachedResponse(file)
+        } else {
+          println("Not found on filesystem: "+uriString)
+          val urlClasspath = URLResolver.classloader.getResource(local)
+          if (urlClasspath == null) {
+            println("Not found in classpath: "+urlClasspath)
+            null
+          } else {
+            CachedResponse(urlClasspath.openStream())
+          }
+        }
       }
       case _ => null
     }
@@ -36,8 +57,13 @@
   def put(uri:URI, conn:URLConnection) = null
 }
 
+
 object URLResolver {
 
+  val clazz:Class[_] = this.getClass
+  val classloader = this.getClass.getClassLoader
+
+
   val prefixes = Map(
     "http://www.w3.org/TR/ruby/"   -> "WWW/TR/2001/REC-ruby-20010531/",
     "http://www.w3.org/TR/xhtml1/" -> "WWW/TR/2002/REC-xhtml1-20020801/",
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.