changeset 352:f3cf25dbba52

+ switch for Urldecod
author Eric Prud'hommeaux <eric@w3.org>
date Wed, 16 Feb 2011 17:56:29 -0500
parents c74c83253026
children 883351632db8
files directmapping-webapp/src/main/scala/Servlet.scala directmapping/src/main/scala/DirectMapping.scala
diffstat 2 files changed, 41 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/directmapping-webapp/src/main/scala/Servlet.scala	Wed Feb 16 13:21:52 2011 -0500
+++ b/directmapping-webapp/src/main/scala/Servlet.scala	Wed Feb 16 17:56:29 2011 -0500
@@ -13,6 +13,8 @@
 
 import org.w3.rdf.jena._
 
+import java.util.regex._ 
+
 object DirectMappingWebapp {
 
   val defaultSQL = 
@@ -216,15 +218,24 @@
   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, "N-TRIPLE")
+    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
   }
 
   override def doPost(request:HttpServletRequest, response:HttpServletResponse) = {
-    val hierarchy = request.getParameter("hierarchy") == "on"
+    val urlencode = request.getParameter("urlencode") == "on"
     request.getParameter("sql") match {
       case null | "" => processIndex(request, response)
-      case sql       => processSQL(request, response, sql, hierarchy)
+      case sql       => processSQL(request, response, sql, urlencode)
     }
   }
 
@@ -259,7 +270,7 @@
             { List("1table0rows", "1table1row", "1table2columns1row", "1table3columns1row", "2duplicates0nulls", "varchar_varchar_1row", "1table1compositeprimarykey3columns1row", "1table1primarykey1column1row", "2tables1primarykey1foreingkey") map renderVar }
 	  </div>
 	  </div>
-	  <p><input name="hierarchy" checked="checked" type="checkbox" />Detects hierarchy relation</p>
+	  <p><input name="urlencode" checked="checked" type="checkbox" />Urlencode IRI components (vs. just encode [%,#=/>])</p>
 	  <p><input value="clear" type="button" onclick="document.getElementById('sql').value = '';" /></p>
 	  <p><input id="submit" value="submit SQL" type="submit" /></p>
         </form>
@@ -272,12 +283,12 @@
       </body>
     </html>
 
-  def processSQL(request:HttpServletRequest, response:HttpServletResponse, sql:String, hierarchy:Boolean) {
+  def processSQL(request:HttpServletRequest, response:HttpServletResponse, sql:String, urlencode:Boolean) {
 
-    val result =
+    val result:String =
       try {
         val db = SQLParser.toDB(sql)
-        DirectMapping.HierarchyDetection = hierarchy
+        DirectMapping.Urlencode = urlencode
         val computedGraph:Graph = directDB(db)
         jenaSerializer(computedGraph)
       } catch {
--- a/directmapping/src/main/scala/DirectMapping.scala	Wed Feb 16 13:21:52 2011 -0500
+++ b/directmapping/src/main/scala/DirectMapping.scala	Wed Feb 16 17:56:29 2011 -0500
@@ -3,6 +3,7 @@
 import org.w3.rdf.{Model => RDFModel, Implicits => RDFImplicits, _}
 
 import org.w3.rdb.RDB._
+import java.net.URLEncoder
 
 trait DirectMappingModule extends RDFModel {
 
@@ -60,6 +61,8 @@
      */
     var HierarchyDetection = true
 
+    var Urlencode = true
+
     /**
      * The mapping functions implementing
      * <http://www.w3.org/2001/sw/rdb2rdf/directGraph/>
@@ -198,8 +201,10 @@
       IRI(UE(rn) + "/" + pairs.mkString("_") + "#_")
     }
   
-    def predicatemap (rn:RelName, as:List[AttrName]) : IRI =
-      IRI(UE(rn) + "#" + as.mkString("_"))
+    def predicatemap (rn:RelName, as:List[AttrName]) : IRI = {
+      val encoded = as.map {c => UE(c)} mkString("_")
+      IRI(UE(rn) + "#" + encoded)
+    }
 
     // TODO: aren't they already part of the RDF model?
     def XSD (d:Datatype) : IRI =
@@ -220,7 +225,22 @@
 	case _ => TypedLiteral(l.s, XSD(d))
       }
   
-    def UE (s:String) : String = s.replaceAll(" ", "+")
+    def UE (s:String) : String = {
+      if (Urlencode) {
+	val r = URLEncoder.encode(s, "UTF-8")
+	r
+      } else {
+	s.replaceAll("%", "%25")
+	 .replaceAll("\\+", "%2B")
+	 .replaceAll(" ", "+")
+	 .replaceAll(",", "%2C")
+	 .replaceAll("#", "%23")
+	 .replaceAll("=", "%3D")
+	 .replaceAll("/", "%2F")
+	 .replaceAll("<", "%3C")
+	 .replaceAll(">", "%3E")
+      }
+    }
 
   }
 
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.