use of org.walkmod.conf.entities.WriterConfig in project walkmod-core by walkmod.
the class AddIncludesOrExcludesXMLAction method doAction.
@Override
public void doAction() throws Exception {
Document document = provider.getDocument();
Element rootElement = document.getDocumentElement();
NodeList children = rootElement.getChildNodes();
int childSize = children.getLength();
boolean chainFound = false;
boolean containsChains = false;
if (chain == null) {
chain = "default";
}
for (int i = 0; i < childSize; i++) {
Node childNode = children.item(i);
if (childNode instanceof Element) {
Element child = (Element) childNode;
final String nodeName = child.getNodeName();
String writerPath = "src/main/java";
if ("chain".equals(nodeName)) {
containsChains = true;
String name = child.getAttribute("name");
if (name.equals(chain)) {
chainFound = true;
NodeList chainChildren = child.getChildNodes();
int chainChildrenSize = chainChildren.getLength();
boolean existsReader = false;
boolean existsWriter = false;
for (int j = 0; j < chainChildrenSize; j++) {
Node chainChild = chainChildren.item(j);
if (chainChild instanceof Element) {
Element elementChain = (Element) chainChild;
String elementName = elementChain.getNodeName();
if (elementName.equals("reader") && setToReader) {
existsReader = true;
updateElement(elementChain);
writerPath = elementChain.getAttribute("path");
} else if (elementName.equals("writer") && setToWriter) {
existsWriter = true;
updateElement(elementChain);
}
}
}
if (!existsReader && setToReader) {
Element reader = document.createElement("reader");
reader.setAttribute("path", "src/main/java");
updateElement(reader);
if (chainChildrenSize == 0) {
child.appendChild(reader);
} else {
child.insertBefore(reader, chainChildren.item(0));
}
}
if (!existsWriter && setToWriter) {
Element writer = document.createElement("writer");
writer.setAttribute("path", writerPath);
updateElement(writer);
child.appendChild(writer);
}
}
}
}
}
if (!chainFound) {
ChainConfig chainCfg = null;
if (!containsChains) {
Configuration configuration = new ConfigurationImpl();
provider.setConfiguration(configuration);
// we write specifically a default chain, and
// afterwards, we
// add the requested one.
provider.loadChains();
Collection<ChainConfig> chainCfgs = configuration.getChainConfigs();
chainCfg = chainCfgs.iterator().next();
NodeList child = rootElement.getChildNodes();
int limit = child.getLength();
for (int i = 0; i < limit; i++) {
Node item = child.item(i);
if (item instanceof Element) {
Element auxElem = (Element) item;
if (auxElem.getNodeName().equals("transformation")) {
rootElement.removeChild(auxElem);
}
}
}
if (!chain.equals("default")) {
rootElement.appendChild(createChainElement(chainCfg));
chainCfg = new ChainConfigImpl();
chainCfg.setName(chain);
provider.addDefaultReaderConfig(chainCfg);
provider.addDefaultWriterConfig(chainCfg);
provider.addDefaultWalker(chainCfg);
}
} else {
chainCfg = new ChainConfigImpl();
chainCfg.setName(chain);
provider.addDefaultReaderConfig(chainCfg);
provider.addDefaultWriterConfig(chainCfg);
provider.addDefaultWalker(chainCfg);
}
if (setToReader) {
ReaderConfig rcfg = chainCfg.getReaderConfig();
String[] aux = new String[includes.size()];
rcfg.setIncludes(includes.toArray(aux));
chainCfg.setReaderConfig(rcfg);
}
if (setToWriter) {
WriterConfig wcfg = chainCfg.getWriterConfig();
String[] aux = new String[includes.size()];
wcfg.setIncludes(includes.toArray(aux));
chainCfg.setWriterConfig(wcfg);
}
rootElement.appendChild(createChainElement(chainCfg));
}
provider.persist();
}
use of org.walkmod.conf.entities.WriterConfig in project walkmod-core by walkmod.
the class LanguageConfigurationProvider method updateNulls.
private void updateNulls() {
Collection<ChainConfig> ccs = configuration.getChainConfigs();
Element rootElement = document.getDocumentElement();
if (ccs != null) {
for (ChainConfig cc : ccs) {
ReaderConfig rc = cc.getReaderConfig();
if (rc.getType() == null) {
rc.setType(rootElement.getAttribute("reader"));
}
if (rc.getPath() == null) {
rc.setPath(rootElement.getAttribute("path"));
}
WriterConfig wc = cc.getWriterConfig();
if (wc.getType() == null) {
wc.setType(rootElement.getAttribute("writer"));
}
if (wc.getPath() == null) {
wc.setPath(rootElement.getAttribute("path"));
}
WalkerConfig walkc = cc.getWalkerConfig();
if (walkc.getType() == null) {
walkc.setType(rootElement.getAttribute("walker"));
}
if (walkc.getParserConfig().getType() == null) {
if (!"".equals(rootElement.getAttribute("parser"))) {
walkc.getParserConfig().setType(rootElement.getAttribute("parser"));
}
}
List<TransformationConfig> transformations = walkc.getTransformations();
if (transformations != null) {
for (TransformationConfig tc : transformations) {
if (tc.isMergeable()) {
if (tc.getMergePolicy() == null) {
tc.setMergePolicy(DEFAULT_MERGE_ENGINE_NAME);
}
}
}
}
wc.setPatcherType(rootElement.getAttribute("patcher"));
}
}
}
use of org.walkmod.conf.entities.WriterConfig in project walkmod-core by walkmod.
the class AddChainYMLAction method doAction.
@Override
public void doAction(JsonNode chainsNode) throws Exception {
ArrayNode chainsList = null;
ObjectMapper mapper = provider.getObjectMapper();
if (chainsNode != null) {
if (!chainsNode.has("chains")) {
chainsList = new ArrayNode(mapper.getNodeFactory());
if (chainsNode.isObject()) {
ObjectNode aux = (ObjectNode) chainsNode;
aux.set("chains", chainsList);
} else {
throw new TransformerException("The root element is not a JSON node");
}
} else {
JsonNode aux = chainsNode.get("chains");
if (aux.isArray()) {
chainsList = (ArrayNode) chainsNode.get("chains");
} else {
throw new TransformerException("The plugins element is not a valid array");
}
}
}
ObjectNode chainNode = new ObjectNode(mapper.getNodeFactory());
ReaderConfig readerCfg = chainCfg.getReaderConfig();
if (readerCfg != null) {
if (chainsNode == null) {
chainsNode = new ObjectNode(mapper.getNodeFactory());
ObjectNode aux = (ObjectNode) chainsNode;
chainsList = new ArrayNode(mapper.getNodeFactory());
aux.set("chains", chainsList);
}
ObjectNode readerNode = new ObjectNode(mapper.getNodeFactory());
chainNode.set("reader", readerNode);
populateWriterReader(readerNode, readerCfg.getPath(), readerCfg.getType(), readerCfg.getIncludes(), readerCfg.getExcludes(), readerCfg.getParameters());
} else {
provider.addDefaultReaderConfig(chainCfg);
}
WalkerConfig walkerCfg = chainCfg.getWalkerConfig();
if (walkerCfg != null) {
ObjectNode walkerNode = null;
String type = walkerCfg.getType();
if (type != null) {
if (chainsNode == null) {
chainsNode = new ObjectNode(mapper.getNodeFactory());
ObjectNode aux = (ObjectNode) chainsNode;
chainsList = new ArrayNode(mapper.getNodeFactory());
aux.set("chains", chainsList);
}
walkerNode = new ObjectNode(mapper.getNodeFactory());
chainNode.set("walker", walkerNode);
walkerNode.set("type", new TextNode(type));
}
Map<String, Object> wparams = walkerCfg.getParams();
if (wparams != null && !wparams.isEmpty()) {
if (walkerNode == null) {
if (chainsNode == null) {
chainsNode = new ObjectNode(mapper.getNodeFactory());
ObjectNode aux = (ObjectNode) chainsNode;
chainsList = new ArrayNode(mapper.getNodeFactory());
aux.set("chains", chainsList);
}
walkerNode = new ObjectNode(mapper.getNodeFactory());
chainNode.set("walker", walkerNode);
}
populateParams(walkerNode, wparams);
}
String rootNamespace = walkerCfg.getRootNamespace();
if (rootNamespace != null) {
if (walkerNode == null) {
if (chainsNode == null) {
chainsNode = new ObjectNode(mapper.getNodeFactory());
ObjectNode aux = (ObjectNode) chainsNode;
chainsList = new ArrayNode(mapper.getNodeFactory());
aux.set("chains", chainsList);
}
walkerNode = new ObjectNode(mapper.getNodeFactory());
chainNode.set("walker", walkerNode);
}
walkerNode.set("root-namespace", new TextNode(rootNamespace));
}
List<TransformationConfig> transformationList = walkerCfg.getTransformations();
if (transformationList != null && !transformationList.isEmpty()) {
ArrayNode transformationListNode = new ArrayNode(mapper.getNodeFactory());
if (walkerNode == null) {
if (chainsNode == null) {
ObjectNode aux = new ObjectNode(mapper.getNodeFactory());
aux.set("transformations", transformationListNode);
chainsNode = aux;
} else {
chainNode.set("transformations", transformationListNode);
}
} else {
walkerNode.set("transformations", transformationListNode);
}
for (TransformationConfig transCfg : transformationList) {
ObjectNode transformationNode = new ObjectNode(mapper.getNodeFactory());
transformationListNode.add(transformationNode);
createTransformation(transformationNode, transCfg);
}
}
}
WriterConfig writerCfg = chainCfg.getWriterConfig();
if (writerCfg != null) {
if (chainsNode == null) {
chainsNode = new ObjectNode(mapper.getNodeFactory());
ObjectNode aux = (ObjectNode) chainsNode;
chainsList = new ArrayNode(mapper.getNodeFactory());
aux.set("chains", chainsList);
}
ObjectNode writerNode = new ObjectNode(mapper.getNodeFactory());
chainNode.set("writer", writerNode);
populateWriterReader(writerNode, writerCfg.getPath(), writerCfg.getType(), writerCfg.getIncludes(), writerCfg.getExcludes(), writerCfg.getParams());
} else {
provider.addDefaultWriterConfig(chainCfg);
}
if (chainsList != null) {
int beforePos = -1;
if (before != null) {
Iterator<JsonNode> it = chainsList.iterator();
int i = 0;
while (it.hasNext() && beforePos == -1) {
JsonNode next = it.next();
if (next.get("name").equals(before)) {
beforePos = i;
}
i++;
}
}
if (beforePos == -1) {
chainsList.add(chainNode);
} else {
chainsList.insert(beforePos, chainNode);
}
}
if (readerCfg != null || walkerCfg != null || writerCfg != null) {
provider.write(chainsNode);
}
}
use of org.walkmod.conf.entities.WriterConfig 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.conf.entities.WriterConfig in project walkmod-core by walkmod.
the class AbstractChainConfigurationProvider method addDefaultWriterConfig.
public void addDefaultWriterConfig(ChainConfig ac) {
WriterConfig wc = new WriterConfigImpl();
wc.setPath(ac.getReaderConfig().getPath());
wc.setType(null);
ac.setWriterConfig(wc);
}
Aggregations