/
GraphMLWriter.java
244 lines (182 loc) · 7.38 KB
/
GraphMLWriter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package com.lordofthejars.nosqlunit.graph.parser;
import static com.lordofthejars.nosqlunit.graph.parser.TypeCaster.getStringType;
import java.io.OutputStream;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.xml.XMLConstants;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Relationship;
import com.lordofthejars.nosqlunit.neo4j.Neo4jLowLevelOps;
public class GraphMLWriter {
private GraphDatabaseService graphDatabaseService;
private String xmlSchemaLocation;
public GraphMLWriter(GraphDatabaseService graphDatabaseService) {
this.graphDatabaseService = graphDatabaseService;
}
public void write(OutputStream os) throws XMLStreamException {
Map<String, String> vertexKeyTypes = Collections.EMPTY_MAP;
Map<String, String> edgeKeyTypes = Collections.EMPTY_MAP;
List<Node> nodes = loadAllNodes();
vertexKeyTypes = findNodeKeys(nodes);
List<Relationship> relationships = loadAllRelationships();
edgeKeyTypes = findRelationshipKeys(relationships);
final XMLOutputFactory inputFactory = XMLOutputFactory.newInstance();
XMLStreamWriter writer = inputFactory.createXMLStreamWriter(os, "UTF8");
writer.writeStartDocument();
writeRootNode(writer);
writeKeysForEdges(edgeKeyTypes, writer);
writeKeysForNodes(vertexKeyTypes, writer);
writeGraphElement(writer);
writeNodes(nodes, writer);
writeRelationships(relationships, writer);
writeEndGraphs(writer);
writer.writeEndDocument();
writer.flush();
writer.close();
}
private List<Relationship> loadAllRelationships() {
List<Relationship> relationships = new ArrayList<Relationship>();
Iterator<Relationship> relationshipIterator = Neo4jLowLevelOps.getAllRelationships(graphDatabaseService);
while (relationshipIterator.hasNext()) {
relationships.add(relationshipIterator.next());
}
return relationships;
}
private List<Node> loadAllNodes() {
List<Node> nodes = new ArrayList<Node>();
Iterator<Node> nodeIterator = Neo4jLowLevelOps.getAllNodes(this.graphDatabaseService);
while (nodeIterator.hasNext()) {
nodes.add(nodeIterator.next());
}
return nodes;
}
private Map<String, String> findRelationshipKeys(Iterable<Relationship> relationships) {
Map<String, String> edgeKeyTypes = new HashMap<String, String>();
for (Relationship relationship : relationships) {
Iterable<String> keys = relationship.getPropertyKeys();
for (String key : keys) {
if (!edgeKeyTypes.containsKey(key)) {
edgeKeyTypes.put(key, getStringType(relationship.getProperty(key)));
}
}
}
return edgeKeyTypes;
}
private Map<String, String> findNodeKeys(Iterable<Node> nodes) {
Map<String, String> vertexKeyTypes = new HashMap<String, String>();
for (Node node : nodes) {
Iterable<String> keys = node.getPropertyKeys();
for (String key : keys) {
if (!vertexKeyTypes.containsKey(key)) {
vertexKeyTypes.put(key, getStringType(node.getProperty(key)));
}
}
}
return vertexKeyTypes;
}
private void writeEndGraphs(XMLStreamWriter writer) throws XMLStreamException {
writer.writeEndElement(); // graph
writer.writeEndElement(); // graphml
}
private void writeRelationships(List<Relationship> relationships, XMLStreamWriter writer) throws XMLStreamException {
for (Relationship edge : relationships) {
writer.writeStartElement(GraphMLTokens.EDGE);
writer.writeAttribute(GraphMLTokens.ID, Long.toString(edge.getId()));
writer.writeAttribute(GraphMLTokens.SOURCE, Long.toString(edge.getStartNode().getId()));
writer.writeAttribute(GraphMLTokens.TARGET, Long.toString(edge.getEndNode().getId()));
writer.writeAttribute(GraphMLTokens.LABEL, edge.getType().name());
for (String key : edge.getPropertyKeys()) {
writer.writeStartElement(GraphMLTokens.DATA);
writer.writeAttribute(GraphMLTokens.KEY, key);
Object value = edge.getProperty(key);
if (null != value) {
if(TypeCaster.isArray(value)) {
writeArray(value, writer);
} else {
writer.writeCharacters(value.toString());
}
}
writer.writeEndElement();
}
writer.writeEndElement();
}
}
private void writeArray(Object array, XMLStreamWriter writer) throws XMLStreamException {
int length = Array.getLength(array);
for (int i = 0; i < length-1; i ++) {
Object arrayElement = Array.get(array, i);
writer.writeCharacters(arrayElement.toString());
writer.writeCharacters(", ");
}
if(length > 0) {
writer.writeCharacters(Array.get(array, length-1).toString());
}
}
private void writeNodes(List<Node> nodes, XMLStreamWriter writer) throws XMLStreamException {
for (Node node : nodes) {
writer.writeStartElement(GraphMLTokens.NODE);
writer.writeAttribute(GraphMLTokens.ID, Long.toString(node.getId()));
Iterable<String> keys = node.getPropertyKeys();
for (String key : keys) {
writer.writeStartElement(GraphMLTokens.DATA);
writer.writeAttribute(GraphMLTokens.KEY, key);
Object value = node.getProperty(key);
if (null != value) {
writer.writeCharacters(value.toString());
}
writer.writeEndElement();
}
writer.writeEndElement();
}
}
private void writeGraphElement(XMLStreamWriter writer) throws XMLStreamException {
writer.writeStartElement(GraphMLTokens.GRAPH);
writer.writeAttribute(GraphMLTokens.ID, GraphMLTokens.G);
writer.writeAttribute(GraphMLTokens.EDGEDEFAULT, GraphMLTokens.DIRECTED);
}
private void writeRootNode(XMLStreamWriter writer) throws XMLStreamException {
writer.writeStartElement(GraphMLTokens.GRAPHML);
writer.writeAttribute(GraphMLTokens.XMLNS, GraphMLTokens.GRAPHML_XMLNS);
// XML Schema instance namespace definition (xsi)
writer.writeAttribute(XMLConstants.XMLNS_ATTRIBUTE + ":" + GraphMLTokens.XML_SCHEMA_NAMESPACE_TAG,
XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI);
// XML Schema location
writer.writeAttribute(GraphMLTokens.XML_SCHEMA_NAMESPACE_TAG + ":"
+ GraphMLTokens.XML_SCHEMA_LOCATION_ATTRIBUTE, GraphMLTokens.GRAPHML_XMLNS
+ " "
+ (this.xmlSchemaLocation == null ? GraphMLTokens.DEFAULT_GRAPHML_SCHEMA_LOCATION
: this.xmlSchemaLocation));
}
private void writeKeysForNodes(Map<String, String> vertexKeyTypes, XMLStreamWriter writer)
throws XMLStreamException {
// node keys
for (String key : vertexKeyTypes.keySet()) {
writer.writeStartElement(GraphMLTokens.KEY);
writer.writeAttribute(GraphMLTokens.ID, key);
writer.writeAttribute(GraphMLTokens.FOR, GraphMLTokens.NODE);
writer.writeAttribute(GraphMLTokens.ATTR_NAME, key);
writer.writeAttribute(GraphMLTokens.ATTR_TYPE, vertexKeyTypes.get(key));
writer.writeEndElement();
}
}
private void writeKeysForEdges(Map<String, String> edgeKeyTypes, XMLStreamWriter writer) throws XMLStreamException {
// edge key
for (String key : edgeKeyTypes.keySet()) {
writer.writeStartElement(GraphMLTokens.KEY);
writer.writeAttribute(GraphMLTokens.ID, key);
writer.writeAttribute(GraphMLTokens.FOR, GraphMLTokens.EDGE);
writer.writeAttribute(GraphMLTokens.ATTR_NAME, key);
writer.writeAttribute(GraphMLTokens.ATTR_TYPE, edgeKeyTypes.get(key));
writer.writeEndElement();
}
}
}