Skip to content
PdfFactory.java 3.69 KiB
Newer Older
Carlos Galindo's avatar
Carlos Galindo committed
/*
 * EDG, a library to generate and slice Expression Dependence Graphs.
 * Copyright (c) 2021. David Insa, Sergio Pérez, Josep Silva, Salvador Tamarit.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

Carlos Galindo's avatar
Carlos Galindo committed
package edg;

import java.io.File;
import java.io.IOException;
Carlos Galindo's avatar
Carlos Galindo committed
import java.util.List;
import java.util.Map;
import java.util.Set;
Carlos Galindo's avatar
Carlos Galindo committed

import edg.config.Config;
import edg.graph.EDG;
import edg.graph.Edge;
Carlos Galindo's avatar
Carlos Galindo committed
import edg.graph.Node;
import misc.Misc;
import misc.util.Flusher;

public class PdfFactory
{
	public static void createPdf(File outputFile, EDG edg)
	{
		PdfFactory.createPdf(outputFile, edg, null, null, null);
	}
	public static void createPdf(File outputFile, EDG edg, Map<Edge.Type, Boolean> edgeFlags)
Carlos Galindo's avatar
Carlos Galindo committed
	{
		PdfFactory.createPdf(outputFile, edg, null, null, edgeFlags);
	}
	public static void createPdf(File outputFile, EDG edg, Node slicingCriterion, Set<Node> slice)
Carlos Galindo's avatar
Carlos Galindo committed
	{
		PdfFactory.createPdf(outputFile, edg, slicingCriterion, slice, null);
	}
	public static void createPdf(File outputFile, EDG edg, Node slicingCriterion, Set<Node> slice, Map<Edge.Type, Boolean> edgeFlags)
Carlos Galindo's avatar
Carlos Galindo committed
	{
		final File dotOutputFile;
		try {
			dotOutputFile = File.createTempFile("PdfFactory-graph", ".dot");
		} catch (IOException e) {
			e.printStackTrace();
			return;
		}
Carlos Galindo's avatar
Carlos Galindo committed

		DotFactory.createDot(dotOutputFile, edg, slicingCriterion, slice, edgeFlags);
		PdfFactory.createPdf(outputFile, dotOutputFile);
		dotOutputFile.delete();
Carlos Galindo's avatar
Carlos Galindo committed
	}
Carlos Galindo's avatar
Carlos Galindo committed
	public static void createPdf(File outputFile, File dotFile)
	{
		try
		{
			final String dotPath = dotFile.getAbsolutePath();
			final String outputPath = outputFile.getAbsolutePath();
			final Process process = new ProcessBuilder()
					.command("dot", "-Tpdf", "-o", outputPath, dotPath)
					.inheritIO()
					.start();
Carlos Galindo's avatar
Carlos Galindo committed

			int result = process.waitFor();
			if (result != 0)
				throw new Exception("Error generating pdf from dot file, exit code: " + result);
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}

	public static void createSvg(File outputFile, EDG edg, Node slicingCriterion, Set<Node> slice)
	{
		PdfFactory.createSvg(outputFile, edg, slicingCriterion, slice, null);
	}

	public static void createSvg(File outputFile, EDG edg, Node slicingCriterion, Set<Node> slice, Map<Edge.Type, Boolean> edgeFlags)
	{
		final File dotOutputFile;
		try {
			dotOutputFile = File.createTempFile("PdfFactory-graph", ".dot");
		} catch (IOException e) {
			e.printStackTrace();
			return;
		}

		DotFactory.createDot(dotOutputFile, edg, slicingCriterion, slice, edgeFlags);
		PdfFactory.createSvg(outputFile, dotOutputFile);
		dotOutputFile.delete();
	}

	public static void createSvg(File outputFile, File dotFile)
	{
		try
		{
			final String dotPath = dotFile.getAbsolutePath();
			final String outputPath = outputFile.getAbsolutePath();
			final Process process = new ProcessBuilder()
					.command("dot", "-Tsvg", "-o", outputPath, dotPath)
					.inheritIO()
					.start();

			int result = process.waitFor();
			if (result != 0)
				throw new Exception("Error generating pdf from dot file, exit code: " + result);
Carlos Galindo's avatar
Carlos Galindo committed
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}

	private PdfFactory()
	{
Carlos Galindo's avatar
Carlos Galindo committed
	}
}