org.jgrapht.ext.VisioExporter Maven / Gradle / Ivy
/* ==========================================
* JGraphT : a free Java graph-theory library
* ==========================================
*
* Project Info: http://jgrapht.sourceforge.net/
* Project Creator: Barak Naveh (http://sourceforge.net/users/barak_naveh)
*
* (C) Copyright 2003-2008, by Barak Naveh and Contributors.
*
* This program and the accompanying materials are dual-licensed under
* either
*
* (a) the terms of the GNU Lesser General Public License version 2.1
* as published by the Free Software Foundation, or (at your option) any
* later version.
*
* or (per the licensee's choosing)
*
* (b) the terms of the Eclipse Public License v1.0 as published by
* the Eclipse Foundation.
*/
/* ------------------
* VisioExporter.java
* ------------------
* (C) Copyright 2003-2008, by Avner Linder and Contributors.
*
* Original Author: Avner Linder
* Contributor(s): Barak Naveh
*
* $Id$
*
* Changes
* -------
* 27-May-2004 : Initial Version (AL);
*
*/
package org.jgrapht.ext;
import java.io.*;
import java.util.*;
import org.jgrapht.*;
/**
* Exports a graph to a csv format that can be imported into MS Visio.
*
* Tip: By default, the exported graph doesn't show link directions.
* To show link directions:
*
*
* - Select All (Ctrl-A)
* - Right Click the selected items
* - Format/Line...
* - Line ends: End: (choose an arrow)
*
*
* @author Avner Linder
*/
public class VisioExporter
{
private VertexNameProvider vertexNameProvider;
/**
* Creates a new VisioExporter object with the specified naming policy.
*
* @param vertexNameProvider the vertex name provider to be used for naming
* the Visio shapes.
*/
public VisioExporter(VertexNameProvider vertexNameProvider)
{
this.vertexNameProvider = vertexNameProvider;
}
/**
* Creates a new VisioExporter object.
*/
public VisioExporter()
{
this(new StringNameProvider());
}
/**
* Exports the specified graph into a Visio csv file format.
*
* @param output the print stream to which the graph to be exported.
* @param g the graph to be exported.
*/
public void export(OutputStream output, Graph g)
{
PrintStream out = new PrintStream(output);
for (Iterator i = g.vertexSet().iterator(); i.hasNext();) {
exportVertex(out, i.next());
}
for (Iterator i = g.edgeSet().iterator(); i.hasNext();) {
exportEdge(out, i.next(), g);
}
out.flush();
}
private void exportEdge(PrintStream out, E edge, Graph g)
{
String sourceName =
vertexNameProvider.getVertexName(g.getEdgeSource(edge));
String targetName =
vertexNameProvider.getVertexName(g.getEdgeTarget(edge));
out.print("Link,");
// create unique ShapeId for link
out.print(sourceName);
out.print("-->");
out.print(targetName);
// MasterName and Text fields left blank
out.print(",,,");
out.print(sourceName);
out.print(",");
out.print(targetName);
out.print("\n");
}
private void exportVertex(PrintStream out, V vertex)
{
String name = vertexNameProvider.getVertexName(vertex);
out.print("Shape,");
out.print(name);
out.print(",,"); // MasterName field left empty
out.print(name);
out.print("\n");
}
}
// End VisioExporter.java
© 2015 - 2025 Weber Informatics LLC | Privacy Policy