Search in sources :

Example 1 with MustacheNotFoundException

use of com.github.mustachejava.MustacheNotFoundException in project syndesis by syndesisio.

the class IconGenerator method generate.

public static String generate(final String template, final String name) {
    Mustache mustache;
    try {
        mustache = MUSTACHE_FACTORY.compile("/icon-generator/" + template + ".svg.mustache");
    } catch (final MustacheNotFoundException e) {
        LOG.warn("Unable to load icon template for: `{}`, will use default template", template);
        LOG.debug("Unable to load icon template for: {}", template, e);
        mustache = MUSTACHE_FACTORY.compile("/icon-generator/default.svg.mustache");
    }
    final Map<String, String> data = new HashMap<>();
    final String color = COLORS[(int) (Math.random() * COLORS.length)];
    data.put("color", color);
    data.put("letter", LETTERS.get(Character.toUpperCase(name.charAt(0))));
    try (StringWriter icon = new StringWriter()) {
        mustache.execute(icon, data).flush();
        final String trimmed = trimXml(icon.toString());
        return "data:image/svg+xml," + ESCAPER.escape(trimmed);
    } catch (final IOException e) {
        throw new SyndesisServerException("Unable to generate icon from template `" + template + "`, for name: " + name, e);
    }
}
Also used : MustacheNotFoundException(com.github.mustachejava.MustacheNotFoundException) StringWriter(java.io.StringWriter) HashMap(java.util.HashMap) SyndesisServerException(io.syndesis.common.util.SyndesisServerException) Mustache(com.github.mustachejava.Mustache) IOException(java.io.IOException)

Aggregations

Mustache (com.github.mustachejava.Mustache)1 MustacheNotFoundException (com.github.mustachejava.MustacheNotFoundException)1 SyndesisServerException (io.syndesis.common.util.SyndesisServerException)1 IOException (java.io.IOException)1 StringWriter (java.io.StringWriter)1 HashMap (java.util.HashMap)1