Search in sources :

Example 6 with WriterConfig

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

the class XMLConfigurationProvider method loadWriter.

public void loadWriter(Element child, ChainConfig ac) {
    if ("writer".equals(child.getNodeName())) {
        WriterConfig wc = new WriterConfigImpl();
        String path = child.getAttribute("path");
        if ("".equals(path)) {
            throw new ConfigurationException("Invalid writer definition: " + "A path attribute must be specified");
        }
        wc.setPath(path);
        String type = child.getAttribute("type");
        if ("".equals(type)) {
            wc.setType(null);
        } else {
            wc.setType(type);
        }
        NodeList childs = child.getChildNodes();
        if (childs != null) {
            int max = childs.getLength();
            List<String> excludes = new LinkedList<String>();
            List<String> includes = new LinkedList<String>();
            for (int i = 0; i < max; i++) {
                Node n = childs.item(i);
                String nodeName = n.getNodeName();
                if ("exclude".equals(nodeName)) {
                    Element exclude = (Element) n;
                    excludes.add(exclude.getAttribute("wildcard"));
                } else if ("include".equals(nodeName)) {
                    Element include = (Element) n;
                    includes.add(include.getAttribute("wildcard"));
                } else if (!"param".equals(nodeName)) {
                    throw new ConfigurationException("Invalid writer definition. Only exclude or include tags are supported");
                }
            }
            if (!excludes.isEmpty()) {
                String[] excludesArray = new String[excludes.size()];
                int j = 0;
                for (String exclude : excludes) {
                    excludesArray[j] = exclude;
                    j++;
                }
                wc.setExcludes(excludesArray);
            }
            if (!includes.isEmpty()) {
                String[] includesArray = new String[includes.size()];
                int j = 0;
                for (String include : includes) {
                    includesArray[j] = include;
                    j++;
                }
                wc.setIncludes(includesArray);
            }
        }
        wc.setParams(getParams(child));
        ac.setWriterConfig(wc);
    }
}
Also used : ConfigurationException(org.walkmod.conf.ConfigurationException) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) WriterConfigImpl(org.walkmod.conf.entities.impl.WriterConfigImpl) LinkedList(java.util.LinkedList) WriterConfig(org.walkmod.conf.entities.WriterConfig)

Example 7 with WriterConfig

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

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

Example 9 with WriterConfig

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

Aggregations

WriterConfig (org.walkmod.conf.entities.WriterConfig)9 ReaderConfig (org.walkmod.conf.entities.ReaderConfig)6 WalkerConfig (org.walkmod.conf.entities.WalkerConfig)5 Element (org.w3c.dom.Element)4 ChainConfig (org.walkmod.conf.entities.ChainConfig)3 TransformationConfig (org.walkmod.conf.entities.TransformationConfig)3 WriterConfigImpl (org.walkmod.conf.entities.impl.WriterConfigImpl)3 LinkedList (java.util.LinkedList)2 Document (org.w3c.dom.Document)2 Node (org.w3c.dom.Node)2 NodeList (org.w3c.dom.NodeList)2 ChainWriter (org.walkmod.ChainWriter)2 Configuration (org.walkmod.conf.entities.Configuration)2 ChainConfigImpl (org.walkmod.conf.entities.impl.ChainConfigImpl)2 ConfigurationImpl (org.walkmod.conf.entities.impl.ConfigurationImpl)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 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