From: Daniel C. \(kzu\) <dca...@us...> - 2004-10-21 20:42:17
|
Update of /cvsroot/mvp-xml/Design/v1/src/CustomTools/XmlValidate In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv28388/v1/src/CustomTools/XmlValidate Added Files: XmlValidate.cs Log Message: --- 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 /// </para> /// </remarks> [Guid("C4033005-CF59-492e-A7ED-911E0B7E04D5")] [CustomTool("XmlValidate", "Microsoft IPE Xml Validation", 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); string schemalocation = base.FileNamespace; // Schema location is relative to document. if (!Path.IsPathRooted(schemalocation)) { schemalocation = Path.Combine(Path.GetDirectoryName(inputFileName), schemalocation); } using (Stream fs = File.OpenRead(inputFileName)) { XmlValidatingReader vr = new XmlValidatingReader(new XmlTextReader(fs)); using (Stream xs = File.OpenRead(schemalocation)) { 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 } } |