Search in sources :

Example 21 with TransformationConfig

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

the class YAMLConfigurationProviderTest method testAddChainTransformation.

@Test
public void testAddChainTransformation() throws Exception {
    Map<String, String> params = new HashMap<String, String>();
    params.put("refactoringConfigFile", "src/conf/refactoring-methods.json");
    AddTransformationCommand command = new AddTransformationCommand("walkmod:commons:method-refactor", null, false, null, null, null, params, false);
    File file = new File("src/test/resources/yaml/addchain.yml");
    if (file.exists()) {
        file.delete();
    }
    file.createNewFile();
    FileUtils.write(file, "");
    try {
        YAMLConfigurationProvider provider = new YAMLConfigurationProvider(file.getPath());
        Configuration conf = new ConfigurationImpl();
        provider.init(conf);
        TransformationConfig transformationCfg = command.buildTransformationCfg();
        provider.addTransformationConfig(null, null, transformationCfg, false, null, null);
        String output = FileUtils.readFileToString(file);
        String desiredOutput = "transformations:\n";
        desiredOutput += "- type: \"walkmod:commons:method-refactor\"\n";
        desiredOutput += "  params:\n";
        desiredOutput += "    refactoringConfigFile: \"src/conf/refactoring-methods.json\"";
        Assert.assertEquals(desiredOutput, output);
    } finally {
        if (file.exists()) {
            file.delete();
        }
    }
}
Also used : Configuration(org.walkmod.conf.entities.Configuration) HashMap(java.util.HashMap) TransformationConfig(org.walkmod.conf.entities.TransformationConfig) AddTransformationCommand(org.walkmod.commands.AddTransformationCommand) File(java.io.File) ConfigurationImpl(org.walkmod.conf.entities.impl.ConfigurationImpl) Test(org.junit.Test)

Example 22 with TransformationConfig

use of org.walkmod.conf.entities.TransformationConfig 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 23 with TransformationConfig

use of org.walkmod.conf.entities.TransformationConfig 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 24 with TransformationConfig

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

the class AddTransformationCommand method buildTransformationCfg.

public TransformationConfig buildTransformationCfg() {
    TransformationConfig tconfig = new TransformationConfigImpl();
    tconfig.setType(type.get(0));
    tconfig.isMergeable(isMergeable);
    tconfig.setMergePolicy(mergePolicy);
    tconfig.setName(name);
    if (params != null) {
        tconfig.setParameters(new HashMap<String, Object>(params));
    }
    return tconfig;
}
Also used : TransformationConfigImpl(org.walkmod.conf.entities.impl.TransformationConfigImpl) TransformationConfig(org.walkmod.conf.entities.TransformationConfig)

Example 25 with TransformationConfig

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

the class ConfigurationImpl method preparePlugins.

@Override
public void preparePlugins() {
    Collection<PluginConfig> pluginCfg = getPlugins();
    HashSet<String> plugins = new HashSet<String>();
    HashSet<String> previousPlugins = new HashSet<String>();
    if (pluginCfg == null || pluginCfg.isEmpty()) {
        pluginCfg = new LinkedList<PluginConfig>();
    } else {
        for (PluginConfig pc : pluginCfg) {
            previousPlugins.add(pc.getGroupId() + ":" + pc.getArtifactId());
        }
    }
    Collection<ChainConfig> chains = getChainConfigs();
    if (chains != null) {
        Iterator<ChainConfig> it = chains.iterator();
        while (it.hasNext()) {
            ChainConfig cc = it.next();
            composeName(cc.getReaderConfig().getType(), plugins);
            composeName(cc.getWalkerConfig().getParserConfig().getType(), plugins);
            List<TransformationConfig> trans = cc.getWalkerConfig().getTransformations();
            if (trans != null) {
                for (TransformationConfig transformation : trans) {
                    String type = transformation.getType();
                    if (!type.equals("script") && !type.equals("template")) {
                        composeName(type, plugins);
                    }
                }
            }
            composeName(cc.getWriterConfig().getType(), plugins);
        }
    }
    Collection<ProviderConfig> providers = getProviderConfigurations();
    if (providers != null) {
        for (ProviderConfig provider : providers) {
            String type = provider.getType();
            composeName(type, plugins);
        }
    }
    Collection<InitializerConfig> initializers = getInitializers();
    if (initializers != null) {
        for (InitializerConfig initializer : initializers) {
            plugins.add(initializer.getPluginGroupId() + ":walkmod-" + initializer.getPluginArtifactId() + "-plugin");
        }
    }
    for (String id : plugins) {
        if (!previousPlugins.contains(id)) {
            String[] parts = id.split(":");
            PluginConfig cfg = new PluginConfigImpl();
            cfg.setGroupId(parts[0].trim());
            cfg.setArtifactId(parts[1].trim());
            cfg.setVersion("latest.integration");
            pluginCfg.add(cfg);
        }
    }
    setPlugins(pluginCfg);
}
Also used : ProviderConfig(org.walkmod.conf.entities.ProviderConfig) InitializerConfig(org.walkmod.conf.entities.InitializerConfig) TransformationConfig(org.walkmod.conf.entities.TransformationConfig) ChainConfig(org.walkmod.conf.entities.ChainConfig) PluginConfig(org.walkmod.conf.entities.PluginConfig) HashSet(java.util.HashSet)

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