| |||||||||||||||||
Source File | Conditionals | Statements | Methods | TOTAL | |||||||||||||
Model\XmlModel\XmlFileValidator.cs | 100,0 % | 100,0 % | 100,0 % | 100,0 % |
|
1 | #region Copyright | |
2 | /* | |
3 | ShowX. Maps business objects into presentation layer. | |
4 | Copyright (C) 2005 Jesus Diaz. | |
5 | ||
6 | This library is free software; you can redistribute it and/or | |
7 | modify it under the terms of the GNU Lesser General Public | |
8 | License as published by the Free Software Foundation; either | |
9 | version 2.1 of the License, or (at your option) any later version. | |
10 | ||
11 | This library is distributed in the hope that it will be useful, | |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | Lesser General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU Lesser General Public | |
17 | License along with this library; if not, write to the Free Software | |
18 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. | |
19 | ||
20 | For project details see: http://showx.sourceforge.net | |
21 | */ | |
22 | #endregion | |
23 | ||
24 | using System.Xml; | |
25 | using System.IO; | |
26 | using System.Xml.Schema; | |
27 | using System.Reflection; | |
28 | using log4net; | |
29 | ||
30 | namespace ShowX.Model.XmlModel | |
31 | { | |
32 | /// <summary> | |
33 | /// Implements a file validation, based on xsd embedded resource. | |
34 | /// </summary> | |
35 | public class XmlFileValidator : IXmlFileValidator | |
36 | { | |
37 | private static readonly ILog log = LogManager.GetLogger( | |
38 | typeof(XmlFileValidator)); | |
39 | ||
40 | private bool validated = true; | |
41 | ||
42 | #region IXmlValidator Members | |
43 | ||
44 | /// <summary> | |
45 | /// Validate a given XmlNode over an embedded xsd file. | |
46 | /// </summary> | |
47 | /// <param name="node">XmlNode to validate.</param> | |
48 | /// <param name="embeddedXsdRes">Embedded xsd to use for validation.</param> | |
49 | /// <returns>True if validation node is sucessful, false otherwise.</returns> | |
50 | 46 | public bool Validate(System.Xml.XmlNode node, string embeddedXsdRes) |
51 | { | |
52 | 46 | log.Debug(string.Format("Validating Xml node against '{0}'", |
53 | embeddedXsdRes)); | |
54 | ||
55 | 46 | XmlValidatingReader vr = new XmlValidatingReader(node.OuterXml, |
56 | XmlNodeType.Element, new XmlParserContext(null, | |
57 | new XmlNamespaceManager(new NameTable()),null,XmlSpace.None)); | |
58 | ||
59 | 46 | Assembly asm = Assembly.GetExecutingAssembly(); |
60 | ||
61 | 46 | Stream embeddedXsdStream = asm.GetManifestResourceStream( |
62 | embeddedXsdRes); | |
63 | ||
64 | 46 | XmlTextReader sr = new XmlTextReader(embeddedXsdStream); |
65 | ||
66 | 46 | ValidationEventHandler eHandler = new ValidationEventHandler( |
67 | XmlValidator_EventHandler); | |
68 | ||
69 | 46 | XmlSchema schema = XmlSchema.Read(sr,eHandler); |
70 | ||
71 | 46 | vr.Schemas.Add(schema); |
72 | 46 | vr.ValidationEventHandler += eHandler; |
73 | ||
74 | 46 | vr.ValidationType = ValidationType.Schema; |
75 | ||
76 | 931 | while (vr.Read()); |
77 | ||
78 | 46 | return validated; |
79 | } | |
80 | ||
81 | /// <summary> | |
82 | /// Event handler to handle validation events. | |
83 | /// </summary> | |
84 | /// <param name="sender">Object who sends event.</param> | |
85 | /// <param name="e">Validation event arguments.</param> | |
86 | 4 | public void XmlValidator_EventHandler(object sender, ValidationEventArgs e) |
87 | { | |
88 | 4 | validated = false; |
89 | 4 | log.Fatal(string.Format("Validation error: {0}. Severity: {1}." |
90 | ,e.Message,e.Severity)); | |
91 | } | |
92 | #endregion | |
93 | } | |
94 | } | |
95 |
|