Skip to content

Commit

Permalink
Implement desc tag in GraphML import #2030
Browse files Browse the repository at this point in the history
  • Loading branch information
mbastian committed Dec 29, 2021
1 parent f0ba5e2 commit 80affdf
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ Development and Distribution License("CDDL") (collectively, the
import org.gephi.io.importer.api.EdgeDirection;
import org.gephi.io.importer.api.EdgeDirectionDefault;
import org.gephi.io.importer.api.EdgeDraft;
import org.gephi.io.importer.api.ElementDraft;
import org.gephi.io.importer.api.Issue;
import org.gephi.io.importer.api.NodeDraft;
import org.gephi.io.importer.api.PropertiesAssociations;
Expand Down Expand Up @@ -94,6 +95,7 @@ public class ImporterGraphML implements FileImporter, LongTask {
private static final String ATTRIBUTE_FOR = "for";
private static final String ATTVALUE = "data";
private static final String ATTVALUE_FOR = "key";
private static final String DESC = "desc";
private final PropertiesAssociations properties = new PropertiesAssociations();
private final HashMap<String, NodeProperties> nodePropertiesAttributes = new HashMap<>();
private final HashMap<String, EdgeProperties> edgePropertiesAttributes = new HashMap<>();
Expand Down Expand Up @@ -262,6 +264,8 @@ private void readNode(XMLStreamReader reader, NodeDraft parent) throws Exception
readNodeAttValue(reader, node);
} else if (NODE.equalsIgnoreCase(name)) {
readNode(reader, node);
} else if (DESC.equals(name)) {
readDesc(reader, node);
}
break;

Expand Down Expand Up @@ -379,6 +383,31 @@ private void readNodeAttValue(XMLStreamReader reader, NodeDraft node) throws Exc
}
}

private void readDesc(XMLStreamReader reader, ElementDraft element) throws Exception {
StringBuilder value = new StringBuilder();
boolean end = false;
while (reader.hasNext() && !end) {
int xmltype = reader.next();

switch (xmltype) {
case XMLStreamReader.CHARACTERS:
if (!xmlReader.isWhiteSpace()) {
value.append(xmlReader.getText());
}
break;
case XMLStreamReader.END_ELEMENT:
if (DESC.equalsIgnoreCase(xmlReader.getLocalName())) {
end = true;
}
break;
}
}

if (!value.toString().isEmpty()) {
element.setLabel(value.toString());
}
}

private void readEdge(XMLStreamReader reader) throws Exception {
String id = "";
String source = "";
Expand Down Expand Up @@ -441,8 +470,11 @@ private void readEdge(XMLStreamReader reader) throws Exception {

switch (elemType) {
case XMLStreamReader.START_ELEMENT:
if (ATTVALUE.equalsIgnoreCase(xmlReader.getLocalName())) {
String name = xmlReader.getLocalName();
if (ATTVALUE.equalsIgnoreCase(name)) {
readEdgeAttValue(reader, edge);
} else if (DESC.equals(name)) {
readDesc(reader, edge);
}
break;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package org.gephi.io.importer.plugin.file;

import org.gephi.io.importer.api.Container;
import org.gephi.io.importer.impl.ImportContainerImpl;
import org.junit.Assert;
import org.junit.Test;

public class GraphMLTest {

@Test
public void testWithDescTag() {
ImporterGraphML importer = new ImporterGraphML();
importer.setReader(Utils.getReader("withdesc.graphml"));

Container container = new ImportContainerImpl();
importer.execute(container.getLoader());
Assert.assertTrue(container.verify());

Utils.assertSameLabels(Utils.toNodesArray(container), "Node Zero", "Node One");
Utils.assertSameLabels(Utils.toEdgesArray(container), "Edge Zero");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import java.util.List;
import java.util.stream.Collectors;
import org.gephi.io.importer.api.Container;
import org.gephi.io.importer.api.ContainerUnloader;
import org.gephi.io.importer.api.EdgeDraft;
import org.gephi.io.importer.api.ElementDraft;
import org.gephi.io.importer.api.NodeDraft;
Expand All @@ -36,8 +35,14 @@ public static EdgeDraft[] toEdgesArray(Container container) {

public static void assertSameIds(ElementDraft[] actual, String... ids) {
Assert.assertEquals(ids.length, actual.length);
Assert.assertEquals(Arrays.stream(actual).map(ElementDraft::getId).collect(Collectors.toSet()),
new HashSet<>(Arrays.asList(ids)));
Assert.assertEquals(new HashSet<>(Arrays.asList(ids)),
Arrays.stream(actual).map(ElementDraft::getId).collect(Collectors.toSet()));
}

public static void assertSameLabels(ElementDraft[] actual, String... labels) {
Assert.assertEquals(labels.length, actual.length);
Assert.assertEquals(new HashSet<>(Arrays.asList(labels)),
Arrays.stream(actual).map(ElementDraft::getLabel).collect(Collectors.toSet()));
}

public static Reader getReader(String fileName) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<graphml xmlns="http://graphml.graphdrawing.org/xmlns"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns
http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd">
<graph edgedefault="directed">
<node id="n0">
<desc>Node Zero</desc>
</node>
<node id="n1">
<desc>Node One</desc>
</node>
<edge id="e0" source="n0" target="n1">
<desc>Edge Zero</desc>
</edge>
</graph>
</graphml>

0 comments on commit 80affdf

Please sign in to comment.