public static void AdjustParameters(Dictionary <string, string> Arguments) { foreach (string element in PathParameters) { if (Arguments.ContainsKey(element)) { int status = 0; status = FileTool.FileDirectoryOrNothing(Arguments[element]); if (status == (int)FileTool.filesystemObjectStatus.Directory) { Arguments[element] = Arguments[element] + @"\"; } } } if (Arguments.ContainsKey(@"LOG")) { if (File.Exists(Arguments[@"LOG"])) { File.Delete(Arguments[@"LOG"]); } Arguments[@"LOG"] = Arguments[@"LOG"].Trim(); } }
public static string ValidateParameters(Dictionary <string, string> Arguments) { string ErrorMessage = string.Empty; if (string.IsNullOrEmpty(ErrorMessage) && !Arguments.ContainsKey(@"INPUT")) { ErrorMessage = @"No input specified."; } if (string.IsNullOrEmpty(ErrorMessage)) { int i = 1; int actions = 0; while (i <= ActionParameters.Length) { if (Arguments.ContainsKey(ActionParameters[i - 1])) { actions++; } i++; } if (actions == 0) { ErrorMessage = @"Nothing for Xmltool to do."; } } // special validation of input if (string.IsNullOrEmpty(ErrorMessage)) { if (!Arguments[@"INPUT"].Contains(@"*")) { FileSystemParameters.Add(@"INPUT"); } } if (Arguments.ContainsKey(@"OUTPUT")) { string OutputPath = Path.GetDirectoryName(Arguments["OUTPUT"]); if (!Directory.Exists(OutputPath)) { ErrorMessage = @"Output path does not exist: " + @OutputPath; } } if (string.IsNullOrEmpty(ErrorMessage)) { foreach (string element in FileSystemParameters) { if (Arguments.ContainsKey(element)) { int status = 0; status = FileTool.FileDirectoryOrNothing(Arguments[element]); switch (status) { case (int)FileTool.filesystemObjectStatus.NonExistant: ErrorMessage = @Arguments[element] + @" is not a file or directory."; break; case (int)FileTool.filesystemObjectStatus.Directory: break; case (int)FileTool.filesystemObjectStatus.File: break; default: // shouldn't happen ErrorMessage = @"I am not sure what " + @element + @" is."; break; } } } } return(ErrorMessage); }
public static string run(Arguments args) { string ErrorMessage = string.Empty; System.IO.DirectoryInfo directory; System.IO.FileInfo[] filesList; StreamWriter outputfile = null; directory = new System.IO.DirectoryInfo(args.BasePath); filesList = directory.GetFiles(args.Glob); if (filesList.Length <= 0) { return(@"No files found by given input criteria."); } if (args.LogIt == true) { outputfile = new StreamWriter(args.Log, true); } int FilesWorkedOn = 0; foreach (FileInfo file in filesList) { string newxml = null; XmlDocument x = new XmlDocument(); x.Load(file.FullName); newxml = x.OuterXml; if (args.Xslt == true) { XslCompiledTransform myXslTrans = new XslCompiledTransform(); myXslTrans.Load(args.XsltFile); StringWriter stringwriter = new StringWriter(); XmlWriterSettings settings = new XmlWriterSettings(); settings.Indent = true; settings.IndentChars = "\t"; settings.OmitXmlDeclaration = true; XmlWriter myWriter = XmlWriter.Create(stringwriter, settings); myXslTrans.Transform(x, null, myWriter); newxml = stringwriter.ToString(); } if (args.PrettyPrint == true) { newxml = PrettyXml(newxml); } x.LoadXml(newxml); if (args.Overwrite == true) { x.Save(file.FullName); } else { int status = 0; status = FileTool.FileDirectoryOrNothing(args.Output); switch (status) { case (int)FileTool.filesystemObjectStatus.Directory: x.Save(args.Output + @"\" + @file.Name); break; case 0: case (int)FileTool.filesystemObjectStatus.File: x.Save(args.Output); if (filesList.Length > 1 && FilesWorkedOn == 0) { log(args, outputfile, @"Warning: Multiple input files found yet output is a single file."); } break; default: break; } } FilesWorkedOn++; log(args, outputfile, @"Processed Xml " + file.FullName); } if (outputfile != null) { outputfile.Flush(); outputfile.Close(); } return(ErrorMessage); }