io.continual.templating.impl.velocity.VelocityTemplateEngine Maven / Gradle / Ivy
package io.continual.templating.impl.velocity;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.util.LinkedList;
import java.util.Map;
import java.util.Properties;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.app.event.EventCartridge;
import org.apache.velocity.app.event.IncludeEventHandler;
import org.apache.velocity.context.Context;
import org.apache.velocity.exception.ResourceNotFoundException;
import org.apache.velocity.runtime.RuntimeConstants;
import org.apache.velocity.runtime.resource.Resource;
import org.apache.velocity.runtime.resource.loader.ResourceLoader;
import org.apache.velocity.util.ExtProperties;
import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import io.continual.builder.Builder.BuildFailure;
import io.continual.services.ServiceContainer;
import io.continual.services.SimpleService;
import io.continual.templating.ContinualTemplateCatalog;
import io.continual.templating.ContinualTemplateContext;
import io.continual.templating.ContinualTemplateEngine;
import io.continual.templating.ContinualTemplateSource;
import io.continual.templating.ContinualTemplateSource.TemplateNotFoundException;
import io.continual.util.data.json.JsonVisitor;
import io.continual.util.data.json.JsonVisitor.ObjectVisitor;
public class VelocityTemplateEngine extends SimpleService implements ContinualTemplateEngine
{
public VelocityTemplateEngine ( ServiceContainer sc, JSONObject config ) throws BuildFailure
{
fTemplateCatalog = sc.get ( config.optString ( "templateCatalog", "" ), ContinualTemplateCatalog.class );
final Properties props = new Properties ();
if ( fTemplateCatalog != null )
{
props.setProperty ( RuntimeConstants.RESOURCE_LOADER, "catalog" );
props.setProperty ( "resource.loader.catalog.class", CatalogLoader.class.getName () );
props.setProperty ( "resource.loader.catalog.cache", Boolean.valueOf ( config.optBoolean ( "catalogCache", true ) ).toString () );
}
fEngine = new VelocityEngine ( props );
if ( fTemplateCatalog != null )
{
fEngine.setProperty ( "resource.loader.catalog.instance", new CatalogLoader () );
}
fEngine.init ();
fBaseContext = new VelocityContext ();
// enable relative template finding
{
final EventCartridge ec = new EventCartridge ();
ec.addEventHandler ( new InternalIncludeRelativePath () );
ec.attachToContext ( fBaseContext );
log.info ( "Registered InternalIncludeRelativePath for relative template path handling." );
}
// compatibility for some older drumlin-based systems
fBaseContext.put ( "servletRoot", "" );
fBaseContext.put ( "warRoot", "" );
// addl context from setup
final JSONObject contextData = config == null ? null : config.optJSONObject ( "context" );
JsonVisitor.forEachElement ( contextData, new ObjectVisitor