Search in sources :

Example 6 with Context

use of com.github.jknack.handlebars.Context in project ballerina by ballerina-lang.

the class BalGenerationUtils method writeBallerina.

/**
 * Write ballerina definition of a <code>object</code> to a file as described by <code>template.</code>
 *
 * @param object       Context object to be used by the template parser
 * @param templateDir  Directory with all the templates required for generating the source file
 * @param templateName Name of the parent template to be used
 * @param outPath      Destination path for writing the resulting source file
 * @throws IOException when file operations fail
 */
public static void writeBallerina(Object object, String templateDir, String templateName, String outPath) throws IOException {
    PrintWriter writer = null;
    try {
        Template template = compileTemplate(templateDir, templateName);
        Context context = Context.newBuilder(object).resolver(FieldValueResolver.INSTANCE).build();
        writer = new PrintWriter(outPath, "UTF-8");
        writer.println(template.apply(context));
    } finally {
        if (writer != null) {
            writer.close();
        }
    }
}
Also used : Context(com.github.jknack.handlebars.Context) PrintWriter(java.io.PrintWriter) Template(com.github.jknack.handlebars.Template)

Example 7 with Context

use of com.github.jknack.handlebars.Context in project ballerina by ballerina-lang.

the class CodeGenerator method writeBallerina.

/**
 * Write ballerina definition of a <code>object</code> to a file as described by <code>template.</code>
 *
 * @param object       Context object to be used by the template parser
 * @param templateDir  Directory with all the templates required for generating the source file
 * @param templateName Name of the parent template to be used
 * @param outPath      Destination path for writing the resulting source file
 * @throws IOException when file operations fail
 */
public void writeBallerina(Object object, String templateDir, String templateName, String outPath) throws IOException {
    PrintWriter writer = null;
    try {
        Template template = compileTemplate(templateDir, templateName);
        Context context = Context.newBuilder(object).resolver(MapValueResolver.INSTANCE, JavaBeanValueResolver.INSTANCE, FieldValueResolver.INSTANCE).build();
        writer = new PrintWriter(outPath, "UTF-8");
        writer.println(template.apply(context));
    } finally {
        if (writer != null) {
            writer.close();
        }
    }
}
Also used : Context(com.github.jknack.handlebars.Context) PrintWriter(java.io.PrintWriter) Template(com.github.jknack.handlebars.Template)

Example 8 with Context

use of com.github.jknack.handlebars.Context in project ballerina by ballerina-lang.

the class Writer method writeHtmlDocument.

/**
 * Write the HTML document from the Page object for a bal package.
 * @param object Page object which is generated from the bal package.
 * @param packageTemplateName hbs template file to be used.
 * @param filePath path of the file to write the output.
 */
public static void writeHtmlDocument(Object object, String packageTemplateName, String filePath) {
    String templatesFolderPath = System.getProperty(BallerinaDocConstants.TEMPLATES_FOLDER_PATH_KEY, File.separator + "docerina-templates" + File.separator + "html");
    PrintWriter writer = null;
    try {
        Handlebars handlebars = new Handlebars().with(new ClassPathTemplateLoader(templatesFolderPath), new FileTemplateLoader(templatesFolderPath));
        handlebars.registerHelpers(StringHelpers.class);
        Template template = handlebars.compile(packageTemplateName);
        writer = new PrintWriter(filePath, "UTF-8");
        Context context = Context.newBuilder(object).resolver(FieldValueResolver.INSTANCE).build();
        writer.println(template.apply(context));
        out.println("HTML file written: " + filePath);
    } catch (IOException e) {
        out.println("docerina: could not write HTML file " + filePath + System.lineSeparator() + e.getMessage());
    } finally {
        if (writer != null) {
            writer.close();
        }
    }
}
Also used : Context(com.github.jknack.handlebars.Context) Handlebars(com.github.jknack.handlebars.Handlebars) ClassPathTemplateLoader(com.github.jknack.handlebars.io.ClassPathTemplateLoader) IOException(java.io.IOException) FileTemplateLoader(com.github.jknack.handlebars.io.FileTemplateLoader) PrintWriter(java.io.PrintWriter) Template(com.github.jknack.handlebars.Template)

Example 9 with Context

use of com.github.jknack.handlebars.Context in project ballerina by ballerina-lang.

the class CodeGenerator method getConvertedString.

/**
 * Get converted string for given service definition.
 *
 * @param object       Object to be built as parsable context
 * @param templateDir  Template directory which contains templates for specific output type.
 * @param templateName Name of the template to be use for code generation.
 * @return generated string representation of passed object(ballerina service node)
 * @throws CodeGeneratorException when error occurs while compile, build context.
 */
public String getConvertedString(Object object, String templateDir, String templateName) throws CodeGeneratorException {
    Template template = compileTemplate(templateDir, templateName);
    Context context = Context.newBuilder(object).resolver(MapValueResolver.INSTANCE, JavaBeanValueResolver.INSTANCE, FieldValueResolver.INSTANCE).build();
    try {
        return template.apply(context);
    } catch (IOException e) {
        throw new CodeGeneratorException("Error while generating converted string", e);
    }
}
Also used : Context(com.github.jknack.handlebars.Context) CodeGeneratorException(org.ballerinalang.code.generator.exception.CodeGeneratorException) IOException(java.io.IOException) Template(com.github.jknack.handlebars.Template)

Example 10 with Context

use of com.github.jknack.handlebars.Context in project fess by codelibs.

the class ViewHelper method createCacheContent.

public String createCacheContent(final Map<String, Object> doc, final String[] queries) {
    final FessConfig fessConfig = ComponentUtil.getFessConfig();
    final FileTemplateLoader loader = new FileTemplateLoader(ResourceUtil.getViewTemplatePath().toFile());
    final Handlebars handlebars = new Handlebars(loader);
    Locale locale = ComponentUtil.getRequestManager().getUserLocale();
    if (locale == null) {
        locale = Locale.ENGLISH;
    }
    String url = DocumentUtil.getValue(doc, fessConfig.getIndexFieldUrl(), String.class);
    if (url == null) {
        url = ComponentUtil.getMessageManager().getMessage(locale, "labels.search_unknown");
    }
    doc.put(fessConfig.getResponseFieldUrlLink(), getUrlLink(doc));
    String createdStr;
    final Date created = DocumentUtil.getValue(doc, fessConfig.getIndexFieldCreated(), Date.class);
    if (created != null) {
        final SimpleDateFormat sdf = new SimpleDateFormat(CoreLibConstants.DATE_FORMAT_ISO_8601_EXTEND);
        createdStr = sdf.format(created);
    } else {
        createdStr = ComponentUtil.getMessageManager().getMessage(locale, "labels.search_unknown");
    }
    doc.put(CACHE_MSG, ComponentUtil.getMessageManager().getMessage(locale, "labels.search_cache_msg", url, createdStr));
    doc.put(QUERIES, queries);
    String cache = DocumentUtil.getValue(doc, fessConfig.getIndexFieldCache(), String.class);
    if (cache != null) {
        final String mimetype = DocumentUtil.getValue(doc, fessConfig.getIndexFieldMimetype(), String.class);
        if (!ComponentUtil.getFessConfig().isHtmlMimetypeForCache(mimetype)) {
            cache = StringEscapeUtils.escapeHtml4(cache);
        }
        cache = ComponentUtil.getPathMappingHelper().replaceUrls(cache);
        if (queries != null && queries.length > 0) {
            doc.put(HL_CACHE, replaceHighlightQueries(cache, queries));
        } else {
            doc.put(HL_CACHE, cache);
        }
    } else {
        doc.put(fessConfig.getIndexFieldCache(), StringUtil.EMPTY);
        doc.put(HL_CACHE, StringUtil.EMPTY);
    }
    try {
        final Template template = handlebars.compile(cacheTemplateName);
        final Context hbsContext = Context.newContext(doc);
        return template.apply(hbsContext);
    } catch (final Exception e) {
        logger.warn("Failed to create a cache response.", e);
    }
    return null;
}
Also used : Locale(java.util.Locale) Context(com.github.jknack.handlebars.Context) ServletContext(javax.servlet.ServletContext) Handlebars(com.github.jknack.handlebars.Handlebars) FileTemplateLoader(com.github.jknack.handlebars.io.FileTemplateLoader) FessConfig(org.codelibs.fess.mylasta.direction.FessConfig) SimpleDateFormat(com.ibm.icu.text.SimpleDateFormat) Date(java.util.Date) UnsupportedEncodingException(java.io.UnsupportedEncodingException) FessSystemException(org.codelibs.fess.exception.FessSystemException) ClientAbortException(org.apache.catalina.connector.ClientAbortException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) Template(com.github.jknack.handlebars.Template)

Aggregations

Context (com.github.jknack.handlebars.Context)12 Template (com.github.jknack.handlebars.Template)10 IOException (java.io.IOException)8 Handlebars (com.github.jknack.handlebars.Handlebars)6 PrintWriter (java.io.PrintWriter)3 UIContext (com.github.bordertech.wcomponents.UIContext)2 WComponent (com.github.bordertech.wcomponents.WComponent)2 SystemException (com.github.bordertech.wcomponents.util.SystemException)2 ClassPathTemplateLoader (com.github.jknack.handlebars.io.ClassPathTemplateLoader)2 FileTemplateLoader (com.github.jknack.handlebars.io.FileTemplateLoader)2 FileNotFoundException (java.io.FileNotFoundException)2 Test (org.junit.Test)2 TemplateLoader (com.github.jknack.handlebars.io.TemplateLoader)1 Gson (com.google.gson.Gson)1 JsonObject (com.google.gson.JsonObject)1 SimpleDateFormat (com.ibm.icu.text.SimpleDateFormat)1 RoutingContext (io.vertx.ext.web.RoutingContext)1 File (java.io.File)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 Type (java.lang.reflect.Type)1