Commits

Sam Adams committed daf7278

Fixed SPARQL Result HTML

Comments (0)

Files changed (5)

chempound-webapp/src/main/java/net/chempound/webapp/ChempoundWebappLauncher.java

         final URI uri = injector.getInstance(Key.get(URI.class, BaseUri.class));
         final int port = uri.getPort() == -1 ? 80 : uri.getPort();
 
+        final String path = uri.getPath().replaceFirst("(.*)/$", "$1");
+        
         final Component component = injector.getInstance(Component.class);
         component.getServers().add(Protocol.HTTP, port)
                 .getContext().getParameters().add("maxThreads", "256");
-        component.getDefaultHost().attach(injector.getInstance(Application.class));
+        component.getDefaultHost().attach(path, injector.getInstance(Application.class));
         component.start();
     }
 

chempound-webapp/src/main/java/net/chempound/webapp/sparql/SparqlResource.java

 import com.hp.hpl.jena.query.*;
 import com.hp.hpl.jena.tdb.TDB;
 import freemarker.template.Template;
+import freemarker.template.TemplateException;
 import net.chempound.datastore.TripleStore;
 import net.chempound.services.FreemarkerService;
 import org.restlet.data.Form;
 import org.restlet.ext.freemarker.TemplateRepresentation;
 import org.restlet.representation.Representation;
 import org.restlet.representation.StreamRepresentation;
+import org.restlet.representation.WriterRepresentation;
 import org.restlet.resource.Get;
 import org.restlet.resource.Post;
 import org.restlet.resource.ServerResource;
 
 import javax.inject.Inject;
 import java.io.*;
+import java.util.HashMap;
+import java.util.Map;
 
 /**
  * @author Sam Adams
 public class SparqlResource extends ServerResource {
 
     private final FreemarkerService freemarker;
-
     private final TripleStore tripleStore;
 
-
     @Inject
     public SparqlResource(final FreemarkerService freemarker, final TripleStore tripleStore) {
         this.freemarker = freemarker;
     // MalformedQuery return 400
     // QueryRequestRefused return 500
 
-
     @Get
     public Representation doGet() throws IOException {
         final Form form = getQuery();
         final QueryExecution qExec = tripleStore.createQueryExecution(query);
         qExec.getContext().set(TDB.symUnionDefaultGraph, true);
 
-        return new StreamRepresentation(html ? MediaType.TEXT_HTML : MediaType.APPLICATION_SPARQL_RESULTS_XML) {
-            @Override
-            public InputStream getStream() throws IOException {
-                final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
-                write(buffer);
-                return new ByteArrayInputStream(buffer.toByteArray());
-            }
-            @Override
-            public void write(final OutputStream outputStream) throws IOException {
+        
+        return html ? new HTMLRepresentation(qExec, queryString) : new XMLRepresentation(qExec);
+    }
+    
+    class HTMLRepresentation extends WriterRepresentation {
+
+        private final QueryExecution qExec;
+        private final String query;
+
+        public HTMLRepresentation(final QueryExecution qExec, final String query) {
+            super(MediaType.TEXT_HTML);
+            this.qExec = qExec;
+            this.query = query;
+        }
+
+        @Override
+        public void write(final Writer writer) throws IOException {
+            final Template template = freemarker.getTemplate("sparql-result.ftl");
+            try {
                 final ResultSet results = qExec.execSelect();
                 try {
-                    if (html) {
-                        ResultSetFormatter.outputAsXML(outputStream, results, "/static/sparql2html.xslt");
-                    } else {
-                        ResultSetFormatter.outputAsXML(outputStream, results);
-                    }
-               } finally {
-                    qExec.close();
+                    final Map<String, Object> model = new HashMap<String, Object>();
+                    model.put("query", query);
+                    model.put("resultVars", results.getResultVars());
+                    model.put("results", results);
+                    template.process(model, writer);
+                } catch (TemplateException te) {
+                    throw new IOException("Template processing error "+ te.getMessage(), te);
                 }
+            } finally {
+                qExec.close();
             }
-        };
+        }
+    }
+    
+    class XMLRepresentation extends StreamRepresentation {
+
+        private final QueryExecution qExec;
+
+        public XMLRepresentation(final QueryExecution qExec) {
+            super(MediaType.APPLICATION_XML);
+            this.qExec = qExec;
+        }
+
+        @Override
+        public InputStream getStream() throws IOException {
+            final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
+            write(buffer);
+            return new ByteArrayInputStream(buffer.toByteArray());
+        }
+        @Override
+        public void write(final OutputStream outputStream) throws IOException {
+            try {
+                final ResultSet results = qExec.execSelect();
+                ResultSetFormatter.outputAsXML(outputStream, results);
+            } finally {
+                qExec.close();
+            }
+        }
+        
     }
 
+
 }

chempound-webapp/src/main/resources/net/chempound/static/sparql2html.xslt

-<?xml version="1.0" encoding="UTF-8"?>
-
-<!--
-
-XSLT script to format SPARQL Query Results XML Format into xhtml
-
-Copyright (c) 2004, 2005 World Wide Web Consortium, (Massachusetts
-Institute of Technology, European Research Consortium for
-Informatics and Mathematics, Keio University). All Rights
-Reserved. This work is distributed under the W3C® Software
-License [1] in the hope that it will be useful, but WITHOUT ANY
-WARRANTY; without even the implied warranty of MERCHANTABILITY or
-FITNESS FOR A PARTICULAR PURPOSE.
-
-[1] http://www.w3.org/Consortium/Legal/2002/copyright-software-20021231
-
-$Id: result-to-html.xsl,v 1.1 2008/01/15 02:25:58 eric Exp $
-
--->
-
-<xsl:stylesheet version="1.0"
-  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
-  xmlns="http://www.w3.org/1999/xhtml"
-  xmlns:res="http://www.w3.org/2005/sparql-results#"
-  exclude-result-prefixes="res xsl">
-
-  <!--
-  <xsl:output
-    method="html"
-    media-type="text/html"
-    doctype-public="-//W3C//DTD HTML 4.01 Transitional//EN"
-    indent="yes"
-    encoding="UTF-8"/>
-  -->
-
-  <!-- or this? -->
-
-  <xsl:output
-    method="xml"
-    indent="yes"
-    encoding="UTF-8"
-    doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN"
-    doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"
-    omit-xml-declaration="no" />
-
-
-  <xsl:template name="header">
-
-    <div>
-      <h2>Header</h2>
-      <xsl:for-each select="res:head/res:link">
-	<p>Link to <xsl:value-of select="@href"/></p>
-      </xsl:for-each>
-    </div>
-  </xsl:template>
-
-  <xsl:template name="boolean-result">
-
-    <div>
-      <h2>Boolean Result</h2>
-      <p>Value <xsl:value-of select="res:boolean"/></p>
-    </div>
-  </xsl:template>
-
-
-  <xsl:template name="vb-result">
-    <div>
-
-      <h2>Variable Bindings Result</h2>
-
-      <table border="1">
-	<xsl:text>
-	</xsl:text>
-	<tr>
-	  <xsl:for-each select="res:head/res:variable">
-	    <th><xsl:value-of select="@name"/></th>
-	  </xsl:for-each>
-
-	</tr>
-	<xsl:text>
-	</xsl:text>
-	<xsl:for-each select="res:results/res:result">
-	  <tr>
-	    <xsl:apply-templates select="."/>
-	  </tr>
-	</xsl:for-each>
-      </table>
-
-    </div>
-  </xsl:template>
-
-  <xsl:template match="res:result">
-    <xsl:variable name="current" select="."/>
-    <xsl:for-each select="//res:head/res:variable">
-      <xsl:variable name="name" select="@name"/>
-      <td>
-	<xsl:choose>
-
-	  <xsl:when test="$current/res:binding[@name=$name]">
-	    <!-- apply template for the correct value type (bnode, uri,
-	    literal) -->
-	    <xsl:apply-templates select="$current/res:binding[@name=$name]"/>
-	  </xsl:when>
-	  <xsl:otherwise>
-	    <!-- no binding available for this variable in this solution -->
-	    [unbound]
-	  </xsl:otherwise>
-	</xsl:choose>
-
-      </td>
-    </xsl:for-each>
-  </xsl:template>
-
-  <xsl:template match="res:bnode">
-    <xsl:text>nodeID </xsl:text>
-    <xsl:value-of select="text()"/>
-  </xsl:template>
-
-  <xsl:template match="res:uri">
-    <xsl:variable name="uri" select="text()"/>
-    <xsl:text>URI </xsl:text>
-    <xsl:value-of select="$uri"/>
-  </xsl:template>
-
-  <xsl:template match="res:literal">
-    <xsl:choose>
-      <xsl:when test="@datatype">
-
-	<!-- datatyped literal value -->
-	<xsl:value-of select="text()"/> (datatype <xsl:value-of select="@datatype"/> )
-      </xsl:when>
-      <xsl:when test="@xml:lang">
-	<!-- lang-string -->
-	<xsl:value-of select="text()"/> @ <xsl:value-of select="@xml:lang"/>
-      </xsl:when>
-
-      <xsl:when test="string-length(text()) != 0">
-	<!-- present and not empty -->
-	<xsl:value-of select="text()"/>
-      </xsl:when>
-      <xsl:when test="string-length(text()) = 0">
-	<!-- present and empty -->
-	[empty literal]
-      </xsl:when>
-    </xsl:choose>
-
-  </xsl:template>
-
-  <xsl:template match="res:sparql">
-      <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
-      <head>
-          <title>SPARQL Query Results to XHTML</title>
-          <link rel="stylesheet" href="/static/chempound.css"/>
-      </head>
-
-      <body>
-
-      <div id="header">
-          <div class="content">
-              <h1>chempound data repository</h1>
-          </div>
-      </div>
-      <div id="menu">
-          <div class="content">
-              <a href="/" class="first">Home</a>
-              <a href="/content/">Browse</a>
-              <a href="/search/">Search</a>
-              <a href="/sparql/">SPARQL</a>
-              <!-- <a href="/feeds/">Feeds</a> -->
-          </div>
-      </div>
-
-      <div id="body">
-          <div class="content sparql-results">
-              <h1>SPARQL Query Results to XHTML</h1>
-
-	<xsl:if test="res:head/res:link">
-	  <xsl:call-template name="header"/>
-	</xsl:if>
-
-	<xsl:choose>
-	  <xsl:when test="res:boolean">
-	    <xsl:call-template name="boolean-result" />
-
-	  </xsl:when>
-
-	  <xsl:when test="res:results">
-	    <xsl:call-template name="vb-result" />
-	  </xsl:when>
-
-	</xsl:choose>
-
-            </div>
-
-        </div>
-
-        <div id="footer">
-            <p></p>
-        </div>
-
-        </body>
-    </html>
-  </xsl:template>
-</xsl:stylesheet>

chempound-webapp/src/main/resources/net/chempound/templates/sparql-result.ftl

+<#-- @ftlvariable name="results" type="com.hp.hpl.jena.query.ResultSet" -->
+<@layout.basic
+    title="SPARQL Query Results"
+>
+
+<h1>SPARQL Results</h1>
+
+<h2>Query</h2>
+
+<pre>
+${query?html}
+</pre>
+
+<h2>Results</h2>
+
+<table class="sparql-results">
+    <tr>
+<#list resultVars as var>
+        <th>${var}</th>
+</#list>
+    </tr>
+<#list results as result>
+    <tr>
+    <#list resultVars as var>
+        <td>${result.get(var).toString()}</td>
+    </#list>
+    </tr>
+</#list>
+</table>
+
+</@layout.basic>

chempound-webapp/src/main/resources/net/chempound/web/css/chempound.css

     margin-left: 12px;
     width: 350px;
 }
+
+
+table.sparql-results {
+    border-width: 1px;
+    border-spacing: 2px;
+    border-style: outset;
+    border-color: gray;
+    border-collapse: collapse;
+    background-color: white;
+}
+table.sparql-results th {
+    border-width: 1px;
+    padding: 2px;
+    border-style: outset;
+    border-color: gray;
+    background-color: white;
+    -moz-border-radius: ;
+}
+table.sparql-results td {
+    border-width: 1px;
+    padding: 2px;
+    border-style: outset;
+    border-color: gray;
+    background-color: white;
+    -moz-border-radius: ;
+}