/// <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(ParserValidationMessage.NewError(ex.Message).ToString()); } catch (Exception ex) { //and other things that could go wrong IsValid = false; WriteToLog(ParserValidationMessage.NewError(ex.Message).ToString()); } }
public IParser Parse(String filename) { // See if the given file exists and validate the schema if it does. FileInfo file = new FileInfo(filename); if (!file.Exists) { throw new FileNotFoundException("Could not load config file", filename); } ValidateSchema(filename); // Load the document. XmlDocument doc = new XmlDocument(); Stream filestream = XIncludeReader.GetStream(file.FullName); doc.Load(filestream); filestream.Close(); if (doc == null) { throw new InvalidOperationException("Could not parse xml document: " + file.Name); } ConfigurationElement options = null; XmlNode root = doc.DocumentElement["config"]; if (root != null) { options = new ConfigurationElement(root); } else { options = new ConfigurationElement(); } // If the root directory is not specified, make it the directory the config file is loaded from. if (options.RootDirectory.Equals(String.Empty)) { options.RootDirectory = file.DirectoryName + "\\"; } if (!options.RootDirectory.EndsWith("\\")) { options.RootDirectory += "\\"; } ParserElement parserElement = ParserElement.ParseFromXml(options, doc); String parserClass = parserElement.Class.Equals(String.Empty) ? "Spring2.DataTierGenerator.Parser.XmlParser" : parserElement.Class; Object o = null; try { System.Type parserType = System.Type.GetType(parserClass, true); Object[] args = { parserElement, options, doc }; o = System.Activator.CreateInstance(parserType, args); } catch (Exception ex) { Console.Out.WriteLine("ERROR: could not create class " + parserClass + ".\n" + ex.ToString()); return(null); } IParser parser = o as IParser; if (parser == null) { Console.Out.WriteLine("ERROR: class " + parserElement.Class + " does not support IParser interface.\n"); return(null); } parser.Validate(); // parser.IsValid = isValid; // foreach (ParserValidationMessage message in errors) { // //parser.Errors.Add(message); // } errors.Clear(); return(parser); }
public void Parse(String filename) { FileInfo file = new FileInfo(filename); if (!file.Exists) { throw new FileNotFoundException("Could not load config file", filename); } isValid = true; ValidateSchema(filename); doc = new XmlDocument(); Stream filestream = XIncludeReader.GetStream(filename); doc.Load(filestream); filestream.Close(); if (doc == null) { throw new InvalidOperationException("Could not parse xml document: " + filename); } // event handler for all of the ParseFromXml methods ParserValidationDelegate vd = new ParserValidationDelegate(ParserValidationEventHandler); XmlNode root = doc.DocumentElement["config"]; if (root != null) { this.options = new Configuration(root, vd); } else { this.options = new Configuration(); } sqltypes = SqlTypeElement.ParseFromXml(doc, vd); types = TypeElement.ParseFromXml(options, doc, vd); // parse generate/task information so that type registration will happen before other types are loaded generator = GeneratorElement.ParseFromXml(options, doc, vd); TaskElement.RegisterTypes(doc, options, generator.Tasks, types); // see if we want to generate collections for all entities XmlNodeList collectionElement = doc.DocumentElement.GetElementsByTagName("collections"); XmlNode collectionNode = collectionElement[0]; if (collectionNode.Attributes["generateall"] == null) { options.GenerateAllCollections = false; } else { options.GenerateAllCollections = Boolean.Parse(collectionNode.Attributes["generateall"].Value.ToString()); } // if the root directory is not specified, make it the directory the config file is loaded from if (options.RootDirectory.Equals(String.Empty)) { options.RootDirectory = file.DirectoryName + "\\"; } if (!options.RootDirectory.EndsWith("\\")) { options.RootDirectory += "\\"; } enumtypes = EnumElement.ParseFromXml(options, doc, sqltypes, types, vd); databases = DatabaseElement.ParseFromXml(options, doc, sqltypes, types, vd); entities = EntityElement.ParseFromXml(options, doc, sqltypes, types, DatabaseElement.GetAllSqlEntities(databases), vd); messages = MessageElement.ParseFromXml(options, doc, sqltypes, types, DatabaseElement.GetAllSqlEntities(databases), vd); reportExtractions = ReportExtractionElement.ParseFromXml(options, doc, sqltypes, types, entities, vd); ArrayList collectableClasses = new ArrayList(); ArrayList autoGenerateClasses = new ArrayList(); collectableClasses.AddRange(entities); collectableClasses.AddRange(reportExtractions); autoGenerateClasses.AddRange(entities); autoGenerateClasses.AddRange(reportExtractions); collections = CollectionElement.ParseFromXml(options, doc, sqltypes, types, vd, collectableClasses, autoGenerateClasses, (ArrayList)entities); CreateSqlElementAssociations(vd); CreateEntityElementAssociations(vd); Validate(vd); }
public ConfigParser(String filename) { FileInfo file = new FileInfo(filename); if (!file.Exists) { throw new FileNotFoundException("Could not load config file", filename); } isValid = true; ValidateSchema(filename); doc = new XmlDocument(); Stream filestream = XIncludeReader.GetStream(filename); doc.Load(filestream); filestream.Close(); if (doc == null) { throw new InvalidOperationException("Could not parse xml document: " + filename); } // event handler for all of the ParseFromXml methods ParserValidationDelegate vd = new ParserValidationDelegate(ParserValidationEventHandler); XmlNode root = doc.DocumentElement["config"]; if (root != null) { this.options = new Configuration(root, vd); } else { this.options = new Configuration(); } // If the root directory is not specified, make it the directory the config file is loaded from. if (options.RootDirectory.Equals(String.Empty)) { options.RootDirectory = file.DirectoryName + "\\"; } if (!options.RootDirectory.EndsWith("\\")) { options.RootDirectory += "\\"; } parser = ParserElement.ParseFromXml(options, doc, vd); generator = GeneratorElement.ParseFromXml(options, doc, vd); sqltypes = SqlTypeElement.ParseFromXml(doc, vd); types = TypeElement.ParseFromXml(options, doc, vd); if (parser.Class.Equals(String.Empty)) { enumtypes = EnumElement.ParseFromXml(options, doc, sqltypes, types, vd); databases = DatabaseElement.ParseFromXml(options, doc, sqltypes, types, vd); entities = EntityElement.ParseFromXml(options, doc, sqltypes, types, DatabaseElement.GetAllSqlEntities(databases), vd); collections = CollectionElement.ParseFromXml(options, doc, sqltypes, types, vd, (ArrayList)entities); } else { Object o = null; try { System.Type clazz = System.Type.GetType(parser.Class, true); Object[] args = { parser, options, doc, sqltypes, types, vd }; o = System.Activator.CreateInstance(clazz, args); } catch (Exception ex) { Console.Out.WriteLine("ERROR: could not create class " + parser.Class + ".\n" + ex.ToString()); } if (o is IParser) { IParser p = (IParser)o; enumtypes = (ArrayList)p.Enums; collections = (ArrayList)p.Collections; databases = (ArrayList)p.Databases; entities = (ArrayList)p.Entities; } else { Console.Out.WriteLine("ERROR: class " + parser.Class + " does not support IParser interface.\n"); } } Validate(vd); }