use of org.pentaho.di.trans.steps.xmlinput.XMLInputFieldPosition in project pentaho-kettle by pentaho.
the class XMLInputDialog method getValues.
/**
* Get all the values defined in a Node
*
* @param node
* The node to examine
* @param row
* The
*/
private void getValues(Node node, RowMetaInterface row, List<XMLInputFieldPosition> path, int level) throws KettleException {
String baseName = "";
for (int p = 0; p < path.size(); p++) {
XMLInputFieldPosition pos = path.get(p);
String elementName = pos.getName() + pos.getElementNr();
if (!elementName.startsWith("#")) {
baseName += StringUtil.initCap(new ValueMetaAndData("p", elementName).toString());
}
}
// Add the root element
if (level == 0) {
if (XMLHandler.getNodeValue(node) != null) {
XMLInputFieldPosition attrPos = new XMLInputFieldPosition(node.getNodeName(), XMLInputFieldPosition.XML_ROOT);
path.add(attrPos);
String root = StringUtil.initCap(new ValueMetaAndData("a", node.getNodeName()).toString());
String fieldName = baseName + root;
if (row.searchValueMeta(fieldName) == null) {
// Not there yet: add it!
// Add the fieldname...
ValueMeta field = new ValueMeta(fieldName, ValueMeta.TYPE_STRING);
// Add the path to this attribute to the origin of the
// field...
String encoded = XMLInputFieldPosition.encodePath(path);
field.setOrigin(encoded);
row.addValueMeta(field);
}
// Now remove the root from the path again, it's not needed
// realy...
path.remove(path.size() - 1);
}
}
// First check out the attributes...
String[] attributes = XMLHandler.getNodeAttributes(node);
if (attributes != null) {
for (int i = 0; i < attributes.length; i++) {
XMLInputFieldPosition attrPos = new XMLInputFieldPosition(attributes[i], XMLInputFieldPosition.XML_ATTRIBUTE);
path.add(attrPos);
String attribute = StringUtil.initCap(new ValueMetaAndData("a", attributes[i]).toString());
String fieldName = baseName + attribute;
// See if this fieldname already exists in Row...
if (row.searchValueMeta(fieldName) == null) {
// Add the fieldname...
ValueMeta field = new ValueMeta(fieldName, ValueMeta.TYPE_STRING);
// Add the path to this attribute to the origin of the
// field...
String encoded = XMLInputFieldPosition.encodePath(path);
field.setOrigin(encoded);
row.addValueMeta(field);
}
path.remove(path.size() - 1);
}
}
// Then get the elements
String[] elements = XMLHandler.getNodeElements(node);
if (elements != null) {
for (int e = 0; e < elements.length; e++) {
// Count the number of occurrences of this element...
int occurrences = XMLHandler.countNodes(node, elements[e]);
for (int o = 0; o < occurrences; o++) {
Node itemNode = XMLHandler.getSubNodeByNr(node, elements[e], o, false);
XMLInputFieldPosition xmlPos = new XMLInputFieldPosition(elements[e], XMLInputFieldPosition.XML_ELEMENT, o + 1);
path.add(xmlPos);
getValues(itemNode, row, path, level + 1);
// remove the last one again
path.remove(path.size() - 1);
}
}
} else {
if (path.size() > 0) {
int idxLast = path.size() - 1;
XMLInputFieldPosition last = path.get(idxLast);
path.remove(idxLast);
if (path.size() > 0) {
String encoded = XMLInputFieldPosition.encodePath(path);
if (row.searchValueMeta(baseName) == null) {
ValueMeta value = new ValueMeta(baseName, ValueMeta.TYPE_STRING);
value.setOrigin(encoded);
row.addValueMeta(value);
}
}
path.add(last);
}
}
}
use of org.pentaho.di.trans.steps.xmlinput.XMLInputFieldPosition in project pentaho-kettle by pentaho.
the class XMLInputDialog method get.
private void get() {
boolean finished = false;
int elementsFound = 0;
try {
XMLInputMeta meta = new XMLInputMeta();
getInfo(meta);
// check if the path is given
if (!checkInputPositionsFilled(meta)) {
return;
}
EnterNumberDialog dialog = new EnterNumberDialog(shell, 1000, "Number of elements to scan", "Enter the number of elements to scan (0=all)");
int maxElements = dialog.open();
// OK, let's try to walk through the complete tree
// no fields found...
RowMetaInterface row = new RowMeta();
// Keep the list of positions
// ArrayList of XMLInputFieldPosition
List<XMLInputFieldPosition> path = new ArrayList<XMLInputFieldPosition>();
FileInputList inputList = meta.getFiles(transMeta);
for (int f = 0; f < inputList.getFiles().size() && !finished; f++) {
// Open the file...
Node rootNode = XMLHandler.loadXMLFile(inputList.getFile(f), transMeta.environmentSubstitute(meta.getFileBaseURI()), meta.isIgnoreEntities(), meta.isNamespaceAware());
// Position to the repeating item
for (int p = 0; rootNode != null && p < meta.getInputPosition().length - 1; p++) {
rootNode = XMLHandler.getSubNode(rootNode, meta.getInputPosition()[p]);
}
if (rootNode == null) {
// Specified node not found: return!
return;
}
if (meta.getInputPosition().length > 1) {
// Count the number of rootnodes
String itemElement = meta.getInputPosition()[meta.getInputPosition().length - 1];
int nrItems = XMLHandler.countNodes(rootNode, itemElement);
for (int i = 0; i < nrItems && !finished; i++) {
Node itemNode = XMLHandler.getSubNodeByNr(rootNode, itemElement, i, false);
if (i >= meta.getNrRowsToSkip()) {
getValues(itemNode, row, path, 0);
elementsFound++;
if (elementsFound >= maxElements && maxElements > 0) {
finished = true;
}
}
}
} else {
// Only search the root node
//
getValues(rootNode, row, path, 0);
elementsFound++;
if (elementsFound >= maxElements && maxElements > 0) {
finished = true;
}
}
}
// add the values to the grid...
for (int i = 0; i < row.size(); i++) {
ValueMetaInterface v = row.getValueMeta(i);
TableItem item = new TableItem(wFields.table, SWT.NONE);
item.setText(1, v.getName());
item.setText(2, v.getTypeDesc());
item.setText(11, v.getOrigin());
}
wFields.removeEmptyRows();
wFields.setRowNums();
wFields.optWidth(true);
} catch (KettleException e) {
new ErrorDialog(shell, BaseMessages.getString(PKG, "XMLInputDialog.ErrorParsingData.DialogTitle"), BaseMessages.getString(PKG, "XMLInputDialog.ErrorParsingData.DialogMessage"), e);
}
}
Aggregations