use of org.walkmod.conf.entities.impl.ChainConfigImpl 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();
}
}
use of org.walkmod.conf.entities.impl.ChainConfigImpl 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());
}
use of org.walkmod.conf.entities.impl.ChainConfigImpl in project walkmod-core by walkmod.
the class YAMLConfigurationProviderTest method testEmptyChain.
@Test
public void testEmptyChain() throws Exception {
File file = new File("src/test/resources/yaml/addEmptychain.yml");
if (file.exists()) {
file.delete();
}
file.createNewFile();
FileUtils.write(file, "");
YAMLConfigurationProvider provider = new YAMLConfigurationProvider(file.getPath());
Configuration conf = new ConfigurationImpl();
provider.init(conf);
ChainConfig chainCfg = new ChainConfigImpl();
try {
provider.addChainConfig(chainCfg, false, null);
String output = FileUtils.readFileToString(file);
Assert.assertEquals("", output);
} finally {
if (file.exists()) {
file.delete();
}
}
}
Aggregations