1
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
2
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
6
using System.Xml.Schema;
8
namespace ICSharpCode.AvalonEdit.Highlighting.Xshd
11
/// Static class with helper methods to load XSHD highlighting files.
13
public static class HighlightingLoader
17
/// Lodas a syntax definition from the xml reader.
19
public static XshdSyntaxDefinition LoadXshd(XmlReader reader)
21
return LoadXshd(reader, false);
24
internal static XshdSyntaxDefinition LoadXshd(XmlReader reader, bool skipValidation)
27
throw new ArgumentNullException("reader");
29
reader.MoveToContent();
30
if (reader.NamespaceURI == V2Loader.Namespace) {
31
return V2Loader.LoadDefinition(reader, skipValidation);
33
return V1Loader.LoadDefinition(reader, skipValidation);
35
} catch (XmlSchemaException ex) {
36
throw WrapException(ex, ex.LineNumber, ex.LinePosition);
37
} catch (XmlException ex) {
38
throw WrapException(ex, ex.LineNumber, ex.LinePosition);
42
static Exception WrapException(Exception ex, int lineNumber, int linePosition)
44
return new HighlightingDefinitionInvalidException(FormatExceptionMessage(ex.Message, lineNumber, linePosition), ex);
47
internal static string FormatExceptionMessage(string message, int lineNumber, int linePosition)
52
return "Error at position (line " + lineNumber + ", column " + linePosition + "):\n" + message;
55
internal static XmlReader GetValidatingReader(XmlReader input, bool ignoreWhitespace, XmlSchemaSet schemaSet)
57
XmlReaderSettings settings = new XmlReaderSettings();
58
settings.CloseInput = true;
59
settings.IgnoreComments = true;
60
settings.IgnoreWhitespace = ignoreWhitespace;
61
if (schemaSet != null) {
62
settings.Schemas = schemaSet;
63
settings.ValidationType = ValidationType.Schema;
65
return XmlReader.Create(input, settings);
68
internal static XmlSchemaSet LoadSchemaSet(XmlReader schemaInput)
70
XmlSchemaSet schemaSet = new XmlSchemaSet();
71
schemaSet.Add(null, schemaInput);
72
schemaSet.ValidationEventHandler += delegate(object sender, ValidationEventArgs args) {
73
throw new HighlightingDefinitionInvalidException(args.Message);
79
#region Load Highlighting from XSHD
81
/// Creates a highlighting definition from the XSHD file.
83
public static IHighlightingDefinition Load(XshdSyntaxDefinition syntaxDefinition, IHighlightingDefinitionReferenceResolver resolver)
85
if (syntaxDefinition == null)
86
throw new ArgumentNullException("syntaxDefinition");
87
return new XmlHighlightingDefinition(syntaxDefinition, resolver);
91
/// Creates a highlighting definition from the XSHD file.
93
public static IHighlightingDefinition Load(XmlReader reader, IHighlightingDefinitionReferenceResolver resolver)
95
return Load(LoadXshd(reader), resolver);