changeset 21:01104d643edf

+ URLResolver
author Alexandre Bertails <bertails@w3.org>
date Sat, 06 Aug 2011 10:15:01 -0400
parents fe54e09128fb
children 526f2fa99fb0
files src/main/scala/URLResolver.scala
diffstat 1 files changed, 50 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/scala/URLResolver.scala	Sat Aug 06 10:15:01 2011 -0400
@@ -0,0 +1,50 @@
+package org.w3.prc
+
+import java.net.{URI, CacheResponse, ResponseCache, URLConnection}
+import java.io.{File, InputStream, FileInputStream}
+import java.util.{Map => JMap, List => JList}
+
+case class CachedResponse(in:InputStream) extends CacheResponse {
+  def getBody() = in
+  def getHeaders() = null
+}
+
+object CachedResponse {
+  def apply(file:File):CachedResponse = CachedResponse(new FileInputStream(file))
+  def apply(filename:String):CachedResponse = CachedResponse(new File(filename))
+}
+
+case class URLResolver(base:File) extends ResponseCache {
+
+  assert(base exists, base.getAbsolutePath + " does not exist")
+
+  def get(uri:URI,
+          rqstMethod:String,
+          rqstHeaders:JMap[String, JList[String]]):CacheResponse = {
+    val uriString = uri.toString
+    println(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 null
+      }
+      case _ => null
+    }
+  }
+
+  def put(uri:URI, conn:URLConnection) = null
+}
+
+object URLResolver {
+
+  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/",
+    "http://www.w3.org/" -> "WWW/"
+  )
+
+  def apply(base:String):URLResolver = URLResolver(new File(base))
+
+}
+
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.