+ URL resolver from local filesystem
authorAlexandre Bertails <bertails@w3.org>
Thu, 28 Jul 2011 11:22:58 -0400
changeset 9 0ee027a61ab9
parent 8 776f07e1dce3
child 10 9c673e76aaf8
+ URL resolver from local filesystem
src/main/scala/FragmentDependencies.scala
src/main/scala/URLResolver.scala
--- a/src/main/scala/FragmentDependencies.scala	Wed Jul 27 17:30:56 2011 -0400
+++ b/src/main/scala/FragmentDependencies.scala	Thu Jul 28 11:22:58 2011 -0400
@@ -270,9 +270,15 @@
 }
 
 
+
+import java.net.ResponseCache
+
 object Main extends App {
 
   override def main(args: Array[String]):Unit = {
+
+    ResponseCache.setDefault(URLResolver("/home/betehess/W3C"))
+
     args.toList match {
       case "info" :: _ => println("blah")
       case "dependencies" :: pages => {
@@ -288,8 +294,4 @@
 
   }
   
-
-
-  
-
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/scala/URLResolver.scala	Thu Jul 28 11:22:58 2011 -0400
@@ -0,0 +1,40 @@
+package org.w3.generator
+
+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
+    if (uriString startsWith "http://www.w3.org/") {
+      val local = uriString.replaceAll("^http://www.w3.org/", "WWW/")
+      val file = new File(base, local)
+      if (file exists) CachedResponse(file) else null
+    } else {
+      null
+    }
+  }
+
+  def put(uri:URI, conn:URLConnection) = null
+}
+
+object URLResolver {
+  def apply(base:String):URLResolver = URLResolver(new File(base))
+}
+