Coverage Report - org.dishevelled.graph.io.graphml.GraphMLWriter
 
Classes in this File Line Coverage Branch Coverage Complexity
GraphMLWriter
85%
69/81
77%
17/22
3.714
GraphMLWriter$ValueHandler
N/A
N/A
3.714
 
 1  
 /*
 2  
 
 3  
     dsh-graph-io  Directed graph readers and writers.
 4  
     Copyright (c) 2008-2012 held jointly by the individual authors.
 5  
 
 6  
     This library is free software; you can redistribute it and/or modify it
 7  
     under the terms of the GNU Lesser General Public License as published
 8  
     by the Free Software Foundation; either version 3 of the License, or (at
 9  
     your option) any later version.
 10  
 
 11  
     This library is distributed in the hope that it will be useful, but WITHOUT
 12  
     ANY WARRANTY; with out even the implied warranty of MERCHANTABILITY or
 13  
     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
 14  
     License for more details.
 15  
 
 16  
     You should have received a copy of the GNU Lesser General Public License
 17  
     along with this library;  if not, write to the Free Software Foundation,
 18  
     Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA.
 19  
 
 20  
     > http://www.fsf.org/licensing/licenses/lgpl.html
 21  
     > http://www.opensource.org/licenses/lgpl-license.php
 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  
  * GraphML writer.
 46  
  *
 47  
  * @param <N> node value type
 48  
  * @param <E> edge value type
 49  
  * @author  Michael Heuer
 50  
  * @version $Revision$ $Date$
 51  
  */
 52  
 public final class GraphMLWriter<N, E>
 53  
     implements GraphWriter<N, E>
 54  
 {
 55  
     /** XML output factory. */
 56  
     private final XMLOutputFactory outputFactory;
 57  
 
 58  
     /** Node value handler. */
 59  
     private ValueHandler<N> nodeValueHandler;
 60  
 
 61  
     /** Edge value handler. */
 62  
     private ValueHandler<E> edgeValueHandler;
 63  
 
 64  
 
 65  
     /**
 66  
      * Create a new GraphML writer.
 67  
      */
 68  
     public GraphMLWriter()
 69  7
     {
 70  7
         outputFactory = XMLOutputFactory.newInstance();
 71  7
     }
 72  
 
 73  
 
 74  
     /**
 75  
      * Set the node value handler for this GraphML writer to <code>nodeValueHandler</code>.
 76  
      *
 77  
      * @param nodeValueHandler node value handler
 78  
      */
 79  
     public void setNodeValueHandler(final ValueHandler<N> nodeValueHandler)
 80  
     {
 81  2
         this.nodeValueHandler = nodeValueHandler;
 82  2
     }
 83  
 
 84  
     /**
 85  
      * Set the edge value handler for this GraphML writer to <code>edgeValueHandler</code>.
 86  
      *
 87  
      * @param edgeValueHandler edge value handler
 88  
      */
 89  
     public void setEdgeValueHandler(final ValueHandler<E> edgeValueHandler)
 90  
     {
 91  2
         this.edgeValueHandler = edgeValueHandler;
 92  2
     }
 93  
 
 94  
     /** {@inheritDoc} */
 95  
     public void write(final Graph<N, E> graph, final File file)
 96  
         throws IOException
 97  
     {
 98  7
         if (graph == null)
 99  
         {
 100  2
             throw new IllegalArgumentException("graph must not be null");
 101  
         }
 102  5
         if (file == null)
 103  
         {
 104  1
             throw new IllegalArgumentException("file must not be null");
 105  
         }
 106  
         try
 107  
         {
 108  4
             XMLStreamWriter writer = outputFactory.createXMLStreamWriter(new FileWriter(file));
 109  4
             write(graph, writer);
 110  
         }
 111  0
         catch (XMLStreamException e)
 112  
         {
 113  
             //throw new IOException(e); jdk1.6+
 114  0
             throw new IOException(e.getMessage());
 115  4
         }
 116  4
     }
 117  
 
 118  
     /** {@inheritDoc} */
 119  
     public void write(final Graph<N, E> graph, final OutputStream outputStream)
 120  
         throws IOException
 121  
     {
 122  0
         if (graph == null)
 123  
         {
 124  0
             throw new IllegalArgumentException("graph must not be null");
 125  
         }
 126  0
         if (outputStream == null)
 127  
         {
 128  0
             throw new IllegalArgumentException("outputStream must not be null");
 129  
         }
 130  
         try
 131  
         {
 132  0
             XMLStreamWriter writer = outputFactory.createXMLStreamWriter(outputStream);
 133  0
             write(graph, writer);
 134  
         }
 135  0
         catch (XMLStreamException e)
 136  
         {
 137  
             //throw new IOException(e);
 138  0
             throw new IOException(e.getMessage());
 139  0
         }
 140  0
     }
 141  
 
 142  
     /**
 143  
      * Write the specified graph to the specified XML stream writer.
 144  
      *
 145  
      * @param graph graph to write
 146  
      * @param writer XML stream writer to write to
 147  
      * @throws IOException if an error occurs
 148  
      * @throws XMLStreamException if an error occurs
 149  
      */
 150  
     private void write(final Graph<N, E> graph, final XMLStreamWriter writer)
 151  
         throws IOException, XMLStreamException
 152  
     {
 153  4
         writer.writeStartDocument("1.0");
 154  
         //writer.writeStartDocument("UTF-8", "1.0");
 155  4
         writer.writeStartElement("graphml");
 156  4
         writer.writeDefaultNamespace("http://graphml.graphdrawing.org/xmlns");
 157  4
         writer.writeNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
 158  4
         writer.writeAttribute("xsi:schemaLocation", "http://graphml.graphdrawing.org/xmlns http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd");
 159  4
         if (nodeValueHandler != null)
 160  
         {
 161  1
             writer.writeStartElement("key");
 162  1
             writer.writeAttribute("id", "k0");
 163  1
             writer.writeAttribute("for", "node");
 164  1
             writer.writeEndElement(); // </key>
 165  
         }
 166  4
         if (edgeValueHandler != null)
 167  
         {
 168  1
             writer.writeStartElement("key");
 169  1
             writer.writeAttribute("id", "k1");
 170  1
             writer.writeAttribute("for", "edge");
 171  1
             writer.writeEndElement(); // </key>
 172  
         }
 173  4
         writer.writeStartElement("graph");
 174  4
         writer.writeAttribute("id", "G");
 175  4
         writer.writeAttribute("edgedefault", "directed");
 176  4
         int n = 0;
 177  4
         Map<Node<N, E>, String> nodeIds = new HashMap<Node<N, E>, String>(graph.nodeCount());
 178  4
         for (Node<N, E> node : graph.nodes())
 179  
         {
 180  15
             String nodeId = nodeIds.get(node);
 181  15
             if (nodeId == null)
 182  
             {
 183  15
                 nodeId = "n" + n;
 184  15
                 n++;
 185  15
                 nodeIds.put(node, nodeId);
 186  
             }
 187  15
             writer.writeStartElement("node");
 188  15
             writer.writeAttribute("id", nodeId);
 189  15
             if (nodeValueHandler != null)
 190  
             {
 191  5
                 writer.writeStartElement("data");
 192  5
                 writer.writeAttribute("key", "k0");
 193  5
                 nodeValueHandler.write(node.getValue(), writer);
 194  5
                 writer.writeEndElement(); // </data>
 195  
             }
 196  15
             writer.writeEndElement(); // </node>
 197  15
         }
 198  4
         int e = 0;
 199  4
         for (Edge<N, E> edge : graph.edges())
 200  
         {
 201  60
             String edgeId = "e" + e;
 202  60
             e++;
 203  60
             writer.writeStartElement("edge");
 204  60
             writer.writeAttribute("id", edgeId);
 205  60
             writer.writeAttribute("source", nodeIds.get(edge.source()));
 206  60
             writer.writeAttribute("target", nodeIds.get(edge.target()));
 207  60
             if (edgeValueHandler != null)
 208  
             {
 209  20
                 writer.writeStartElement("data");
 210  20
                 writer.writeAttribute("key", "k1");
 211  20
                 edgeValueHandler.write(edge.getValue(), writer);
 212  20
                 writer.writeEndElement(); // </data>
 213  
             }
 214  60
             writer.writeEndElement(); // </edge>
 215  60
         }
 216  4
         writer.writeEndElement(); // </graph>
 217  4
         writer.writeEndElement(); // </graphml>
 218  4
         writer.close();
 219  4
     }
 220  
 
 221  
     /**
 222  
      * Value handler.
 223  
      *
 224  
      * @param <V> value type
 225  
      */
 226  
     public interface ValueHandler<V>
 227  
     {
 228  
 
 229  
         /**
 230  
          * Write the specified value to the specified XML stream writer.
 231  
          *
 232  
          * @param value value to write
 233  
          * @param writer XML stream writer to write to, must not be null
 234  
          * @throws IOException if an error occurs
 235  
          * @throws XMLStreamException if an error occurs
 236  
          */
 237  
         void write(V value, XMLStreamWriter writer) throws IOException, XMLStreamException;
 238  
     }
 239  
 }