Search in sources :

Example 36 with Configuration

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

the class AddConfigurationParameterXMLAction method doAction.

@Override
public void doAction() throws Exception {
    List<Element> elementsToModify = new LinkedList<Element>();
    Document document = provider.getDocument();
    Element rootElement = document.getDocumentElement();
    NodeList children = rootElement.getChildNodes();
    int childSize = children.getLength();
    boolean isTransformationList = false;
    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();
            Element chainToAnalyze = null;
            if ("chain".equals(nodeName)) {
                String chainName = child.getAttribute("name");
                if (chain != null && !chain.equals("") && chain.equals(chainName)) {
                    chainToAnalyze = child;
                } else if (chain == null) {
                    chainToAnalyze = child;
                }
                if (chainToAnalyze != null) {
                    NodeList chainChildren = chainToAnalyze.getChildNodes();
                    int limit = chainChildren.getLength();
                    for (int j = 0; j < limit; j++) {
                        Node chainChild = chainChildren.item(j);
                        if (chainChild instanceof Element) {
                            Element chainElement = (Element) chainChild;
                            final String chainElementName = chainElement.getNodeName();
                            if (category != null && category.equals(chainElementName) || category == null) {
                                analizeBean(chainElement, type, name, elementsToModify);
                                if (chainElementName.equals("walker") && (category == null || category.equals("transformation"))) {
                                    NodeList walkerChildren = chainChild.getChildNodes();
                                    int limit2 = walkerChildren.getLength();
                                    for (int k = 0; k < limit2; k++) {
                                        Node walkerChild = walkerChildren.item(k);
                                        if (walkerChild instanceof Element) {
                                            if (walkerChild.getNodeName().equals("transformations")) {
                                                NodeList transform = walkerChild.getChildNodes();
                                                int limit3 = transform.getLength();
                                                for (int h = 0; h < limit3; h++) {
                                                    Node transformationItem = transform.item(h);
                                                    if (transformationItem instanceof Element) {
                                                        analizeBean((Element) transformationItem, type, name, elementsToModify);
                                                    }
                                                }
                                            } else {
                                                analizeBean((Element) walkerChild, type, name, elementsToModify);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            } else if ("transformation".equals(nodeName)) {
                isTransformationList = true;
                analizeBean(child, type, name, elementsToModify);
            } else if ("conf-providers".equals(nodeName)) {
                NodeList provChildren = child.getChildNodes();
                int limit = provChildren.getLength();
                for (int k = 0; k < limit; k++) {
                    analizeBean((Element) provChildren.item(k), type, name, elementsToModify);
                }
            }
        }
    }
    if (isTransformationList) {
        if (category == null || Arrays.asList("writer", "reader", "walker").contains(category)) {
            Configuration configuration = new ConfigurationImpl();
            provider.setConfiguration(configuration);
            provider.loadChains();
            Map<String, Object> params = new HashMap<String, Object>();
            params.put(param, value);
            Collection<ChainConfig> chainCfgs = configuration.getChainConfigs();
            ChainConfig 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);
                        i--;
                    }
                }
            }
            if (chain == null || chain.equals("default")) {
                chainCfg.getWriterConfig().setParams(params);
            } else {
                throw new TransformerException("The parameter needs to be set to an element of an existing chain and : [" + chain + " does not exist]");
            }
            rootElement.appendChild(createChainElement(chainCfg));
            provider.persist();
        }
        return;
    }
    Iterator<Element> it = elementsToModify.iterator();
    while (it.hasNext()) {
        Element current = it.next();
        NodeList childrenElement = current.getChildNodes();
        int limit = childrenElement.getLength();
        boolean found = false;
        for (int i = 0; i < limit && !found; i++) {
            Node item = childrenElement.item(i);
            if (item.getNodeName().equals("param")) {
                Element aux = (Element) item;
                if (aux.getAttribute("name").equals(param)) {
                    aux.setTextContent(value);
                    found = true;
                }
            }
        }
        if (!found) {
            Element paramElement = document.createElement("param");
            paramElement.setAttribute("name", param);
            paramElement.setTextContent(value);
            current.appendChild(paramElement);
        }
    }
    if (!elementsToModify.isEmpty()) {
        provider.persist();
    }
}
Also used : Configuration(org.walkmod.conf.entities.Configuration) HashMap(java.util.HashMap) Element(org.w3c.dom.Element) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Document(org.w3c.dom.Document) LinkedList(java.util.LinkedList) ChainConfig(org.walkmod.conf.entities.ChainConfig) ConfigurationImpl(org.walkmod.conf.entities.impl.ConfigurationImpl) TransformerException(javax.xml.transform.TransformerException)

Example 37 with Configuration

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

the class AddTransformationXMLAction method doAction.

public void doAction() throws Exception {
    Document document = provider.getDocument();
    Element rootElement = document.getDocumentElement();
    NodeList children = rootElement.getChildNodes();
    int childSize = children.getLength();
    if (chain == null || "".equals(chain)) {
        chain = "default";
    }
    Element beforeChain = null;
    if (!"default".equals(chain)) {
        boolean appended = false;
        boolean isTransformationList = false;
        for (int i = 0; i < childSize && !isTransformationList && !appended; i++) {
            Node childNode = children.item(i);
            if (childNode instanceof Element) {
                Element child = (Element) childNode;
                final String nodeName = child.getNodeName();
                if ("chain".equals(nodeName)) {
                    String name = child.getAttribute("name");
                    if (before != null && name.equals(before)) {
                        beforeChain = child;
                    }
                    if (name.equals(chain)) {
                        Element transfElement = createTransformationElement(transformationCfg);
                        NodeList innerChainNodes = child.getChildNodes();
                        int maxK = innerChainNodes.getLength();
                        boolean added = false;
                        boolean hasWalker = false;
                        for (int k = 0; k < maxK && !added; k++) {
                            Element chainInnerElem = (Element) innerChainNodes.item(k);
                            hasWalker = hasWalker || chainInnerElem.getNodeName().equals("walker");
                            if (hasWalker) {
                                NodeList transfList = chainInnerElem.getChildNodes();
                                int maxj = transfList.getLength();
                                for (int j = maxj; j >= 0 && !added; j--) {
                                    if (transfList.item(j).getNodeName().equals("transformations")) {
                                        if (order == null || order == j) {
                                            transfList.item(j).appendChild(transfElement);
                                            added = true;
                                        }
                                    }
                                }
                            } else if (chainInnerElem.getNodeName().equals("writer")) {
                                child.insertBefore(transfElement, chainInnerElem);
                                added = true;
                            }
                        }
                        if (!added) {
                            child.appendChild(transfElement);
                        }
                        appended = true;
                    }
                } else if ("transformation".equals(nodeName)) {
                    isTransformationList = true;
                }
            }
        }
        Element defaultChainElement = null;
        if (isTransformationList) {
            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();
            ChainConfig 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);
                        i--;
                    }
                }
            }
            defaultChainElement = createChainElement(chainCfg);
        }
        if (!appended) {
            ChainConfig chainCfg = new ChainConfigImpl();
            chainCfg.setName(chain);
            provider.addDefaultReaderConfig(chainCfg);
            provider.addDefaultWriterConfig(chainCfg);
            if (path != null && !"".equals(path.trim())) {
                chainCfg.getReaderConfig().setPath(path);
                chainCfg.getWriterConfig().setPath(path);
            }
            provider.addDefaultWalker(chainCfg);
            WalkerConfig walkerCfg = chainCfg.getWalkerConfig();
            List<TransformationConfig> transfs = new LinkedList<TransformationConfig>();
            transfs.add(transformationCfg);
            walkerCfg.setTransformations(transfs);
            chainCfg.setWalkerConfig(walkerCfg);
            if (beforeChain != null) {
                rootElement.insertBefore(createChainElement(chainCfg), beforeChain);
            } else {
                if (before == null && defaultChainElement != null) {
                    rootElement.appendChild(defaultChainElement);
                }
                rootElement.appendChild(createChainElement(chainCfg));
                if ("default".equals(before) && defaultChainElement != null) {
                    rootElement.appendChild(defaultChainElement);
                }
            }
        }
        provider.persist();
    } else {
        Element chainNode = null;
        boolean containsChains = false;
        for (int i = 0; i < childSize && chainNode == null; i++) {
            Node childNode = children.item(i);
            if (childNode instanceof Element) {
                Element auxNode = (Element) childNode;
                final String nodeName = auxNode.getNodeName();
                containsChains = "chain".equals(nodeName);
                if (auxNode.getAttribute("name").equals(chain)) {
                    chainNode = auxNode;
                }
            }
        }
        if (containsChains) {
            if (chainNode != null) {
                String attrName = chainNode.getAttribute("name");
                if (attrName == null || attrName.equals("") || attrName.equals("default")) {
                    NodeList chainChildren = chainNode.getChildNodes();
                    if (path != null && !"".equals(path.trim())) {
                        for (int i = 0; i < chainChildren.getLength(); i++) {
                            Node childNode = chainChildren.item(i);
                            String nodeType = childNode.getNodeName();
                            if (nodeType.equals("reader")) {
                                Element aux = (Element) childNode;
                                if (!aux.getAttribute("path").equals(path.trim())) {
                                    throw new TransformerException("The user must specify a chain name (new or existing) where to add the transformation: [" + transformationCfg.getType() + "]");
                                }
                            }
                        }
                    }
                    if (chainChildren.getLength() > 0) {
                        Node lastElem = chainChildren.item(chainChildren.getLength() - 1);
                        if (lastElem.getNodeName().equals("writer")) {
                            chainNode.insertBefore(createTransformationElement(transformationCfg), lastElem);
                        }
                        if (lastElem.getNodeName().equals("transformation")) {
                            chainNode.appendChild(createTransformationElement(transformationCfg));
                        }
                        if (lastElem.getNodeName().equals("walker")) {
                            lastElem.appendChild(createTransformationElement(transformationCfg));
                        }
                        provider.persist();
                        return;
                    }
                }
            } else {
                ChainConfig chainCfg = new ChainConfigImpl();
                chainCfg.setName("default");
                provider.addDefaultReaderConfig(chainCfg);
                provider.addDefaultWriterConfig(chainCfg);
                provider.addDefaultWalker(chainCfg);
                WalkerConfig walkerCfg = chainCfg.getWalkerConfig();
                List<TransformationConfig> transfs = new LinkedList<TransformationConfig>();
                transfs.add(transformationCfg);
                walkerCfg.setTransformations(transfs);
                chainCfg.setWalkerConfig(walkerCfg);
                rootElement.appendChild(createChainElement(chainCfg));
                provider.persist();
                return;
            }
        }
        if (path != null && !"".equals(path.trim())) {
            Configuration configuration = new ConfigurationImpl();
            provider.setConfiguration(configuration);
            provider.loadChains();
            Collection<ChainConfig> chainCfgs = configuration.getChainConfigs();
            if (chainCfgs.isEmpty()) {
                ChainConfig chainCfg = new ChainConfigImpl();
                chainCfg.setName("default");
                provider.addDefaultReaderConfig(chainCfg);
                provider.addDefaultWriterConfig(chainCfg);
                if (path != null && !"".equals(path.trim())) {
                    chainCfg.getReaderConfig().setPath(path);
                    chainCfg.getWriterConfig().setPath(path);
                }
                provider.addDefaultWalker(chainCfg);
                WalkerConfig walkerCfg = chainCfg.getWalkerConfig();
                List<TransformationConfig> transfs = new LinkedList<TransformationConfig>();
                transfs.add(transformationCfg);
                walkerCfg.setTransformations(transfs);
                chainCfg.setWalkerConfig(walkerCfg);
                NodeList childrenNodes = rootElement.getChildNodes();
                int limitChildren = childrenNodes.getLength();
                for (int i = 0; i < limitChildren; i++) {
                    rootElement.removeChild(childrenNodes.item(i));
                    i--;
                }
                rootElement.appendChild(createChainElement(chainCfg));
                provider.persist();
                return;
            } else {
                ChainConfig chainCfg = chainCfgs.iterator().next();
                chainCfg.getReaderConfig().setPath(path);
                chainCfg.getWriterConfig().setPath(path);
                List<TransformationConfig> transfs = chainCfg.getWalkerConfig().getTransformations();
                if (order != null && order < transfs.size()) {
                    transfs.add(order, transformationCfg);
                } else {
                    transfs.add(transformationCfg);
                }
                document.removeChild(rootElement);
                document.appendChild(createChainElement(chainCfg));
            }
            provider.persist();
            return;
        }
        rootElement.appendChild(createTransformationElement(transformationCfg));
        provider.persist();
    }
}
Also used : Configuration(org.walkmod.conf.entities.Configuration) WalkerConfig(org.walkmod.conf.entities.WalkerConfig) Element(org.w3c.dom.Element) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) TransformationConfig(org.walkmod.conf.entities.TransformationConfig) ChainConfigImpl(org.walkmod.conf.entities.impl.ChainConfigImpl) Document(org.w3c.dom.Document) LinkedList(java.util.LinkedList) ChainConfig(org.walkmod.conf.entities.ChainConfig) ConfigurationImpl(org.walkmod.conf.entities.impl.ConfigurationImpl) TransformerException(javax.xml.transform.TransformerException)

Example 38 with Configuration

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

the class LanguageConfigurationProviderTest method testNullOverwriting.

@Test
public void testNullOverwriting() {
    LanguageConfigurationProvider provider = new LanguageConfigurationProvider();
    Configuration conf = new ConfigurationImpl();
    ChainConfig cc = new ChainConfigImpl();
    cc.setName("test-chain");
    ReaderConfig reader = new ReaderConfig();
    WalkerConfig walker = new WalkerConfigImpl();
    TransformationConfig transformation = new TransformationConfigImpl();
    transformation.isMergeable(true);
    List<TransformationConfig> transf = new LinkedList<TransformationConfig>();
    transf.add(transformation);
    walker.setParserConfig(new ParserConfigImpl());
    walker.setTransformations(transf);
    WriterConfig writer = new WriterConfigImpl();
    cc.setReaderConfig(reader);
    cc.setWalkerConfig(walker);
    cc.setWriterConfig(writer);
    conf.addChainConfig(cc);
    provider.init(conf);
    provider.load();
    Assert.assertNotNull(reader.getPath());
    Assert.assertNotNull(reader.getType());
    Assert.assertNotNull(walker.getType());
    Assert.assertNotNull(walker.getParserConfig().getType());
    Assert.assertNotNull(writer.getPath());
    Assert.assertNotNull(writer.getType());
    Assert.assertNotNull(transformation.getMergePolicy());
    Assert.assertNotNull(conf.getMergePolicies());
    Collection<MergePolicyConfig> mergec = conf.getMergePolicies();
    Assert.assertEquals(1, mergec.size());
    MergePolicyConfig mpc = mergec.iterator().next();
    Assert.assertNotNull(mpc.getDefaultObjectPolicy());
    Assert.assertNotNull(mpc.getDefaultTypePolicy());
    Map<String, String> entries = mpc.getPolicyEntries();
    Assert.assertEquals(2, entries.size());
}
Also used : MergePolicyConfig(org.walkmod.conf.entities.MergePolicyConfig) ParserConfigImpl(org.walkmod.conf.entities.impl.ParserConfigImpl) Configuration(org.walkmod.conf.entities.Configuration) WalkerConfig(org.walkmod.conf.entities.WalkerConfig) TransformationConfig(org.walkmod.conf.entities.TransformationConfig) ChainConfigImpl(org.walkmod.conf.entities.impl.ChainConfigImpl) WalkerConfigImpl(org.walkmod.conf.entities.impl.WalkerConfigImpl) LinkedList(java.util.LinkedList) ChainConfig(org.walkmod.conf.entities.ChainConfig) WriterConfig(org.walkmod.conf.entities.WriterConfig) TransformationConfigImpl(org.walkmod.conf.entities.impl.TransformationConfigImpl) ReaderConfig(org.walkmod.conf.entities.ReaderConfig) ConfigurationImpl(org.walkmod.conf.entities.impl.ConfigurationImpl) WriterConfigImpl(org.walkmod.conf.entities.impl.WriterConfigImpl) Test(org.junit.Test)

Example 39 with Configuration

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

the class PluginsConfigurationProviderTest method testNullOverwriting.

@Test
public void testNullOverwriting() {
    PluginsConfigurationProvider provider = new PluginsConfigurationProvider();
    Configuration conf = new ConfigurationImpl();
    provider.init(conf);
    provider.load();
    Collection<PluginConfig> plugins = conf.getPlugins();
    Assert.assertNotNull(plugins);
    Assert.assertEquals(1, plugins.size());
}
Also used : PluginConfig(org.walkmod.conf.entities.PluginConfig) Configuration(org.walkmod.conf.entities.Configuration) ConfigurationImpl(org.walkmod.conf.entities.impl.ConfigurationImpl) Test(org.junit.Test)

Example 40 with Configuration

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

the class PluginsConfigurationProviderTest method testInvalidPlugins.

@Test
public void testInvalidPlugins() {
    IvyConfigurationProvider provider = new IvyConfigurationProvider();
    Configuration conf = new ConfigurationImpl();
    provider.init(conf);
    PluginConfig pc = new PluginConfigImpl();
    pc.setGroupId("foo");
    pc.setArtifactId("bar");
    pc.setVersion("10");
    conf.setPlugins(new LinkedList());
    conf.getPlugins().add(pc);
    Exception exception = null;
    try {
        provider.load();
    } catch (ConfigurationException e) {
        exception = e;
    }
    Assert.assertNotNull(exception);
}
Also used : PluginConfig(org.walkmod.conf.entities.PluginConfig) Configuration(org.walkmod.conf.entities.Configuration) ConfigurationException(org.walkmod.conf.ConfigurationException) PluginConfigImpl(org.walkmod.conf.entities.impl.PluginConfigImpl) ConfigurationImpl(org.walkmod.conf.entities.impl.ConfigurationImpl) LinkedList(java.util.LinkedList) ConfigurationException(org.walkmod.conf.ConfigurationException) Test(org.junit.Test)

Aggregations

Configuration (org.walkmod.conf.entities.Configuration)58 ConfigurationImpl (org.walkmod.conf.entities.impl.ConfigurationImpl)48 Test (org.junit.Test)43 File (java.io.File)38 TransformationConfig (org.walkmod.conf.entities.TransformationConfig)23 LinkedList (java.util.LinkedList)21 AddTransformationCommand (org.walkmod.commands.AddTransformationCommand)21 ChainConfig (org.walkmod.conf.entities.ChainConfig)11 PluginConfig (org.walkmod.conf.entities.PluginConfig)10 HashMap (java.util.HashMap)6 WalkModException (org.walkmod.exceptions.WalkModException)6 V2_AsciiTable (de.vandermeer.asciitable.v2.V2_AsciiTable)5 IOException (java.io.IOException)5 WalkModFacade (org.walkmod.WalkModFacade)5 ConfigurationManager (org.walkmod.conf.ConfigurationManager)5 ProviderConfig (org.walkmod.conf.entities.ProviderConfig)5 PluginConfigImpl (org.walkmod.conf.entities.impl.PluginConfigImpl)5 Document (org.w3c.dom.Document)4 NodeList (org.w3c.dom.NodeList)4 InvalidConfigurationException (org.walkmod.exceptions.InvalidConfigurationException)4