mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-08 06:25:45 +01:00
added method to read templates from a reader
This commit is contained in:
@@ -30,11 +30,13 @@
|
||||
*/
|
||||
|
||||
|
||||
|
||||
package sonia.scm.template;
|
||||
|
||||
//~--- JDK imports ------------------------------------------------------------
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.Reader;
|
||||
|
||||
/**
|
||||
* The {@link TemplateEngine} searches for {@link Template}s and prepares the
|
||||
@@ -63,6 +65,23 @@ public interface TemplateEngine
|
||||
*/
|
||||
public Template getTemplate(String templatePath) throws IOException;
|
||||
|
||||
/**
|
||||
* Creates a template of the given reader. Note some template implementations
|
||||
* will cache the template by its id.
|
||||
*
|
||||
*
|
||||
* @param templateIdentifier id of the template
|
||||
* @param reader template reader
|
||||
*
|
||||
* @return template created from the reader
|
||||
*
|
||||
* @throws IOException
|
||||
*
|
||||
* @since 1.22
|
||||
*/
|
||||
public Template getTemplate(String templateIdentifier, Reader reader)
|
||||
throws IOException;
|
||||
|
||||
/**
|
||||
* Returns the type of this template engine.
|
||||
*
|
||||
|
||||
@@ -30,6 +30,7 @@
|
||||
*/
|
||||
|
||||
|
||||
|
||||
package sonia.scm.template;
|
||||
|
||||
//~--- non-JDK imports --------------------------------------------------------
|
||||
@@ -43,6 +44,7 @@ import static org.junit.Assert.*;
|
||||
//~--- JDK imports ------------------------------------------------------------
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.Reader;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.HashSet;
|
||||
@@ -166,6 +168,24 @@ public class TemplateEngineFactoryTest
|
||||
throw new UnsupportedOperationException("Not supported yet.");
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param templateIdentifier
|
||||
* @param reader
|
||||
*
|
||||
* @return
|
||||
*
|
||||
* @throws IOException
|
||||
*/
|
||||
@Override
|
||||
public Template getTemplate(String templateIdentifier, Reader reader)
|
||||
throws IOException
|
||||
{
|
||||
throw new UnsupportedOperationException("Not supported yet.");
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
@@ -190,6 +210,24 @@ public class TemplateEngineFactoryTest
|
||||
private static class FakeTemplateEngine2 implements TemplateEngine
|
||||
{
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param templateIdentifier
|
||||
* @param reader
|
||||
*
|
||||
* @return
|
||||
*
|
||||
* @throws IOException
|
||||
*/
|
||||
@Override
|
||||
public Template getTemplate(String templateIdentifier, Reader reader)
|
||||
throws IOException
|
||||
{
|
||||
throw new UnsupportedOperationException("Not supported yet.");
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
|
||||
@@ -51,6 +51,7 @@ import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.io.Reader;
|
||||
|
||||
import java.util.Locale;
|
||||
|
||||
@@ -152,6 +153,34 @@ public class FreemarkerTemplateEngine implements TemplateEngine
|
||||
return template;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param templateIdentifier
|
||||
* @param reader
|
||||
*
|
||||
* @return
|
||||
*
|
||||
* @throws IOException
|
||||
*/
|
||||
@Override
|
||||
public Template getTemplate(String templateIdentifier, Reader reader)
|
||||
throws IOException
|
||||
{
|
||||
if (logger.isTraceEnabled())
|
||||
{
|
||||
logger.trace("try to create freemarker template from reader with id {}",
|
||||
templateIdentifier);
|
||||
}
|
||||
|
||||
freemarker.template.Template t =
|
||||
new freemarker.template.Template(templateIdentifier, reader,
|
||||
configuration, ENCODING);
|
||||
|
||||
return new FreemarkerTemplate(t);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
|
||||
@@ -47,6 +47,7 @@ import org.slf4j.LoggerFactory;
|
||||
//~--- JDK imports ------------------------------------------------------------
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.Reader;
|
||||
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
@@ -87,6 +88,32 @@ public class MustacheTemplateEngine implements TemplateEngine
|
||||
|
||||
//~--- get methods ----------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param templateIdentifier
|
||||
* @param reader
|
||||
*
|
||||
* @return
|
||||
*
|
||||
* @throws IOException
|
||||
*/
|
||||
@Override
|
||||
public Template getTemplate(String templateIdentifier, Reader reader)
|
||||
throws IOException
|
||||
{
|
||||
if (logger.isTraceEnabled())
|
||||
{
|
||||
logger.trace("try to create mustache template from reader with id {}",
|
||||
templateIdentifier);
|
||||
}
|
||||
|
||||
Mustache mustache = factory.compile(reader, templateIdentifier);
|
||||
|
||||
return new MustacheTemplate(templateIdentifier, mustache);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
|
||||
@@ -30,10 +30,13 @@
|
||||
*/
|
||||
|
||||
|
||||
|
||||
package sonia.scm.template;
|
||||
|
||||
//~--- JDK imports ------------------------------------------------------------
|
||||
|
||||
import java.io.InputStream;
|
||||
|
||||
import javax.servlet.ServletContext;
|
||||
|
||||
/**
|
||||
@@ -82,4 +85,19 @@ public class FreemarkerTemplateEngineTest extends TemplateEngineTestBase
|
||||
{
|
||||
return "sonia/scm/template/002.ftl";
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param resource
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
protected InputStream getResource(String resource)
|
||||
{
|
||||
return FreemarkerTemplateEngineTest.class.getResourceAsStream(
|
||||
"/sonia/scm/template/".concat(resource).concat(".ftl"));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30,10 +30,13 @@
|
||||
*/
|
||||
|
||||
|
||||
|
||||
package sonia.scm.template;
|
||||
|
||||
//~--- JDK imports ------------------------------------------------------------
|
||||
|
||||
import java.io.InputStream;
|
||||
|
||||
import javax.servlet.ServletContext;
|
||||
|
||||
/**
|
||||
@@ -82,4 +85,19 @@ public class MustacheTemplateEngineTest extends TemplateEngineTestBase
|
||||
{
|
||||
return "sonia/scm/template/001.mustache";
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param resource
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
protected InputStream getResource(String resource)
|
||||
{
|
||||
return MustacheTemplateEngineTest.class.getResourceAsStream(
|
||||
"/sonia/scm/template/".concat(resource).concat(".mustache"));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30,10 +30,12 @@
|
||||
*/
|
||||
|
||||
|
||||
|
||||
package sonia.scm.template;
|
||||
|
||||
//~--- non-JDK imports --------------------------------------------------------
|
||||
|
||||
import com.google.common.collect.Maps;
|
||||
import com.google.common.io.Closeables;
|
||||
|
||||
import org.junit.Test;
|
||||
@@ -46,9 +48,13 @@ import static org.mockito.Mockito.*;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.StringWriter;
|
||||
|
||||
import java.net.URL;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import javax.servlet.ServletContext;
|
||||
|
||||
/**
|
||||
@@ -86,6 +92,16 @@ public abstract class TemplateEngineTestBase
|
||||
*/
|
||||
public abstract String getTemplateResource();
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param resource
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
protected abstract InputStream getResource(String resource);
|
||||
|
||||
//~--- methods --------------------------------------------------------------
|
||||
|
||||
/**
|
||||
@@ -135,6 +151,45 @@ public abstract class TemplateEngineTestBase
|
||||
assertNotNull(engine.getTemplate(getTemplateResource()));
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @throws IOException
|
||||
*/
|
||||
@Test
|
||||
public void testGetTemplateFromReader() throws IOException
|
||||
{
|
||||
ServletContext context = mock(ServletContext.class);
|
||||
TemplateEngine engine = createEngine(context);
|
||||
|
||||
InputStream input = null;
|
||||
|
||||
try
|
||||
{
|
||||
input = getResource("007");
|
||||
|
||||
Template template = engine.getTemplate("007",
|
||||
new InputStreamReader(input));
|
||||
|
||||
assertNotNull(template);
|
||||
|
||||
Map<String, Object> env = Maps.newHashMap();
|
||||
|
||||
env.put("time", "Lunchtime");
|
||||
|
||||
StringWriter writer = new StringWriter();
|
||||
|
||||
template.execute(writer, env);
|
||||
assertEquals("Time is an illusion. Lunchtime doubly so.",
|
||||
writer.toString());
|
||||
}
|
||||
finally
|
||||
{
|
||||
Closeables.closeQuietly(input);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
|
||||
1
scm-webapp/src/test/resources/sonia/scm/template/007.ftl
Normal file
1
scm-webapp/src/test/resources/sonia/scm/template/007.ftl
Normal file
@@ -0,0 +1 @@
|
||||
Time is an illusion. ${time} doubly so.
|
||||
@@ -0,0 +1 @@
|
||||
Time is an illusion. {{time}} doubly so.
|
||||
Reference in New Issue
Block a user