--- posUpdate/posUpdate/XmlPatch.cs 2013/03/21 10:40:24 710 +++ xmlTools/trunk/posUpdate/XmlPatch.cs 2013/05/09 10:37:17 867 @@ -13,16 +13,19 @@ namespace xmlTools { String fileName; String forceFiles = ""; + bool globalNoBackups = false; - public XmlPatch(String file) + public XmlPatch(String file, bool noBackups) { fileName = file; + globalNoBackups = noBackups; } - public XmlPatch(String file, String forceInFiles) + public XmlPatch(String file, String forceInFiles, bool noBackups) { fileName = file; forceFiles = forceInFiles; //We support apply the operation in diverse forced files (NameOfFile parameter will be ignored) + globalNoBackups = noBackups; } /// @@ -92,7 +95,7 @@ namespace xmlTools //---------------------------------------------------Parse Operation command (start) try { - if (forceFiles == null) + if (String.IsNullOrEmpty(forceFiles)) { File = getPatchParameter(operation, "File"); } @@ -111,14 +114,14 @@ namespace xmlTools return false; } - if (Element == "") + if (String.IsNullOrEmpty(Element)) { return false; } //---------------------------------------------------Parse Operation command (end) List filesToProcess = new List(); - if (File == "") + if (String.IsNullOrEmpty(File)) { filesToProcess = Util.getAllXmlFiles(); //no file specified, use all xml files found in same folder } @@ -134,8 +137,10 @@ namespace xmlTools //---------------------------------------------------XML Injection (start) foreach (String currFile in filesToProcess) { - - Util.backupFile(currFile); + if (!this.globalNoBackups && !Util.ContainsIgnoreCase(operation, "NoBackups")) // only skip backup if specified via global parameter or in patch file + { + Util.backupFile(currFile); + } XmlDocument xdoc = new XmlDocument(); xdoc.Load(currFile); @@ -182,7 +187,7 @@ namespace xmlTools //---------------------------------------------------Parse Operation command (start) try { - if (forceFiles == null) + if (String.IsNullOrEmpty(forceFiles)) { File = getPatchParameter(operation, "File"); } @@ -201,7 +206,7 @@ namespace xmlTools return false; } - if (Element == "") + if (String.IsNullOrEmpty(Element)) { return false; } @@ -209,7 +214,7 @@ namespace xmlTools //---------------------------------------------------Parse Operation command (end) List filesToProcess = new List(); - if (File == "") + if (String.IsNullOrEmpty(File)) { filesToProcess = Util.getAllXmlFiles(); //no file specified, use all xml files found in same folder } @@ -227,7 +232,10 @@ namespace xmlTools foreach (String currFile in filesToProcess) { - Util.backupFile(currFile); + if (!this.globalNoBackups && !Util.ContainsIgnoreCase(operation, "NoBackups")) // only skip backup if specified via global parameter or in patch file + { + Util.backupFile(currFile); + } XmlDocument xdoc = new XmlDocument(); xdoc.Load(currFile); @@ -263,7 +271,7 @@ namespace xmlTools command = command.Replace("@COMMAND ", ""); //get only the command to process - if (command.Trim() == "") + if (String.IsNullOrEmpty(command.Trim())) { Program.printAppError(Program.appErrors.PATCH_COMMAND_NOT_FOUND, "Error parsing commandOperation in Patch file: Command is empty."); return false; @@ -276,27 +284,27 @@ namespace xmlTools string paramType = ""; // Filename already exists? - if (command.IndexOf("filename:") != -1) + if (Util.ContainsIgnoreCase(command,"filename:")) { paramType = "filename:"; } - else if (command.IndexOf("filename=") != -1) + else if (Util.ContainsIgnoreCase(command, "filename=")) { paramType = "filename="; } // Add the filename if it doesn't exists else { - command = command.Insert(command.Length-1," -filename:" + this.forceFiles); // -2 to be inside quotes + command = command.Insert(command.Length," -filename:" + this.forceFiles); } - if (paramType != "") + if (!String.IsNullOrEmpty(paramType)) { int startIdx = command.IndexOf(paramType) + paramType.Length; int endIdx = command.IndexOf(" ", startIdx); // it may end with space if (endIdx == -1) { - endIdx = command.IndexOf("\"", startIdx); // or with quotes + endIdx = command.IndexOf("\n", startIdx); // or with endline } string currFilename = command.Substring(startIdx, endIdx - startIdx); command = command.Replace(currFilename, this.forceFiles); @@ -304,34 +312,12 @@ namespace xmlTools } - command = command.Replace("\"", ""); // remove quotes - - ProcessStartInfo startInfo = new ProcessStartInfo(); - if (!Util.IsRunningOnMono()) + if (this.globalNoBackups && !Util.ContainsIgnoreCase(command,"nobackups")) // add noBackup flag if provided as global parameter { - startInfo.FileName = Util.getExeFileName(); - } - else{ - startInfo.FileName = "mono"; - } - if (!Util.IsRunningOnMono()) - { - startInfo.Arguments = command; - } - else{ - startInfo.Arguments = Util.getExeFileName() + " " + command; + command = command.Insert(command.Length, " -nobackups"); } - startInfo.UseShellExecute = false; // necessary to redirect output - startInfo.RedirectStandardOutput = true; - startInfo.RedirectStandardError = true; - startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; // hide new process window - Process p = System.Diagnostics.Process.Start(startInfo); - p.OutputDataReceived += commandStdOutputReceived; - p.ErrorDataReceived += commandStdErrorReceived; - p.BeginOutputReadLine(); - p.BeginErrorReadLine(); - p.WaitForExit(); + Program.Main(Util.stringToArgsArray(command)); // use the current process is more efficient than start a new one } catch (Exception e) { @@ -342,49 +328,6 @@ namespace xmlTools return true; } - /// - /// Reads asynchronously output from the new process where the command will be executed - /// - /// - /// - private void commandStdOutputReceived(object sender, DataReceivedEventArgs e) - { - - string myData = e.Data; - - if (myData != null) - { - if (myData.EndsWith("\n")) - { - Console.Write(myData); - } - else - { - Console.WriteLine(myData); - } - } - - } - - private void commandStdErrorReceived(object sender, DataReceivedEventArgs e) - { - - string myData = e.Data; - - if (myData != null) - { - if (myData.EndsWith("\n")) - { - Console.Error.Write(myData); - } - else - { - Console.Error.WriteLine(myData); - } - } - - } - private string getPatchParameter(string line, string parameterName) { string result = "";