/* * 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 . */ package edg; import java.io.File; import java.io.IOException; import java.util.List; import java.util.Map; import java.util.Set; import edg.config.Config; import edg.graph.EDG; import edg.graph.Edge; 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 edgeFlags) { PdfFactory.createPdf(outputFile, edg, null, null, edgeFlags); } public static void createPdf(File outputFile, EDG edg, Node slicingCriterion, Set slice) { PdfFactory.createPdf(outputFile, edg, slicingCriterion, slice, null); } public static void createPdf(File outputFile, EDG edg, Node slicingCriterion, Set slice, Map 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.createPdf(outputFile, dotOutputFile); dotOutputFile.delete(); } 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(); 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 slice) { PdfFactory.createSvg(outputFile, edg, slicingCriterion, slice, null); } public static void createSvg(File outputFile, EDG edg, Node slicingCriterion, Set slice, Map 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); } catch (Exception e) { e.printStackTrace(); } } private PdfFactory() { } }