use of org.walkmod.conf.entities.ChainConfig 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");
}
use of org.walkmod.conf.entities.ChainConfig in project walkmod-core by walkmod.
the class AddConfigurationParameterXMLAction method doAction.
@Override
public void doAction() throws Exception {
List<Element> elementsToModify = new LinkedList<Element>();
Document document = provider.getDocument();
Element rootElement = document.getDocumentElement();
NodeList children = rootElement.getChildNodes();
int childSize = children.getLength();
boolean isTransformationList = false;
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();
Element chainToAnalyze = null;
if ("chain".equals(nodeName)) {
String chainName = child.getAttribute("name");
if (chain != null && !chain.equals("") && chain.equals(chainName)) {
chainToAnalyze = child;
} else if (chain == null) {
chainToAnalyze = child;
}
if (chainToAnalyze != null) {
NodeList chainChildren = chainToAnalyze.getChildNodes();
int limit = chainChildren.getLength();
for (int j = 0; j < limit; j++) {
Node chainChild = chainChildren.item(j);
if (chainChild instanceof Element) {
Element chainElement = (Element) chainChild;
final String chainElementName = chainElement.getNodeName();
if (category != null && category.equals(chainElementName) || category == null) {
analizeBean(chainElement, type, name, elementsToModify);
if (chainElementName.equals("walker") && (category == null || category.equals("transformation"))) {
NodeList walkerChildren = chainChild.getChildNodes();
int limit2 = walkerChildren.getLength();
for (int k = 0; k < limit2; k++) {
Node walkerChild = walkerChildren.item(k);
if (walkerChild instanceof Element) {
if (walkerChild.getNodeName().equals("transformations")) {
NodeList transform = walkerChild.getChildNodes();
int limit3 = transform.getLength();
for (int h = 0; h < limit3; h++) {
Node transformationItem = transform.item(h);
if (transformationItem instanceof Element) {
analizeBean((Element) transformationItem, type, name, elementsToModify);
}
}
} else {
analizeBean((Element) walkerChild, type, name, elementsToModify);
}
}
}
}
}
}
}
}
} else if ("transformation".equals(nodeName)) {
isTransformationList = true;
analizeBean(child, type, name, elementsToModify);
} else if ("conf-providers".equals(nodeName)) {
NodeList provChildren = child.getChildNodes();
int limit = provChildren.getLength();
for (int k = 0; k < limit; k++) {
analizeBean((Element) provChildren.item(k), type, name, elementsToModify);
}
}
}
}
if (isTransformationList) {
if (category == null || Arrays.asList("writer", "reader", "walker").contains(category)) {
Configuration configuration = new ConfigurationImpl();
provider.setConfiguration(configuration);
provider.loadChains();
Map<String, Object> params = new HashMap<String, Object>();
params.put(param, value);
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--;
}
}
}
if (chain == null || chain.equals("default")) {
chainCfg.getWriterConfig().setParams(params);
} else {
throw new TransformerException("The parameter needs to be set to an element of an existing chain and : [" + chain + " does not exist]");
}
rootElement.appendChild(createChainElement(chainCfg));
provider.persist();
}
return;
}
Iterator<Element> it = elementsToModify.iterator();
while (it.hasNext()) {
Element current = it.next();
NodeList childrenElement = current.getChildNodes();
int limit = childrenElement.getLength();
boolean found = false;
for (int i = 0; i < limit && !found; i++) {
Node item = childrenElement.item(i);
if (item.getNodeName().equals("param")) {
Element aux = (Element) item;
if (aux.getAttribute("name").equals(param)) {
aux.setTextContent(value);
found = true;
}
}
}
if (!found) {
Element paramElement = document.createElement("param");
paramElement.setAttribute("name", param);
paramElement.setTextContent(value);
current.appendChild(paramElement);
}
}
if (!elementsToModify.isEmpty()) {
provider.persist();
}
}
use of org.walkmod.conf.entities.ChainConfig 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.ChainConfig in project walkmod-core by walkmod.
the class DefaultChainAdapterFactory method createChainProxy.
@Override
public ChainAdapter createChainProxy(Configuration configuration, String chain) {
Collection<ChainConfig> acs = configuration.getChainConfigs();
if (acs != null) {
Iterator<ChainConfig> it = acs.iterator();
boolean end = false;
ChainConfig acfg = null;
while (it.hasNext() && !end) {
acfg = it.next();
end = chain.equals(acfg.getName());
}
if (end) {
LOG.debug("Chain " + chain + " found");
ChainAdapter ap = new DefaultChainAdapter();
ap.setChainConfig(acfg);
ap.setChainInvocation(new DefaultChainInvocation());
ap.prepare();
return ap;
}
}
return null;
}
use of org.walkmod.conf.entities.ChainConfig 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());
}
Aggregations