Search in sources :

Example 31 with Configuration

use of freemarker.template.Configuration in project leopard by tanhaichao.

the class FtlView method getTemplate.

protected Template getTemplate(String name) throws IOException {
    Configuration config = configurer.getConfiguration();
    config.setTemplateLoader(new ClassTemplateLoader(this.getClass(), folder));
    return config.getTemplate(name, "UTF-8");
}
Also used : Configuration(freemarker.template.Configuration) ClassTemplateLoader(freemarker.cache.ClassTemplateLoader)

Example 32 with Configuration

use of freemarker.template.Configuration in project vcell by virtualcell.

the class SimDataValuesServerResource method get_html.

@Override
public Representation get_html() {
    VCellApiApplication application = ((VCellApiApplication) getApplication());
    User vcellUser = application.getVCellUser(getChallengeResponse(), AuthenticationPolicy.ignoreInvalidCredentials);
    SimDataValuesRepresentation simDataValues = getSimDataValuesRepresentation(vcellUser);
    Map<String, Object> dataModel = new HashMap<String, Object>();
    // +"?"+VCellApiApplication.REDIRECTURL_FORMNAME+"="+getRequest().getResourceRef().toUrl());
    dataModel.put("loginurl", "/" + VCellApiApplication.LOGINFORM);
    dataModel.put("logouturl", "/" + VCellApiApplication.LOGOUT + "?" + VCellApiApplication.REDIRECTURL_FORMNAME + "=" + Reference.encode(getRequest().getResourceRef().toUrl().toString()));
    if (vcellUser != null) {
        dataModel.put("userid", vcellUser.getName());
    }
    dataModel.put("userId", getAttribute(PARAM_USER));
    dataModel.put("simId", getQueryValue(PARAM_SIM_ID));
    Long startRowParam = getLongQueryValue(PARAM_START_ROW);
    if (startRowParam != null) {
        dataModel.put("startRow", startRowParam);
    } else {
        dataModel.put("startRow", 1);
    }
    Long maxRowsParam = getLongQueryValue(PARAM_MAX_ROWS);
    if (maxRowsParam != null) {
        dataModel.put("maxRows", maxRowsParam);
    } else {
        dataModel.put("maxRows", 10);
    }
    dataModel.put("simdatavalues", simDataValues);
    int numVars = simDataValues.getVariables().length;
    if (numVars > 1) {
        StringBuffer buffer = new StringBuffer();
        String firstRow = "\"";
        for (int i = 0; i < numVars; i++) {
            firstRow += simDataValues.getVariables()[i].getName();
            if (i < numVars - 1) {
                firstRow += ",";
            }
        }
        firstRow += "\\n\" + \n";
        buffer.append(firstRow);
        int numTimes = simDataValues.getVariables()[0].getValues().length;
        for (int t = 0; t < numTimes; t++) {
            String row = "\"";
            for (int v = 0; v < numVars; v++) {
                row += simDataValues.getVariables()[v].getValues()[t];
                if (v < numVars - 1) {
                    row += ",";
                }
            }
            row += "\\n\"";
            if (t < numTimes - 1) {
                row += " + \n";
            }
            buffer.append(row);
        }
        String csvdata = buffer.toString();
        // String csvdata = "\"t,x,y\\n\" + \n" +
        // "\"0,0,0\\n\" + \n" +
        // "\"1,1,1\\n\" + \n" +
        // "\"2,2,4\\n\" + \n" +
        // "\"3,3,9\\n\" + \n" +
        // "\"4,4,16\\n\" + \n" +
        // "\"5,5,25\\n\"";
        dataModel.put("csvdata", csvdata);
    }
    Gson gson = new Gson();
    dataModel.put("jsonResponse", gson.toJson(simDataValues));
    Configuration templateConfiguration = application.getTemplateConfiguration();
    Representation formFtl = new ClientResource(LocalReference.createClapReference("/simdatavalues.ftl")).get();
    TemplateRepresentation templateRepresentation = new TemplateRepresentation(formFtl, templateConfiguration, dataModel, MediaType.TEXT_HTML);
    return templateRepresentation;
}
Also used : TemplateRepresentation(org.restlet.ext.freemarker.TemplateRepresentation) User(org.vcell.util.document.User) Configuration(freemarker.template.Configuration) HashMap(java.util.HashMap) Gson(com.google.gson.Gson) TemplateRepresentation(org.restlet.ext.freemarker.TemplateRepresentation) SimDataValuesRepresentation(org.vcell.rest.common.SimDataValuesRepresentation) Representation(org.restlet.representation.Representation) SimDataValuesRepresentation(org.vcell.rest.common.SimDataValuesRepresentation) VCellApiApplication(org.vcell.rest.VCellApiApplication) ClientResource(org.restlet.resource.ClientResource)

Example 33 with Configuration

use of freemarker.template.Configuration in project vcell by virtualcell.

the class PublicationsServerResource method get_html.

@Override
public Representation get_html() {
    VCellApiApplication application = ((VCellApiApplication) getApplication());
    User vcellUser = application.getVCellUser(getChallengeResponse(), AuthenticationPolicy.ignoreInvalidCredentials);
    PublicationRepresentation[] publications = getPublicationRepresentations(vcellUser);
    Map<String, Object> dataModel = new HashMap<String, Object>();
    // +"?"+VCellApiApplication.REDIRECTURL_FORMNAME+"="+getRequest().getResourceRef().toUrl());
    dataModel.put("loginurl", "/" + VCellApiApplication.LOGINFORM);
    dataModel.put("logouturl", "/" + VCellApiApplication.LOGOUT + "?" + VCellApiApplication.REDIRECTURL_FORMNAME + "=" + Reference.encode(getRequest().getResourceRef().toUrl().toString()));
    if (vcellUser != null) {
        dataModel.put("userid", vcellUser.getName());
    }
    dataModel.put("pubId", getQueryValue(PARAM_PUB_ID));
    dataModel.put("orderBy", getQueryValue(PARAM_ORDERBY));
    dataModel.put("publications", Arrays.asList(publications));
    Gson gson = new Gson();
    dataModel.put("jsonResponse", gson.toJson(publications));
    Configuration templateConfiguration = application.getTemplateConfiguration();
    Representation formFtl = new ClientResource(LocalReference.createClapReference("/publications.ftl")).get();
    TemplateRepresentation templateRepresentation = new TemplateRepresentation(formFtl, templateConfiguration, dataModel, MediaType.TEXT_HTML);
    return templateRepresentation;
}
Also used : TemplateRepresentation(org.restlet.ext.freemarker.TemplateRepresentation) User(org.vcell.util.document.User) Configuration(freemarker.template.Configuration) HashMap(java.util.HashMap) Gson(com.google.gson.Gson) TemplateRepresentation(org.restlet.ext.freemarker.TemplateRepresentation) PublicationRepresentation(org.vcell.rest.common.PublicationRepresentation) Representation(org.restlet.representation.Representation) PublicationRepresentation(org.vcell.rest.common.PublicationRepresentation) VCellApiApplication(org.vcell.rest.VCellApiApplication) ClientResource(org.restlet.resource.ClientResource)

Example 34 with Configuration

use of freemarker.template.Configuration in project camel by apache.

the class FreemarkerComponent method getConfiguration.

public synchronized Configuration getConfiguration() {
    if (configuration == null) {
        configuration = new Configuration();
        configuration.setTemplateLoader(new URLTemplateLoader() {

            @Override
            protected URL getURL(String name) {
                try {
                    return ResourceHelper.resolveMandatoryResourceAsUrl(getCamelContext().getClassResolver(), name);
                } catch (Exception e) {
                    // so we should return null to signal the resource could not be found
                    return null;
                }
            }
        });
    }
    return (Configuration) configuration.clone();
}
Also used : URLTemplateLoader(freemarker.cache.URLTemplateLoader) Configuration(freemarker.template.Configuration) URL(java.net.URL)

Example 35 with Configuration

use of freemarker.template.Configuration in project camel by apache.

the class FreemarkerEndpoint method onExchange.

@Override
protected void onExchange(Exchange exchange) throws Exception {
    String path = getResourceUri();
    ObjectHelper.notNull(configuration, "configuration");
    ObjectHelper.notNull(path, "resourceUri");
    String newResourceUri = exchange.getIn().getHeader(FreemarkerConstants.FREEMARKER_RESOURCE_URI, String.class);
    if (newResourceUri != null) {
        exchange.getIn().removeHeader(FreemarkerConstants.FREEMARKER_RESOURCE_URI);
        log.debug("{} set to {} creating new endpoint to handle exchange", FreemarkerConstants.FREEMARKER_RESOURCE_URI, newResourceUri);
        FreemarkerEndpoint newEndpoint = findOrCreateEndpoint(getEndpointUri(), newResourceUri);
        newEndpoint.onExchange(exchange);
        return;
    }
    Reader reader = null;
    String content = exchange.getIn().getHeader(FreemarkerConstants.FREEMARKER_TEMPLATE, String.class);
    if (content != null) {
        // use content from header
        reader = new StringReader(content);
        // remove the header to avoid it being propagated in the routing
        exchange.getIn().removeHeader(FreemarkerConstants.FREEMARKER_TEMPLATE);
    }
    Object dataModel = exchange.getIn().getHeader(FreemarkerConstants.FREEMARKER_DATA_MODEL, Object.class);
    if (dataModel == null) {
        dataModel = ExchangeHelper.createVariableMap(exchange);
    }
    // let freemarker parse and generate the result in buffer
    Template template;
    if (reader != null) {
        log.debug("Freemarker is evaluating template read from header {} using context: {}", FreemarkerConstants.FREEMARKER_TEMPLATE, dataModel);
        template = new Template("temp", reader, new Configuration());
    } else {
        log.debug("Freemarker is evaluating {} using context: {}", path, dataModel);
        if (getEncoding() != null) {
            template = configuration.getTemplate(path, getEncoding());
        } else {
            template = configuration.getTemplate(path);
        }
    }
    StringWriter buffer = new StringWriter();
    template.process(dataModel, buffer);
    buffer.flush();
    // now lets output the results to the exchange
    Message out = exchange.getOut();
    out.setBody(buffer.toString());
    out.setHeaders(exchange.getIn().getHeaders());
    out.setAttachments(exchange.getIn().getAttachments());
}
Also used : Configuration(freemarker.template.Configuration) StringWriter(java.io.StringWriter) Message(org.apache.camel.Message) StringReader(java.io.StringReader) Reader(java.io.Reader) StringReader(java.io.StringReader) Template(freemarker.template.Template)

Aggregations

Configuration (freemarker.template.Configuration)72 Template (freemarker.template.Template)33 HashMap (java.util.HashMap)23 Writer (java.io.Writer)17 File (java.io.File)15 IOException (java.io.IOException)15 StringWriter (java.io.StringWriter)15 User (org.vcell.util.document.User)11 DefaultObjectWrapper (freemarker.template.DefaultObjectWrapper)10 TemplateRepresentation (org.restlet.ext.freemarker.TemplateRepresentation)10 Representation (org.restlet.representation.Representation)10 ClientResource (org.restlet.resource.ClientResource)10 VCellApiApplication (org.vcell.rest.VCellApiApplication)10 Gson (com.google.gson.Gson)9 TemplateException (freemarker.template.TemplateException)8 ClassTemplateLoader (freemarker.cache.ClassTemplateLoader)7 OutputStreamWriter (java.io.OutputStreamWriter)7 StringTemplateLoader (freemarker.cache.StringTemplateLoader)5 ThirdEyeAnomalyConfiguration (com.linkedin.thirdeye.anomaly.ThirdEyeAnomalyConfiguration)4 FileTemplateLoader (freemarker.cache.FileTemplateLoader)4