using System;
using System.Xml;
using System.Xml.Schema;
// Copyright (c) 2005 Accelerated Pictures, LLC
//
// 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.
//
namespace ColladaNET.Import{
/// <summary>
/// Static class which provides validating readers for the Collada schema
/// </summary>
public class ValidatingReader
{
#region Accessors
private static string kSchemaFilename = @"ColladaNET.Schema.Collada.xsd";
private static XmlSchemaCollection mSchemas = new XmlSchemaCollection();
#endregion
#region Public Static Methods
public static XmlDocument ReadFile( string sourceFilename )
{
ReadSchemas();
XmlTextReader reader = new XmlTextReader( sourceFilename );
return ReadStream( reader );
}
/// <summary>
/// Reads stream and validates XML against schema.
/// Throws XmlSchemaException on invalid Xml.
/// </summary>
public static XmlDocument ReadStream( XmlTextReader reader )
{
//
// see C# Programmer's Cookbook by Allen Jones
// ch 5.8 "Validate and XML Document against a Schema"
//
ReadSchemas();
XmlValidatingReader validator = new XmlValidatingReader( reader );
validator.ValidationType = ValidationType.Schema;
validator.Schemas.Add( mSchemas );
XmlDocument result = new XmlDocument();
result.Load( validator ); // throws System.Xml.Schema.XmlSchemaException if invalid
// Q: Not sure if this is HACKY or if it's the right way to do it
Collada.NS = new XmlNamespaceManager(result.NameTable);
Collada.NS.AddNamespace("dae", "http://www.collada.org/2005/COLLADASchema");
return result;
}
#endregion
#region Private Methods
private static void ReadSchemas()
{
if ( mSchemas.Count == 0 )
{
string [] foo = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceNames();
System.IO.Stream file = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream( kSchemaFilename ) ;
XmlTextReader reader = new XmlTextReader( file );
try
{
mSchemas.Add( null , reader );
}
finally
{
reader.Close();
file.Close();
}
}
}
#endregion
}
}
|