Skip to content

Commit

Permalink
Merge pull request #252 from logzio/DEV-20299-Fix-Sawmill-XML-Process…
Browse files Browse the repository at this point in the history
…or-Creates-Empty-Object

DEV-20299 Fix Sawmill XML Processor Creates Empty Object
  • Loading branch information
yotamlevy3 committed Jan 6, 2021
2 parents c674794 + 5a05507 commit 0cb482b
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 3 deletions.
3 changes: 2 additions & 1 deletion sawmill-core/src/main/java/io/logz/sawmill/Doc.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.logz.sawmill;

import static com.google.common.base.Preconditions.checkState;
import org.apache.commons.collections4.MapUtils;

import java.util.ArrayList;
Expand All @@ -9,6 +8,8 @@
import java.util.Map;
import java.util.Optional;

import static com.google.common.base.Preconditions.checkState;

public class Doc {

private final Map<String, Object> source;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,33 +104,38 @@ public ProcessResult process(Doc doc) {
xmlNodes.forEach(doc::addField);
}
}

return ProcessResult.success();
}

private Map<String, Object> extractNodes(Node parent) {
Map<String, Object> xmlNodes = new HashMap<>();
NodeList nodes = parent.getChildNodes();

for (int i = 0; i < nodes.getLength(); i++) {
Node node = nodes.item(i);

String key = node.getNodeName();
Object value;

if (node.getChildNodes().getLength() == 1 &&
node.getChildNodes().item(0).getNodeValue() != null) {
if (node.getChildNodes().getLength() == 1
&& node.getChildNodes().item(0) != null
&& node.getChildNodes().item(0).getNodeValue() != null ) {
value = node.getChildNodes().item(0).getNodeValue();
} else {
value = extractNodes(node);
}

xmlNodes.compute(key, (k, oldVal) -> {
if (node.getChildNodes().item(0) == null) { return oldVal; }
if (oldVal == null) return value;
if (oldVal instanceof List) {
((List) oldVal).add(value);
return oldVal;
}
return new ArrayList<>(Arrays.asList(oldVal, value));
});

}

return xmlNodes;
Expand Down Expand Up @@ -190,5 +195,6 @@ public Map<String, String> getXpath() {
public boolean isStoreXml() {
return storeXml;
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ public void testValidXml() {
assertThat(processResult.isSucceeded()).isTrue();
assertThat((String) doc.getField("country.id")).isEqualTo("1");
assertThat((String) doc.getField("country.name")).isEqualTo("Israel");
assertThat(doc.hasField("country.TestEmptyField")).isFalse();
assertThatThrownBy(() -> doc.getField("country.TestEmptyField")).isInstanceOf(IllegalStateException.class);
assertThat((List) doc.getField("country.cities.city"))
.isEqualTo(Arrays.asList(ImmutableMap.of("name", "Jerusalem"),
ImmutableMap.of("name", "Tel Aviv")));
Expand Down
1 change: 1 addition & 0 deletions sawmill-core/src/test/resources/xml_valid.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<country>
<id>1</id>
<name>Israel</name>
<TestEmptyField></TestEmptyField>
<cities>
<city><name>Jerusalem</name></city>
<city><name>Tel Aviv</name></city>
Expand Down

0 comments on commit 0cb482b

Please sign in to comment.