From: Daniel C. \(kzu\) <dca...@us...> - 2005-01-03 17:51:55
|
Update of /cvsroot/mvp-xml/Design/v1/src/CustomTools In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv9289/v1/src/CustomTools Added Files: XmlValidate.cs Log Message: Initial version of XmlValidate custom tool and ResX two Class tool. --- NEW FILE: XmlValidate.cs --- #region Usage /* Usage: * Add an .xml file to the project and set: * Custom Tool: XmlValidate * Custom Tool Namespace: SchemaFile.xsd[#Capitalize] * First segment is the schema to use for validation. * Optional second segment allows to use the functionality described * in http://weblogs.asp.net/cazzu/archive/2004/05/10/129106.aspx. */ #endregion Usage #region using using System; using System.Collections; using System.ComponentModel; using System.ComponentModel.Design; using System.IO; using System.Reflection; using System.Runtime.InteropServices; using System.Text; using System.Xml; using System.Xml.Schema; #endregion namespace Mvp.Xml.Design.CustomTools { /// <summary> /// Validates an XML file with an XSD schema. /// </summary> /// <remarks> /// Usage: Add an .xml file to the project and set: /// <para> /// Custom Tool: XmlValidate /// Custom Tool Namespace: SchemaFile.xsd[,SchemaFile-n,xsd]* /// </para> /// </remarks> [Guid("E14D5AEF-9DAA-420f-8927-47A3B5682069")] [CustomTool("Mvp.Xml.Validate", "MVP XML Document Validation Tool", false)] [ComVisible(true)] [VersionSupport("7.1")] [VersionSupport("7.0")] [CategorySupport(CategorySupportAttribute.CSharpCategory)] [CategorySupport(CategorySupportAttribute.VBCategory)] public class XmlValidate : CustomTool { #region GenerateCode StringBuilder _errors; /// <summary> /// Generates the output. /// </summary> protected override byte[] GenerateCode(string inputFileName, string inputFileContent) { try { _errors = new StringBuilder(); if (base.FileNameSpace == null || base.FileNameSpace == String.Empty) return System.Text.Encoding.ASCII.GetBytes( SR.XmlValidate_NoSchemaLocation); using (Stream fs = File.OpenRead(inputFileName)) { XmlValidatingReader vr = new XmlValidatingReader(new XmlTextReader(fs)); string[] schemalocations = base.FileNameSpace.Split(';'); foreach (string schemalocation in schemalocations) { if (schemalocation.Length == 0) { continue; } string path = schemalocation; // Schema location is relative to document. if (!Path.IsPathRooted(schemalocation)) { path = Path.Combine(Path.GetDirectoryName(inputFileName), schemalocation); } using (Stream xs = File.OpenRead(path)) { vr.Schemas.Add(XmlSchema.Read(xs, null)); } } vr.ValidationEventHandler += new ValidationEventHandler(OnValidate); // Validate it! while (vr.Read()) {} string errors = _errors.ToString(); if (errors.Length == 0) { return System.Text.Encoding.ASCII.GetBytes( SR.XmlValidate_Succeeded); } else { return System.Text.Encoding.ASCII.GetBytes( SR.XmlValidate_Failed(errors)); } } } catch (Exception e) { return System.Text.Encoding.ASCII.GetBytes( SR.XmlValidate_GeneralError(e)); } } private void OnValidate(object sender, ValidationEventArgs e) { _errors.AppendFormat("{0}: {1}\n", e.Severity, e.Message); } #endregion GenerateCode #region GetDefaultExtension /// <summary> /// This tool reports validation errors. /// </summary> public override string GetDefaultExtension() { return ".txt"; } #endregion GetDefaultExtension #region Registration and Installation /// <summary> /// Registers the generator. /// </summary> [ComRegisterFunction] public static void RegisterClass(Type type) { CustomTool.Register(typeof(XmlValidate)); } /// <summary> /// Unregisters the generator. /// </summary> [ComUnregisterFunction] public static void UnregisterClass(Type t) { CustomTool.UnRegister(typeof(XmlValidate)); } #endregion Registration and Installation } } |