Exemplo n.º 1
0
        public bool Parse(out XmlErrorCollection errorCollection, out SchemeGenerator schemeGenerator)
        {
            if (_xDoc == null)
            {
                throw new ArgumentNullException("XML document is not valid.");
            }

            //Store a reference to a new scheme generator and error collector for ease of use later on.
            //This object is not responsible for their lifetime once Parse() has finished!
            _schemeGenerator = new SchemeGenerator();
            _errorCollection = new XmlErrorCollection();

            FoundElements foundElements = new FoundElements();

            //Iterate through all elements.
            IEnumerable <XElement> elements = _xDoc.Elements();

            foreach (XElement element in elements)
            {
                //Handle scheme element.
                if (element.Name.LocalName == ElementTypes.Metascheme.ToString())
                {
                    if (!foundElements.Contains(ElementTypes.Metascheme))
                    {
                        foundElements.Add(ElementTypes.Metascheme, element);
                        ParseMetaschemeElement(element);
                    }
                    else
                    {
                        _errorCollection.AddRepeatedElement(element);
                    }
                }
                //Invalid element.
                else
                {
                    _errorCollection.AddInvalidElement(element);
                }
            }

            //If we didn't get the scheme element, we didn't do anything just now!
            if (!foundElements.Contains(ElementTypes.Metascheme))
            {
                _errorCollection.AddElementNotFound(ElementTypes.Metascheme.ToString());
            }

            //Set the out variables and lose our stored references to them.
            schemeGenerator  = _schemeGenerator;
            _schemeGenerator = null;

            errorCollection  = _errorCollection;
            _errorCollection = null;

            return(errorCollection.Errors.Count == 0);
        }
Exemplo n.º 2
0
        public static bool Generate(string inputPath, SchemeVersion schemeVersion, string outputPath = null, int?seed = null, TextWriter errorTextWriter = null)
        {
            if (!File.Exists(inputPath))
            {
                if (errorTextWriter != null)
                {
                    errorTextWriter.WriteLine("File {0} does not exist.", inputPath);
                }
                return(false);
            }

            if (schemeVersion < SchemeVersion.Armageddon1 || schemeVersion > SchemeVersion.Armageddon3)
            {
                if (errorTextWriter != null)
                {
                    errorTextWriter.WriteLine("Invalid scheme version {0}.", schemeVersion);
                }
                return(false);
            }

            if (outputPath == null)
            {
                outputPath = Path.ChangeExtension(inputPath, "wsc");
            }

            Randomisation.SchemeGenerator schemeGenerator    = null;
            XmlParser.XmlErrorCollection  xmlErrorCollection = null;

            bool parseSucceeded = false;

            try
            {
                XmlParser.XmlParser schemeXmlParse = new XmlParser.XmlParser(inputPath);
                parseSucceeded = schemeXmlParse.Parse(out xmlErrorCollection, out schemeGenerator);
            }
            catch (Exception e)
            {
                if (errorTextWriter != null)
                {
                    errorTextWriter.WriteLine(String.Format("Error while parsing XML file: {0}\r\nStack:\r\n{1}", e.Message, e.StackTrace));
                }
                return(false);
            }

            if (xmlErrorCollection != null)
            {
                if (xmlErrorCollection.Errors.Count > 0)
                {
                    if (errorTextWriter != null)
                    {
                        errorTextWriter.WriteLine("Found {0} XML error(s):", xmlErrorCollection.Errors.Count);
                    }

                    foreach (XmlParser.XmlError xmlError in xmlErrorCollection.Errors)
                    {
                        if (xmlError.HasLineNumber())
                        {
                            if (errorTextWriter != null)
                            {
                                errorTextWriter.WriteLine(" - Line {0}: {1}", xmlError.lineNumber, xmlError.errorString);
                            }
                        }
                        else
                        {
                            if (errorTextWriter != null)
                            {
                                errorTextWriter.WriteLine(" - Unknown line: {0}", xmlError.errorString);
                            }
                        }
                    }
                }
            }

            if (!parseSucceeded)
            {
                return(false);
            }

            try
            {
                Random rng        = seed.HasValue ? new Random(seed.Value) : new Random();
                Scheme testScheme = schemeGenerator.GenerateScheme(rng, schemeVersion);

                using (FileStream fs = new FileStream(outputPath, FileMode.Create, FileAccess.Write))
                {
                    testScheme.Serialise(fs);
                    fs.Close();
                }
            }
            catch (Exception e)
            {
                if (errorTextWriter != null)
                {
                    errorTextWriter.WriteLine(String.Format("Error while writing scheme file: {0}\r\nStack:\r\n{1}", e.Message, e.StackTrace));
                }
                return(false);
            }

            return(true);
        }