Search in sources :

Example 6 with WalkerConfig

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

use of org.walkmod.conf.entities.WalkerConfig 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)

Example 8 with WalkerConfig

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

the class AbstractXMLConfigurationAction method createChainElement.

public Element createChainElement(ChainConfig chainCfg) {
    Document document = provider.getDocument();
    Element element = document.createElement("chain");
    String name = chainCfg.getName();
    if (name != null && !"".equals(name)) {
        element.setAttribute("name", chainCfg.getName());
    }
    ReaderConfig rConfig = chainCfg.getReaderConfig();
    if (rConfig != null) {
        if (rConfig.getType() != null || rConfig.getPath() != null || rConfig.getIncludes() != null || rConfig.getExcludes() != null || rConfig.getParameters() != null) {
            Element reader = document.createElement("reader");
            createReaderOrWriterContent(reader, rConfig.getPath(), rConfig.getType(), rConfig.getParameters(), rConfig.getIncludes(), rConfig.getExcludes());
            element.appendChild(reader);
        }
    }
    WalkerConfig wConfig = chainCfg.getWalkerConfig();
    if (wConfig != null) {
        // (param*, parser?, transformations)
        Map<String, Object> params = wConfig.getParams();
        List<Element> result = createTransformationList(wConfig.getTransformations());
        if (params == null && (wConfig.getType() == null || "".equals(wConfig.getType()))) {
            if (result != null) {
                for (Element transformationElement : result) {
                    element.appendChild(transformationElement);
                }
            }
        } else {
            Element walker = document.createElement("walker");
            String type = wConfig.getType();
            if (type != null && !"".equals(type)) {
                walker.setAttribute("type", type);
            }
            List<Element> paramListEment = createParamsElement(params);
            if (paramListEment != null) {
                for (Element param : paramListEment) {
                    walker.appendChild(param);
                }
            }
            Element transformationList = document.createElement("transformations");
            if (result != null) {
                for (Element transformationElement : result) {
                    transformationList.appendChild(transformationElement);
                }
            }
            walker.appendChild(transformationList);
            element.appendChild(walker);
        }
    }
    WriterConfig writerConfig = chainCfg.getWriterConfig();
    if (writerConfig != null) {
        if (writerConfig.getType() != null || writerConfig.getPath() != null || writerConfig.getExcludes() != null || writerConfig.getIncludes() != null || writerConfig.getParams() != null) {
            Element writer = document.createElement("writer");
            createReaderOrWriterContent(writer, writerConfig.getPath(), writerConfig.getType(), writerConfig.getParams(), writerConfig.getIncludes(), writerConfig.getExcludes());
            element.appendChild(writer);
        }
    }
    return element;
}
Also used : WalkerConfig(org.walkmod.conf.entities.WalkerConfig) Element(org.w3c.dom.Element) ReaderConfig(org.walkmod.conf.entities.ReaderConfig) Document(org.w3c.dom.Document) WriterConfig(org.walkmod.conf.entities.WriterConfig)

Example 9 with WalkerConfig

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

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

the class DefaultChainAdapter method prepare.

@Override
public void prepare() throws WalkModException {
    setName(ac.getName());
    ReaderConfig readerConfig = ac.getReaderConfig();
    WriterConfig writerConfig = ac.getWriterConfig();
    String modelName = readerConfig.getPath();
    String modelType = readerConfig.getType();
    ChainReader reader = readerConfig.getModelReader();
    if (reader == null) {
        try {
            reader = (ChainReader) ac.getConfiguration().getBean(modelType, readerConfig.getParameters());
        } catch (Exception e2) {
            throw new WalkModException("The model " + modelName + ", whose type is " + modelType + "in the architecture " + getName() + " cannot be loaded ", e2);
        }
    }
    readerConfig.setModelReader(reader);
    reader.setPath(readerConfig.getPath());
    reader.setExcludes(readerConfig.getExcludes());
    reader.setIncludes(readerConfig.getIncludes());
    try {
        setResource(reader.read());
        LOG.debug("Model " + modelName + " loaded");
    } catch (Exception e2) {
        throw new WalkModException("The model " + modelName + ", whose type is " + modelType + "in the architecture " + getName() + " cannot be read ", e2);
    }
    WalkerConfig wc = ac.getWalkerConfig();
    ChainWalkerAdapter wa = new DefaultChainWalkerAdapter();
    setWalkerAdapter(wa);
    ChainWalker walker = wc.getWalker();
    if (walker == null) {
        walker = (ChainWalker) ac.getConfiguration().getBean(wc.getType(), wc.getParams());
    }
    wc.setWalker(walker);
    wa.setWalker(walker);
    wa.setWalkerConfig(wc);
    wa.setArchitectureProxy(this);
    wa.setWalkerInvocation(new DefaultChainWalkerInvocation());
    ChainWriter writer = writerConfig.getModelWriter();
    if (writer == null) {
        try {
            writer = (ChainWriter) ac.getConfiguration().getBean(writerConfig.getType(), writerConfig.getParams());
        } catch (Exception e2) {
            throw new WalkModException("The writer " + ", whose type is " + writerConfig.getType() + "in the architecture " + getName() + " cannot be read ", e2);
        }
    }
    writerConfig.setModelWriter(writer);
    writer.setPath(writerConfig.getPath());
    setChainWriter(writer);
    wa.prepare();
    ai.init(this);
}
Also used : ChainWalker(org.walkmod.ChainWalker) ChainWriter(org.walkmod.ChainWriter) WalkModException(org.walkmod.exceptions.WalkModException) ChainReader(org.walkmod.ChainReader) WalkerConfig(org.walkmod.conf.entities.WalkerConfig) ReaderConfig(org.walkmod.conf.entities.ReaderConfig) ChainWalkerAdapter(org.walkmod.ChainWalkerAdapter) WalkModException(org.walkmod.exceptions.WalkModException) WriterConfig(org.walkmod.conf.entities.WriterConfig)

Aggregations

WalkerConfig (org.walkmod.conf.entities.WalkerConfig)11 ChainConfig (org.walkmod.conf.entities.ChainConfig)6 LinkedList (java.util.LinkedList)5 Element (org.w3c.dom.Element)5 ReaderConfig (org.walkmod.conf.entities.ReaderConfig)5 TransformationConfig (org.walkmod.conf.entities.TransformationConfig)5 WriterConfig (org.walkmod.conf.entities.WriterConfig)5 ChainConfigImpl (org.walkmod.conf.entities.impl.ChainConfigImpl)5 WalkerConfigImpl (org.walkmod.conf.entities.impl.WalkerConfigImpl)5 ParserConfigImpl (org.walkmod.conf.entities.impl.ParserConfigImpl)4 JsonNode (com.fasterxml.jackson.databind.JsonNode)3 Node (org.w3c.dom.Node)3 NodeList (org.w3c.dom.NodeList)3 ConfigurationException (org.walkmod.conf.ConfigurationException)3 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 TransformerException (javax.xml.transform.TransformerException)2 Document (org.w3c.dom.Document)2