Search in sources :

Example 26 with TransformationConfig

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

the class PrintTransformationsCommand method execute.

@Override
public void execute() throws Exception {
    if (help) {
        command.usage("transformations");
    } else {
        if (chain == null) {
            chain = new LinkedList<String>();
        }
        if (chain.isEmpty()) {
            chain.add("default");
        }
        WalkModFacade facade = new WalkModFacade(OptionsBuilder.options());
        Configuration cfg = facade.getConfiguration();
        if (cfg != null) {
            Collection<ChainConfig> chains = cfg.getChainConfigs();
            if (chains != null) {
                Iterator<ChainConfig> it = chains.iterator();
                ChainConfig selected = null;
                while (it.hasNext() && selected == null) {
                    ChainConfig current = it.next();
                    if (current.getName().equals(chain.get(0))) {
                        selected = current;
                    }
                }
                if (selected != null) {
                    at = new V2_AsciiTable();
                    at.addRule();
                    at.addRow("TRANSFORMATION TYPE", "PARAMETERS", "NAME/ALIAS");
                    at.addStrongRule();
                    List<TransformationConfig> transformations = selected.getWalkerConfig().getTransformations();
                    if (transformations != null) {
                        for (TransformationConfig transf : transformations) {
                            Map<String, Object> parameters = transf.getParameters();
                            if (parameters == null || parameters.isEmpty()) {
                                at.addRow(transf.getType(), "", transf.getName());
                            } else {
                                Set<String> keys = parameters.keySet();
                                int i = 0;
                                for (String key : keys) {
                                    if (i == 0) {
                                        String name = transf.getName();
                                        if (name == null) {
                                            name = "";
                                        }
                                        at.addRow(transf.getType(), "-" + key + ":" + parameters.get(key), "");
                                    } else {
                                        at.addRow("", "-" + key + ":" + parameters.get(key), "");
                                    }
                                    i++;
                                }
                            }
                            at.addRule();
                        }
                    }
                }
            }
        } else {
            log.error("Sorry, the current directory does not contain a walkmod configuration file or it is invalid.");
            at = new V2_AsciiTable();
            at.addRule();
            at.addRow("TRANSFORMATION TYPE", "PARAMETERS", "NAME/ALIAS");
            at.addStrongRule();
            at.addRule();
        }
    }
}
Also used : Configuration(org.walkmod.conf.entities.Configuration) TransformationConfig(org.walkmod.conf.entities.TransformationConfig) ChainConfig(org.walkmod.conf.entities.ChainConfig) V2_AsciiTable(de.vandermeer.asciitable.v2.V2_AsciiTable) WalkModFacade(org.walkmod.WalkModFacade)

Example 27 with TransformationConfig

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

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

the class AbstractWalker method visit.

protected void visit(Object element, List<Object> visitors, List<TransformationConfig> transformations, VisitorContext context) throws Exception {
    if (rootNamespace != null && !"".equals(rootNamespace)) {
        String qualifiedName = getResource().getNearestNamespace(element, NAMESPACE_SEPARATOR);
        if (rootNamespace.startsWith(qualifiedName)) {
            return;
        }
    }
    if (visitors.isEmpty()) {
        context.addResultNode(element);
    } else {
        int index = 0;
        for (Object visitor : visitors) {
            boolean isMergeable = transformations.get(index).isMergeable();
            String mergePolicy = transformations.get(index).getMergePolicy();
            if (mergePolicy != null) {
                isMergeable = true;
            }
            if (isMergeable && mergePolicy == null) {
                mergePolicy = "default";
            }
            Method[] methods = visitor.getClass().getMethods();
            List<Object> restVisitors = visitors.subList(index + 1, visitors.size());
            List<TransformationConfig> restTransformations = transformations.subList(index + 1, transformations.size());
            Set<String> visitedTypes = new HashSet<String>();
            for (int j = 0; j < methods.length; j++) {
                if (methods[j].getName().equals("visit")) {
                    Class<?> type = methods[j].getParameterTypes()[0];
                    if ((!visitedTypes.contains(element.getClass().getName())) && type.isInstance(element)) {
                        visitedTypes.add(type.getName());
                        int paramsLength = methods[j].getParameterTypes().length;
                        Object[] params = new Object[paramsLength];
                        params[0] = element;
                        VisitorContext args = new VisitorContext(getChainConfig());
                        args.putAll(context);
                        if (paramsLength == 2) {
                            params[1] = args;
                        }
                        methods[j].invoke(visitor, params);
                        context.getVisitorMessages().addAll(args.getVisitorMessages());
                        MergeEngine me = null;
                        if (isMergeable) {
                            me = chainConfig.getConfiguration().getMergeEngine(mergePolicy);
                        }
                        if (args.hasResultNodes()) {
                            Iterator<Object> it = args.getResultNodes().iterator();
                            while (it.hasNext()) {
                                Object currentArg = it.next();
                                if (isMergeable) {
                                    currentArg = merge(currentArg, me, context);
                                }
                                context.addResultNode(currentArg);
                                visit(currentArg, restVisitors, restTransformations, context);
                            }
                            return;
                        } else {
                            context.addResultNode(element);
                        }
                    }
                }
            }
            index++;
        }
    }
}
Also used : TransformationConfig(org.walkmod.conf.entities.TransformationConfig) Method(java.lang.reflect.Method) MergeEngine(org.walkmod.merger.MergeEngine) HashSet(java.util.HashSet)

Example 29 with TransformationConfig

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

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

the class XMLConfigurationProviderTest method testAddConfigurationParameter.

@Test
public void testAddConfigurationParameter() throws Exception {
    AddTransformationCommand command = new AddTransformationCommand("imports-cleaner", "mychain", false, null, null, null, null, false);
    File aux = new File("src/test/resources/xmlparams");
    aux.mkdirs();
    File xml = new File(aux, "walkmod.xml");
    XMLConfigurationProvider prov = new XMLConfigurationProvider(xml.getPath(), false);
    try {
        prov.createConfig();
        TransformationConfig transfCfg = command.buildTransformationCfg();
        prov.addTransformationConfig("mychain", null, transfCfg, false, null, null);
        prov.addConfigurationParameter("testParam", "hello", "imports-cleaner", null, null, null, false);
        String output = FileUtils.readFileToString(xml);
        System.out.println(output);
        Assert.assertTrue(output.contains("testParam") && output.contains("hello"));
    } finally {
        FileUtils.deleteDirectory(aux);
    }
}
Also used : TransformationConfig(org.walkmod.conf.entities.TransformationConfig) AddTransformationCommand(org.walkmod.commands.AddTransformationCommand) File(java.io.File) Test(org.junit.Test)

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