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));
}
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);
}
}
}
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());
}
Aggregations