Search in sources :

Example 1 with ConfigurationImpl

use of org.walkmod.conf.entities.impl.ConfigurationImpl 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();
}
Also used : Configuration(org.walkmod.conf.entities.Configuration) Element(org.w3c.dom.Element) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) ChainConfigImpl(org.walkmod.conf.entities.impl.ChainConfigImpl) Document(org.w3c.dom.Document) ChainConfig(org.walkmod.conf.entities.ChainConfig) WriterConfig(org.walkmod.conf.entities.WriterConfig) ReaderConfig(org.walkmod.conf.entities.ReaderConfig) ConfigurationImpl(org.walkmod.conf.entities.impl.ConfigurationImpl)

Example 2 with ConfigurationImpl

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

the class XMLConfigurationProviderTest method testRemoveTransformationsRecursively.

@Test
public void testRemoveTransformationsRecursively() throws Exception {
    List<String> list = new LinkedList<String>();
    list.add("imports-cleaner");
    File aux = new File("src/test/resources/xmlmultimodule");
    aux.mkdirs();
    File module0 = new File(aux, "module0");
    File module1 = new File(aux, "module1");
    File cfg0 = new File(module0, "walkmod.xml");
    File cfg1 = new File(module1, "walkmod.xml");
    module0.mkdir();
    module1.mkdir();
    File xml = new File(aux, "walkmod.xml");
    XMLConfigurationProvider prov = new XMLConfigurationProvider(xml.getPath(), false);
    try {
        Configuration conf = new ConfigurationImpl();
        prov.init(conf);
        prov.createConfig();
        prov.addModules(Arrays.asList("module0", "module1"));
        AddTransformationCommand command0 = new AddTransformationCommand("license-applier", "mychain", false, null, null, null, null, true);
        prov.addTransformationConfig("mychain", null, command0.buildTransformationCfg(), true, null, null);
        String output = FileUtils.readFileToString(cfg0);
        System.out.println(output);
        AddTransformationCommand command = new AddTransformationCommand("imports-cleaner", "mychain", false, null, null, null, null, true);
        prov.addTransformationConfig("mychain", null, command.buildTransformationCfg(), true, null, null);
        output = FileUtils.readFileToString(cfg0);
        System.out.println(output);
        prov.removeTransformations("mychain", list, true);
        output = FileUtils.readFileToString(cfg0);
        Assert.assertTrue(!output.contains("imports-cleaner"));
        Assert.assertTrue(output.contains("license-applier"));
        output = FileUtils.readFileToString(cfg1);
        Assert.assertTrue(!output.contains("imports-cleaner"));
        Assert.assertTrue(output.contains("license-applier"));
    } finally {
        if (aux.exists()) {
            FileUtils.deleteDirectory(aux);
        }
    }
}
Also used : Configuration(org.walkmod.conf.entities.Configuration) AddTransformationCommand(org.walkmod.commands.AddTransformationCommand) File(java.io.File) ConfigurationImpl(org.walkmod.conf.entities.impl.ConfigurationImpl) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Example 3 with ConfigurationImpl

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

the class XMLConfigurationProviderTest method testRemoveTranformation.

@Test
public void testRemoveTranformation() throws Exception {
    List<String> list = new LinkedList<String>();
    list.add("imports-cleaner");
    File aux = new File("src/test/resources/xml");
    aux.mkdirs();
    File xml = new File(aux, "walkmod.xml");
    XMLConfigurationProvider prov = new XMLConfigurationProvider(xml.getPath(), false);
    try {
        Configuration conf = new ConfigurationImpl();
        prov.init(conf);
        prov.createConfig();
        AddTransformationCommand command = new AddTransformationCommand("imports-cleaner", null, false, null, null, null, null, false);
        prov.addTransformationConfig(null, null, command.buildTransformationCfg(), false, null, null);
        prov.removeTransformations(null, list, false);
        String output = FileUtils.readFileToString(xml);
        Assert.assertTrue(!output.contains("imports-cleaner"));
    } finally {
        if (xml.exists()) {
            xml.delete();
        }
    }
}
Also used : Configuration(org.walkmod.conf.entities.Configuration) AddTransformationCommand(org.walkmod.commands.AddTransformationCommand) File(java.io.File) ConfigurationImpl(org.walkmod.conf.entities.impl.ConfigurationImpl) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Example 4 with ConfigurationImpl

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

the class XMLConfigurationProviderTest method testRemoveModule.

@Test
public void testRemoveModule() throws Exception {
    File aux = new File("src/test/resources/xml");
    aux.mkdirs();
    File xml = new File(aux, "walkmod.xml");
    XMLConfigurationProvider prov = new XMLConfigurationProvider(xml.getPath(), false);
    try {
        Configuration conf = new ConfigurationImpl();
        prov.init(conf);
        prov.createConfig();
        List<String> modules = new LinkedList<String>();
        modules.add("module1");
        prov.addModules(modules);
        String output = FileUtils.readFileToString(xml);
        System.out.println(output);
        Assert.assertTrue(output.contains("module1"));
        prov.removeModules(modules);
        output = FileUtils.readFileToString(xml);
        System.out.println(output);
        Assert.assertTrue(!output.contains("module1"));
    } finally {
        if (xml.exists()) {
            xml.delete();
        }
    }
}
Also used : Configuration(org.walkmod.conf.entities.Configuration) File(java.io.File) ConfigurationImpl(org.walkmod.conf.entities.impl.ConfigurationImpl) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Example 5 with ConfigurationImpl

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

the class XMLConfigurationProviderTest method testVersion1_1.

@Test
public void testVersion1_1() throws Exception {
    XMLConfigurationProvider prov = new XMLConfigurationProvider("src/test/resources/multimodule/walkmod.xml", false);
    Configuration conf = new ConfigurationImpl();
    prov.init(conf);
    prov.load();
    Assert.assertEquals(0, conf.getChainConfigs().size());
    Assert.assertEquals(2, conf.getModules().size());
}
Also used : Configuration(org.walkmod.conf.entities.Configuration) ConfigurationImpl(org.walkmod.conf.entities.impl.ConfigurationImpl) Test(org.junit.Test)

Aggregations

ConfigurationImpl (org.walkmod.conf.entities.impl.ConfigurationImpl)50 Configuration (org.walkmod.conf.entities.Configuration)48 Test (org.junit.Test)43 File (java.io.File)36 AddTransformationCommand (org.walkmod.commands.AddTransformationCommand)21 LinkedList (java.util.LinkedList)20 TransformationConfig (org.walkmod.conf.entities.TransformationConfig)20 PluginConfig (org.walkmod.conf.entities.PluginConfig)9 ChainConfig (org.walkmod.conf.entities.ChainConfig)8 HashMap (java.util.HashMap)6 Document (org.w3c.dom.Document)4 Element (org.w3c.dom.Element)4 Node (org.w3c.dom.Node)4 NodeList (org.w3c.dom.NodeList)4 ProviderConfig (org.walkmod.conf.entities.ProviderConfig)4 ChainConfigImpl (org.walkmod.conf.entities.impl.ChainConfigImpl)4 PluginConfigImpl (org.walkmod.conf.entities.impl.PluginConfigImpl)4 AddCfgProviderCommand (org.walkmod.commands.AddCfgProviderCommand)3 TransformerException (javax.xml.transform.TransformerException)2 AddPluginCommand (org.walkmod.commands.AddPluginCommand)2