using System; using System.Xml; using System.Xml.Linq; using CommandLine; using Sprache; using XNeedle.Parser; using XNeedle.Parser.Elements; using XNeedle.Transformation; namespace XNeedle { class Program { public class Options { [Value(0, MetaName = "xn", Required = true, HelpText = "XN transformation definition")] public string XnDefinition { get; set; } [Option('i', "input", HelpText = "Input XML file", Required = true)] public string Input { get; set; } [Option('o', "output", HelpText = "Output XML file", Required = true)] public string Output { get; set; } [Option('v', "verbose", Required = false, HelpText = "Set output to verbose messages.")] public bool Verbose { get; set; } } static void Main(string[] args) { CommandLine.Parser.Default.ParseArguments(args) .WithParsed(RunOptions) .WithNotParsed(HandleParseError); } static void RunOptions(Options opts) { var masterRule = (Rule)XnParser.RuleObject.Parse(File.ReadAllText(opts.XnDefinition)); var doc = XDocument.Load(opts.Input, LoadOptions.PreserveWhitespace); masterRule.Transform(doc); doc.Save(opts.Output, SaveOptions.DisableFormatting); Console.WriteLine($"Applied \"{opts.XnDefinition}\" to \"{opts.Input}\" and saved result to \"{opts.Output}\""); } static void HandleParseError(IEnumerable errs) {} } public static class DocumentExtensions { public static XmlDocument ToXmlDocument(this XDocument xDocument) { var xmlDocument = new XmlDocument(); using(var xmlReader = xDocument.CreateReader()) { xmlDocument.Load(xmlReader); } return xmlDocument; } public static XDocument ToXDocument(this XmlDocument xmlDocument) { using (var nodeReader = new XmlNodeReader(xmlDocument)) { nodeReader.MoveToContent(); return XDocument.Load(nodeReader); } } } }