1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 package org.dishevelled.graph.io.graphml;
25
26 import java.io.File;
27 import java.io.FileWriter;
28 import java.io.IOException;
29 import java.io.OutputStream;
30
31 import java.util.HashMap;
32 import java.util.Map;
33
34 import javax.xml.stream.XMLOutputFactory;
35 import javax.xml.stream.XMLStreamException;
36 import javax.xml.stream.XMLStreamWriter;
37
38 import org.dishevelled.graph.Edge;
39 import org.dishevelled.graph.Node;
40 import org.dishevelled.graph.Graph;
41
42 import org.dishevelled.graph.io.GraphWriter;
43
44
45
46
47
48
49
50
51
52 public final class GraphMLWriter<N, E>
53 implements GraphWriter<N, E>
54 {
55
56 private final XMLOutputFactory outputFactory;
57
58
59 private ValueHandler<N> nodeValueHandler;
60
61
62 private ValueHandler<E> edgeValueHandler;
63
64
65
66
67
68 public GraphMLWriter()
69 {
70 outputFactory = XMLOutputFactory.newInstance();
71 }
72
73
74
75
76
77
78
79 public void setNodeValueHandler(final ValueHandler<N> nodeValueHandler)
80 {
81 this.nodeValueHandler = nodeValueHandler;
82 }
83
84
85
86
87
88
89 public void setEdgeValueHandler(final ValueHandler<E> edgeValueHandler)
90 {
91 this.edgeValueHandler = edgeValueHandler;
92 }
93
94
95 public void write(final Graph<N, E> graph, final File file)
96 throws IOException
97 {
98 if (graph == null)
99 {
100 throw new IllegalArgumentException("graph must not be null");
101 }
102 if (file == null)
103 {
104 throw new IllegalArgumentException("file must not be null");
105 }
106 try
107 {
108 XMLStreamWriter writer = outputFactory.createXMLStreamWriter(new FileWriter(file));
109 write(graph, writer);
110 }
111 catch (XMLStreamException e)
112 {
113
114 throw new IOException(e.getMessage());
115 }
116 }
117
118
119 public void write(final Graph<N, E> graph, final OutputStream outputStream)
120 throws IOException
121 {
122 if (graph == null)
123 {
124 throw new IllegalArgumentException("graph must not be null");
125 }
126 if (outputStream == null)
127 {
128 throw new IllegalArgumentException("outputStream must not be null");
129 }
130 try
131 {
132 XMLStreamWriter writer = outputFactory.createXMLStreamWriter(outputStream);
133 write(graph, writer);
134 }
135 catch (XMLStreamException e)
136 {
137
138 throw new IOException(e.getMessage());
139 }
140 }
141
142
143
144
145
146
147
148
149
150 private void write(final Graph<N, E> graph, final XMLStreamWriter writer)
151 throws IOException, XMLStreamException
152 {
153 writer.writeStartDocument("1.0");
154
155 writer.writeStartElement("graphml");
156 writer.writeDefaultNamespace("http://graphml.graphdrawing.org/xmlns");
157 writer.writeNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
158 writer.writeAttribute("xsi:schemaLocation", "http://graphml.graphdrawing.org/xmlns http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd");
159 if (nodeValueHandler != null)
160 {
161 writer.writeStartElement("key");
162 writer.writeAttribute("id", "k0");
163 writer.writeAttribute("for", "node");
164 writer.writeEndElement();
165 }
166 if (edgeValueHandler != null)
167 {
168 writer.writeStartElement("key");
169 writer.writeAttribute("id", "k1");
170 writer.writeAttribute("for", "edge");
171 writer.writeEndElement();
172 }
173 writer.writeStartElement("graph");
174 writer.writeAttribute("id", "G");
175 writer.writeAttribute("edgedefault", "directed");
176 int n = 0;
177 Map<Node<N, E>, String> nodeIds = new HashMap<Node<N, E>, String>(graph.nodeCount());
178 for (Node<N, E> node : graph.nodes())
179 {
180 String nodeId = nodeIds.get(node);
181 if (nodeId == null)
182 {
183 nodeId = "n" + n;
184 n++;
185 nodeIds.put(node, nodeId);
186 }
187 writer.writeStartElement("node");
188 writer.writeAttribute("id", nodeId);
189 if (nodeValueHandler != null)
190 {
191 writer.writeStartElement("data");
192 writer.writeAttribute("key", "k0");
193 nodeValueHandler.write(node.getValue(), writer);
194 writer.writeEndElement();
195 }
196 writer.writeEndElement();
197 }
198 int e = 0;
199 for (Edge<N, E> edge : graph.edges())
200 {
201 String edgeId = "e" + e;
202 e++;
203 writer.writeStartElement("edge");
204 writer.writeAttribute("id", edgeId);
205 writer.writeAttribute("source", nodeIds.get(edge.source()));
206 writer.writeAttribute("target", nodeIds.get(edge.target()));
207 if (edgeValueHandler != null)
208 {
209 writer.writeStartElement("data");
210 writer.writeAttribute("key", "k1");
211 edgeValueHandler.write(edge.getValue(), writer);
212 writer.writeEndElement();
213 }
214 writer.writeEndElement();
215 }
216 writer.writeEndElement();
217 writer.writeEndElement();
218 writer.close();
219 }
220
221
222
223
224
225
226 public interface ValueHandler<V>
227 {
228
229
230
231
232
233
234
235
236
237 void write(V value, XMLStreamWriter writer) throws IOException, XMLStreamException;
238 }
239 }