use of org.walkmod.walkers.VisitorContext in project walkmod-core by walkmod.
the class DefaultTemplateVisitor method doPlainOutput.
public void doPlainOutput(String templateResult, VisitorContext context) throws Exception {
WriterConfig writerConfig = context.getArchitectureConfig().getWriterConfig();
ChainWriter chainWriter = writerConfig.getModelWriter();
if (output == null) {
String fileName = currentTemplate.getName();
if (context.containsKey(AbstractWalker.ORIGINAL_FILE_KEY)) {
log.debug("Original file path found");
File originalFile = (File) context.get(AbstractWalker.ORIGINAL_FILE_KEY);
String fullPath = originalFile.getPath();
String readerPath = context.getArchitectureConfig().getReaderConfig().getPath();
fileName = fullPath.substring(readerPath.length());
if (fileName.startsWith(File.separator)) {
fileName = fileName.substring(1);
}
} else {
log.debug("working with the template name");
}
int pos = fileName.lastIndexOf(".");
if (pos != -1) {
log.debug("Removing the existing suffix");
fileName = fileName.substring(0, pos);
}
log.warn("Setting a default output file! [" + fileName + ".result]");
VisitorContext auxCtxt = new VisitorContext();
File defaultOutputFile = new File(writerConfig.getPath(), fileName + "." + suffix);
if (!defaultOutputFile.exists()) {
log.info("++" + defaultOutputFile.getAbsolutePath());
defaultOutputFile.getParentFile().mkdirs();
defaultOutputFile.createNewFile();
}
auxCtxt.put(AbstractWalker.ORIGINAL_FILE_KEY, defaultOutputFile);
chainWriter.write(templateResult, auxCtxt);
} else {
String outputFile = output;
// validates if it is a template name to reduce
// computation
char[] chars = outputFile.toCharArray();
boolean isGString = false;
for (int i = 0; i < chars.length && !isGString; i++) {
isGString = chars[i] == '$' || chars[i] == '<';
}
if (isGString) {
GStringTemplateEngine engine = new GStringTemplateEngine();
Template templateName = engine.createTemplate(output);
StringWriter stringWriter = new StringWriter();
Writer platformWriter = new PlatformLineWriter(stringWriter);
templateName.make(context).writeTo(platformWriter);
outputFile = platformWriter.toString();
}
File file = new File(outputFile);
VisitorContext auxCtxt = new VisitorContext();
auxCtxt.put(AbstractWalker.ORIGINAL_FILE_KEY, file);
auxCtxt.put("append", Boolean.TRUE);
if (!file.exists()) {
log.info("++" + file.getAbsolutePath());
file.getParentFile().mkdirs();
file.createNewFile();
}
chainWriter.write(templateResult, auxCtxt);
}
}
Aggregations