Пример #1
0
        private static Boolean ValidateScenario(string scenarioFile)
        {
            Regex schemaFileRegex = new Regex("SchemaLocation=\"(.*?)\"");

            // First, find the schema file name in the scenario file;
            //string dddClientDir = String.Format("\\\\{0}\\DDDClient", System.Environment.MachineName) + "\\";
            string dddClientDir = "\\\\" + ServerOptions.DDDClientPath + "\\";// Path.GetDirectoryName(ServerOptions.ScenarioSchemaPath) + "\\";

            //string dddClientDir = ServerOptions.ScenarioSchemaPath + "\\";
            namedSchemaFile = "DDDSchema_4_0.xsd";//default setting
            string oneLine;

            try
            {
                StreamReader scenarioReader = new StreamReader(scenarioFile);
                while ((oneLine = scenarioReader.ReadLine()) != null)
                {
                    if (schemaFileRegex.IsMatch(oneLine))
                    {
                        Match m = schemaFileRegex.Match(oneLine);

                        namedSchemaFile = m.Groups[1].Value;
                        if (".xsd" != namedSchemaFile.Substring(namedSchemaFile.Length - 4).ToLower())
                        {
                            namedSchemaFile += ".xsd";
                        }
                        switch (namedSchemaFile)
                        {                              //
                        case "DDDSchema_4_0.xsd":
                            parser = new Parser_4_0(); //DDD SP1
                            break;

                        case "DDDSchema_4_0_2.xsd":
                            parser = new Parser_4_0_2();    //DDD 4.0 SP2
                            break;

                        case "DDDSchema_4_1.xsd":
                            parser = new Parser_4_1();
                            break;

                        case "DDDSchema_4_1_1.xsd":
                            parser = new Parser_4_1();
                            break;

                        case "DDDSchema_4_2.xsd":
                            parser = new Parser_4_2();
                            break;

                        default:
                            System.Windows.Forms.MessageBox.Show("Unknown Schema File: " + namedSchemaFile);
                            throw new ApplicationException("Unknown schema file: " + namedSchemaFile);
                        }
                        scenarioReader.Close();
                        break;
                    }
                }
            }
            catch (SystemException e)// only occurs for i/o errors, not for unfound schema name
            {
                System.Windows.Forms.MessageBox.Show("Error reading scenario file " + scenarioFile + ": " + e.Message);

                throw new ApplicationException("Error reading scenario file " + scenarioFile, e);
            }

/*
 * at this point
 * Have found the schema file (or there was none)
 * Have used schema file to choose appropriate parser
 */
            namedSchemaFile = dddClientDir + namedSchemaFile;
            Boolean returnValue = true;
            // Create the XmlNodeReader object.
            XmlDocument doc = new XmlDocument();

            doc.Load(scenarioFile);
            XmlNodeReader nodeReader = new XmlNodeReader(doc);

            // Set the validation settings.
            XmlReaderSettings settings = new XmlReaderSettings();

            settings.ValidationType = ValidationType.Schema;
            settings.Schemas.Add(null, namedSchemaFile);//+".xsd");
            settings.ValidationEventHandler += new ValidationEventHandler(SchemaValidationHandler);
            settings.ValidationFlags        |= XmlSchemaValidationFlags.ReportValidationWarnings;
            // Create a validating reader that wraps the XmlNodeReader object.
            XmlReader reader = XmlReader.Create(nodeReader, settings);

            parser.SetReader(reader);
            // Parse the XML file.
            try
            {
                while (reader.Read())
                {
                    ;
                }
            }
            catch (SystemException e)
            {
                returnValue = false;
                System.Windows.Forms.MessageBox.Show("Error in XML Validation:\n"
                                                     + " Scenario File: " + scenarioFile + "\n"
                                                     + "Schema File : " + namedSchemaFile + "\n"
                                                     + e.Message);
            }
            finally
            {
                nodeReader.Close();
                reader.Close();
            }
            return(returnValue);
        }