/// <summary> /// Performs the validation of the XDocument that is configured using the setup from the constructor. /// </summary> /// <param name="isSaving">Sets whether this is a standalone validation or part of save.</param> /// <returns>List of error strings from the validation attempt.</returns> public int Validate(bool isSaving = false) { //List<string> errors = new List<string>(); try { var schemas = new XmlSchemaSet(); schemas.Add("urn:newrelic-config", _schemaPath); var settings = new XmlReaderSettings(); settings.ValidationType = ValidationType.Schema; settings.Schemas = schemas; settings.ValidationEventHandler += new ValidationEventHandler(ValidationHandler); XmlReader reader; if (isSaving) { ConfigWriter writer = new ConfigWriter(); reader = XmlReader.Create(StringToStream(writer.ToString()), settings); } else { reader = XmlReader.Create(_configPath, settings); } while (reader.Read()) { ; } reader.Close(); if (_errors.Count > 0) { OutputErrors(_errors); } if (File.Exists(System.Windows.Forms.Application.StartupPath + "\\enabledebug") || File.Exists(Application.StartupPath + "\\enabledebug.txt")) { return(0); } return(_errors.Count); } catch (Exception ex) { log.Error(ex); return(-1); } }
/// <summary> /// Performs the validation of the XDocument that is configured using the setup from the constructor. /// </summary> /// <param name="isSaving">Sets whether this is a standalone validation or part of save.</param> /// <returns>List of error strings from the validation attempt.</returns> public int Validate(bool isSaving = false) { //List<string> errors = new List<string>(); try { var schemas = new XmlSchemaSet(); schemas.Add("urn:newrelic-config", _schemaPath); var settings = new XmlReaderSettings(); settings.ValidationType = ValidationType.Schema; settings.Schemas = schemas; settings.ValidationEventHandler += new ValidationEventHandler(ValidationHandler); XmlReader reader; if (isSaving) { ConfigWriter writer = new ConfigWriter(); reader = XmlReader.Create(StringToStream(writer.ToString()), settings); } else { reader = XmlReader.Create(_configPath, settings); } while (reader.Read()) ; reader.Close(); if (_errors.Count > 0) { OutputErrors(_errors); } if (File.Exists(System.Windows.Forms.Application.StartupPath + "\\enabledebug") || File.Exists(Application.StartupPath + "\\enabledebug.txt")) { return 0; } return _errors.Count; } catch(Exception ex) { log.Error(ex); return -1; } }
/// <summary> /// Click event handler for saving the new config file. Validates prior to saving. Displays the error/save message box. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void SaveFile_Click(object sender, EventArgs e) { try { var formProcessor = new FormProcessor(ContentFlow); var cv = new ConfigValidator(ConfigPath, SchemaPath); if (cv.Validate(true) != 0) { throw new InvalidDataException("XSD-based Validation Failed. Check for errors."); } var cf = new ConfigWriter(ConfigPath); cf.Write(); Saved.Text = "Saved newrelic.config successfully"; Saved.BackColor = Color.FromArgb(244, 144, 0); Saved.Visible = true; _savedTimer.Start(); } catch (Exception ex) { log.Error("01 - " + ex.Message); Saved.Text = ex.Message; Saved.BackColor = Color.DarkRed; Saved.Visible = true; _savedTimer.Start(); } }
/// <summary> /// Click event handler for saving the new config file to a new location. Validates prior to saving. Displays the error/save message box. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void SaveFileAs_Click(object sender, EventArgs e) { try { var formProcessor = new FormProcessor(ContentFlow); var cv = new ConfigValidator(ConfigPath, SchemaPath); if (cv.Validate(true) != 0) { throw new InvalidDataException("XSD-based Validation Failed. Check for errors."); } var cf = new ConfigWriter(); var safeFile = new SaveFileDialog(); safeFile.Title = "Select the new location for the newrelic.config file"; safeFile.FileName = "newrelic.config"; safeFile.Filter = "config files (*.config)|*.config|All files (*.*)|*.*"; safeFile.FilterIndex = 0; safeFile.RestoreDirectory = true; if (safeFile.ShowDialog() == DialogResult.OK) { cf.Write(safeFile.FileName); } Saved.Text = "Saved newrelic.config successfully"; Saved.BackColor = Color.FromArgb(244, 144, 0); Saved.Visible = true; _savedTimer.Start(); } catch (Exception ex) { log.Error(ex); Saved.Text = ex.Message; Saved.BackColor = Color.DarkRed; Saved.Visible = true; _savedTimer.Start(); } }