/// <summary> /// Initializes the object from a BRE Validations fact object. /// </summary> /// <param name="breValidations">The BRE Validations object.</param> /// <returns>The initialized Validations object.</returns> internal Validations InitialiseFromBreValidations(Libraries.Facts.Validations breValidations) { this.errors.Clear(); this.warnings.Clear(); this.information.Clear(); foreach (var error in breValidations.Errors) { this.errors.Add( new ValidationEntry( ValidationLevel.Error, error.Description, error.Type, error.Code, error.Severity)); } foreach (var warning in breValidations.Warnings) { this.warnings.Add( new ValidationEntry( ValidationLevel.Warning, warning.Description, warning.Type, warning.Code, warning.Severity)); } foreach (var info in breValidations.Information) { this.information.Add( new ValidationEntry( ValidationLevel.Information, info.Description, info.Type, info.Code, info.Severity)); } return(this); }
/// <summary> /// Validate an XML document using a BRE policy. /// </summary> /// <param name="xmlDocument"> /// The XML message document to be validated. /// </param> /// <param name="documentType"> /// The document type of the XML document, as used in the validation rules. /// </param> /// <returns> /// A <see cref="Validations"/> object containing the results of all validations. /// </returns> public Validations ValidateDocument(XmlNode xmlDocument, string documentType) { var validations = new Validations(); if (this.directive == null) { return(validations); } if (string.IsNullOrWhiteSpace(this.directive.ValidationPolicyName)) { return(validations); } var trace = Convert.ToBoolean(ConfigurationManager.AppSettings[Resources.AppSettingsEsbBreTrace]); var tempFileName = string.Empty; if (trace) { var traceFileFolder = ConfigurationManager.AppSettings[Resources.AppSettingsEsbBreTraceFileLocation]; if (!string.IsNullOrWhiteSpace(traceFileFolder)) { while (traceFileFolder.EndsWith(@"\")) { traceFileFolder = traceFileFolder.Substring(0, traceFileFolder.Length - 1); } } if (string.IsNullOrWhiteSpace(traceFileFolder)) { traceFileFolder = @"."; } tempFileName = string.Format( @"{0}\ValidationPolicyTrace_{1}_{2}.txt", traceFileFolder, DateTime.Now.ToString("yyyyMMdd"), Guid.NewGuid()); } var breValidations = new Libraries.Facts.Validations(); // Determine if static support is being used by rule engine and create the array of short-term facts var shortTermFacts = IsStaticSupport() ? new object[] { new TypedXmlDocument(documentType, xmlDocument), breValidations } : new object[] { new TypedXmlDocument(documentType, xmlDocument), breValidations, new XmlHelper() }; // Assert the XML messsage and a validation object. var policyName = this.directive.ValidationPolicyName; Version version; Version.TryParse(this.directive.ValidationPolicyVersion, out version); if (Convert.ToBoolean(ConfigurationManager.AppSettings[Resources.AppSettingsEsbBrePolicyTester])) { PolicyTester policyTester = null; int min; int maj; if (version == null) { maj = 1; min = 0; } else { maj = version.Major; min = version.Minor; } try { // Use PolicyTester var srs = new SqlRuleStore(GetRuleStoreConnectionString()); var rsi = new RuleSetInfo(policyName, maj, min); var ruleSet = srs.GetRuleSet(rsi); if (ruleSet == null) { throw new RuleSetNotFoundException( string.Format(Resources.ExceptionRsNotInStore, policyName, maj, min)); } policyTester = new PolicyTester(ruleSet); if (trace) { // Create the debug tracking object var dti = new DebugTrackingInterceptor(tempFileName); // Execute the policy with trace policyTester.Execute(shortTermFacts, dti); Trace.Write( "[Esb.BizTalk.Orchestration.Directive] ValidateMessage Trace: " + dti.GetTraceOutput()); } else { // Execute the policy policyTester.Execute(shortTermFacts); } } finally { if (policyTester != null) { policyTester.Dispose(); } } } else { var policy = version == null ? new Policy(policyName) : new Policy(policyName, version.Major, version.Minor); try { if (trace) { // Create the debug tacking object var dti = new DebugTrackingInterceptor(tempFileName); // Execute the policy with trace policy.Execute(shortTermFacts, dti); Trace.Write( "[Esb.BizTalk.Orchestration.Directive] ValidateMessage Trace: " + dti.GetTraceOutput()); } else { // Execute the policy policy.Execute(shortTermFacts); } } finally { policy.Dispose(); } } // Throw an exception if dictated by the policy if (this.directive.ErrorOnInvalid && breValidations.ErrorCount > 0) { throw new ValidationException(string.Format("\r\nValidation Errors:\r\n{0}", breValidations.ToString(Libraries.Facts.ValidationLevel.Error))); } return(validations.InitialiseFromBreValidations(breValidations)); }