/// <summary> /// Validates the XML document based on the provided schema definitions. /// </summary> /// <param name="mainSchemaDefinition">A <c>string</c> representing the main schema definition.</param> /// <param name="nestedSchemaDefinitions">A <see cref="IDictionary{TKey,TValue}"/> containing /// zero to more nested schema definitions.</param> /// <exception cref="CriticalFileReadException">Thrown when the XML document does not match /// the provided schema definitions.</exception> /// <exception cref="ArgumentException">Thrown when <paramref name="mainSchemaDefinition"/> does not /// reference the default schema definition <c>ConfiguratieSchema.xsd</c>.</exception> /// <exception cref="ArgumentException">Thrown when: /// <list type="bullet"> /// <item><paramref name="mainSchemaDefinition"/> is invalid.</item> /// <item><paramref name="nestedSchemaDefinitions"/> contains invalid schema definition values.</item> /// <item><paramref name="mainSchemaDefinition"/>, all together with its referenced /// <paramref name="nestedSchemaDefinitions"/>, contains an invalid schema definition.</item> /// <item><paramref name="nestedSchemaDefinitions"/> contains schema definitions that are not /// referenced by <see cref="mainSchemaDefinition"/>.</item> /// </list> /// </exception> private void ValidateToSchema(string mainSchemaDefinition, IDictionary <string, string> nestedSchemaDefinitions) { if (!mainSchemaDefinition.Contains(defaultSchemaName)) { throw new ArgumentException($"'{nameof(mainSchemaDefinition)}' does not reference the default schema '{defaultSchemaName}'."); } IDictionary <string, string> extendedNestedSchemaDefinitions = new Dictionary <string, string>(nestedSchemaDefinitions); extendedNestedSchemaDefinitions.Add(defaultSchemaName, Resources.ConfiguratieSchema); var combinedXmlSchemaDefinition = new CombinedXmlSchemaDefinition(mainSchemaDefinition, extendedNestedSchemaDefinitions); try { combinedXmlSchemaDefinition.Validate(xmlDocument); } catch (XmlSchemaValidationException exception) { string message = string.Format(Resources.CalculationConfigurationReader_Configuration_contains_no_valid_xml_line_0_position_1_reason_2, exception.LineNumber, exception.LinePosition, exception.Message); throw new CriticalFileReadException(new FileReaderErrorMessageBuilder(xmlFilePath).Build(message), exception); } }
/// <summary> /// Gets the correct schema definition depending on the version. /// </summary> /// <param name="schemaDefinitions">All the schema definitions.</param> /// <returns>The schema definition that corresponds to the XML file.</returns> /// <exception cref="CriticalFileReadException">Thrown when the version /// from the XML file is not supported.</exception> private CalculationConfigurationSchemaDefinition GetSchemaDefinition(IEnumerable <CalculationConfigurationSchemaDefinition> schemaDefinitions) { int versionNumber; try { var combinedXmlSchemaDefinition = new CombinedXmlSchemaDefinition(Resources.VersieSchema, new Dictionary <string, string>()); combinedXmlSchemaDefinition.Validate(xmlDocument); versionNumber = GetVersionNumber(); } catch (XmlSchemaValidationException) { versionNumber = 0; } CalculationConfigurationSchemaDefinition schemaDefinition = schemaDefinitions.SingleOrDefault(sd => sd.VersionNumber == versionNumber); if (schemaDefinition == null) { string message = new FileReaderErrorMessageBuilder(xmlFilePath) .Build(Resources.CalculationConfigurationReader_GetSchemaDefinition_Not_supported_version); throw new CriticalFileReadException(message); } return(schemaDefinition); }