~halega/+junk/sharpdevelop

« back to all changes in this revision

Viewing changes to src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Highlighting/Xshd/HighlightingLoader.cs

  • Committer: sk
  • Date: 2011-09-10 05:17:57 UTC
  • Revision ID: halega@halega.com-20110910051757-qfouz1llya9m6boy
4.1.0.7915 Release Candidate 1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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)
 
3
 
 
4
using System;
 
5
using System.Xml;
 
6
using System.Xml.Schema;
 
7
 
 
8
namespace ICSharpCode.AvalonEdit.Highlighting.Xshd
 
9
{
 
10
        /// <summary>
 
11
        /// Static class with helper methods to load XSHD highlighting files.
 
12
        /// </summary>
 
13
        public static class HighlightingLoader
 
14
        {
 
15
                #region XSHD loading
 
16
                /// <summary>
 
17
                /// Lodas a syntax definition from the xml reader.
 
18
                /// </summary>
 
19
                public static XshdSyntaxDefinition LoadXshd(XmlReader reader)
 
20
                {
 
21
                        return LoadXshd(reader, false);
 
22
                }
 
23
                
 
24
                internal static XshdSyntaxDefinition LoadXshd(XmlReader reader, bool skipValidation)
 
25
                {
 
26
                        if (reader == null)
 
27
                                throw new ArgumentNullException("reader");
 
28
                        try {
 
29
                                reader.MoveToContent();
 
30
                                if (reader.NamespaceURI == V2Loader.Namespace) {
 
31
                                        return V2Loader.LoadDefinition(reader, skipValidation);
 
32
                                } else {
 
33
                                        return V1Loader.LoadDefinition(reader, skipValidation);
 
34
                                }
 
35
                        } catch (XmlSchemaException ex) {
 
36
                                throw WrapException(ex, ex.LineNumber, ex.LinePosition);
 
37
                        } catch (XmlException ex) {
 
38
                                throw WrapException(ex, ex.LineNumber, ex.LinePosition);
 
39
                        }
 
40
                }
 
41
                
 
42
                static Exception WrapException(Exception ex, int lineNumber, int linePosition)
 
43
                {
 
44
                        return new HighlightingDefinitionInvalidException(FormatExceptionMessage(ex.Message, lineNumber, linePosition), ex);
 
45
                }
 
46
                
 
47
                internal static string FormatExceptionMessage(string message, int lineNumber, int linePosition)
 
48
                {
 
49
                        if (lineNumber <= 0)
 
50
                                return message;
 
51
                        else
 
52
                                return "Error at position (line " + lineNumber + ", column " + linePosition + "):\n" + message;
 
53
                }
 
54
                
 
55
                internal static XmlReader GetValidatingReader(XmlReader input, bool ignoreWhitespace, XmlSchemaSet schemaSet)
 
56
                {
 
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;
 
64
                        }
 
65
                        return XmlReader.Create(input, settings);
 
66
                }
 
67
                
 
68
                internal static XmlSchemaSet LoadSchemaSet(XmlReader schemaInput)
 
69
                {
 
70
                        XmlSchemaSet schemaSet = new XmlSchemaSet();
 
71
                        schemaSet.Add(null, schemaInput);
 
72
                        schemaSet.ValidationEventHandler += delegate(object sender, ValidationEventArgs args) {
 
73
                                throw new HighlightingDefinitionInvalidException(args.Message);
 
74
                        };
 
75
                        return schemaSet;
 
76
                }
 
77
                #endregion
 
78
                
 
79
                #region Load Highlighting from XSHD
 
80
                /// <summary>
 
81
                /// Creates a highlighting definition from the XSHD file.
 
82
                /// </summary>
 
83
                public static IHighlightingDefinition Load(XshdSyntaxDefinition syntaxDefinition, IHighlightingDefinitionReferenceResolver resolver)
 
84
                {
 
85
                        if (syntaxDefinition == null)
 
86
                                throw new ArgumentNullException("syntaxDefinition");
 
87
                        return new XmlHighlightingDefinition(syntaxDefinition, resolver);
 
88
                }
 
89
                
 
90
                /// <summary>
 
91
                /// Creates a highlighting definition from the XSHD file.
 
92
                /// </summary>
 
93
                public static IHighlightingDefinition Load(XmlReader reader, IHighlightingDefinitionReferenceResolver resolver)
 
94
                {
 
95
                        return Load(LoadXshd(reader), resolver);
 
96
                }
 
97
                #endregion
 
98
        }
 
99
}