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 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); }