commit 83e5e764cbe15bc1a067501983e31081932d4934
parent 5455acf2a61090aa5810eefff788dbf290719b68
Author: Dan Callaghan <djc@djc.id.au>
Date: Sun, 28 Mar 2010 21:48:00 +1000
spring-webmvc integration
Diffstat:
4 files changed, 161 insertions(+), 1 deletion(-)
diff --git a/pom.xml b/pom.xml
@@ -77,7 +77,12 @@
<dependency>
<groupId>au.com.miskinhill.jena</groupId>
<artifactId>jena</artifactId>
- <version>2.6.2.2-SNAPSHOT</version>
+ <version>2.6.2.1</version>
+ </dependency>
+ <dependency>
+ <groupId>org.deadlit</groupId>
+ <artifactId>jena-spring</artifactId>
+ <version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>joda-time</groupId>
@@ -90,6 +95,16 @@
<version>3.0.1.RELEASE</version>
</dependency>
<dependency>
+ <groupId>org.springframework</groupId>
+ <artifactId>spring-webmvc</artifactId>
+ <version>3.0.1.RELEASE</version>
+ </dependency>
+ <dependency>
+ <groupId>javax.servlet</groupId>
+ <artifactId>servlet-api</artifactId>
+ <version>2.5</version>
+ </dependency>
+ <dependency>
<groupId>org.codehaus.woodstox</groupId>
<artifactId>woodstox-core-asl</artifactId>
<version>4.0.6</version>
diff --git a/src/main/java/au/com/miskinhill/rdftemplate/TemplateInterpolator.java b/src/main/java/au/com/miskinhill/rdftemplate/TemplateInterpolator.java
@@ -1,8 +1,10 @@
package au.com.miskinhill.rdftemplate;
+import java.io.InputStream;
import java.io.Reader;
import java.io.StringReader;
import java.io.StringWriter;
+import java.io.Writer;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Deque;
@@ -79,6 +81,22 @@ public class TemplateInterpolator {
}
}
+ @SuppressWarnings("unchecked")
+ public void interpolate(InputStream inputStream, RDFNode node, Writer writer) {
+ try {
+ final XMLEventWriter eventWriter = outputFactory.createXMLEventWriter(writer);
+ XMLEventConsumer destination = new XMLEventConsumer() {
+ @Override
+ public void add(XMLEvent event) throws XMLStreamException {
+ eventWriter.add(event);
+ }
+ };
+ interpolate(inputFactory.createXMLEventReader(inputStream), node, destination);
+ } catch (XMLStreamException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
public void interpolate(Reader reader, RDFNode node, final Collection<XMLEvent> destination) {
interpolate(reader, node, new XMLEventConsumer() {
@Override
diff --git a/src/main/java/au/com/miskinhill/rdftemplate/view/RDFTemplateView.java b/src/main/java/au/com/miskinhill/rdftemplate/view/RDFTemplateView.java
@@ -0,0 +1,76 @@
+package au.com.miskinhill.rdftemplate.view;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.Locale;
+import java.util.Map;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.deadlit.rdf.util.SdbTemplate;
+import org.deadlit.rdf.util.SdbTemplate.ModelExecutionCallbackWithoutResult;
+import org.springframework.web.servlet.view.AbstractTemplateView;
+
+import au.com.miskinhill.rdftemplate.TemplateInterpolator;
+import au.com.miskinhill.rdftemplate.selector.SelectorFactory;
+
+import com.hp.hpl.jena.rdf.model.Model;
+import com.hp.hpl.jena.rdf.model.Resource;
+
+public class RDFTemplateView extends AbstractTemplateView {
+
+ public static final String NODE_URI_KEY = "nodeUri";
+
+ private TemplateInterpolator templateInterpolator;
+ private SelectorFactory selectorFactory;
+ private SdbTemplate sdbTemplate;
+
+ public void setSelectorFactory(SelectorFactory selectorFactory) {
+ this.selectorFactory = selectorFactory;
+ }
+
+ public void setSdbTemplate(SdbTemplate sdbTemplate) {
+ this.sdbTemplate = sdbTemplate;
+ }
+
+ @Override
+ public void afterPropertiesSet() throws Exception {
+ super.afterPropertiesSet();
+ if (selectorFactory == null) {
+ throw new IllegalArgumentException("Property 'selectorFactory' is required");
+ }
+ if (sdbTemplate == null) {
+ throw new IllegalArgumentException("Property 'sdbTemplate' is required");
+ }
+ this.templateInterpolator = new TemplateInterpolator(selectorFactory);
+ }
+
+ @Override
+ protected void renderMergedTemplateModel(final Map<String, Object> model,
+ final HttpServletRequest request, final HttpServletResponse response)
+ throws Exception {
+ final InputStream inputStream = getApplicationContext().getResource(getUrl()).getInputStream();
+ try {
+ sdbTemplate.withModel(new ModelExecutionCallbackWithoutResult() {
+ @Override
+ protected void executeWithoutResult(Model rdfModel) {
+ Resource node = rdfModel.getResource((String) model.get(NODE_URI_KEY));
+ try {
+ templateInterpolator.interpolate(inputStream, node, response.getWriter());
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
+ }
+ });
+ } finally {
+ inputStream.close();
+ }
+ }
+
+ @Override
+ public boolean checkResource(Locale locale) throws Exception {
+ return getApplicationContext().getResource(getUrl()).exists();
+ }
+
+}
diff --git a/src/main/java/au/com/miskinhill/rdftemplate/view/RDFTemplateViewResolver.java b/src/main/java/au/com/miskinhill/rdftemplate/view/RDFTemplateViewResolver.java
@@ -0,0 +1,51 @@
+package au.com.miskinhill.rdftemplate.view;
+
+import org.deadlit.rdf.util.SdbTemplate;
+import org.springframework.beans.factory.InitializingBean;
+import org.springframework.web.servlet.view.AbstractTemplateView;
+import org.springframework.web.servlet.view.AbstractTemplateViewResolver;
+
+import au.com.miskinhill.rdftemplate.selector.SelectorFactory;
+
+public class RDFTemplateViewResolver extends AbstractTemplateViewResolver implements InitializingBean {
+
+ private SelectorFactory selectorFactory;
+ private SdbTemplate sdbTemplate;
+
+ public RDFTemplateViewResolver() {
+ super();
+ setViewClass(requiredViewClass());
+ }
+
+ public void setSelectorFactory(SelectorFactory selectorFactory) {
+ this.selectorFactory = selectorFactory;
+ }
+
+ public void setSdbTemplate(SdbTemplate sdbTemplate) {
+ this.sdbTemplate = sdbTemplate;
+ }
+
+ @Override
+ public void afterPropertiesSet() throws Exception {
+ if (selectorFactory == null) {
+ throw new IllegalArgumentException("Property 'selectorFactory' is required");
+ }
+ if (sdbTemplate == null) {
+ throw new IllegalArgumentException("Property 'sdbTemplate' is required");
+ }
+ }
+
+ @Override
+ protected Class<? extends AbstractTemplateView> requiredViewClass() {
+ return RDFTemplateView.class;
+ }
+
+ @Override
+ protected RDFTemplateView buildView(String viewName) throws Exception {
+ RDFTemplateView view = (RDFTemplateView) super.buildView(viewName);
+ view.setSelectorFactory(selectorFactory);
+ view.setSdbTemplate(sdbTemplate);
+ return view;
+ }
+
+}