Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2839_HiveProjectManagement/HeuristicLab.ExtLibs/HeuristicLab.AvalonEdit/5.0.1/AvalonEdit-5.0.1/Highlighting/Xshd/HighlightingLoader.cs @ 15866

Last change on this file since 15866 was 11700, checked in by jkarder, 10 years ago

#2077: created branch and added first version

File size: 4.1 KB
Line 
1// Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
2//
3// Permission is hereby granted, free of charge, to any person obtaining a copy of this
4// software and associated documentation files (the "Software"), to deal in the Software
5// without restriction, including without limitation the rights to use, copy, modify, merge,
6// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
7// to whom the Software is furnished to do so, subject to the following conditions:
8//
9// The above copyright notice and this permission notice shall be included in all copies or
10// substantial portions of the Software.
11//
12// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
13// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
15// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
16// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
17// DEALINGS IN THE SOFTWARE.
18
19using System;
20using System.Xml;
21using System.Xml.Schema;
22
23namespace ICSharpCode.AvalonEdit.Highlighting.Xshd
24{
25  /// <summary>
26  /// Static class with helper methods to load XSHD highlighting files.
27  /// </summary>
28  public static class HighlightingLoader
29  {
30    #region XSHD loading
31    /// <summary>
32    /// Lodas a syntax definition from the xml reader.
33    /// </summary>
34    public static XshdSyntaxDefinition LoadXshd(XmlReader reader)
35    {
36      return LoadXshd(reader, false);
37    }
38   
39    internal static XshdSyntaxDefinition LoadXshd(XmlReader reader, bool skipValidation)
40    {
41      if (reader == null)
42        throw new ArgumentNullException("reader");
43      try {
44        reader.MoveToContent();
45        if (reader.NamespaceURI == V2Loader.Namespace) {
46          return V2Loader.LoadDefinition(reader, skipValidation);
47        } else {
48          return V1Loader.LoadDefinition(reader, skipValidation);
49        }
50      } catch (XmlSchemaException ex) {
51        throw WrapException(ex, ex.LineNumber, ex.LinePosition);
52      } catch (XmlException ex) {
53        throw WrapException(ex, ex.LineNumber, ex.LinePosition);
54      }
55    }
56   
57    static Exception WrapException(Exception ex, int lineNumber, int linePosition)
58    {
59      return new HighlightingDefinitionInvalidException(FormatExceptionMessage(ex.Message, lineNumber, linePosition), ex);
60    }
61   
62    internal static string FormatExceptionMessage(string message, int lineNumber, int linePosition)
63    {
64      if (lineNumber <= 0)
65        return message;
66      else
67        return "Error at position (line " + lineNumber + ", column " + linePosition + "):\n" + message;
68    }
69   
70    internal static XmlReader GetValidatingReader(XmlReader input, bool ignoreWhitespace, XmlSchemaSet schemaSet)
71    {
72      XmlReaderSettings settings = new XmlReaderSettings();
73      settings.CloseInput = true;
74      settings.IgnoreComments = true;
75      settings.IgnoreWhitespace = ignoreWhitespace;
76      if (schemaSet != null) {
77        settings.Schemas = schemaSet;
78        settings.ValidationType = ValidationType.Schema;
79      }
80      return XmlReader.Create(input, settings);
81    }
82   
83    internal static XmlSchemaSet LoadSchemaSet(XmlReader schemaInput)
84    {
85      XmlSchemaSet schemaSet = new XmlSchemaSet();
86      schemaSet.Add(null, schemaInput);
87      schemaSet.ValidationEventHandler += delegate(object sender, ValidationEventArgs args) {
88        throw new HighlightingDefinitionInvalidException(args.Message);
89      };
90      return schemaSet;
91    }
92    #endregion
93   
94    #region Load Highlighting from XSHD
95    /// <summary>
96    /// Creates a highlighting definition from the XSHD file.
97    /// </summary>
98    public static IHighlightingDefinition Load(XshdSyntaxDefinition syntaxDefinition, IHighlightingDefinitionReferenceResolver resolver)
99    {
100      if (syntaxDefinition == null)
101        throw new ArgumentNullException("syntaxDefinition");
102      return new XmlHighlightingDefinition(syntaxDefinition, resolver);
103    }
104   
105    /// <summary>
106    /// Creates a highlighting definition from the XSHD file.
107    /// </summary>
108    public static IHighlightingDefinition Load(XmlReader reader, IHighlightingDefinitionReferenceResolver resolver)
109    {
110      return Load(LoadXshd(reader), resolver);
111    }
112    #endregion
113  }
114}
Note: See TracBrowser for help on using the repository browser.