Esempio n. 1
0
        public CbrEvaluator(CbrNodeConfiguration cbrConfig, Func<XrmUri, XDocument> xrmResourceProvider)
        {
            this.cbrConfig = cbrConfig;

            if (cbrConfig.DefaultTarget == null) {
                throw new InvalidOperationException("The default CBR target is not specified.");
            }

            IInclusionResolver xrmInclusionResolver = new XrmInclusionResolver(xrmResourceProvider);
            branches = new Dictionary<Validator, string>();
            foreach (var branch in cbrConfig.Branches) {
                if (branch.Value == null) {
                    throw new InvalidOperationException(string.Format(
                        "CBR branch '{0}' does not specify the target node.",
                        branch.Key));
                }

                XDocument xSchema = xrmResourceProvider(branch.Key);
                Validator validator = Validator.Create(xSchema);
                ValidatorSettings validatorSettings = new ValidatorSettings {
                    InclusionsResolver = xrmInclusionResolver
                };
                branches.Add(validator, branch.Value.Name);
            }
        }
Esempio n. 2
0
 public void CreateValidatorNoDefaultPhaseSpecified()
 {
     XDocument xSchema = Resources.Provider.LoadXmlDocument("GoodSchemas.no_default_phase.xml");
     ValidatorSettings settings = new ValidatorSettings()
     {
         Phase = "#DEFAULT"
     };
     Assert.Throws<ArgumentException>(() => Validator.Create(xSchema, settings));
 }
Esempio n. 3
0
 public void CreateValidatorGoodSchemaWithBadSettings()
 {
     XDocument xSchema = Resources.Provider.LoadXmlDocument(PHASES_SCHEMA);
     ValidatorSettings settings = new ValidatorSettings()
     {
         Phase = null
     };
     Assert.Throws<ArgumentNullException>(() => Validator.Create(xSchema, settings));
 }
Esempio n. 4
0
 public void CreateValidatorGoodSchemaWithCustomSettings()
 {
     XDocument xSchema = Resources.Provider.LoadXmlDocument(PHASES_SCHEMA);
     ValidatorSettings settings = new ValidatorSettings()
     {
         Phase = "#DEFAULT",
         InclusionsResolver = new CustomInclusionResolver()
     };
     Validator validator = Validator.Create(xSchema, settings);
     Assert.NotNull(validator);
 }
Esempio n. 5
0
        public void SimpleValidation()
        {
            XDocument xSch = Resources.Provider.LoadXmlDocument("inclusions_sch.xml");
            XDocument xIn = Resources.Provider.LoadXmlDocument("inclusions_xml.xml");

            ValidatorSettings settings = new ValidatorSettings();
            settings.InclusionsResolver = new InclusionsEmbededResourceResolver();
            Validator validator = Validator.Create(xSch, settings);
            ValidatorResults results = validator.Validate(xIn, true);

            Assert.True(results.IsValid);
        }
Esempio n. 6
0
        /// <summary>
        /// Preprocesses the XML schema (in place).
        /// </summary>
        /// <remarks>
        /// The preprocessing can be turned off in the validator settings.
        /// </remarks>
        /// <param name="xSchema">Schema</param>
        /// <param name="nsManager">Namespace manager</param>
        /// <param name="settings">Validator settings</param>
        /// <exception cref="SyntaxException">If any of the intermediate
        /// results is not valid.</exception>
        private static void Preprocess(XDocument xSchema, XmlNamespaceManager nsManager, ValidatorSettings settings)
        {
            if (!settings.Preprocessing)
            {
                return;
            }

            ValidatorSettings valArgs = new ValidatorSettings();

            valArgs.Preprocessing = false;

            // validation - phaseA
            XDocument        xPhaseA         = Resources.Provider.SchemaPhaseA;
            Validator        validatorPhaseA = Validator.Create(xPhaseA, valArgs);
            ValidatorResults resultsA        = validatorPhaseA.Validate(xSchema, true);

            if (!resultsA.IsValid)
            {
                throw new SyntaxException(resultsA.GetMessages());
            }

            Preprocessor.ResolveInclusions(xSchema, settings.InclusionsResolver, nsManager);

            // validation - phaseB
            XDocument        xPhaseB         = Resources.Provider.SchemaPhaseB;
            Validator        validatorPhaseB = Validator.Create(xPhaseB, valArgs);
            ValidatorResults resultsB        = validatorPhaseB.Validate(xSchema, true);

            if (!resultsB.IsValid)
            {
                throw new SyntaxException(resultsB.GetMessages());
            }

            Preprocessor.ResolveAbstractPatterns(xSchema, nsManager);
            Preprocessor.ResolveAbstractRules(xSchema, nsManager);
            Preprocessor.ResolvePhase(xSchema, nsManager, settings.Phase);
            Preprocessor.ResolveDiagnostics(xSchema, nsManager);

            // validation - phaseC
            XDocument        xPhaseC         = Resources.Provider.SchemaPhaseC;
            Validator        validatorPhaseC = Validator.Create(xPhaseC, valArgs);
            ValidatorResults resultsC        = validatorPhaseC.Validate(xSchema, true);

            if (!resultsC.IsValid)
            {
                throw new SyntaxException(resultsC.GetMessages());
            }

            Preprocessor.ResolveLets(xSchema, nsManager);
            Preprocessor.ResolveAncillaryElements(xSchema, nsManager);
        }
Esempio n. 7
0
        /// <summary>
        /// Creates a new Validator instance given a Schematron syntax schema and custom
        /// validator settings.
        /// </summary>
        /// <param name="xSchema">ISO Schematron complex syntax schema.
        /// Must not be null.</param>
        /// <param name="settings">Validator settings</param>
        /// <returns>A new Validator instance</returns>
        /// <exception cref="System.ArgumentException"/>
        /// <exception cref="System.ArgumentNullException"/>
        /// <exception cref="SyntaxException"/>
        public static Validator Create(XDocument xSchema, ValidatorSettings settings)
        {
            if (xSchema == null)
            {
                throw new ArgumentNullException("xSchema");
            }

            if (settings == null)
            {
                settings = new ValidatorSettings();
            }

            if (settings.InclusionsResolver == null)
            {
                settings.InclusionsResolver = new FileInclusionResolver();
            }

            if (xSchema.Root == null)
            {
                throw new ArgumentException("Schema must contain root node.");
            }

            Validator validator = null;

            // make a deep copy of the supplied XML
            XDocument xSchemaCopy = new XDocument(xSchema);

            // resolve ISO namespace
            XmlNamespaceManager nsManager = new XmlNamespaceManager(new NameTable());

            nsManager.AddNamespace("sch", Constants.ISONamespace);

            settings.Phase = DetermineSchemaPhase(xSchemaCopy.Root, settings.Phase, nsManager);

            // preprocessing - turn to minimal form
            Preprocess(xSchemaCopy, nsManager, settings);

            // deserialization
            Schema minimalizedSchema = SchemaDeserializer.Deserialize(xSchemaCopy, nsManager);

            // xpath preprocessing
            CompileXPathExpressions(minimalizedSchema);

            // create instance
            validator           = new Validator();
            validator.schema    = minimalizedSchema;
            validator.MinSyntax = xSchemaCopy;

            return(validator);
        }
Esempio n. 8
0
 public SchematronSchemaTest()
 {
     goodSchemaSettings = new ValidatorSettings()
     {
         InclusionsResolver = new CustomInclusionResolver()
         {
             Prefix = GOOD_SCHEMAS
         }
     };
     badSchemaSettings = new ValidatorSettings()
     {
         InclusionsResolver = new CustomInclusionResolver()
         {
             Prefix = BAD_SCHEMAS
         }
     };
 }
Esempio n. 9
0
        public void SimpleValidation_InvalidInstance()
        {
            XDocument xSch = Resources.Provider.LoadXmlDocument("inclusions_sch.xml");
            XDocument xIn = Resources.Provider.LoadXmlDocument("inclusions_xml_invalid.xml");

            ValidatorSettings settings = new ValidatorSettings();
            settings.InclusionsResolver = new InclusionsEmbededResourceResolver();
            Validator validator = Validator.Create(xSch, settings);

            // full validation
            ValidatorResults results1 = validator.Validate(xIn, true);

            // partial validation
            ValidatorResults results2 = validator.Validate(xIn, false);

            Assert.False(results1.IsValid);

            Assert.False(results2.IsValid);
        }
Esempio n. 10
0
        public void SimpleValidation_InvalidInstance()
        {
            XDocument xSch = Resources.Provider.LoadXmlDocument("phases_sch.xml");
            XDocument xIn = Resources.Provider.LoadXmlDocument("phases_xml_invalid.xml");

            // #ALL
            ValidatorSettings settings = new ValidatorSettings();
            settings.Phase = "#ALL";
            Validator validator = Validator.Create(xSch, settings);
            ValidatorResults results = validator.Validate(xIn, true);

            Assert.False(results.IsValid);

            // #DEFAULT
            settings = new ValidatorSettings();
            settings.Phase = "#DEFAULT";
            validator = Validator.Create(xSch, settings);
            results = validator.Validate(xIn, true);

            Assert.True(results.IsValid);

            // A
            settings = new ValidatorSettings();
            settings.Phase = "A";
            validator = Validator.Create(xSch, settings);
            results = validator.Validate(xIn, true);

            Assert.True(results.IsValid);

            // B
            settings = new ValidatorSettings();
            settings.Phase = "B";
            validator = Validator.Create(xSch, settings);
            results = validator.Validate(xIn, true);

            Assert.False(results.IsValid);
        }
Esempio n. 11
0
        /// <summary>
        /// Preprocesses the XML schema (in place).
        /// </summary>
        /// <remarks>
        /// The preprocessing can be turned off in the validator settings.
        /// </remarks>
        /// <param name="xSchema">Schema</param>
        /// <param name="nsManager">Namespace manager</param>
        /// <param name="settings">Validator settings</param>
        /// <exception cref="SyntaxException">If any of the intermediate
        /// results is not valid.</exception>
        private static void Preprocess(XDocument xSchema, XmlNamespaceManager nsManager, ValidatorSettings settings)
        {
            if (!settings.Preprocessing)
            {
                return;
            }

            ValidatorSettings valArgs = new ValidatorSettings();
            valArgs.Preprocessing = false;

            // validation - phaseA
            XDocument xPhaseA = Resources.Provider.SchemaPhaseA;
            Validator validatorPhaseA = Validator.Create(xPhaseA, valArgs);
            ValidatorResults resultsA = validatorPhaseA.Validate(xSchema, true);
            if (!resultsA.IsValid)
            {
                throw new SyntaxException(resultsA.GetMessages());
            }

            Preprocessor.ResolveInclusions(xSchema, settings.InclusionsResolver, nsManager);

            // validation - phaseB
            XDocument xPhaseB = Resources.Provider.SchemaPhaseB;
            Validator validatorPhaseB = Validator.Create(xPhaseB, valArgs);
            ValidatorResults resultsB = validatorPhaseB.Validate(xSchema, true);
            if (!resultsB.IsValid)
            {
                throw new SyntaxException(resultsB.GetMessages());
            }

            Preprocessor.ResolveAbstractPatterns(xSchema, nsManager);
            Preprocessor.ResolveAbstractRules(xSchema, nsManager);
            Preprocessor.ResolvePhase(xSchema, nsManager, settings.Phase);
            Preprocessor.ResolveDiagnostics(xSchema, nsManager);

            // validation - phaseC
            XDocument xPhaseC = Resources.Provider.SchemaPhaseC;
            Validator validatorPhaseC = Validator.Create(xPhaseC, valArgs);
            ValidatorResults resultsC = validatorPhaseC.Validate(xSchema, true);
            if (!resultsC.IsValid)
            {
                throw new SyntaxException(resultsC.GetMessages());
            }

            Preprocessor.ResolveLets(xSchema, nsManager);
            Preprocessor.ResolveAncillaryElements(xSchema, nsManager);
        }
Esempio n. 12
0
        /// <summary>
        /// Creates a new Validator instance given a Schematron syntax schema and custom
        /// validator settings.
        /// </summary>
        /// <param name="xSchema">ISO Schematron complex syntax schema.
        /// Must not be null.</param>
        /// <param name="settings">Validator settings</param>
        /// <returns>A new Validator instance</returns>     
        /// <exception cref="System.ArgumentException"/>
        /// <exception cref="System.ArgumentNullException"/>
        /// <exception cref="SyntaxException"/>
        public static Validator Create(XDocument xSchema, ValidatorSettings settings)
        {
            if (xSchema == null)
            {
                throw new ArgumentNullException("xSchema");
            }

            if (settings == null)
            {
                settings = new ValidatorSettings();
            }

            if (settings.InclusionsResolver == null)
            {
                settings.InclusionsResolver = new FileInclusionResolver();
            }

            if (xSchema.Root == null)
            {
                throw new ArgumentException("Schema must contain root node.");
            }

            Validator validator = null;

            // make a deep copy of the supplied XML
            XDocument xSchemaCopy = new XDocument(xSchema);

            // resolve ISO namespace
            XmlNamespaceManager nsManager = new XmlNamespaceManager(new NameTable());
            nsManager.AddNamespace("sch", Constants.ISONamespace);

            settings.Phase = DetermineSchemaPhase(xSchemaCopy.Root, settings.Phase, nsManager);

            // preprocessing - turn to minimal form
            Preprocess(xSchemaCopy, nsManager, settings);

            // deserialization
            Schema minimalizedSchema = SchemaDeserializer.Deserialize(xSchemaCopy, nsManager);

            // xpath preprocessing
            CompileXPathExpressions(minimalizedSchema);

            // create instance
            validator = new Validator();
            validator.schema = minimalizedSchema;
            validator.MinSyntax = xSchemaCopy;

            return validator;
        }