if (normalize) {
vertices = new ArrayList<Vertex>();
for (Vertex v : graph.getVertices()) {
((Collection<Vertex>) vertices).add(v);
}
Collections.sort((List<Vertex>) vertices, new LexicographicalElementComparator());
} else {
vertices = graph.getVertices();
}
for (Vertex vertex : vertices) {
writer.writeStartElement(GraphMLTokens.NODE);
writer.writeAttribute(GraphMLTokens.ID, vertex.getId().toString());
Collection<String> keys;
if (normalize) {
keys = new ArrayList<String>();
keys.addAll(vertex.getPropertyKeys());
Collections.sort((List<String>) keys);
} else {
keys = vertex.getPropertyKeys();
}
for (String key : keys) {
writer.writeStartElement(GraphMLTokens.DATA);
writer.writeAttribute(GraphMLTokens.KEY, key);
Object value = vertex.getProperty(key);
if (null != value) {
writer.writeCharacters(value.toString());
}
writer.writeEndElement();
}
writer.writeEndElement();
}
if (normalize) {
List<Edge> edges = new ArrayList<Edge>();
for (Vertex vertex : graph.getVertices()) {
for (Edge edge : vertex.getEdges(Direction.OUT)) {
edges.add(edge);
}
}
Collections.sort(edges, new LexicographicalElementComparator());
for (Edge edge : edges) {
writer.writeStartElement(GraphMLTokens.EDGE);
writer.writeAttribute(GraphMLTokens.ID, edge.getId().toString());
writer.writeAttribute(GraphMLTokens.SOURCE, edge.getVertex(Direction.OUT).getId().toString());