diff --git a/timbuctoo-instancev4/src/main/java/nl/knaw/huygens/timbuctoo/server/TimbuctooV4.java b/timbuctoo-instancev4/src/main/java/nl/knaw/huygens/timbuctoo/server/TimbuctooV4.java index 8800ce62d3..79948b356c 100644 --- a/timbuctoo-instancev4/src/main/java/nl/knaw/huygens/timbuctoo/server/TimbuctooV4.java +++ b/timbuctoo-instancev4/src/main/java/nl/knaw/huygens/timbuctoo/server/TimbuctooV4.java @@ -84,6 +84,7 @@ import nl.knaw.huygens.timbuctoo.v5.dataset.DataSetRepository; import nl.knaw.huygens.timbuctoo.v5.dropwizard.DataSetFactoryManager; import nl.knaw.huygens.timbuctoo.v5.dropwizard.contenttypes.CsvWriter; +import nl.knaw.huygens.timbuctoo.v5.dropwizard.contenttypes.GraphMlWriter; import nl.knaw.huygens.timbuctoo.v5.dropwizard.contenttypes.GraphVizWriter; import nl.knaw.huygens.timbuctoo.v5.dropwizard.contenttypes.JsonLdWriter; import nl.knaw.huygens.timbuctoo.v5.dropwizard.contenttypes.JsonWriter; @@ -282,7 +283,8 @@ public void run(TimbuctooConfiguration configuration, Environment environment) t new CsvWriter(), new JsonLdWriter(), new JsonWriter(), - new GraphVizWriter() + new GraphVizWriter(), + new GraphMlWriter() ); final PaginationArgumentsHelper argHelper = new PaginationArgumentsHelper(configuration.getCollectionFilters()); diff --git a/timbuctoo-instancev4/src/main/java/nl/knaw/huygens/timbuctoo/v5/dropwizard/contenttypes/GraphMlWriter.java b/timbuctoo-instancev4/src/main/java/nl/knaw/huygens/timbuctoo/v5/dropwizard/contenttypes/GraphMlWriter.java new file mode 100644 index 0000000000..9604794681 --- /dev/null +++ b/timbuctoo-instancev4/src/main/java/nl/knaw/huygens/timbuctoo/v5/dropwizard/contenttypes/GraphMlWriter.java @@ -0,0 +1,19 @@ +package nl.knaw.huygens.timbuctoo.v5.dropwizard.contenttypes; + +import nl.knaw.huygens.timbuctoo.v5.serializable.serializations.GraphMlSerialization; +import javax.ws.rs.Produces; + +@Produces(GraphMlWriter.MIME_TYPE) +public class GraphMlWriter extends SerializerWriter { + + public static final String MIME_TYPE = "text/graphml"; + + public GraphMlWriter() { + super(GraphMlSerialization::new); + } + + @Override + public String getMimeType() { + return MIME_TYPE; + } +} diff --git a/timbuctoo-instancev4/src/main/java/nl/knaw/huygens/timbuctoo/v5/serializable/serializations/GraphMlSerialization.java b/timbuctoo-instancev4/src/main/java/nl/knaw/huygens/timbuctoo/v5/serializable/serializations/GraphMlSerialization.java new file mode 100644 index 0000000000..c2d6af1d88 --- /dev/null +++ b/timbuctoo-instancev4/src/main/java/nl/knaw/huygens/timbuctoo/v5/serializable/serializations/GraphMlSerialization.java @@ -0,0 +1,69 @@ +package nl.knaw.huygens.timbuctoo.v5.serializable.serializations; + +import java.io.IOException; +import java.io.OutputStream; +import java.io.PrintWriter; +import java.util.HashMap; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.atomic.AtomicInteger; + +import nl.knaw.huygens.timbuctoo.v5.serializable.SerializableResult; +import nl.knaw.huygens.timbuctoo.v5.serializable.serializations.base.CollectionsOfEntitiesSerialization; + +public class GraphMlSerialization extends CollectionsOfEntitiesSerialization { + + private final PrintWriter writer; + private int countEdge = 1; + + public GraphMlSerialization(OutputStream outputStream) throws IOException { + writer = new PrintWriter(outputStream); + } + + public void serialize(SerializableResult serializableResult) throws IOException { + super.serialize(serializableResult); + Map entityIds = new HashMap<>(); + AtomicInteger nodeCounter = new AtomicInteger(); + writeHeader(); + for (Map entities : allEntities.values()) { + for (Map.Entry entity : entities.entrySet()) { + Integer nodeId = entityIds.computeIfAbsent(entity.getKey(), k -> nodeCounter.incrementAndGet()); + writer.println(" "); + writer.println(" " + entity.getValue() + ""); + writer.println(" " + entity.getKey() + ""); + writer.println(" "); + for (Map.Entry> relation : entity.getValue().relations.entrySet()) { + for (String otherNodeUri : relation.getValue()) { + Integer otherNodeId = entityIds.computeIfAbsent(otherNodeUri, k -> nodeCounter.incrementAndGet()); + writer.println(" "); + writer.println(" " + relation.getKey() + ""); + writer.println(" "); + countEdge++; + } + } + } + } + writeFooter(); + writer.flush(); + writer.close(); + } + + + protected void writeHeader() { + String header = "\n" + + ""; + writer.println(header); + writer.println(" "); + } + + protected void writeFooter() { + writer.println(" "); + String footer = ""; + writer.println(footer); + } + +}