use of org.walkmod.ChainWriter 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);
}
}
use of org.walkmod.ChainWriter in project walkmod-core by walkmod.
the class DefaultChainWalkerAdapter method prepare.
@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public void prepare() throws WalkModException {
walker.setResource(getModel());
walker.setRootNamespace(config.getRootNamespace());
ChainWriter mw = ap.getChainWriter();
mw.setExcludes(config.getChainConfig().getWriterConfig().getExcludes());
mw.setIncludes(config.getChainConfig().getWriterConfig().getIncludes());
walker.setWriter(ap.getChainWriter());
walker.setChainConfig(config.getChainConfig());
ChainConfig ac = config.getChainConfig();
Object visitor;
Configuration c = ac.getConfiguration();
Parser parser = null;
String parserType = config.getParserConfig().getType();
if (parserType != null) {
Object parserInstance = c.getBean(parserType, config.getParserConfig().getParameters());
if (parserInstance != null) {
if (parserInstance instanceof Parser) {
parser = (Parser) parserInstance;
walker.setParser(parser);
} else {
throw new WalkModException("The parser " + parserType + " must implement " + Parser.class.getName());
}
} else {
throw new WalkModException("The parser " + parserType + " does not exist.");
}
}
Collection<TransformationConfig> cfgs = getTransformationConfig();
if (cfgs != null) {
for (TransformationConfig config : cfgs) {
setName(config.getName());
visitor = config.getVisitorInstance();
if (visitor == null || "".equals(config.getType())) {
visitor = c.getBean(config.getType(), config.getParameters());
}
if (visitor instanceof ResourceModifier) {
((ResourceModifier) visitor).setResource(getModel());
}
if (visitor instanceof ParserAware) {
((ParserAware) visitor).setParser(parser);
}
if (visitor != null) {
LOG.debug("setting chain[\"" + ac.getName() + "\"].transformation[\"" + getName() + "\"].walker = " + walker.getClass().getName());
LOG.debug("setting chain[\"" + ac.getName() + "\"].transformation[\"" + getName() + "\"].visitor = " + visitor.getClass().getName());
visitors.add(visitor);
} else {
walker = null;
visitor = null;
LOG.debug("Transformation[" + getName() + "] without walker and visitor");
}
}
}
walker.setVisitors(visitors);
wi.init(this);
}
use of org.walkmod.ChainWriter in project walkmod-core by walkmod.
the class DefaultChainAdapter method prepare.
@Override
public void prepare() throws WalkModException {
setName(ac.getName());
ReaderConfig readerConfig = ac.getReaderConfig();
WriterConfig writerConfig = ac.getWriterConfig();
String modelName = readerConfig.getPath();
String modelType = readerConfig.getType();
ChainReader reader = readerConfig.getModelReader();
if (reader == null) {
try {
reader = (ChainReader) ac.getConfiguration().getBean(modelType, readerConfig.getParameters());
} catch (Exception e2) {
throw new WalkModException("The model " + modelName + ", whose type is " + modelType + "in the architecture " + getName() + " cannot be loaded ", e2);
}
}
readerConfig.setModelReader(reader);
reader.setPath(readerConfig.getPath());
reader.setExcludes(readerConfig.getExcludes());
reader.setIncludes(readerConfig.getIncludes());
try {
setResource(reader.read());
LOG.debug("Model " + modelName + " loaded");
} catch (Exception e2) {
throw new WalkModException("The model " + modelName + ", whose type is " + modelType + "in the architecture " + getName() + " cannot be read ", e2);
}
WalkerConfig wc = ac.getWalkerConfig();
ChainWalkerAdapter wa = new DefaultChainWalkerAdapter();
setWalkerAdapter(wa);
ChainWalker walker = wc.getWalker();
if (walker == null) {
walker = (ChainWalker) ac.getConfiguration().getBean(wc.getType(), wc.getParams());
}
wc.setWalker(walker);
wa.setWalker(walker);
wa.setWalkerConfig(wc);
wa.setArchitectureProxy(this);
wa.setWalkerInvocation(new DefaultChainWalkerInvocation());
ChainWriter writer = writerConfig.getModelWriter();
if (writer == null) {
try {
writer = (ChainWriter) ac.getConfiguration().getBean(writerConfig.getType(), writerConfig.getParams());
} catch (Exception e2) {
throw new WalkModException("The writer " + ", whose type is " + writerConfig.getType() + "in the architecture " + getName() + " cannot be read ", e2);
}
}
writerConfig.setModelWriter(writer);
writer.setPath(writerConfig.getPath());
setChainWriter(writer);
wa.prepare();
ai.init(this);
}
Aggregations