Search in sources :

Example 1 with WalkerConfigImpl

use of org.walkmod.conf.entities.impl.WalkerConfigImpl 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 2 with WalkerConfigImpl

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

the class AbstractChainConfigurationProvider method addDefaultWalker.

public void addDefaultWalker(ChainConfig ac) {
    WalkerConfig wc = new WalkerConfigImpl();
    wc.setType(null);
    wc.setParserConfig(new ParserConfigImpl());
    ac.setWalkerConfig(wc);
}
Also used : ParserConfigImpl(org.walkmod.conf.entities.impl.ParserConfigImpl) WalkerConfig(org.walkmod.conf.entities.WalkerConfig) WalkerConfigImpl(org.walkmod.conf.entities.impl.WalkerConfigImpl)

Example 3 with WalkerConfigImpl

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

the class JSONConfigParser method getWalker.

public WalkerConfig getWalker(JsonNode current) {
    WalkerConfig walkerCfg = new WalkerConfigImpl();
    if (current.has("type")) {
        walkerCfg.setType(current.get("type").asText());
    }
    if (current.has("parser")) {
        ParserConfig parserCfg = new ParserConfigImpl();
        JsonNode parserNode = current.get("parser");
        parserCfg.setType(parserNode.get("type").asText());
        parserCfg.setParameters(getParams(parserNode));
        walkerCfg.setParserConfig(parserCfg);
    }
    walkerCfg.setTransformations(getTransformationCfgs(current));
    if (current.has("root-namespace")) {
        walkerCfg.setRootNamespace(current.get("root-namespace").asText());
    }
    walkerCfg.setParams(getParams(current));
    return walkerCfg;
}
Also used : ParserConfigImpl(org.walkmod.conf.entities.impl.ParserConfigImpl) JsonNode(com.fasterxml.jackson.databind.JsonNode) WalkerConfigImpl(org.walkmod.conf.entities.impl.WalkerConfigImpl)

Example 4 with WalkerConfigImpl

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

the class XMLConfigurationProvider method loadWalkerConfig.

public void loadWalkerConfig(Element element, ChainConfig ac) {
    NodeList children;
    Node walkerNode = element;
    if ("walker".equals(walkerNode.getNodeName())) {
        WalkerConfig wc = new WalkerConfigImpl();
        String type = ((Element) walkerNode).getAttribute("type");
        if ("".equals(type)) {
            wc.setType(null);
        } else {
            wc.setType(type);
        }
        wc.setParams(getParams((Element) walkerNode));
        wc.setRootNamespace(((Element) walkerNode).getAttribute("root-namespace"));
        children = walkerNode.getChildNodes();
        if (children.getLength() > 3) {
            throw new ConfigurationException("Invalid walker definition in the " + "architecture" + ac.getName() + ". Please, verify the dtd");
        }
        int max = children.getLength();
        int transformationIndex = wc.getParams().size();
        if (transformationIndex < max) {
            final String nodeName = children.item(transformationIndex).getNodeName();
            if (("parser").equals(nodeName)) {
                loadParserConfig((Element) children.item(transformationIndex), wc);
                transformationIndex = 1;
            } else {
                wc.setParserConfig(new ParserConfigImpl());
            }
        } else {
            wc.setParserConfig(new ParserConfigImpl());
        }
        if (transformationIndex < max) {
            loadTransformationConfigs((Element) children.item(transformationIndex), wc);
        }
        ac.setWalkerConfig(wc);
    } else {
        throw new ConfigurationException("Invalid architecture definition. " + "A walker element must be defined in the architecture element " + ac.getName());
    }
}
Also used : ParserConfigImpl(org.walkmod.conf.entities.impl.ParserConfigImpl) ConfigurationException(org.walkmod.conf.ConfigurationException) WalkerConfig(org.walkmod.conf.entities.WalkerConfig) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) WalkerConfigImpl(org.walkmod.conf.entities.impl.WalkerConfigImpl)

Example 5 with WalkerConfigImpl

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

the class XMLConfigurationProvider method loadChains.

public void loadChains() throws ConfigurationException {
    Element rootElement = document.getDocumentElement();
    NodeList children = rootElement.getChildNodes();
    int childSize = children.getLength();
    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();
            if ("chain".equals(nodeName)) {
                ChainConfig ac = new ChainConfigImpl();
                if ("".equals(child.getAttribute("name"))) {
                    if (i == 0) {
                        ac.setName("chain_" + (i + 1));
                    } else {
                        ac.setName("chain_" + (i + 1));
                    }
                } else {
                    ac.setName(child.getAttribute("name"));
                }
                NodeList childrenModel = child.getChildNodes();
                ac.setParameters(getParams(child));
                int index = 0;
                if ("reader".equals(childrenModel.item(index).getNodeName())) {
                    loadReaderConfig((Element) childrenModel.item(index), ac);
                    index++;
                } else {
                    addDefaultReaderConfig(ac);
                }
                if (index >= childrenModel.getLength()) {
                    throw new ConfigurationException("Invalid architecture definition for the " + "element" + ac.getName());
                }
                if ("walker".equals(childrenModel.item(index).getNodeName())) {
                    loadWalkerConfig((Element) childrenModel.item(index), ac);
                    index++;
                } else if ("transformation".equals(childrenModel.item(index).getNodeName())) {
                    addDefaultWalker(ac, child);
                } else {
                    throw new ConfigurationException("Invalid transformation chain. A walker or at least one transformation must be specified");
                }
                if (index > childrenModel.getLength()) {
                    throw new ConfigurationException("Invalid architecture definition for the " + "element" + ac.getName());
                }
                boolean found = false;
                while (index < childrenModel.getLength() && !found) {
                    if ("writer".equals(childrenModel.item(index).getNodeName())) {
                        found = true;
                        loadWriter((Element) childrenModel.item(index), ac);
                    }
                    index++;
                }
                if (!found) {
                    addDefaultWriterConfig(ac);
                }
                configuration.addChainConfig(ac);
            } else if ("transformation".equals(nodeName)) {
                ChainConfig ac = new ChainConfigImpl();
                ac.setName("default");
                List<TransformationConfig> transformationConfigs = getTransformationItems(rootElement, true);
                WalkerConfig wc = new WalkerConfigImpl();
                wc.setType(null);
                wc.setParserConfig(new ParserConfigImpl());
                wc.setTransformations(transformationConfigs);
                addDefaultReaderConfig(ac);
                ac.setWalkerConfig(wc);
                addDefaultWriterConfig(ac);
                configuration.addChainConfig(ac);
                i = i + transformationConfigs.size() - 1;
            }
        }
    }
    LOG.debug("Transformation chains loaded");
}
Also used : ParserConfigImpl(org.walkmod.conf.entities.impl.ParserConfigImpl) WalkerConfig(org.walkmod.conf.entities.WalkerConfig) Element(org.w3c.dom.Element) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) ChainConfigImpl(org.walkmod.conf.entities.impl.ChainConfigImpl) WalkerConfigImpl(org.walkmod.conf.entities.impl.WalkerConfigImpl) ChainConfig(org.walkmod.conf.entities.ChainConfig) ConfigurationException(org.walkmod.conf.ConfigurationException) List(java.util.List) LinkedList(java.util.LinkedList) NodeList(org.w3c.dom.NodeList)

Aggregations

WalkerConfigImpl (org.walkmod.conf.entities.impl.WalkerConfigImpl)6 WalkerConfig (org.walkmod.conf.entities.WalkerConfig)5 ParserConfigImpl (org.walkmod.conf.entities.impl.ParserConfigImpl)5 LinkedList (java.util.LinkedList)3 ChainConfig (org.walkmod.conf.entities.ChainConfig)3 ChainConfigImpl (org.walkmod.conf.entities.impl.ChainConfigImpl)3 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 Element (org.w3c.dom.Element)2 Node (org.w3c.dom.Node)2 NodeList (org.w3c.dom.NodeList)2 ConfigurationException (org.walkmod.conf.ConfigurationException)2 TransformationConfig (org.walkmod.conf.entities.TransformationConfig)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 TextNode (com.fasterxml.jackson.databind.node.TextNode)1 List (java.util.List)1 Test (org.junit.Test)1 Configuration (org.walkmod.conf.entities.Configuration)1 MergePolicyConfig (org.walkmod.conf.entities.MergePolicyConfig)1