Search in sources :

Example 6 with ChainConfig

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

the class YAMLConfigurationProviderTest method testTransformationsYaml.

@Test
public void testTransformationsYaml() {
    YAMLConfigurationProvider provider = new YAMLConfigurationProvider("src/test/resources/yaml/basic.yml");
    Configuration conf = new ConfigurationImpl();
    provider.init(conf);
    provider.load();
    Assert.assertNotNull(conf.getChainConfigs());
    ChainConfig cc = conf.getChainConfigs().iterator().next();
    Assert.assertNotNull(cc.getWalkerConfig().getTransformations());
    Iterator<TransformationConfig> it = cc.getWalkerConfig().getTransformations().iterator();
    TransformationConfig tc = it.next();
    Assert.assertEquals("walkmod:commons:method-refactor", tc.getType());
    Assert.assertEquals("src/conf/refactoring-methods.json", tc.getParameters().get("refactoringConfigFile"));
}
Also used : Configuration(org.walkmod.conf.entities.Configuration) TransformationConfig(org.walkmod.conf.entities.TransformationConfig) ConfigurationImpl(org.walkmod.conf.entities.impl.ConfigurationImpl) ChainConfig(org.walkmod.conf.entities.ChainConfig) Test(org.junit.Test)

Example 7 with ChainConfig

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

the class YAMLConfigurationProviderTest method testChainsYaml.

@Test
public void testChainsYaml() {
    YAMLConfigurationProvider provider = new YAMLConfigurationProvider("src/test/resources/yaml/chains.yml");
    Configuration conf = new ConfigurationImpl();
    provider.init(conf);
    provider.load();
    Assert.assertNotNull(conf.getChainConfigs());
    ChainConfig cc = conf.getChainConfigs().iterator().next();
    Iterator<TransformationConfig> it = cc.getWalkerConfig().getTransformations().iterator();
    TransformationConfig first = it.next();
    Assert.assertEquals("walkmod:commons:method-refactor", first.getType());
    Assert.assertNotNull(cc.getReaderConfig());
    Assert.assertNotNull(cc.getWriterConfig());
}
Also used : Configuration(org.walkmod.conf.entities.Configuration) TransformationConfig(org.walkmod.conf.entities.TransformationConfig) ConfigurationImpl(org.walkmod.conf.entities.impl.ConfigurationImpl) ChainConfig(org.walkmod.conf.entities.ChainConfig) Test(org.junit.Test)

Example 8 with ChainConfig

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

the class AddTransformationYMLAction method doAction.

@Override
public void doAction(JsonNode chainsNode) throws Exception {
    ArrayNode transformationsNode = null;
    boolean isMultiModule = chainsNode.has("modules");
    ObjectMapper mapper = provider.getObjectMapper();
    if (!isMultiModule) {
        boolean validChainName = chain != null && !"".equals(chain) && !"default".equals(chain);
        if (!chainsNode.has("chains")) {
            if (chainsNode.has("transformations")) {
                JsonNode aux = chainsNode.get("transformations");
                if (aux.isArray()) {
                    transformationsNode = (ArrayNode) aux;
                }
                if (!validChainName) {
                    ObjectNode auxRoot = (ObjectNode) chainsNode;
                    if (transformationsNode == null) {
                        transformationsNode = new ArrayNode(mapper.getNodeFactory());
                    }
                    auxRoot.set("transformations", transformationsNode);
                } else {
                    // reset the root
                    chainsNode = new ObjectNode(mapper.getNodeFactory());
                    ObjectNode auxRoot = (ObjectNode) chainsNode;
                    // the default chain list added
                    ObjectNode chainObject = new ObjectNode(mapper.getNodeFactory());
                    chainObject.set("name", new TextNode("default"));
                    chainObject.set("transformations", transformationsNode);
                    ArrayNode chainsListNode = new ArrayNode(mapper.getNodeFactory());
                    // the requested chain added
                    ObjectNode newChain = new ObjectNode(mapper.getNodeFactory());
                    newChain.set("name", new TextNode(chain));
                    if (path != null && !"".equals(path.trim())) {
                        ObjectNode readerNode = new ObjectNode(mapper.getNodeFactory());
                        newChain.set("reader", readerNode);
                        populateWriterReader(readerNode, path, null, null, null, null);
                        ObjectNode writerNode = new ObjectNode(mapper.getNodeFactory());
                        newChain.set("writer", writerNode);
                        populateWriterReader(writerNode, path, null, null, null, null);
                    }
                    transformationsNode = new ArrayNode(mapper.getNodeFactory());
                    newChain.set("transformations", transformationsNode);
                    if (before == null || !"default".equals(before)) {
                        chainsListNode.add(chainObject);
                    }
                    chainsListNode.add(newChain);
                    if (before != null && "default".equals(before)) {
                        chainsListNode.add(chainObject);
                    }
                    auxRoot.set("chains", chainsListNode);
                }
            } else {
                ObjectNode auxRoot = (ObjectNode) chainsNode;
                transformationsNode = new ArrayNode(mapper.getNodeFactory());
                boolean writeChainInfo = validChainName;
                if (!writeChainInfo) {
                    writeChainInfo = path != null && !"".equals(path.trim());
                    chain = "default";
                }
                if (writeChainInfo) {
                    ArrayNode auxChainsList = new ArrayNode(mapper.getNodeFactory());
                    ObjectNode aux = new ObjectNode(mapper.getNodeFactory());
                    auxChainsList.add(aux);
                    aux.set("name", new TextNode(chain));
                    if (path != null && !"".equals(path.trim())) {
                        ObjectNode readerNode = new ObjectNode(mapper.getNodeFactory());
                        aux.set("reader", readerNode);
                        populateWriterReader(readerNode, path, null, null, null, null);
                    }
                    auxRoot.set("chains", auxChainsList);
                    if (path != null && !"".equals(path.trim())) {
                        ObjectNode writerNode = new ObjectNode(mapper.getNodeFactory());
                        aux.set("writer", writerNode);
                        populateWriterReader(writerNode, path, null, null, null, null);
                    }
                    auxRoot = aux;
                }
                auxRoot.set("transformations", transformationsNode);
            }
        } else {
            if (validChainName) {
                JsonNode aux = chainsNode.get("chains");
                boolean found = false;
                if (aux.isArray()) {
                    Iterator<JsonNode> it = aux.elements();
                    while (it.hasNext()) {
                        JsonNode next = it.next();
                        if (next.has("name")) {
                            String id = next.get("name").asText();
                            if (chain.equals(id)) {
                                found = true;
                                if (next.has("transformations")) {
                                    JsonNode auxTrans = next.get("transformations");
                                    if (auxTrans.isArray()) {
                                        transformationsNode = (ArrayNode) auxTrans;
                                    } else {
                                        throw new Exception("The chain [" + chain + "] does not have a valid transformations node");
                                    }
                                } else if (next.isObject()) {
                                    ObjectNode auxNext = (ObjectNode) next;
                                    transformationsNode = new ArrayNode(mapper.getNodeFactory());
                                    auxNext.set("transformations", transformationsNode);
                                } else {
                                    throw new Exception("The chain [" + chain + "] does not have a valid structure");
                                }
                            }
                        }
                    }
                    if (!found) {
                        ChainConfig chainCfg = new ChainConfigImpl();
                        chainCfg.setName(chain);
                        WalkerConfig walkerCfg = new WalkerConfigImpl();
                        List<TransformationConfig> transfs = new LinkedList<TransformationConfig>();
                        transfs.add(transformationCfg);
                        walkerCfg.setTransformations(transfs);
                        chainCfg.setWalkerConfig(walkerCfg);
                        provider.addChainConfig(chainCfg, false, before);
                        return;
                    }
                }
            } else {
                ObjectNode node = new ObjectNode(mapper.getNodeFactory());
                node.set("name", new TextNode(chain));
                ArrayNode transNodes = new ArrayNode(mapper.getNodeFactory());
                node.set("transformations", transNodes);
                ArrayNode array = (ArrayNode) chainsNode.get("chains");
                array.add(node);
                ObjectNode transformationNode = new ObjectNode(mapper.getNodeFactory());
                transNodes.add(transformationNode);
                createTransformation(transformationNode, transformationCfg);
                return;
            }
        }
        if (transformationsNode != null) {
            ObjectNode transformationNode = new ObjectNode(mapper.getNodeFactory());
            if (order != null && order < transformationsNode.size()) {
                transformationsNode.insert(order, transformationNode);
            } else {
                transformationsNode.add(transformationNode);
            }
            createTransformation(transformationNode, transformationCfg);
            provider.write(chainsNode);
            return;
        } else if (chain != null) {
            throw new Exception("The chain [" + chain + "] does not exists");
        }
    }
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) WalkerConfig(org.walkmod.conf.entities.WalkerConfig) TransformationConfig(org.walkmod.conf.entities.TransformationConfig) ChainConfigImpl(org.walkmod.conf.entities.impl.ChainConfigImpl) JsonNode(com.fasterxml.jackson.databind.JsonNode) TextNode(com.fasterxml.jackson.databind.node.TextNode) WalkerConfigImpl(org.walkmod.conf.entities.impl.WalkerConfigImpl) LinkedList(java.util.LinkedList) ChainConfig(org.walkmod.conf.entities.ChainConfig) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 9 with ChainConfig

use of org.walkmod.conf.entities.ChainConfig 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);
}
Also used : ChainWriter(org.walkmod.ChainWriter) ResourceModifier(org.walkmod.ResourceModifier) WalkModException(org.walkmod.exceptions.WalkModException) Configuration(org.walkmod.conf.entities.Configuration) TransformationConfig(org.walkmod.conf.entities.TransformationConfig) ParserAware(org.walkmod.walkers.ParserAware) ChainConfig(org.walkmod.conf.entities.ChainConfig) Parser(org.walkmod.walkers.Parser)

Example 10 with ChainConfig

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

the class ConfigurationImpl method execute.

@Override
public void execute(String userDir, Options options, String... chains) {
    ChainAdapterFactory apf = new DefaultChainAdapterFactory();
    Summary.getInstance().clear();
    Collection<ChainConfig> chainCfgs = getChainConfigs();
    if (chainCfgs != null && !chainCfgs.isEmpty()) {
        if (chains == null || chains.length == 0) {
            executeAllChains(options, apf);
        } else {
            for (String chain : chains) {
                executeChain(userDir, options, apf, chain);
            }
        }
    }
}
Also used : DefaultChainAdapterFactory(org.walkmod.impl.DefaultChainAdapterFactory) DefaultChainAdapterFactory(org.walkmod.impl.DefaultChainAdapterFactory) ChainAdapterFactory(org.walkmod.ChainAdapterFactory) ChainConfig(org.walkmod.conf.entities.ChainConfig)

Aggregations

ChainConfig (org.walkmod.conf.entities.ChainConfig)22 Configuration (org.walkmod.conf.entities.Configuration)11 TransformationConfig (org.walkmod.conf.entities.TransformationConfig)11 LinkedList (java.util.LinkedList)8 ChainConfigImpl (org.walkmod.conf.entities.impl.ChainConfigImpl)8 ConfigurationImpl (org.walkmod.conf.entities.impl.ConfigurationImpl)8 WalkerConfig (org.walkmod.conf.entities.WalkerConfig)6 Test (org.junit.Test)5 Element (org.w3c.dom.Element)5 Node (org.w3c.dom.Node)4 NodeList (org.w3c.dom.NodeList)4 PluginConfig (org.walkmod.conf.entities.PluginConfig)4 Document (org.w3c.dom.Document)3 ChainAdapter (org.walkmod.ChainAdapter)3 ProviderConfig (org.walkmod.conf.entities.ProviderConfig)3 ReaderConfig (org.walkmod.conf.entities.ReaderConfig)3 WriterConfig (org.walkmod.conf.entities.WriterConfig)3 WalkModException (org.walkmod.exceptions.WalkModException)3 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 V2_AsciiTable (de.vandermeer.asciitable.v2.V2_AsciiTable)2