| 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.xgmml; |
| 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 | |
public final class XgmmlGraphWriter<N, E> |
| 52 | |
implements GraphWriter<N, E> |
| 53 | |
{ |
| 54 | |
|
| 55 | |
private final XMLOutputFactory outputFactory; |
| 56 | |
|
| 57 | |
|
| 58 | |
private final Map<String, ValueHandler<N>> nodeValueHandlers; |
| 59 | |
|
| 60 | |
|
| 61 | |
private final Map<String, ValueHandler<E>> edgeValueHandlers; |
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
public XgmmlGraphWriter() |
| 68 | 9 | { |
| 69 | 9 | outputFactory = XMLOutputFactory.newInstance(); |
| 70 | 9 | nodeValueHandlers = new HashMap<String, ValueHandler<N>>(); |
| 71 | 9 | edgeValueHandlers = new HashMap<String, ValueHandler<E>>(); |
| 72 | 9 | } |
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | |
|
| 78 | |
|
| 79 | |
|
| 80 | |
|
| 81 | |
public void addNodeValueHandler(final String name, final ValueHandler<N> nodeValueHandler) |
| 82 | |
{ |
| 83 | 4 | if (name == null) |
| 84 | |
{ |
| 85 | 1 | throw new IllegalArgumentException("name must not be null"); |
| 86 | |
} |
| 87 | 3 | if (nodeValueHandler == null) |
| 88 | |
{ |
| 89 | 1 | throw new IllegalArgumentException("nodeValueHandler must not be null"); |
| 90 | |
} |
| 91 | 2 | nodeValueHandlers.put(name, nodeValueHandler); |
| 92 | 2 | } |
| 93 | |
|
| 94 | |
|
| 95 | |
|
| 96 | |
|
| 97 | |
|
| 98 | |
|
| 99 | |
|
| 100 | |
public void addEdgeValueHandler(final String name, final ValueHandler<E> edgeValueHandler) |
| 101 | |
{ |
| 102 | 4 | if (name == null) |
| 103 | |
{ |
| 104 | 1 | throw new IllegalArgumentException("name must not be null"); |
| 105 | |
} |
| 106 | 3 | if (edgeValueHandler == null) |
| 107 | |
{ |
| 108 | 1 | throw new IllegalArgumentException("edgeValueHandler must not be null"); |
| 109 | |
} |
| 110 | 2 | edgeValueHandlers.put(name, edgeValueHandler); |
| 111 | 2 | } |
| 112 | |
|
| 113 | |
|
| 114 | |
public void write(final Graph<N, E> graph, final File file) |
| 115 | |
throws IOException |
| 116 | |
{ |
| 117 | 5 | if (graph == null) |
| 118 | |
{ |
| 119 | 2 | throw new IllegalArgumentException("graph must not be null"); |
| 120 | |
} |
| 121 | 3 | if (file == null) |
| 122 | |
{ |
| 123 | 1 | throw new IllegalArgumentException("file must not be null"); |
| 124 | |
} |
| 125 | |
try |
| 126 | |
{ |
| 127 | 2 | XMLStreamWriter writer = outputFactory.createXMLStreamWriter(new FileWriter(file)); |
| 128 | 2 | write(graph, writer); |
| 129 | |
} |
| 130 | 0 | catch (XMLStreamException e) |
| 131 | |
{ |
| 132 | |
|
| 133 | 0 | throw new IOException(e.getMessage()); |
| 134 | 2 | } |
| 135 | 2 | } |
| 136 | |
|
| 137 | |
|
| 138 | |
public void write(final Graph<N, E> graph, final OutputStream outputStream) |
| 139 | |
throws IOException |
| 140 | |
{ |
| 141 | 0 | if (graph == null) |
| 142 | |
{ |
| 143 | 0 | throw new IllegalArgumentException("graph must not be null"); |
| 144 | |
} |
| 145 | 0 | if (outputStream == null) |
| 146 | |
{ |
| 147 | 0 | throw new IllegalArgumentException("outputStream must not be null"); |
| 148 | |
} |
| 149 | |
try |
| 150 | |
{ |
| 151 | 0 | XMLStreamWriter writer = outputFactory.createXMLStreamWriter(outputStream); |
| 152 | 0 | write(graph, writer); |
| 153 | |
} |
| 154 | 0 | catch (XMLStreamException e) |
| 155 | |
{ |
| 156 | |
|
| 157 | 0 | throw new IOException(e.getMessage()); |
| 158 | 0 | } |
| 159 | 0 | } |
| 160 | |
|
| 161 | |
|
| 162 | |
|
| 163 | |
|
| 164 | |
|
| 165 | |
|
| 166 | |
|
| 167 | |
|
| 168 | |
|
| 169 | |
private void write(final Graph<N, E> graph, final XMLStreamWriter writer) |
| 170 | |
throws IOException, XMLStreamException |
| 171 | |
{ |
| 172 | 2 | writer.writeStartDocument("1.0"); |
| 173 | |
|
| 174 | 2 | writer.writeStartElement("graph"); |
| 175 | 2 | writer.writeDefaultNamespace("http://www.cs.rpi.edu/XGMML"); |
| 176 | 2 | writer.writeNamespace("dc", "http://purl.org/dc/elements/1.1/"); |
| 177 | 2 | writer.writeNamespace("xlink", "http://www.w3.org/1999/xlink"); |
| 178 | 2 | writer.writeNamespace("rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#"); |
| 179 | |
|
| 180 | 2 | writer.writeAttribute("label", "graph0"); |
| 181 | 2 | writer.writeAttribute("directed", "1"); |
| 182 | |
|
| 183 | 2 | int n = 0; |
| 184 | 2 | Map<Node<N, E>, String> nodeIds = new HashMap<Node<N, E>, String>(graph.nodeCount()); |
| 185 | 2 | for (Node<N, E> node : graph.nodes()) |
| 186 | |
{ |
| 187 | 5 | String nodeId = nodeIds.get(node); |
| 188 | 5 | if (nodeId == null) |
| 189 | |
{ |
| 190 | 5 | nodeId = "n" + n; |
| 191 | 5 | n++; |
| 192 | 5 | nodeIds.put(node, nodeId); |
| 193 | |
} |
| 194 | 5 | writer.writeStartElement("node"); |
| 195 | 5 | writer.writeAttribute("id", nodeId); |
| 196 | 5 | writer.writeAttribute("label", nodeId); |
| 197 | 5 | for (Map.Entry<String, ValueHandler<N>> entry : nodeValueHandlers.entrySet()) |
| 198 | |
{ |
| 199 | 5 | writer.writeStartElement("att"); |
| 200 | 5 | writer.writeAttribute("name", entry.getKey()); |
| 201 | 5 | ValueHandler<N> nodeValueHandler = entry.getValue(); |
| 202 | 5 | writer.writeAttribute("type", nodeValueHandler.getType()); |
| 203 | 5 | writer.writeAttribute("value", nodeValueHandler.getValue(node.getValue())); |
| 204 | 5 | writer.writeEndElement(); |
| 205 | 5 | } |
| 206 | 5 | writer.writeEndElement(); |
| 207 | 5 | } |
| 208 | 2 | int e = 0; |
| 209 | 2 | for (Edge<N, E> edge : graph.edges()) |
| 210 | |
{ |
| 211 | 20 | String edgeId = "e" + e; |
| 212 | 20 | e++; |
| 213 | 20 | writer.writeStartElement("edge"); |
| 214 | 20 | writer.writeAttribute("label", edgeId); |
| 215 | 20 | writer.writeAttribute("source", nodeIds.get(edge.source())); |
| 216 | 20 | writer.writeAttribute("target", nodeIds.get(edge.target())); |
| 217 | 20 | for (Map.Entry<String, ValueHandler<E>> entry : edgeValueHandlers.entrySet()) |
| 218 | |
{ |
| 219 | 20 | writer.writeStartElement("att"); |
| 220 | 20 | writer.writeAttribute("name", entry.getKey()); |
| 221 | 20 | ValueHandler<E> edgeValueHandler = entry.getValue(); |
| 222 | 20 | writer.writeAttribute("type", edgeValueHandler.getType()); |
| 223 | 20 | writer.writeAttribute("value", edgeValueHandler.getValue(edge.getValue())); |
| 224 | 20 | writer.writeEndElement(); |
| 225 | 20 | } |
| 226 | 20 | writer.writeEndElement(); |
| 227 | 20 | } |
| 228 | 2 | writer.writeEndElement(); |
| 229 | 2 | writer.close(); |
| 230 | 2 | } |
| 231 | |
|
| 232 | |
|
| 233 | |
|
| 234 | |
|
| 235 | |
|
| 236 | |
|
| 237 | |
public interface ValueHandler<V> |
| 238 | |
{ |
| 239 | |
|
| 240 | |
|
| 241 | |
|
| 242 | |
|
| 243 | |
|
| 244 | |
|
| 245 | |
|
| 246 | |
String getType(); |
| 247 | |
|
| 248 | |
|
| 249 | |
|
| 250 | |
|
| 251 | |
|
| 252 | |
|
| 253 | |
|
| 254 | |
String getValue(V value); |
| 255 | |
} |
| 256 | |
} |