All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.anarres.graphviz.builder.GraphVizUtils Maven / Gradle / Ivy

There is a newer version: 1.0.12
Show newest version
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package org.anarres.graphviz.builder;

import com.google.common.base.Preconditions;
import com.google.common.escape.Escaper;
import com.google.common.escape.Escapers;
import java.io.File;
import java.io.IOException;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;

/**
 * Easy utilities for emitting GraphViz graphs.
 *
 * @author shevek
 */
public class GraphVizUtils {

    public static final Escaper ESCAPE_HTML = Escapers.builder()
            .addEscape('\\', "\\\\")
            .addEscape('\"', "\\\"")
            .addEscape('{', "\\{")
            .addEscape('}', "\\}")
            .addEscape('<', "<")
            .addEscape('>', ">")
            .addEscape('&', "&")
            .addEscape('\n', "
") .addEscape('\r', "") .build(); /* pp */ static int hashCode(@CheckForNull Object o) { return o == null ? 0 : o.hashCode(); } /* pp */ static boolean equals(@CheckForNull Object a, @CheckForNull Object b) { if (a == b) return true; if (a == null) return false; return a.equals(b); } /** * This supports a one-liner: GraphVizUtils.toGraphVizGraph(object).writeTo(new File(...)). * * @param object The object to graph. * @return A newly constructed GraphVizGraph. */ @Nonnull public static GraphVizGraph toGraphVizGraph(@Nonnull GraphVizable object) { Preconditions.checkNotNull(object, "GraphVizable object was null."); GraphVizGraph graph = new GraphVizGraph(); object.toGraphViz(graph); return graph; } /** * Equivalent to {@link #toGraphVizGraph(GraphVizable) toGraphVizGraph}(object).{@link GraphVizGraph#writeTo(java.io.File) writeTo}(file). * * @param file The file to which to write the graph. * @param object The object to convert. * @throws IOException If an I/O error occurs. */ public static void toGraphVizFile(@Nonnull File file, @Nonnull GraphVizable object) throws IOException { toGraphVizGraph(object).writeTo(file); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy