#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
}
}