コード例 #1
0
        /// <summary>
        ///     Parse whole SDF document from a file with given name via <c>StreamingParser</c>.
        ///     Validates SDF document with given SDF Schema while parsing, and returns the document only if it matches the schema.
        ///     Otherwise, exception will be thrown.
        /// </summary>
        /// <param name="filename">Name of a file to read SDF from.</param>
        /// <param name="schema">SDF Schema to validate SDF document with.</param>
        /// <returns>Parsed SDF.</returns>
        public static SDF ParseAndValidateSchema([NotNull] string filename, Schema schema)
        {
            var p = new StreamingParser(filename);

            while (!p.Ended && !p.HasError)
            {
                var t = p.ReadNext();
                if (t == TokenType.DocumentStart)
                {
                    continue;
                }

                if (t == TokenType.DocumentEnd)
                {
                    break;
                }

                if (t == TokenType.NodeEnd)
                {
                    if (!schema.ValidatePartial(p.Document))
                    {
                        p.Close();
                        throw new InvalidDataException("Document already does not match the schema:\n" + schema.ErrorMessage);
                    }
                }
            }

            p.Close();

            if (p.HasError)
            {
                throw new InvalidDataException("Error while stream parsing the file:\n\t" + p.Error);
            }

            if (!schema.Validate(p.Document))
            {
                throw new InvalidDataException("File is read completely, but document does not match the schema:\n" + schema.ErrorMessage);
            }

            return(p.Document);
        }
コード例 #2
0
        /// <summary>
        ///     Parse whole SDF document from a file with given name via <c>StreamingParser</c>.
        /// </summary>
        /// <param name="filename">Name of a file to read SDF from.</param>
        /// <returns>Parsed SDF.</returns>
        public static SDF Parse([NotNull] string filename)
        {
            var p = new StreamingParser(filename);

            while (!p.Ended && !p.HasError)
            {
                var t = p.ReadNext();
                if (t == TokenType.DocumentEnd)
                {
                    break;
                }
            }

            p.Close();

            if (p.HasError)
            {
                throw new InvalidDataException("Error while stream parsing the file:\n\t" + p.Error);
            }

            return(p.Document);
        }