From e30ac57101509209b1597e3a7ebdc1dd56247b5c Mon Sep 17 00:00:00 2001 From: vga91 Date: Thu, 5 Dec 2024 11:11:59 +0100 Subject: [PATCH] [NOID] fix tests --- .../apoc/export/graphml/XmlGraphMLReader.java | 2 ++ core/src/main/java/apoc/load/Xml.java | 26 ++++++++--------- .../src/main/java/apoc/util/ConvertUtil.java | 29 ++++++++++--------- .../java/apoc/export/arrow/ImportArrow.java | 2 +- full/src/main/java/apoc/load/Gexf.java | 2 +- 5 files changed, 32 insertions(+), 29 deletions(-) rename full/src/main/java/apoc/util/ExtendedUtil.java => core/src/main/java/apoc/util/ConvertUtil.java (96%) diff --git a/core/src/main/java/apoc/export/graphml/XmlGraphMLReader.java b/core/src/main/java/apoc/export/graphml/XmlGraphMLReader.java index 2e6b1280f5..a1d21d0e61 100644 --- a/core/src/main/java/apoc/export/graphml/XmlGraphMLReader.java +++ b/core/src/main/java/apoc/export/graphml/XmlGraphMLReader.java @@ -43,6 +43,8 @@ import org.neo4j.graphdb.*; import org.neo4j.procedure.TerminationGuard; +import static apoc.util.ConvertUtil.toValidValue; + /** * Created by mh on 10.07.13. */ diff --git a/core/src/main/java/apoc/load/Xml.java b/core/src/main/java/apoc/load/Xml.java index 76bea64835..3a80bc7a22 100644 --- a/core/src/main/java/apoc/load/Xml.java +++ b/core/src/main/java/apoc/load/Xml.java @@ -115,7 +115,7 @@ public Stream xml( @Name(value = "config", defaultValue = "{}") Map 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") @@ -128,14 +128,14 @@ public Map 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 xmlXpathToMapResult( - @Name("urlOrBinary") Object urlOrBinary, boolean simpleMode, String path, Map config) + @Name("urlOrBinary") Object urlOrBinary, boolean simpleMode, String path, Map config, TerminationGuard terminationGuard) throws Exception { if (config == null) config = Collections.emptyMap(); boolean failOnError = (boolean) config.getOrDefault("failOnError", true); @@ -143,14 +143,14 @@ public static Stream xmlXpathToMapResult( Map 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 parse(InputStream data, boolean simpleMode, String path, boolean failOnError) + public static Stream parse(InputStream data, boolean simpleMode, String path, boolean failOnError, TerminationGuard terminationGuard) throws Exception { List result = new ArrayList<>(); try { @@ -173,7 +173,7 @@ public static Stream parse(InputStream data, boolean simpleMode, Stri for (int i = 0; i < nodeList.getLength(); i++) { final Deque> 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())); } @@ -223,7 +223,7 @@ private boolean proceedReader(XMLStreamReader reader) throws XMLStreamException } } - private void handleNode(Deque> stack, Node node, boolean simpleMode) { + private static void handleNode(Deque> stack, Node node, boolean simpleMode, TerminationGuard terminationGuard) { terminationGuard.check(); // Handle document node @@ -231,7 +231,7 @@ private void handleNode(Deque> stack, Node node, boolean sim 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; } } @@ -248,7 +248,7 @@ private void handleNode(Deque> 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 @@ -290,7 +290,7 @@ private void handleNode(Deque> stack, Node node, boolean sim * @param node * @param elementMap */ - private void handleTypeAndAttributes(Node node, Map elementMap) { + private static void handleTypeAndAttributes(Node node, Map elementMap) { // Set type if (node.getLocalName() != null) { elementMap.put("_type", node.getLocalName()); @@ -312,7 +312,7 @@ private void handleTypeAndAttributes(Node node, Map elementMap) * @param node * @param elementMap */ - private void handleTextNode(Node node, Map elementMap) { + private static void handleTextNode(Node node, Map elementMap) { Object text = ""; int nodeType = node.getNodeType(); switch (nodeType) { @@ -344,7 +344,7 @@ private void handleTextNode(Node node, Map 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(); @@ -682,7 +682,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."); } } diff --git a/full/src/main/java/apoc/util/ExtendedUtil.java b/core/src/main/java/apoc/util/ConvertUtil.java similarity index 96% rename from full/src/main/java/apoc/util/ExtendedUtil.java rename to core/src/main/java/apoc/util/ConvertUtil.java index b2701da279..810d9cf179 100644 --- a/full/src/main/java/apoc/util/ExtendedUtil.java +++ b/core/src/main/java/apoc/util/ConvertUtil.java @@ -3,6 +3,17 @@ import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.json.JsonWriteFeature; import com.fasterxml.jackson.databind.ObjectMapper; +import org.apache.commons.lang3.StringUtils; +import org.neo4j.exceptions.Neo4jException; +import org.neo4j.values.storable.DateTimeValue; +import org.neo4j.values.storable.DateValue; +import org.neo4j.values.storable.DurationValue; +import org.neo4j.values.storable.LocalDateTimeValue; +import org.neo4j.values.storable.LocalTimeValue; +import org.neo4j.values.storable.PointValue; +import org.neo4j.values.storable.TimeValue; +import org.neo4j.values.storable.Values; + import java.time.LocalDate; import java.time.LocalDateTime; import java.time.LocalTime; @@ -14,18 +25,8 @@ import java.util.List; import java.util.Map; import java.util.stream.Collectors; -import org.apache.commons.lang3.StringUtils; -import org.neo4j.exceptions.Neo4jException; -import org.neo4j.values.storable.DateTimeValue; -import org.neo4j.values.storable.DateValue; -import org.neo4j.values.storable.DurationValue; -import org.neo4j.values.storable.LocalDateTimeValue; -import org.neo4j.values.storable.LocalTimeValue; -import org.neo4j.values.storable.PointValue; -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 mapping) { Object fieldName = mapping.get(field); if (object != null && fieldName != null) { @@ -46,8 +47,8 @@ public static Object toValidValue(Object object, String field, Map) object) .entrySet().stream() - .collect(Collectors.toMap( - Map.Entry::getKey, e -> toValidValue(e.getValue(), field, mapping))); + .collect(Collectors.toMap( + Map.Entry::getKey, e -> toValidValue(e.getValue(), field, mapping))); } return getNeo4jValue(object); } @@ -71,7 +72,7 @@ private static Object getNeo4jValue(Object object) { private static Object convertValue(String value, String typeName) { typeName = typeName.toLowerCase(); // Suitable to work with Parquet/Arrow/Gexf switch (typeName) { - // {"crs":"wgs-84-3d","latitude":13.1,"longitude":33.46789,"height":100.0} + // {"crs":"wgs-84-3d","latitude":13.1,"longitude":33.46789,"height":100.0} case "point": return getPointValue(value); case "localdatetime": diff --git a/full/src/main/java/apoc/export/arrow/ImportArrow.java b/full/src/main/java/apoc/export/arrow/ImportArrow.java index ad0390b774..e3143daa4d 100644 --- a/full/src/main/java/apoc/export/arrow/ImportArrow.java +++ b/full/src/main/java/apoc/export/arrow/ImportArrow.java @@ -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; diff --git a/full/src/main/java/apoc/load/Gexf.java b/full/src/main/java/apoc/load/Gexf.java index ae2769a16d..9da5bb58a1 100644 --- a/full/src/main/java/apoc/load/Gexf.java +++ b/full/src/main/java/apoc/load/Gexf.java @@ -46,7 +46,7 @@ public Stream 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)