[12762] | 1 | // -------------------------------------------------------------------------------------------------------------------- |
---|
| 2 | // <copyright file="GraphGeneration.cs" company="Jamie Dixon Ltd"> |
---|
| 3 | // Jamie Dixon |
---|
| 4 | // </copyright> |
---|
| 5 | // <summary> |
---|
| 6 | // Defines the GraphGeneration type. |
---|
| 7 | // </summary> |
---|
| 8 | // -------------------------------------------------------------------------------------------------------------------- |
---|
| 9 | |
---|
| 10 | namespace GraphVizWrapper |
---|
| 11 | { |
---|
| 12 | using System; |
---|
| 13 | using System.Collections.Generic; |
---|
| 14 | using System.Configuration; |
---|
| 15 | using System.IO; |
---|
| 16 | using System.Reflection; |
---|
| 17 | |
---|
| 18 | using Commands; |
---|
| 19 | using Queries; |
---|
| 20 | |
---|
| 21 | /// <summary> |
---|
| 22 | /// The main entry class for the wrapper. |
---|
| 23 | /// </summary> |
---|
| 24 | public class GraphGeneration : IGraphGeneration |
---|
| 25 | { |
---|
| 26 | private const string ProcessFolder = "GraphViz"; |
---|
| 27 | private const string ConfigFile = "config6"; |
---|
| 28 | |
---|
| 29 | private readonly IGetStartProcessQuery startProcessQuery; |
---|
| 30 | private readonly IGetProcessStartInfoQuery getProcessStartInfoQuery; |
---|
| 31 | private readonly IRegisterLayoutPluginCommand registerLayoutPlugincommand; |
---|
| 32 | private Enums.RenderingEngine renderingEngine; |
---|
| 33 | private String graphvizPath = null; |
---|
| 34 | |
---|
| 35 | public GraphGeneration(IGetStartProcessQuery startProcessQuery, IGetProcessStartInfoQuery getProcessStartInfoQuery, IRegisterLayoutPluginCommand registerLayoutPlugincommand) |
---|
| 36 | { |
---|
| 37 | this.startProcessQuery = startProcessQuery; |
---|
| 38 | this.getProcessStartInfoQuery = getProcessStartInfoQuery; |
---|
| 39 | this.registerLayoutPlugincommand = registerLayoutPlugincommand; |
---|
| 40 | |
---|
| 41 | this.graphvizPath = ConfigurationManager.AppSettings["graphVizLocation"]; |
---|
| 42 | } |
---|
| 43 | |
---|
| 44 | #region Properties |
---|
| 45 | |
---|
| 46 | public String GraphvizPath |
---|
| 47 | { |
---|
| 48 | get { return graphvizPath ?? AssemblyDirectory + "/" + ProcessFolder; } |
---|
| 49 | set |
---|
| 50 | { |
---|
| 51 | if (value != null && value.Trim().Length > 0) |
---|
| 52 | { |
---|
| 53 | String path = value.Replace("\\", "/"); |
---|
| 54 | graphvizPath = path.EndsWith("/") ? path.Substring(0, path.LastIndexOf('/')) : path; |
---|
| 55 | } |
---|
| 56 | else |
---|
| 57 | { |
---|
| 58 | graphvizPath = null; |
---|
| 59 | } |
---|
| 60 | } |
---|
| 61 | } |
---|
| 62 | |
---|
| 63 | public Enums.RenderingEngine RenderingEngine |
---|
| 64 | { |
---|
| 65 | get { return this.renderingEngine; } |
---|
| 66 | set { this.renderingEngine = value; } |
---|
| 67 | } |
---|
| 68 | |
---|
| 69 | private string ConfigLocation |
---|
| 70 | { |
---|
| 71 | get |
---|
| 72 | { |
---|
| 73 | return GraphvizPath + "/" + ConfigFile; |
---|
| 74 | } |
---|
| 75 | } |
---|
| 76 | |
---|
| 77 | private bool ConfigExists |
---|
| 78 | { |
---|
| 79 | get |
---|
| 80 | { |
---|
| 81 | return File.Exists(ConfigLocation); |
---|
| 82 | } |
---|
| 83 | } |
---|
| 84 | |
---|
| 85 | private static string AssemblyDirectory |
---|
| 86 | { |
---|
| 87 | get |
---|
| 88 | { |
---|
| 89 | var uriBuilder = new UriBuilder(Assembly.GetExecutingAssembly().CodeBase); |
---|
| 90 | string path = Uri.UnescapeDataString(uriBuilder.Path); |
---|
| 91 | return path.Substring(0, path.LastIndexOf('/')); |
---|
| 92 | } |
---|
| 93 | } |
---|
| 94 | |
---|
| 95 | private string FilePath |
---|
| 96 | { |
---|
| 97 | get { return GraphvizPath + "/" + this.GetRenderingEngine(this.renderingEngine) + ".exe"; } |
---|
| 98 | } |
---|
| 99 | |
---|
| 100 | #endregion |
---|
| 101 | |
---|
| 102 | /// <summary> |
---|
| 103 | /// Generates a graph based on the dot file passed in. |
---|
| 104 | /// </summary> |
---|
| 105 | /// <param name="dotFile"> |
---|
| 106 | /// A string representation of a dot file. |
---|
| 107 | /// </param> |
---|
| 108 | /// <param name="returnType"> |
---|
| 109 | /// The type of file to be returned. |
---|
| 110 | /// </param> |
---|
| 111 | /// <returns> |
---|
| 112 | /// a byte array. |
---|
| 113 | /// </returns> |
---|
| 114 | public byte[] GenerateGraph(string dotFile, Enums.GraphReturnType returnType) |
---|
| 115 | { |
---|
| 116 | |
---|
| 117 | byte[] output; |
---|
| 118 | |
---|
| 119 | if (!ConfigExists) |
---|
| 120 | { |
---|
| 121 | this.registerLayoutPlugincommand.Invoke(FilePath, this.RenderingEngine); |
---|
| 122 | } |
---|
| 123 | |
---|
| 124 | string fileType = this.GetReturnType(returnType); |
---|
| 125 | |
---|
| 126 | var processStartInfo = this.GetProcessStartInfo(fileType); |
---|
| 127 | |
---|
| 128 | using (var process = this.startProcessQuery.Invoke(processStartInfo)) |
---|
| 129 | { |
---|
| 130 | process.BeginErrorReadLine(); |
---|
| 131 | using (var stdIn = process.StandardInput) |
---|
| 132 | { |
---|
| 133 | stdIn.WriteLine(dotFile); |
---|
| 134 | } |
---|
| 135 | using (var stdOut = process.StandardOutput) |
---|
| 136 | { |
---|
| 137 | var baseStream = stdOut.BaseStream; |
---|
| 138 | output = this.ReadFully(baseStream); |
---|
| 139 | } |
---|
| 140 | } |
---|
| 141 | |
---|
| 142 | return output; |
---|
| 143 | } |
---|
| 144 | |
---|
| 145 | #region Private Methods |
---|
| 146 | |
---|
| 147 | private System.Diagnostics.ProcessStartInfo GetProcessStartInfo(string returnType) |
---|
| 148 | { |
---|
| 149 | return this.getProcessStartInfoQuery.Invoke(new ProcessStartInfoWrapper |
---|
| 150 | { |
---|
| 151 | FileName = this.FilePath, |
---|
| 152 | RedirectStandardInput = true, |
---|
| 153 | RedirectStandardOutput = true, |
---|
| 154 | RedirectStandardError = true, |
---|
| 155 | UseShellExecute = false, |
---|
| 156 | Arguments = "-v -o -T" + returnType, |
---|
| 157 | CreateNoWindow = true |
---|
| 158 | }); |
---|
| 159 | } |
---|
| 160 | |
---|
| 161 | private byte[] ReadFully(Stream input) |
---|
| 162 | { |
---|
| 163 | using (var ms = new MemoryStream()) |
---|
| 164 | { |
---|
| 165 | input.CopyTo(ms); |
---|
| 166 | return ms.ToArray(); |
---|
| 167 | } |
---|
| 168 | } |
---|
| 169 | |
---|
| 170 | private string GetReturnType(Enums.GraphReturnType returnType) |
---|
| 171 | { |
---|
| 172 | var nameValues = new Dictionary<Enums.GraphReturnType, string> |
---|
| 173 | { |
---|
| 174 | { Enums.GraphReturnType.Png, "png" }, |
---|
| 175 | { Enums.GraphReturnType.Jpg, "jpg" }, |
---|
| 176 | { Enums.GraphReturnType.Pdf, "pdf" }, |
---|
| 177 | { Enums.GraphReturnType.Plain, "plain" }, |
---|
| 178 | { Enums.GraphReturnType.PlainExt, "plain-ext" }, |
---|
| 179 | { Enums.GraphReturnType.Svg, "svg" } |
---|
| 180 | |
---|
| 181 | }; |
---|
| 182 | return nameValues[returnType]; |
---|
| 183 | } |
---|
| 184 | |
---|
| 185 | private string GetRenderingEngine(Enums.RenderingEngine renderingType) |
---|
| 186 | { |
---|
| 187 | var nameValues = new Dictionary<Enums.RenderingEngine, string> |
---|
| 188 | { |
---|
| 189 | { Enums.RenderingEngine.Dot, "dot" }, |
---|
| 190 | { Enums.RenderingEngine.Neato, "neato" }, |
---|
| 191 | { Enums.RenderingEngine.Twopi, "twopi" }, |
---|
| 192 | { Enums.RenderingEngine.Circo, "circo" }, |
---|
| 193 | { Enums.RenderingEngine.Fdp, "fdp" }, |
---|
| 194 | { Enums.RenderingEngine.Sfdp, "sfdp" }, |
---|
| 195 | { Enums.RenderingEngine.Patchwork, "patchwork" }, |
---|
| 196 | { Enums.RenderingEngine.Osage, "osage" } |
---|
| 197 | }; |
---|
| 198 | return nameValues[renderingType]; |
---|
| 199 | } |
---|
| 200 | |
---|
| 201 | #endregion |
---|
| 202 | } |
---|
| 203 | } |
---|