changeset 371:25da059442b5

+ ddl query parameter
author Alexandre Bertails <bertails@w3.org>
date Wed, 04 May 2011 15:35:48 -0400
parents 3a9aa3b600f8
children 4f0ff4640841
files directmapping-webapp/src/main/scala/code/snippet/HelloWorld.scala directmapping-webapp/src/main/scala/code/snippet/Servlet.scala directmapping-webapp/src/main/webapp/index.html
diffstat 3 files changed, 111 insertions(+), 215 deletions(-) [+]
line wrap: on
line diff
--- a/directmapping-webapp/src/main/scala/code/snippet/HelloWorld.scala	Tue May 03 13:43:48 2011 -0400
+++ b/directmapping-webapp/src/main/scala/code/snippet/HelloWorld.scala	Wed May 04 15:35:48 2011 -0400
@@ -73,6 +73,114 @@
 }
 
 
+class DirectMappingDemo extends JenaModel with RDF2RDFModule with DirectMappingModule {
+
+  import DirectMapping._
+
+  val SQLParser = sql.SqlParser()
+
+  val defaultDDL = "ref_no_pk"
+
+  def render = {
+    val (ddlName, ddl, ddlMessage) = S.param("ddl") match {
+      case Full(name) if QueryManager.getQueries contains name => {
+        (name, QueryManager.getQueries(name), "DDL successfully loaded")
+      }
+      case Full(name) => {
+        (defaultDDL, QueryManager.getQueries(defaultDDL), name + "not found: default DDL used instead")
+      }
+      case _ =>  {
+        (defaultDDL, QueryManager.getQueries(defaultDDL), "Default DDL used")
+      }
+    }
+
+    "#ddl-name [value]" #> ddlName &
+    "#ddl *" #> ddl &
+    "#ddl-message *" #> ddlMessage
+  }
+
+
+
+
+}
+
+object QueryManager {
+
+  import java.net._
+  import java.io._
+  import java.util.jar._
+  import scala.collection.JavaConversions._
+  import scala.io.Source
+
+  /** http://www.uofr.net/~greg/java/get-resource-listing.html */
+  def getResourceListing(path:String):List[String] = {
+    val clazz:Class[_] = this.getClass
+    var dirURL:URL = clazz.getClassLoader().getResource(path)
+    if (dirURL != null && dirURL.getProtocol == "file") {
+      /* A file path: easy enough */
+      new File(dirURL.toURI).list.toList
+    } else {
+      if (dirURL == null) {
+        val me = clazz.getName().replace(".", "/")+".class"
+        dirURL = clazz.getClassLoader().getResource(me)
+      }
+      if (dirURL.getProtocol == "jar") {
+        val jarPath = dirURL.getPath.substring(5, dirURL.getPath().indexOf("!"))
+        val jar:JarFile = new JarFile(URLDecoder.decode(jarPath, "UTF-8"))
+        val entries = jar.entries filter { _.getName startsWith path } map { e => {
+          var entry = e.getName substring path.length
+          val checkSubdir = entry indexOf "/"
+          if (checkSubdir >= 0) entry = entry.substring(0, checkSubdir)
+          entry
+        } }
+        
+        entries
+      }    
+      error("Cannot list files for URL "+dirURL);
+    }
+  }
+
+  lazy val getQueries:Map[String, String] = {
+    val classloader = this.getClass.getClassLoader
+    val entries = getResourceListing("queries/")
+    val queries = entries map { entry => {
+      val url = classloader.getResource("queries/" + entry)
+      val query = Source.fromURL(url, "UTF-8").getLines.mkString("\n")
+      (entry, query)
+    } } toMap
+
+    queries
+  }
+
+}
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
 
 class DirectMapping extends JenaModel with RDF2RDFModule with DirectMappingModule with TurtleModule {
 
--- a/directmapping-webapp/src/main/scala/code/snippet/Servlet.scala	Tue May 03 13:43:48 2011 -0400
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,214 +0,0 @@
-package org.w3.directmapping.servlet
-
-import javax.servlet.http.{HttpServlet, HttpServletRequest, HttpServletResponse} 
-import scala.xml.XML
-import scala.xml.dtd.{DocType, PublicID}
-
-import org.w3.rdf._
-import org.w3.rdb.RDB._
-import org.w3.sql
-import org.w3.rdf.turtle.TurtleModule
-import org.w3.directmapping.DirectMappingModule
-import java.io.File
-
-import org.w3.rdf.jena._
-
-import java.util.regex._ 
-
-object DirectMappingWebapp {
-
-  val defaultSQL = QueryManager.getQueries("defaultSQL")
-
-  val encoding = "utf-8"
-
-  val Style = """
-    #sql { background-color: #e7e7ff; float:left; bordeHer: thin solid #888888; padding: 1em; }
-    #result { background-color: #eee; float:left; border: thin solid #888888; padding: 1em; }
-    .clear { clear:both; }
-    input { margin-bottom: -1em; }
-"""
-
-  def format(varr:String, sql:String):String = {
-    val formattedSQL = sql.replaceAll("\n+$", "").replaceAll("\n", "\\\\n\\\\\n")
-    "var " + varr + " = '" + formattedSQL + "';"
-  }
-
-  def validJSvar(varr:String) = {
-    if ("^[0-9]".r.findFirstIn(varr).isDefined)
-      "_" + varr
-    else
-      varr
-  }
-
-  val Script:String = QueryManager.getQueries map { case (varr, sql) => format(validJSvar(varr), sql) } mkString "\n\n"
-
-
-
-  def renderVar(varr:String) = 
-    <p><input value={ varr } type="button" onclick={ "document.getElementById('sql').value = " + validJSvar(varr) + ";" } /></p>
-
-}
-
-import DirectMappingWebapp._
-
-class DirectMappingWebapp extends HttpServlet with JenaModel with DirectMappingModule with TurtleModule {
-
-  import DirectMapping._
-
-  val SQLParser = sql.SqlParser()
-
-  val turtleParser = new TurtleParser { }
-
-  def jenaSerializer(g:Graph):String = {
-    val m = com.hp.hpl.jena.rdf.model.ModelFactory.createModelForGraph(g.jenaGraph)
-    val s = new java.io.StringWriter
-    m.write(s, "TURTLE")
-    s.toString
-//     m.write(s, "N-TRIPLE")
-//     val r = s.toString
-// println("r: " + r)
-// val p = Pattern.compile("\\\\u([0-9A-F]{4})").matcher(r)
-// val sb = new StringBuffer(r.size) ; val rsb = new StringBuffer(4)
-
-// while (p.find) { rsb.replace(0, rsb.length, p.group(1)) ; p.appendReplacement(sb, Character(rsb)) }
-// p.appendTail(sb)
-//     sb.toString
-  }
-
-  def minEncodeElement (minEncode:Boolean):scala.xml.Elem =
-    if (minEncode) <input name="minEncode" checked="on" type="checkbox" />
-    else <input name="minEncode" type="checkbox" />
-
-  override def doPost(request:HttpServletRequest, response:HttpServletResponse) = {
-    val minEncode = request.getParameter("minEncode") == "on"
-    request.getParameter("sql") match {
-      case null | "" => processIndex(request, response, minEncode)
-      case sql       => processSQL(request, response, sql, minEncode)
-    }
-  }
-
-  override def doGet(request:HttpServletRequest, response:HttpServletResponse) = doPost(request, response)
-
-  val xhtmlDoctype = DocType("html",
-                             PublicID("-//W3C//DTD XHTML 1.0 Strict//EN",
-                                      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"),
-                             Nil)
-
-  def render(sql:String, minEncode:Boolean, result:Option[String]) =
-    <html xmlns="http://www.w3.org/1999/xhtml">
-      <head>
-        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
-        <title>Direct Mapping Demo</title>
-      <style type="text/css" media="print, screen and (min-width: 481px)">{ Style }
-      </style>
-      <script type="text/javascript">{ Script }
-      </script>
-      </head>
-      <body>
-        <h1>Direct Mapping Demo</h1>
-	<p>Input some DDL/SQL and see the resulting <a href="http://www.w3.org/2001/sw/rdb2rdf/directMapping/">Direct Graph</a>. This interface uses GET (vs. POST) so the length of the input SQL will be limited on servers which impose a limit on the URL length.</p>
-	<p>Please send bug reports to <a href="mailto:public-rdb2rdf-comments@w3.org">public-rdb2rdf-comments@w3.org</a>.</p>
-        <form method="get" action="/">
-          <p><textarea rows="15" cols="80" name="sql" id="sql">{ sql }</textarea></p>
-	  <div style="float:right;">
-	  <div style="float:left;">
-            { List("emp_addr", "hier_tabl_proto", "hier_table", "multi_key", "ref_no_pk") map renderVar }
-	  </div>
-	  <div style="float:left;">
-            { List("1table0rows", "1table1row", "1table2columns1row", "1table3columns1row", "2duplicates0nulls", "varchar_varchar_1row", "1table1compositeprimarykey3columns1row", "1table1primarykey1column1row", "2tables1primarykey1foreingkey") map renderVar }
-	  </div>
-	  </div>
-	  <p>{ minEncodeElement(minEncode) }Minimally encode IRI components (just encode [%&lt;+ /=#&gt;])</p>
-	  <p><input value="clear" type="button" onclick="document.getElementById('sql').value = '';" /></p>
-	  <p><input id="submit" value="submit SQL" type="submit" /></p>
-        </form>
-	<div class="clear"/>
-        { if (result isDefined) <pre name="result" id="result">{ result.get }</pre> }
-        <hr class="clear" />
-        <address>
-          <a href="http://www.w3.org/People/Eric/">Eric Prud'hommeaux</a>, <a href="http://www.w3.org/People/Bertails/">Alexandre Bertails</a>, Feb 2011
-        </address>
-      </body>
-    </html>
-
-  def processSQL(request:HttpServletRequest, response:HttpServletResponse, sql:String, minEncode:Boolean) {
-
-    val result:String =
-      try {
-        val db = SQLParser.toDB(sql)
-        DirectMapping.MinEncode = minEncode
-        val computedGraph:Graph = databaseSemantics(db)
-        jenaSerializer(computedGraph)
-      } catch {
-        case e => e.getMessage
-      }
-
-    val html = render(sql, minEncode, Some(result))
-
-    response.setContentType("application/xml; charset='" + encoding + "'")
-
-    XML.write(response.getWriter, html, encoding, false, xhtmlDoctype) 
-
-  }
-
-  def processIndex(request:HttpServletRequest, response:HttpServletResponse, minEncode:Boolean) {
-
-    val index = render(defaultSQL, minEncode, None)
-
-    response.setContentType("application/xml; charset='" + encoding + "'")
-
-    XML.write(response.getWriter, index, encoding, false, xhtmlDoctype)
-
-  }
-
-}
-
-object QueryManager {
-
-  import java.net._
-  import java.io._
-  import java.util.jar._
-  import scala.collection.JavaConversions._
-  import scala.io.Source
-
-  /** http://www.uofr.net/~greg/java/get-resource-listing.html */
-  def getResourceListing(path:String):List[String] = {
-    val clazz:Class[_] = this.getClass
-    var dirURL:URL = clazz.getClassLoader().getResource(path)
-    if (dirURL != null && dirURL.getProtocol == "file") {
-      /* A file path: easy enough */
-      new File(dirURL.toURI).list.toList
-    } else {
-      if (dirURL == null) {
-        val me = clazz.getName().replace(".", "/")+".class"
-        dirURL = clazz.getClassLoader().getResource(me)
-      }
-      if (dirURL.getProtocol == "jar") {
-        val jarPath = dirURL.getPath.substring(5, dirURL.getPath().indexOf("!"))
-        val jar:JarFile = new JarFile(URLDecoder.decode(jarPath, "UTF-8"))
-        val entries = jar.entries filter { _.getName startsWith path } map { e => {
-          var entry = e.getName substring path.length
-          val checkSubdir = entry indexOf "/"
-          if (checkSubdir >= 0) entry = entry.substring(0, checkSubdir)
-          entry
-        } }
-        
-        entries
-      }    
-      error("Cannot list files for URL "+dirURL);
-    }
-  }
-
-  lazy val getQueries:Map[String, String] = {
-    val classloader = this.getClass.getClassLoader
-    val entries = getResourceListing("queries/")
-    val queries = entries map { entry => {
-      val url = classloader.getResource("queries/" + entry)
-      val query = Source.fromURL(url, "UTF-8").getLines.mkString("\n")
-      (entry, query)
-    } } toMap
-
-    queries
-  }
-
-}
--- a/directmapping-webapp/src/main/webapp/index.html	Tue May 03 13:43:48 2011 -0400
+++ b/directmapping-webapp/src/main/webapp/index.html	Wed May 04 15:35:48 2011 -0400
@@ -77,6 +77,8 @@
 
   </head>
   <body>
+
+<div class="lift:DirectMappingDemo">
     <h1>Direct Mapping Demo</h1>
     <p>This webapp lets you play with the Direct Mapping: give it some
     DDL/SQL, tweak the
@@ -134,6 +136,6 @@
       <a href="http://www.w3.org/People/Eric/">Eric Prud'hommeaux</a>, <a href="http://www.w3.org/People/Bertails/">Alexandre Bertails</a>, Apr 2011<br />
       Contributors: <a href="mailto:bvillazon@fi.upm.es">Boris Villazon-Terrazas</a>, <a href="mailto:tgambet@w3.org">Thomas Gambet</a>
     </address>
-    
+</div>
   </body>
 </html>
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.