Search in sources :

Example 1 with NoIndentWriter

use of org.stringtemplate.v4.NoIndentWriter in project infoarchive-sip-sdk by Enterprise-Content-Management.

the class StringTemplate method writeRow.

@Override
public void writeRow(D domainObject, Map<String, ContentInfo> contentInfo, PrintWriter writer) throws IOException {
    ST template = prepareTemplate(templatePrototype, domainObject, contentInfo);
    template.write(new NoIndentWriter(writer));
}
Also used : ST(org.stringtemplate.v4.ST) NoIndentWriter(org.stringtemplate.v4.NoIndentWriter)

Example 2 with NoIndentWriter

use of org.stringtemplate.v4.NoIndentWriter in project buck by facebook.

the class IjProjectWriter method writeToFile.

@VisibleForTesting
protected void writeToFile(ST contents, Path path) throws IOException {
    StringWriter stringWriter = new StringWriter();
    AutoIndentWriter noIndentWriter = new AutoIndentWriter(stringWriter);
    contents.write(noIndentWriter);
    byte[] renderedContentsBytes = noIndentWriter.toString().getBytes(StandardCharsets.UTF_8);
    if (projectFilesystem.exists(path)) {
        Sha1HashCode fileSha1 = projectFilesystem.computeSha1(path);
        Sha1HashCode contentsSha1 = Sha1HashCode.fromHashCode(Hashing.sha1().hashBytes(renderedContentsBytes));
        if (fileSha1.equals(contentsSha1)) {
            return;
        }
    }
    boolean danglingTempFile = false;
    Path tempFile = projectFilesystem.createTempFile(IDEA_CONFIG_DIR_PREFIX, path.getFileName().toString(), ".tmp");
    try {
        danglingTempFile = true;
        try (OutputStream outputStream = projectFilesystem.newFileOutputStream(tempFile)) {
            outputStream.write(contents.render().getBytes());
        }
        projectFilesystem.createParentDirs(path);
        projectFilesystem.move(tempFile, path, StandardCopyOption.REPLACE_EXISTING);
        danglingTempFile = false;
    } finally {
        if (danglingTempFile) {
            projectFilesystem.deleteFileAtPath(tempFile);
        }
    }
}
Also used : AutoIndentWriter(org.stringtemplate.v4.AutoIndentWriter) Path(java.nio.file.Path) StringWriter(java.io.StringWriter) Sha1HashCode(com.facebook.buck.util.sha1.Sha1HashCode) OutputStream(java.io.OutputStream) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 3 with NoIndentWriter

use of org.stringtemplate.v4.NoIndentWriter in project camel by apache.

the class StringTemplateEndpoint method onExchange.

@Override
protected void onExchange(Exchange exchange) throws Exception {
    StringWriter buffer = new StringWriter();
    @SuppressWarnings("unchecked") Map<String, Object> variableMap = exchange.getIn().getHeader(StringTemplateConstants.STRINGTEMPLATE_VARIABLE_MAP, Map.class);
    if (variableMap == null) {
        variableMap = ExchangeHelper.createVariableMap(exchange);
    }
    // getResourceAsInputStream also considers the content cache
    String text = exchange.getContext().getTypeConverter().mandatoryConvertTo(String.class, getResourceAsInputStream());
    ST template = new ST(text, delimiterStart, delimiterStop);
    for (Map.Entry<String, Object> entry : variableMap.entrySet()) {
        template.add(entry.getKey(), entry.getValue());
    }
    log.debug("StringTemplate is writing using attributes: {}", variableMap);
    template.write(new NoIndentWriter(buffer));
    // now lets output the results to the exchange
    Message out = exchange.getOut();
    out.setBody(buffer.toString());
    out.setHeaders(exchange.getIn().getHeaders());
    out.setHeader(StringTemplateConstants.STRINGTEMPLATE_RESOURCE_URI, getResourceUri());
    out.setAttachments(exchange.getIn().getAttachments());
}
Also used : ST(org.stringtemplate.v4.ST) NoIndentWriter(org.stringtemplate.v4.NoIndentWriter) StringWriter(java.io.StringWriter) Message(org.apache.camel.Message) Map(java.util.Map)

Aggregations

StringWriter (java.io.StringWriter)2 NoIndentWriter (org.stringtemplate.v4.NoIndentWriter)2 ST (org.stringtemplate.v4.ST)2 Sha1HashCode (com.facebook.buck.util.sha1.Sha1HashCode)1 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 OutputStream (java.io.OutputStream)1 Path (java.nio.file.Path)1 Map (java.util.Map)1 Message (org.apache.camel.Message)1 AutoIndentWriter (org.stringtemplate.v4.AutoIndentWriter)1