/// <summary> /// validates xml file against embedded schema file /// </summary> /// <param name="filename"></param> private void ValidateSchema(String filename) { try { ResourceManager rm = new ResourceManager(); ValidationEventHandler veh = new ValidationEventHandler(SchemaValidationEventHandler); XmlSchema xsd = XmlSchema.Read(rm.ConfigSchema, veh); XmlTextReader xml = XIncludeReader.GetXmlTextReader(filename); XmlValidatingReader vr = new XmlValidatingReader(xml); vr.Schemas.Add(xsd); vr.ValidationType = ValidationType.Schema; // and validation errors events go to... vr.ValidationEventHandler += veh; // wait until the read is over, its occuring in a different thread - kinda like // when your walking to get a cup of coffee and your mind is in Hawaii while (vr.Read()) { ; } vr.Close(); } catch (UnauthorizedAccessException ex) { //dont have access permission isValid = false; WriteToLog(ParserValidationArgs.NewError(ex.Message).ToString()); } catch (Exception ex) { //and other things that could go wrong isValid = false; WriteToLog(ParserValidationArgs.NewError(ex.Message).ToString()); } }
/// <summary> /// handle XML validation errors /// </summary> /// <param name="sender"></param> /// <param name="args"></param> internal void SchemaValidationEventHandler(object sender, ValidationEventArgs args) { if (args.Severity.Equals(XmlSeverityType.Error)) { isValid = false; WriteToLog(ParserValidationArgs.NewError(args.Message).ToString()); } else { WriteToLog(ParserValidationArgs.NewWarning(args.Message).ToString()); } }
/// <summary> /// validates xml file against embedded schema file /// </summary> /// <param name="filename"></param> private void ValidateSchema(String filename) { try { ValidationEventHandler veh = new ValidationEventHandler(SchemaValidationEventHandler); System.IO.Stream stream = this.GetType().Assembly.GetManifestResourceStream("config.xsd"); if (stream == null) { isValid = false; WriteToLog(ParserValidationArgs.NewError("unable to locate config.xsd as assembly resource").ToString()); } else { XmlSchema schema = XmlSchema.Read(stream, veh); stream.Close(); XmlReaderSettings settings = new XmlReaderSettings(); settings.ValidationType = ValidationType.Schema; settings.Schemas.Add(schema); settings.ValidationEventHandler += veh; XmlReader reader = XmlReader.Create(filename, settings); // wait until the read is over, it's occurring in a different thread - kinda like // when you're walking to get a cup of coffee and your mind is in Hawaii while (reader.Read()) { ; } } } catch (UnauthorizedAccessException ex) { //dont have access permission isValid = false; WriteToLog(ParserValidationArgs.NewError(ex.Message).ToString()); } catch (Exception ex) { //and other things that could go wrong isValid = false; WriteToLog(ParserValidationArgs.NewError(ex.Message).ToString()); } }
private void CreateSqlElementAssociations(ParserValidationDelegate vd) { // find and assign the foreign entity and EnumElement now that they are parsed foreach (DatabaseElement database in databases) { foreach (SqlEntityElement sqlEntity in database.SqlEntities) { foreach (ConstraintElement constraint in sqlEntity.Constraints) { if (constraint.ForeignEntity.Name.Length > 0) { SqlEntityElement entity = SqlEntityElement.FindByName(DatabaseElement.GetAllSqlEntities(databases), constraint.ForeignEntity.Name); if (entity != null) { constraint.ForeignEntity = (SqlEntityElement)entity.Clone(); } else { vd(ParserValidationArgs.NewError("ForeignEntity (" + constraint.ForeignEntity.Name + ") specified in constraint " + constraint.Name + " could not be found as an defined entity")); } } if (constraint.CheckEnum.Name.Length > 0) { EnumElement entity = EnumElement.FindByName(enumtypes as ArrayList, constraint.CheckEnum.Name); if (entity != null) { constraint.CheckEnum = (EnumElement)entity.Clone(); } else { vd(ParserValidationArgs.NewError("CheckEnum (" + constraint.CheckEnum.Name + ") specified in constraint " + constraint.Name + " could not be found as an defined entity")); } } } } } }
private void CreateEntityElementAssociations(ParserValidationDelegate vd) { foreach (EntityElement entity in entities) { //Fields which are entities. foreach (PropertyElement property in entity.Fields) { if (property.Entity.Name.Length > 0) { EntityElement e = EntityElement.FindEntityByName(entities, property.Entity.Name); if (e != null) { property.Entity = e; } else { vd(ParserValidationArgs.NewError("Property (" + property.Name + ") specifies an entity " + property.Entity.Name + " that could not be found as an defined entity")); } } } //Dependant entities. ArrayList dependents = new ArrayList(); foreach (EntityElement dependent in entity.Dependents) { EntityElement e = EntityElement.FindEntityByName(entities, dependent.Name); if (e != null) { dependents.Add(e); } else { vd(ParserValidationArgs.NewError("Entity (" + entity.Name + ") specifies a dependent entity " + dependent.Name + " that could not be found as an defined entity")); } } entity.Dependents = dependents; } }
/// <summary> /// Post-parse validations /// </summary> /// <param name="vd"></param> protected void Validate(ParserValidationDelegate vd) { //TODO: walk through collection to make sure that cross relations are correct. foreach (DatabaseElement database in databases) { foreach (SqlEntityElement sqlentity in database.SqlEntities) { if (sqlentity.GetPrimaryKeyColumns().Count == 0 && (sqlentity.AllowDelete || sqlentity.AllowInsert || sqlentity.AllowUpdate)) { vd(ParserValidationArgs.NewWarning("SqlEntity " + sqlentity.Name + " does not have any primary key columns defined.")); } if (!sqlentity.HasUpdatableColumns() && sqlentity.GenerateUpdateStoredProcScript) { vd(ParserValidationArgs.NewWarning("SqlEntity " + sqlentity.Name + " does not have any editable columns and does not have generateupdatestoredprocscript=\"false\" specified.")); } } } // make sure that all columns are represented in entities foreach (EntityElement entity in entities) { if (entity.SqlEntity.Name.Length > 0) { foreach (ColumnElement column in entity.SqlEntity.Columns) { if (!column.Obsolete && EntityElement.FindAnyFieldByColumnName(entities, column.Name) == null && !entity.HasEntityMappedColumn(column)) { vd(ParserValidationArgs.NewWarning("could not find property representing column " + column.Name + " in entity " + entity.Name + ".")); } } } foreach (PropertyElement property in entity.Fields) { // make sure that obsolete columns are not mapped to properties if (property.Column.Obsolete && property.Column.Name.Length > 0) { vd(ParserValidationArgs.NewWarning("property " + property.Name + " in entity " + entity.Name + " is mapped to column " + property.Column.Name + " which is obsolete.")); } // have property descriptions "inherit" from a column if they are not populated if (property.Column.Description.Length > 0 && property.Description.Length == 0) { property.Description = property.Column.Description; } } } // make sure that enum values are unique foreach (EnumElement enumtype in enumtypes) { Hashtable values = new Hashtable(); foreach (EnumValueElement value in enumtype.Values) { if (values.Contains(value.Code)) { vd(ParserValidationArgs.NewError("Enum " + enumtype.Name + " has the code '" + value.Code + "' specified more than once.")); } else { values.Add(value.Code, value.Code); } } } // find and assign types to collections if available (the TypeElement is needed for templates that need to add namespaces) foreach (CollectionElement collection in Collections) { if (types.Contains(collection.Type.Name)) { collection.Type = (TypeElement)types[collection.Type.Name]; } } foreach (TaskElement task in generator.Tasks) { IWriter w = GetWriter(task.Writer); if (w == null) { vd(ParserValidationArgs.NewError("Task specified writer '" + task.Writer + "' that was not defined.")); } // check to make sure the styler exists if it is specified (optional) if (task.Styler.Length > 0) { IStyler s = GetStyler(task.Styler); if (s == null) { vd(ParserValidationArgs.NewError("Task specified styler '" + task.Styler + "' that was not defined.")); } } } }