Search in sources :

Example 1 with TransformationConfig

use of org.walkmod.conf.entities.TransformationConfig in project walkmod-core by walkmod.

the class AbstractXMLConfigurationAction method createTransformationList.

private List<Element> createTransformationList(List<TransformationConfig> transformations) {
    List<Element> result = null;
    if (transformations != null) {
        result = new LinkedList<Element>();
        Document document = provider.getDocument();
        for (TransformationConfig tcfg : transformations) {
            Element trans = document.createElement("transformation");
            String name = tcfg.getName();
            if (name != null) {
                trans.setAttribute("name", name);
            }
            trans.setAttribute("type", tcfg.getType());
            String mergePolicy = tcfg.getMergePolicy();
            if (mergePolicy != null) {
                trans.setAttribute("merge-policy", mergePolicy);
            }
            if (tcfg.isMergeable()) {
                trans.setAttribute("isMergeable", "true");
            }
            Map<String, Object> params = tcfg.getParameters();
            List<Element> paramListEment = createParamsElement(params);
            if (paramListEment != null) {
                for (Element param : paramListEment) {
                    trans.appendChild(param);
                }
            }
            result.add(trans);
        }
    }
    return result;
}
Also used : Element(org.w3c.dom.Element) TransformationConfig(org.walkmod.conf.entities.TransformationConfig) Document(org.w3c.dom.Document)

Example 2 with TransformationConfig

use of org.walkmod.conf.entities.TransformationConfig 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"));
        }
    }
}
Also used : WalkerConfig(org.walkmod.conf.entities.WalkerConfig) Element(org.w3c.dom.Element) ReaderConfig(org.walkmod.conf.entities.ReaderConfig) TransformationConfig(org.walkmod.conf.entities.TransformationConfig) ChainConfig(org.walkmod.conf.entities.ChainConfig) WriterConfig(org.walkmod.conf.entities.WriterConfig)

Example 3 with TransformationConfig

use of org.walkmod.conf.entities.TransformationConfig in project walkmod-core by walkmod.

the class XMLConfigurationProvider method getTransformationItems.

public List<TransformationConfig> getTransformationItems(Element element, boolean exceptionsEnabled) {
    List<TransformationConfig> transformationConfigs = new LinkedList<TransformationConfig>();
    NodeList transfNodes = element.getChildNodes();
    for (int j = 0; j < transfNodes.getLength(); j++) {
        element = (Element) transfNodes.item(j);
        if ("transformation".equals(element.getNodeName())) {
            TransformationConfig tc = new TransformationConfigImpl();
            String name = element.getAttribute("name");
            String visitor = element.getAttribute("type");
            String isMergeable = element.getAttribute("isMergeable");
            String mergePolicy = element.getAttribute("merge-policy");
            if ("".equals(visitor)) {
                throw new ConfigurationException("Invalid transformation definition: A " + "type attribute must be specified");
            }
            if ("".equals(name)) {
                name = null;
            }
            tc.setName(name);
            tc.setType(visitor);
            tc.setParameters(getParams(element));
            if (isMergeable != null && !("".equals(isMergeable))) {
                tc.isMergeable(Boolean.parseBoolean(isMergeable));
            }
            if (!"".equals(mergePolicy.trim())) {
                tc.isMergeable(true);
                tc.setMergePolicy(mergePolicy);
            }
            transformationConfigs.add(tc);
        }
    }
    return transformationConfigs;
}
Also used : TransformationConfigImpl(org.walkmod.conf.entities.impl.TransformationConfigImpl) ConfigurationException(org.walkmod.conf.ConfigurationException) TransformationConfig(org.walkmod.conf.entities.TransformationConfig) NodeList(org.w3c.dom.NodeList) LinkedList(java.util.LinkedList)

Example 4 with TransformationConfig

use of org.walkmod.conf.entities.TransformationConfig 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);
    }
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) WalkerConfig(org.walkmod.conf.entities.WalkerConfig) TransformationConfig(org.walkmod.conf.entities.TransformationConfig) JsonNode(com.fasterxml.jackson.databind.JsonNode) TextNode(com.fasterxml.jackson.databind.node.TextNode) WriterConfig(org.walkmod.conf.entities.WriterConfig) ReaderConfig(org.walkmod.conf.entities.ReaderConfig) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) TransformerException(javax.xml.transform.TransformerException)

Example 5 with TransformationConfig

use of org.walkmod.conf.entities.TransformationConfig in project walkmod-core by walkmod.

the class PrintChainsCommand method execute.

@Override
public void execute() throws Exception {
    if (help) {
        command.usage("chains");
    } else {
        WalkModFacade facade = new WalkModFacade(OptionsBuilder.options().configurationFile(configurationFile).build());
        Configuration cfg = facade.getConfiguration();
        at = new V2_AsciiTable();
        at.addRule();
        at.addRow("CHAIN", "READER PATH", "WRITER PATH", "TRANSFORMATIONS");
        at.addStrongRule();
        if (cfg == null) {
            at.addRule();
            log.error("Sorry, the current directory does not contain a walkmod configuration file or it is invalid.");
        }
        if (cfg != null) {
            Collection<ChainConfig> chains = cfg.getChainConfigs();
            if (chains != null) {
                for (ChainConfig cc : chains) {
                    List<TransformationConfig> transformations = cc.getWalkerConfig().getTransformations();
                    int numTransformations = transformations.size();
                    int numReaderIncludesExcludes = 0;
                    int includesLength = 0;
                    String[] excludes = cc.getReaderConfig().getExcludes();
                    if (excludes != null) {
                        numReaderIncludesExcludes = excludes.length;
                    }
                    String[] includes = cc.getReaderConfig().getIncludes();
                    if (includes != null) {
                        includesLength = includes.length;
                        numReaderIncludesExcludes += includes.length;
                    }
                    int limit = numReaderIncludesExcludes + 1;
                    if (numTransformations > numReaderIncludesExcludes) {
                        limit = numTransformations;
                    }
                    int includesExcludesWriterLength = 0;
                    int includesWriterLength = 0;
                    String[] excludesWriter = cc.getWriterConfig().getExcludes();
                    if (excludesWriter != null) {
                        includesExcludesWriterLength += excludesWriter.length;
                        if (excludesWriter.length + 1 > limit) {
                            limit = excludesWriter.length + 1;
                        }
                    }
                    String[] includesWriter = cc.getWriterConfig().getIncludes();
                    if (includesWriter != null) {
                        includesExcludesWriterLength += includesWriter.length;
                        includesWriterLength = includesWriter.length;
                        if (includesWriter.length + 1 > limit) {
                            limit = includesWriter.length + 1;
                        }
                    }
                    for (int i = 0; i < limit; i++) {
                        TransformationConfig next = null;
                        String type = "";
                        if (i < numTransformations) {
                            next = transformations.get(i);
                            type = "- " + next.getType();
                        }
                        if (i == 0) {
                            at.addRow(cc.getName(), cc.getReaderConfig().getPath(), cc.getWriterConfig().getPath(), type);
                        } else {
                            String readerWildcard = "";
                            if (i - 1 < includesLength) {
                                readerWildcard = "> " + includes[i - 1];
                            } else {
                                if (i - 1 < numReaderIncludesExcludes) {
                                    readerWildcard = "< " + excludes[i - 1 + includesLength];
                                }
                            }
                            String writerWildcard = "";
                            if (includesWriter != null && i - 1 < includesWriter.length) {
                                writerWildcard = "> " + includesWriter[i - 1];
                            } else if (i - 1 < includesExcludesWriterLength) {
                                writerWildcard = "< " + excludesWriter[i - 1 + includesWriterLength];
                            }
                            at.addRow("", readerWildcard, writerWildcard, type);
                        }
                    }
                    at.addRule();
                }
            }
        }
    }
}
Also used : Configuration(org.walkmod.conf.entities.Configuration) V2_AsciiTable(de.vandermeer.asciitable.v2.V2_AsciiTable) WalkModFacade(org.walkmod.WalkModFacade) TransformationConfig(org.walkmod.conf.entities.TransformationConfig) ChainConfig(org.walkmod.conf.entities.ChainConfig)

Aggregations

TransformationConfig (org.walkmod.conf.entities.TransformationConfig)52 Test (org.junit.Test)40 File (java.io.File)36 AddTransformationCommand (org.walkmod.commands.AddTransformationCommand)36 Configuration (org.walkmod.conf.entities.Configuration)23 ConfigurationImpl (org.walkmod.conf.entities.impl.ConfigurationImpl)20 ChainConfig (org.walkmod.conf.entities.ChainConfig)11 LinkedList (java.util.LinkedList)7 HashMap (java.util.HashMap)5 WalkerConfig (org.walkmod.conf.entities.WalkerConfig)5 Element (org.w3c.dom.Element)3 ReaderConfig (org.walkmod.conf.entities.ReaderConfig)3 WriterConfig (org.walkmod.conf.entities.WriterConfig)3 ChainConfigImpl (org.walkmod.conf.entities.impl.ChainConfigImpl)3 TransformationConfigImpl (org.walkmod.conf.entities.impl.TransformationConfigImpl)3 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)2 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)2 TextNode (com.fasterxml.jackson.databind.node.TextNode)2