From 7d523c654a3b917f279bc8177e82e0d332f1ca28 Mon Sep 17 00:00:00 2001 From: sbohlen Date: Thu, 26 Aug 2010 16:03:32 +0000 Subject: [PATCH] SPRNET-1202 Added call to check if the SchemaSet is already compiled and compile it within a critical section to avoid thread collisions during the compilation of the schema. --- src/Spring/Spring.Core/Util/XmlUtils.cs | 306 ++++++++++++------------ 1 file changed, 157 insertions(+), 149 deletions(-) diff --git a/src/Spring/Spring.Core/Util/XmlUtils.cs b/src/Spring/Spring.Core/Util/XmlUtils.cs index 91ee2561..e252061b 100644 --- a/src/Spring/Spring.Core/Util/XmlUtils.cs +++ b/src/Spring/Spring.Core/Util/XmlUtils.cs @@ -1,150 +1,158 @@ -#region License - -/* - * Copyright 2002-2004 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#endregion - -#region Imports - -using System; -using System.Configuration; -using System.Xml; -using System.IO; -using System.Xml.Schema; - -#endregion - -namespace Spring.Util -{ - /// - /// XML utility methods. - /// - /// Aleksandar Seovic - public class XmlUtils - { -#if !NET_2_0 - /// - /// Gets an appropriate implementation - /// for the supplied . - /// - /// The XML that is going to be read. - /// XML schemas that should be used for validation. - /// Validation event handler. - /// - /// A validating implementation. - /// - public static XmlReader CreateValidatingReader(Stream stream, XmlSchemaCollection schemas, ValidationEventHandler eventHandler) - { - return CreateValidatingReader(stream, new XmlUrlResolver(), schemas, eventHandler); - } - - /// - /// Gets an appropriate implementation - /// for the supplied . - /// - /// The XML that is going to be read. - /// to be used for resolving external references - /// XML schemas that should be used for validation. - /// Validation event handler. - /// - /// A validating implementation. - /// - public static XmlReader CreateValidatingReader(Stream stream, XmlResolver xmlResolver, XmlSchemaCollection schemas, ValidationEventHandler eventHandler) - { - XmlValidatingReader reader = new XmlValidatingReader(new XmlTextReader(stream)); - reader.XmlResolver = xmlResolver; - reader.Schemas.Add(schemas); - reader.ValidationType = ValidationType.Schema; - if (eventHandler != null) - { - reader.ValidationEventHandler += eventHandler; - } - return reader; - } -#else - /// - /// Gets an appropriate implementation - /// for the supplied . - /// - /// The XML that is going to be read. - /// XML schemas that should be used for validation. - /// Validation event handler. - /// - /// A validating implementation. - /// - public static XmlReader CreateValidatingReader(Stream stream, XmlSchemaSet schemas, ValidationEventHandler eventHandler) - { - return CreateValidatingReader(stream, new XmlUrlResolver(), schemas, eventHandler); - } - - /// - /// Gets an appropriate implementation - /// for the supplied . - /// - /// The XML that is going to be read. - /// to be used for resolving external references - /// XML schemas that should be used for validation. - /// Validation event handler. - /// - /// A validating implementation. - /// - public static XmlReader CreateValidatingReader(Stream stream, XmlResolver xmlResolver, XmlSchemaSet schemas, ValidationEventHandler eventHandler) - { - XmlReaderSettings settings = new XmlReaderSettings(); - settings.Schemas.XmlResolver = xmlResolver; - settings.Schemas.Add(schemas); - settings.ValidationType = ValidationType.Schema; - if (eventHandler != null) - { - settings.ValidationEventHandler += eventHandler; - } - - return XmlReader.Create(stream, settings); - } -#endif - - -#if !NET_2_0 - /// - /// Gets an implementation - /// for the supplied . - /// - /// The XML that is going to be read. - /// - /// A non-validating implementation. - /// - public static XmlReader CreateReader(Stream stream) - { - return new XmlTextReader(stream); - } -#else - - /// - /// Gets an appropriate implementation - /// for the supplied . - /// - /// The XML that is going to be read. - /// - /// A non-validating implementation. - /// - public static XmlReader CreateReader(Stream stream) - { - return XmlReader.Create(stream); - } -#endif - } +#region License + +/* + * Copyright 2002-2004 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#endregion + +#region Imports + +using System; +using System.Configuration; +using System.Xml; +using System.IO; +using System.Xml.Schema; + +#endregion + +namespace Spring.Util +{ + /// + /// XML utility methods. + /// + /// Aleksandar Seovic + public class XmlUtils + { +#if !NET_2_0 + /// + /// Gets an appropriate implementation + /// for the supplied . + /// + /// The XML that is going to be read. + /// XML schemas that should be used for validation. + /// Validation event handler. + /// + /// A validating implementation. + /// + public static XmlReader CreateValidatingReader(Stream stream, XmlSchemaCollection schemas, ValidationEventHandler eventHandler) + { + return CreateValidatingReader(stream, new XmlUrlResolver(), schemas, eventHandler); + } + + /// + /// Gets an appropriate implementation + /// for the supplied . + /// + /// The XML that is going to be read. + /// to be used for resolving external references + /// XML schemas that should be used for validation. + /// Validation event handler. + /// + /// A validating implementation. + /// + public static XmlReader CreateValidatingReader(Stream stream, XmlResolver xmlResolver, XmlSchemaCollection schemas, ValidationEventHandler eventHandler) + { + XmlValidatingReader reader = new XmlValidatingReader(new XmlTextReader(stream)); + reader.XmlResolver = xmlResolver; + reader.Schemas.Add(schemas); + reader.ValidationType = ValidationType.Schema; + if (eventHandler != null) + { + reader.ValidationEventHandler += eventHandler; + } + return reader; + } +#else + /// + /// Gets an appropriate implementation + /// for the supplied . + /// + /// The XML that is going to be read. + /// XML schemas that should be used for validation. + /// Validation event handler. + /// + /// A validating implementation. + /// + public static XmlReader CreateValidatingReader(Stream stream, XmlSchemaSet schemas, ValidationEventHandler eventHandler) + { + return CreateValidatingReader(stream, new XmlUrlResolver(), schemas, eventHandler); + } + + /// + /// Gets an appropriate implementation + /// for the supplied . + /// + /// The XML that is going to be read. + /// to be used for resolving external references + /// XML schemas that should be used for validation. + /// Validation event handler. + /// + /// A validating implementation. + /// + public static XmlReader CreateValidatingReader(Stream stream, XmlResolver xmlResolver, XmlSchemaSet schemas, ValidationEventHandler eventHandler) + { + lock (typeof(XmlUtils)) + { + if (!schemas.IsCompiled) + { + schemas.Compile(); + } + + XmlReaderSettings settings = new XmlReaderSettings(); + settings.Schemas.XmlResolver = xmlResolver; + settings.Schemas.Add(schemas); + settings.ValidationType = ValidationType.Schema; + if (eventHandler != null) + { + settings.ValidationEventHandler += eventHandler; + } + + return XmlReader.Create(stream, settings); + } + } +#endif + + +#if !NET_2_0 + /// + /// Gets an implementation + /// for the supplied . + /// + /// The XML that is going to be read. + /// + /// A non-validating implementation. + /// + public static XmlReader CreateReader(Stream stream) + { + return new XmlTextReader(stream); + } +#else + + /// + /// Gets an appropriate implementation + /// for the supplied . + /// + /// The XML that is going to be read. + /// + /// A non-validating implementation. + /// + public static XmlReader CreateReader(Stream stream) + { + return XmlReader.Create(stream); + } +#endif + } } \ No newline at end of file