Skip to content

Commit

Permalink
[NOID] fix 4.4 implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
vga91 committed Dec 6, 2024
1 parent f46717d commit b90febf
Showing 1 changed file with 16 additions and 11 deletions.
27 changes: 16 additions & 11 deletions core/src/main/java/apoc/export/graphml/XmlGraphMLReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -230,17 +230,19 @@ public XmlGraphMLReader(GraphDatabaseService db, Transaction tx) {
}

public enum ReaderType {
GRAPHML("attvalue", KEY, LABEL),
GEXF("data", FOR, KIND);
GRAPHML("attvalue", KEY, LABEL, LABELS),
GEXF("data", FOR, KIND, LABEL);

public String attvalue;
public QName key;
public QName label;
public QName labels;

ReaderType(String attvalue, QName key, QName label) {
ReaderType(String attvalue, QName key, QName label, QName labels) {
this.attvalue = attvalue;
this.key = key;
this.label = label;
this.labels = labels;
}
}

Expand Down Expand Up @@ -284,8 +286,9 @@ public long parseXML(Reader input, TerminationGuard terminationGuard, ReaderType
if (event.isStartElement()) {
StartElement element = event.asStartElement();
String name = element.getName().getLocalPart();
if (name.equals("graphml") || name.equals("graph") || name.equals("gexf")) continue;
if (name.equals("attribute")) {
boolean isNameGexf = readerType.equals(ReaderType.GEXF) && name.equals("gexf");
if (name.equals("graphml") || name.equals("graph") || isNameGexf) continue;
if (readerType.equals(ReaderType.GEXF) && name.equals("attribute")) {
String id = getAttribute(element, ID);
String type = getAttribute(element, DATA_TYPE);
dataMap.put(id, type);
Expand All @@ -311,23 +314,25 @@ public long parseXML(Reader input, TerminationGuard terminationGuard, ReaderType
else relKeys.put(id, key);
continue;
}
if (name.equals(readerType.attvalue)) { // Changed from data to attvalue for node properties in gexf
if (name.equals(readerType.attvalue)) {
if (last == null) continue;
String id = getAttribute(element, readerType.key);
boolean isNode = last instanceof Node;
Key key = isNode ? nodeKeys.get(id) : relKeys.get(id);
if (key == null) key = Key.defaultKey(id, isNode);
final Map.Entry<XMLEvent, Object> eventEntry = getDataEventEntry(reader, key);
final XMLEvent next = eventEntry.getKey();
final Object value = readerType.equals(ReaderType.GRAPHML)
Object value = readerType.equals(ReaderType.GRAPHML)
? eventEntry.getValue()
: getAttribute(element, VALUE);
if (value != null) {
if (this.labels && isNode && id.equals("labels")) {
addLabels((Node) last, value.toString());
} else if (!this.labels || isNode || !id.equals("label")) {
Object convertedValue = toValidValue(value, key.name, dataMap);
last.setProperty(key.name, convertedValue);
value = readerType.equals(ReaderType.GRAPHML)
? toValidValue(value, key.name, dataMap)
: value;
last.setProperty(key.name, value);
if (reporter != null) reporter.update(0, 0, 1);
}
} else if (next.getEventType() == XMLStreamConstants.END_ELEMENT) {
Expand All @@ -342,7 +347,7 @@ public long parseXML(Reader input, TerminationGuard terminationGuard, ReaderType
Node node = tx.getTransaction().createNode();
if (this.labels) {
String labels = getAttribute(
element, LABEL); // Changed from labels to label to fit gexf property format
element, readerType.labels);
addLabels(node, labels);
}
if (storeNodeIds) node.setProperty("id", id);
Expand All @@ -355,7 +360,7 @@ public long parseXML(Reader input, TerminationGuard terminationGuard, ReaderType
}
if (name.equals("edge")) {
tx.increment();
String label = getAttribute(element, readerType.label); // changed from label to kind for gexf
String label = getAttribute(element, readerType.label);
Node from = getByNodeId(cache, tx.getTransaction(), element, XmlNodeExport.NodeType.SOURCE);
Node to = getByNodeId(cache, tx.getTransaction(), element, XmlNodeExport.NodeType.TARGET);

Expand Down

0 comments on commit b90febf

Please sign in to comment.