|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Text; |
| 5 | +using System.Threading.Tasks; |
| 6 | +using System.Xml; |
| 7 | +using System.Xml.XPath; |
| 8 | +using System.IO; |
| 9 | +using System.Diagnostics; |
| 10 | +using Jokedst.GetOpt; |
| 11 | +using XmlDiffLib; |
| 12 | + |
| 13 | +namespace XmlDiffLibConsole |
| 14 | +{ |
| 15 | + class Program |
| 16 | + { |
| 17 | + public enum UsageErrors { InvalidNumberArguments, InvalidArguments } |
| 18 | + public static void ShowUsage(UsageErrors error) |
| 19 | + { |
| 20 | + switch (error) |
| 21 | + { |
| 22 | + case UsageErrors.InvalidNumberArguments: |
| 23 | + Console.WriteLine("ERROR: Invalid number of arguments."); |
| 24 | + break; |
| 25 | + case UsageErrors.InvalidArguments: |
| 26 | + Console.WriteLine("ERROR: Invalid arguments."); |
| 27 | + break; |
| 28 | + default: |
| 29 | + break; |
| 30 | + } |
| 31 | + Console.WriteLine("USAGE: xmldiff <srcfile.xml> <cmpfile.xml> [-o <filename>] [--csv]"); |
| 32 | + } |
| 33 | + |
| 34 | + |
| 35 | + static void Main(string[] args) |
| 36 | + { |
| 37 | + XmlDiffOptions xDiffOptions = new XmlDiffOptions(); |
| 38 | + string fromFile = string.Empty; |
| 39 | + string toFile = string.Empty; |
| 40 | + string outFile = string.Empty; |
| 41 | + bool toCsv = false; |
| 42 | + |
| 43 | + var options = new GetOpt("XmlDiff: Tool for finding the difference between two Xml files.", |
| 44 | + new[] |
| 45 | + { |
| 46 | + new CommandLineOption('o', "outfile", "Output file to write to. Files with csv extension open in Excel.", |
| 47 | + ParameterType.String, o => outFile = (string)o), |
| 48 | + new CommandLineOption('\0', "csv", "Creates a diff csv file and opens in Excel. If no outfile is specified writes output to xmldiff.csv. Default=False", |
| 49 | + ParameterType.None, none => toCsv = true), |
| 50 | + new CommandLineOption('m', "nomatch", "Don't match text node value types (i.e. 0.00 != 0). Default=False", |
| 51 | + ParameterType.None, none => xDiffOptions.MatchValueTypes = false), |
| 52 | + new CommandLineOption('\0', "ignoretypes", "If -m or --nomatch is NOT chosen, then this chooses which match types to ignore. " + |
| 53 | + "Possible values are (string, integer, double, datetime). Multiple values may be separated by '|'", ParameterType.String, |
| 54 | + (types) => |
| 55 | + { |
| 56 | + string[] values = ((string)types).Split('|'); |
| 57 | + foreach (string value in values) |
| 58 | + { |
| 59 | + switch (value.ToLower().Trim()) |
| 60 | + { |
| 61 | + case "string": |
| 62 | + xDiffOptions.IgnoreTextTypes.Add(XmlDiffOptions.IgnoreTextNodeOptions.XmlString); |
| 63 | + break; |
| 64 | + case "integer": |
| 65 | + xDiffOptions.IgnoreTextTypes.Add(XmlDiffOptions.IgnoreTextNodeOptions.XmlInteger); |
| 66 | + break; |
| 67 | + case "double": |
| 68 | + xDiffOptions.IgnoreTextTypes.Add(XmlDiffOptions.IgnoreTextNodeOptions.XmlDouble); |
| 69 | + break; |
| 70 | + case "datetime": |
| 71 | + xDiffOptions.IgnoreTextTypes.Add(XmlDiffOptions.IgnoreTextNodeOptions.XmlDateTime); |
| 72 | + break; |
| 73 | + default: |
| 74 | + throw new CommandLineException("Error parsing enumerated values.", "ignoretypes"); |
| 75 | + } |
| 76 | + } |
| 77 | + }), |
| 78 | + new CommandLineOption('d', "nodetail", "Will not display details of matching nodes. Default=False", ParameterType.None, none => xDiffOptions.MatchDescendants = false), |
| 79 | + new CommandLineOption('c', "case", "Case Sensitive. Default=False", ParameterType.None, none => xDiffOptions.IgnoreCase = false), |
| 80 | + new CommandLineOption('\0', "2way", "Does a comparison in both directions. Default=False", ParameterType.None, none => xDiffOptions.TwoWayMatch = true), |
| 81 | + new CommandLineOption("Required. FromFile", ParameterType.String, file => fromFile = (string)file), |
| 82 | + new CommandLineOption("Required. ToFile", ParameterType.String, file => toFile = (string)file) |
| 83 | + }); |
| 84 | + |
| 85 | + try |
| 86 | + { |
| 87 | + options.ParseOptions(args); |
| 88 | + } |
| 89 | + catch (CommandLineException ex) |
| 90 | + { |
| 91 | + Console.WriteLine("Error: {0}", ex.Message); |
| 92 | + return; |
| 93 | + } |
| 94 | + |
| 95 | + StreamWriter sw; |
| 96 | + XmlDiff xdiff; |
| 97 | + try |
| 98 | + { |
| 99 | + xdiff = new XmlDiff(File.ReadAllText(fromFile), File.ReadAllText(toFile)); |
| 100 | + xdiff.CompareDocuments(xDiffOptions); |
| 101 | + } |
| 102 | + catch (Exception ex) |
| 103 | + { |
| 104 | + Console.WriteLine("Error: {0}", ex.Message); |
| 105 | + return; |
| 106 | + } |
| 107 | + |
| 108 | + if (toCsv) |
| 109 | + { |
| 110 | + try |
| 111 | + { |
| 112 | + string file; |
| 113 | + if (!string.IsNullOrEmpty(outFile)) |
| 114 | + file = outFile; |
| 115 | + else |
| 116 | + file = "xmldiff.csv"; |
| 117 | + sw = new StreamWriter(file); |
| 118 | + sw.Write((toCsv) ? xdiff.ToCSVString() : xdiff.ToJsonString()); |
| 119 | + sw.Close(); |
| 120 | + Process.Start(file); |
| 121 | + } |
| 122 | + catch (IOException ex) |
| 123 | + { |
| 124 | + Console.WriteLine("Error: {0}", ex.Message); |
| 125 | + return; |
| 126 | + } |
| 127 | + } |
| 128 | + else |
| 129 | + { |
| 130 | + if (string.IsNullOrEmpty(outFile)) |
| 131 | + Console.WriteLine(xdiff.ToJsonString()); |
| 132 | + else |
| 133 | + { |
| 134 | + try |
| 135 | + { |
| 136 | + sw = new StreamWriter(outFile); |
| 137 | + sw.WriteLine(xdiff.ToJsonString()); |
| 138 | + sw.Close(); |
| 139 | + } |
| 140 | + catch (IOException ex) |
| 141 | + { |
| 142 | + Console.WriteLine("Error: {0}", ex.Message); |
| 143 | + return; |
| 144 | + } |
| 145 | + } |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + } |
| 150 | + |
| 151 | +} |
0 commit comments