Skip to content

Commit

Permalink
[NOID] fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vga91 committed Dec 5, 2024
1 parent 32da6d9 commit f46717d
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/
package apoc.export.graphml;

import static apoc.util.ConvertUtil.toValidValue;

import apoc.export.util.BatchTransaction;
import apoc.export.util.ExportConfig;
Expand Down
37 changes: 24 additions & 13 deletions core/src/main/java/apoc/load/Xml.java
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public Stream<MapResult> xml(
@Name(value = "config", defaultValue = "{}") Map<String, Object> config,
@Name(value = "simple", defaultValue = "false") boolean simpleMode)
throws Exception {
return xmlXpathToMapResult(urlOrBinary, simpleMode, path, config);
return xmlXpathToMapResult(urlOrBinary, simpleMode, path, config, terminationGuard);
}

@UserFunction("apoc.xml.parse")
Expand All @@ -128,29 +128,39 @@ public Map<String, Object> parse(
throws Exception {
if (config == null) config = Collections.emptyMap();
boolean failOnError = (boolean) config.getOrDefault("failOnError", true);
return parse(new ByteArrayInputStream(data.getBytes(Charset.forName("UTF-8"))), simpleMode, path, failOnError)
return parse(
new ByteArrayInputStream(data.getBytes(Charset.forName("UTF-8"))),
simpleMode,
path,
failOnError,
terminationGuard)
.map(mr -> mr.value)
.findFirst()
.orElse(null);
}

public static Stream<MapResult> xmlXpathToMapResult(
@Name("urlOrBinary") Object urlOrBinary, boolean simpleMode, String path, Map<String, Object> config)
@Name("urlOrBinary") Object urlOrBinary,
boolean simpleMode,
String path,
Map<String, Object> config,
TerminationGuard terminationGuard)
throws Exception {
if (config == null) config = Collections.emptyMap();
boolean failOnError = (boolean) config.getOrDefault("failOnError", true);
try {
Map<String, Object> headers = (Map) config.getOrDefault("headers", Collections.emptyMap());
CountingInputStream is = FileUtils.inputStreamFor(
urlOrBinary, headers, null, (String) config.getOrDefault(COMPRESSION, CompressionAlgo.NONE.name()));
return parse(is, simpleMode, path, failOnError);
return parse(is, simpleMode, path, failOnError, terminationGuard);
} catch (Exception e) {
if (!failOnError) return Stream.of(new MapResult(Collections.emptyMap()));
else throw e;
}
}

public static Stream<MapResult> parse(InputStream data, boolean simpleMode, String path, boolean failOnError)
public static Stream<MapResult> parse(
InputStream data, boolean simpleMode, String path, boolean failOnError, TerminationGuard terminationGuard)
throws Exception {
List<MapResult> result = new ArrayList<>();
try {
Expand All @@ -173,7 +183,7 @@ public static Stream<MapResult> parse(InputStream data, boolean simpleMode, Stri
for (int i = 0; i < nodeList.getLength(); i++) {
final Deque<Map<String, Object>> stack = new LinkedList<>();

handleNode(stack, nodeList.item(i), simpleMode);
handleNode(stack, nodeList.item(i), simpleMode, terminationGuard);
for (int index = 0; index < stack.size(); index++) {
result.add(new MapResult(stack.pollFirst()));
}
Expand Down Expand Up @@ -223,15 +233,16 @@ private boolean proceedReader(XMLStreamReader reader) throws XMLStreamException
}
}

private void handleNode(Deque<Map<String, Object>> stack, Node node, boolean simpleMode) {
private static void handleNode(
Deque<Map<String, Object>> stack, Node node, boolean simpleMode, TerminationGuard terminationGuard) {
terminationGuard.check();

// Handle document node
if (node.getNodeType() == Node.DOCUMENT_NODE) {
NodeList children = node.getChildNodes();
for (int i = 0; i < children.getLength(); i++) {
if (children.item(i).getLocalName() != null) {
handleNode(stack, children.item(i), simpleMode);
handleNode(stack, children.item(i), simpleMode, terminationGuard);
return;
}
}
Expand All @@ -248,7 +259,7 @@ private void handleNode(Deque<Map<String, Object>> stack, Node node, boolean sim

// This is to deal with text between xml tags for example new line characters
if (child.getNodeType() != Node.TEXT_NODE && child.getNodeType() != Node.CDATA_SECTION_NODE) {
handleNode(stack, child, simpleMode);
handleNode(stack, child, simpleMode, terminationGuard);
count++;
} else {
// Deal with text nodes
Expand Down Expand Up @@ -290,7 +301,7 @@ private void handleNode(Deque<Map<String, Object>> stack, Node node, boolean sim
* @param node
* @param elementMap
*/
private void handleTypeAndAttributes(Node node, Map<String, Object> elementMap) {
private static void handleTypeAndAttributes(Node node, Map<String, Object> elementMap) {
// Set type
if (node.getLocalName() != null) {
elementMap.put("_type", node.getLocalName());
Expand All @@ -312,7 +323,7 @@ private void handleTypeAndAttributes(Node node, Map<String, Object> elementMap)
* @param node
* @param elementMap
*/
private void handleTextNode(Node node, Map<String, Object> elementMap) {
private static void handleTextNode(Node node, Map<String, Object> elementMap) {
Object text = "";
int nodeType = node.getNodeType();
switch (nodeType) {
Expand Down Expand Up @@ -344,7 +355,7 @@ private void handleTextNode(Node node, Map<String, Object> elementMap) {
* @param text
* @return
*/
private String normalizeText(String text) {
private static String normalizeText(String text) {
String[] tokens = StringUtils.split(text, "\n");
for (int i = 0; i < tokens.length; i++) {
tokens[i] = tokens[i].trim();
Expand Down Expand Up @@ -682,7 +693,7 @@ private void setPropertyIfNotNull(org.neo4j.graphdb.Node root, String propertyKe
}
}

private RuntimeException generateXmlDoctypeException() {
private static RuntimeException generateXmlDoctypeException() {
throw new RuntimeException("XML documents with a DOCTYPE are not allowed.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import org.neo4j.values.storable.TimeValue;
import org.neo4j.values.storable.Values;

public class ExtendedUtil {
public class ConvertUtil {
public static Object toValidValue(Object object, String field, Map<String, Object> mapping) {
Object fieldName = mapping.get(field);
if (object != null && fieldName != null) {
Expand Down
2 changes: 1 addition & 1 deletion full/src/main/java/apoc/export/arrow/ImportArrow.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import static apoc.export.arrow.ArrowUtils.FIELD_SOURCE_ID;
import static apoc.export.arrow.ArrowUtils.FIELD_TARGET_ID;
import static apoc.export.arrow.ArrowUtils.FIELD_TYPE;
import static apoc.util.ExtendedUtil.toValidValue;
import static apoc.util.ConvertUtil.toValidValue;

import apoc.Extended;
import apoc.Pools;
Expand Down
2 changes: 1 addition & 1 deletion full/src/main/java/apoc/load/Gexf.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public Stream<MapResult> gexf(
throws Exception {
boolean simpleMode = Util.toBoolean(config.getOrDefault("simpleMode", false));
String path = (String) config.getOrDefault("path", "/");
return xmlXpathToMapResult(urlOrBinary, simpleMode, path, config);
return xmlXpathToMapResult(urlOrBinary, simpleMode, path, config, terminationGuard);
}

@Procedure(name = "apoc.import.gexf", mode = Mode.WRITE)
Expand Down

0 comments on commit f46717d

Please sign in to comment.