Browse files

initial commit

  • Loading branch information...
0 parents commit 20333fef9c1ddc4a3fe5f3502f9929c6c1a0a15d @sps committed Mar 27, 2011
6 .gitignore
@@ -0,0 +1,6 @@
+.classpath
+.project
+.settings
+.springBeans
+target/
+*.swp
128 pom.xml
@@ -0,0 +1,128 @@
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+ <parent>
+ <groupId>org.sonatype.oss</groupId>
+ <artifactId>oss-parent</artifactId>
+ <version>5</version>
+ </parent>
+ <groupId>com.github.sps.mustache</groupId>
+ <artifactId>spring-view</artifactId>
+ <packaging>jar</packaging>
+ <version>1.0-SNAPSHOT</version>
+ <name>mustache spring view</name>
+ <description>a spring view for the mustache templating language http://mustache.github.com/</description>
+ <url>http://github.com/sps/mustache-spring-view</url>
+ <licenses>
+ <license>
+ <name>The Apache Software License, Version 2.0</name>
+ <url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
+ <distribution>repo</distribution>
+ </license>
+ </licenses>
+ <scm>
+ <url>http://github.com/sps/mustache-spring-view</url>
+ <connection>scm:git:git://github.com/sps/mustache-spring-view.git</connection>
+ </scm>
+ <developers>
+ <developer>
+ <id>sps</id>
+ <name>Sean Scanlon</name>
+ <email>sean.scanlon@gmail.com</email>
+ </developer>
+ </developers>
+ <properties>
+ <org.springframework.version>3.0.5.RELEASE</org.springframework.version>
+ <mustache.js.version>0.4-SNAPSHOT</mustache.js.version>
+ <mockito.version>1.8.5</mockito.version>
+ <junit.version>4.8.2</junit.version>
+ </properties>
+ <build>
+ <plugins>
+ <plugin>
+ <artifactId>maven-compiler-plugin</artifactId>
+ <configuration>
+ <source>1.5</source>
+ <target>1.5</target>
+ </configuration>
+ </plugin>
+ <plugin>
+ <groupId>org.codehaus.mojo</groupId>
+ <artifactId>cobertura-maven-plugin</artifactId>
+ <version>2.4</version>
+ <configuration>
+ <check>
+ <branchRate>85</branchRate>
+ <lineRate>85</lineRate>
+ <haltOnFailure>true</haltOnFailure>
+ <totalBranchRate>85</totalBranchRate>
+ <totalLineRate>85</totalLineRate>
+ <packageLineRate>85</packageLineRate>
+ <packageBranchRate>85</packageBranchRate>
+ </check>
+ </configuration>
+ <executions>
+ <execution>
+ <goals>
+ <goal>clean</goal>
+ <goal>check</goal>
+ </goals>
+ </execution>
+ </executions>
+ </plugin>
+ </plugins>
+ </build>
+ <dependencies>
+
+ <dependency>
+ <groupId>com.github.spullara.mustache.java</groupId>
+ <artifactId>compiler</artifactId>
+ <version>${mustache.js.version}</version>
+ <exclusions>
+ <exclusion>
+ <groupId>org.eclipse.jetty</groupId>
+ <artifactId>jetty-client</artifactId>
+ </exclusion>
+ </exclusions>
+ </dependency>
+
+ <dependency>
+ <groupId>commons-io</groupId>
+ <artifactId>commons-io</artifactId>
+ <version>2.0.1</version>
+ </dependency>
+
+ <dependency>
+ <groupId>org.springframework</groupId>
+ <artifactId>spring-webmvc</artifactId>
+ <version>${org.springframework.version}</version>
+ </dependency>
+
+ <dependency>
+ <groupId>org.springframework</groupId>
+ <artifactId>spring-core</artifactId>
+ <version>${org.springframework.version}</version>
+ </dependency>
+
+ <dependency>
+ <groupId>javax.servlet</groupId>
+ <artifactId>servlet-api</artifactId>
+ <version>2.4</version>
+ <scope>provided</scope>
+ </dependency>
+
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ <version>${junit.version}</version>
+ <scope>test</scope>
+ </dependency>
+
+ <dependency>
+ <groupId>org.mockito</groupId>
+ <artifactId>mockito-core</artifactId>
+ <version>${mockito.version}</version>
+ <scope>test</scope>
+ </dependency>
+
+ </dependencies>
+</project>
57 src/main/java/org/springframework/web/servlet/view/mustache/MustacheView.java
@@ -0,0 +1,57 @@
+/*
+ * Copyright 2011 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.springframework.web.servlet.view.mustache;
+
+import java.util.Map;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.springframework.web.servlet.view.AbstractTemplateView;
+
+import com.sampullara.mustache.Mustache;
+import com.sampullara.util.FutureWriter;
+
+/**
+ * @author Sean Scanlon <sean.scanlon@gmail.com>
+ *
+ */
+public class MustacheView extends AbstractTemplateView {
+
+ private Mustache template;
+
+ @Override
+ protected void renderMergedTemplateModel(Map<String, Object> model, HttpServletRequest request,
+ HttpServletResponse response) throws Exception {
+
+ response.setContentType(getContentType());
+
+ FutureWriter writer = new FutureWriter(response.getWriter());
+ try {
+ template.execute(writer, model);
+ } finally {
+ writer.flush();
+ }
+ }
+
+ public void setTemplate(Mustache template) {
+ this.template = template;
+ }
+
+ public Mustache getTemplate() {
+ return template;
+ }
+}
83 src/main/java/org/springframework/web/servlet/view/mustache/MustacheViewResolver.java
@@ -0,0 +1,83 @@
+/*
+ * Copyright 2011 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.springframework.web.servlet.view.mustache;
+
+import java.io.FileNotFoundException;
+import java.io.StringWriter;
+
+import org.apache.commons.io.IOUtils;
+import org.springframework.context.ResourceLoaderAware;
+import org.springframework.core.io.Resource;
+import org.springframework.core.io.ResourceLoader;
+import org.springframework.web.servlet.ViewResolver;
+import org.springframework.web.servlet.view.AbstractTemplateViewResolver;
+import org.springframework.web.servlet.view.AbstractUrlBasedView;
+
+import com.sampullara.mustache.Mustache;
+import com.sampullara.mustache.MustacheCompiler;
+
+/**
+ * @author Sean Scanlon <sean.scanlon@gmail.com>
+ *
+ */
+public class MustacheViewResolver extends AbstractTemplateViewResolver implements ViewResolver,
+ ResourceLoaderAware {
+
+ /*
+ * TODO: this will work for now, but will probably want to make some options externally
+ * configurable.
+ */
+ private static final MustacheCompiler MUSTACHE_COMPILER = new MustacheCompiler();
+
+ private ResourceLoader resourceLoader;
+
+ public MustacheViewResolver() {
+ setViewClass(MustacheView.class);
+ }
+
+ @Override
+ protected Class<?> requiredViewClass() {
+ return MustacheView.class;
+ }
+
+ @Override
+ protected AbstractUrlBasedView buildView(String viewName) throws Exception {
+
+ final MustacheView view = (MustacheView) super.buildView(viewName);
+
+ Resource resource = resourceLoader.getResource(view.getUrl());
+ if (resource.exists()) {
+ /*
+ * TODO: might be better to supply the path instead of the entire template.
+ */
+ StringWriter writer = new StringWriter();
+ IOUtils.copy(resource.getInputStream(), writer);
+ Mustache template = MUSTACHE_COMPILER.parse(writer.toString());
+ template.setRoot(resource.getFile().getParentFile());
+ view.setTemplate(template);
+ } else {
+ throw new FileNotFoundException(viewName);
+ }
+
+ return view;
+ }
+
+ @Override
+ public void setResourceLoader(ResourceLoader resourceLoader) {
+ this.resourceLoader = resourceLoader;
+ }
+
+}
77 src/test/java/org/springframework/web/servlet/view/mustache/MustacheViewResolverTest.java
@@ -0,0 +1,77 @@
+/*
+ * Copyright 2011 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.springframework.web.servlet.view.mustache;
+
+import static org.junit.Assert.assertNotNull;
+import static org.mockito.Matchers.anyString;
+
+import java.io.FileNotFoundException;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.Mockito;
+import org.springframework.core.io.ClassPathResource;
+import org.springframework.core.io.Resource;
+import org.springframework.core.io.ResourceLoader;
+
+/**
+ * @author Sean Scanlon <sean.scanlon@gmail.com>
+ *
+ */
+public class MustacheViewResolverTest {
+
+ private static final ClassPathResource TEST_TEMPLATE = new ClassPathResource("test-template.html",
+ MustacheViewResolverTest.class);
+
+ private ResourceLoader resourceLoader;
+ private Resource resource;
+ private MustacheViewResolver viewResolver;
+ private String viewName;
+
+ @Before
+ public void setUp() throws Exception {
+ viewName = "viewname";
+ resourceLoader = Mockito.mock(ResourceLoader.class);
+ resource = Mockito.mock(Resource.class);
+
+ viewResolver = new MustacheViewResolver();
+ viewResolver.setResourceLoader(resourceLoader);
+
+ Mockito.doReturn(resource).when(resourceLoader).getResource(anyString());
+ }
+
+ /**
+ * ensure a {@link FileNotFoundException} is thrown when the view resource cannot be found.
+ *
+ * @throws Exception
+ */
+ @Test(expected = FileNotFoundException.class)
+ public void testFileNotFound() throws Exception {
+ Mockito.doReturn(Boolean.FALSE).when(resource).exists();
+ viewResolver.buildView(viewName);
+ }
+
+ @Test
+ public void testBuildView() throws Exception {
+
+ Mockito.doReturn(Boolean.TRUE).when(resource).exists();
+ Mockito.doReturn(TEST_TEMPLATE.getInputStream())
+ .when(resource)
+ .getInputStream();
+ Mockito.doReturn(TEST_TEMPLATE.getFile()).when(resource).getFile();
+ assertNotNull(viewResolver.buildView(viewName));
+ }
+}
61 src/test/java/org/springframework/web/servlet/view/mustache/MustacheViewTest.java
@@ -0,0 +1,61 @@
+/*
+ * Copyright 2011 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.springframework.web.servlet.view.mustache;
+
+import static org.junit.Assert.assertEquals;
+
+import java.io.PrintWriter;
+import java.util.Collections;
+
+import javax.servlet.http.HttpServletResponse;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.Mockito;
+
+import com.sampullara.mustache.Mustache;
+
+/**
+ * @author Sean Scanlon <sean.scanlon@gmail.com>
+ *
+ */
+public class MustacheViewTest {
+
+ private Mustache template;
+ private HttpServletResponse response;
+ private PrintWriter mockWriter;
+ private MustacheView view;
+
+ @Before
+ public void setUp() throws Exception {
+ template = Mockito.mock(Mustache.class);
+ response = Mockito.mock(HttpServletResponse.class);
+ mockWriter = Mockito.mock(PrintWriter.class);
+
+ view = new MustacheView();
+ view.setTemplate(template);
+ }
+
+ @Test
+ public void testRenderMergedTemplateModel() throws Exception {
+
+ Mockito.doReturn(mockWriter).when(response).getWriter();
+
+ view.renderMergedTemplateModel(Collections.<String, Object> emptyMap(), null, response);
+
+ assertEquals(template, view.getTemplate());
+ }
+}
1 src/test/resources/org/springframework/web/servlet/view/mustache/test-template.html
@@ -0,0 +1 @@
+{{test}}

0 comments on commit 20333fe

Please sign in to comment.