Code Coverage Statistics for Source File
c:\Tools\SD3\src\Libraries\ICSharpCode.TextEditor\Project\Src\Document\HighlightingStrategy\HighlightingDefinitionParser.cs
|
Sequence Point Coverage
N/A
0 of 0
|
Branch Coverage
N/A
0 of 0
|
Lines
110
|
Highlight:
Uncovered Code
Covered Code
| L | V | Source |
|---|---|---|
1 |
// <file> |
|
2 |
// <copyright see="prj:///doc/copyright.txt"/> |
|
3 |
// <license see="prj:///doc/license.txt"/> |
|
4 |
// <owner name="Mike Krüger" email="mike@icsharpcode.net"/> |
|
5 |
// <version>$Revision: 2533 $</version> |
|
6 |
// </file> |
|
7 |
||
8 |
using System; |
|
9 |
using System.Collections.Generic; |
|
10 |
using System.IO; |
|
11 |
using System.Text; |
|
12 |
using System.Windows.Forms; |
|
13 |
using System.Xml; |
|
14 |
using System.Xml.Schema; |
|
15 |
||
16 |
namespace ICSharpCode.TextEditor.Document |
|
17 |
{ |
|
18 |
public static class HighlightingDefinitionParser |
|
19 |
{ |
|
20 |
public static DefaultHighlightingStrategy Parse(SyntaxMode syntaxMode, XmlReader xmlReader) |
|
21 |
{ |
|
22 |
return Parse(null, syntaxMode, xmlReader); |
|
23 |
} |
|
24 |
||
25 |
public static DefaultHighlightingStrategy Parse(DefaultHighlightingStrategy highlighter, SyntaxMode syntaxMode, XmlReader xmlReader) |
|
26 |
{ |
|
27 |
if (syntaxMode == null) |
|
28 |
throw new ArgumentNullException("syntaxMode"); |
|
29 |
if (xmlReader == null) |
|
30 |
throw new ArgumentNullException("xmlTextReader"); |
|
31 |
try { |
|
32 |
List<ValidationEventArgs> errors = null; |
|
33 |
XmlReaderSettings settings = new XmlReaderSettings(); |
|
34 |
Stream shemaStream = typeof(HighlightingDefinitionParser).Assembly.GetManifestResourceStream("ICSharpCode.TextEditor.Resources.Mode.xsd"); |
|
35 |
settings.Schemas.Add("", new XmlTextReader(shemaStream)); |
|
36 |
settings.Schemas.ValidationEventHandler += delegate(object sender, ValidationEventArgs args) { |
|
37 |
if (errors == null) { |
|
38 |
errors = new List<ValidationEventArgs>(); |
|
39 |
} |
|
40 |
errors.Add(args); |
|
41 |
}; |
|
42 |
settings.ValidationType = ValidationType.Schema; |
|
43 |
XmlReader validatingReader = XmlReader.Create(xmlReader, settings); |
|
44 |
||
45 |
XmlDocument doc = new XmlDocument(); |
|
46 |
doc.Load(validatingReader); |
|
47 |
||
48 |
if (highlighter == null) |
|
49 |
highlighter = new DefaultHighlightingStrategy(doc.DocumentElement.Attributes["name"].InnerText); |
|
50 |
||
51 |
if (doc.DocumentElement.HasAttribute("extends")) { |
|
52 |
KeyValuePair<SyntaxMode, ISyntaxModeFileProvider> entry = HighlightingManager.Manager.FindHighlighterEntry(doc.DocumentElement.GetAttribute("extends")); |
|
53 |
if (entry.Key == null) { |
|
54 |
throw new HighlightingDefinitionInvalidException("Cannot find referenced highlighting source " + doc.DocumentElement.GetAttribute("extends")); |
|
55 |
} else { |
|
56 |
highlighter = Parse(highlighter, entry.Key, entry.Value.GetSyntaxModeFile(entry.Key)); |
|
57 |
if (highlighter == null) return null; |
|
58 |
} |
|
59 |
} |
|
60 |
if (doc.DocumentElement.HasAttribute("extensions")) { |
|
61 |
highlighter.Extensions = doc.DocumentElement.GetAttribute("extensions").Split(new char[] { ';', '|' }); |
|
62 |
} |
|
63 |
||
64 |
XmlElement environment = doc.DocumentElement["Environment"]; |
|
65 |
if (environment != null) { |
|
66 |
foreach (XmlNode node in environment.ChildNodes) { |
|
67 |
if (node is XmlElement) { |
|
68 |
XmlElement el = (XmlElement)node; |
|
69 |
if (el.Name == "Custom") { |
|
70 |
highlighter.SetColorFor(el.GetAttribute("name"), el.HasAttribute("bgcolor") ? new HighlightBackground(el) : new HighlightColor(el)); |
|
71 |
} else { |
|
72 |
highlighter.SetColorFor(el.Name, el.HasAttribute("bgcolor") ? new HighlightBackground(el) : new HighlightColor(el)); |
|
73 |
} |
|
74 |
} |
|
75 |
} |
|
76 |
} |
|
77 |
||
78 |
// parse properties |
|
79 |
if (doc.DocumentElement["Properties"]!= null) { |
|
80 |
foreach (XmlElement propertyElement in doc.DocumentElement["Properties"].ChildNodes) { |
|
81 |
highlighter.Properties[propertyElement.Attributes["name"].InnerText] = propertyElement.Attributes["value"].InnerText; |
|
82 |
} |
|
83 |
} |
|
84 |
||
85 |
if (doc.DocumentElement["Digits"]!= null) { |
|
86 |
highlighter.DigitColor = new HighlightColor(doc.DocumentElement["Digits"]); |
|
87 |
} |
|
88 |
||
89 |
XmlNodeList nodes = doc.DocumentElement.GetElementsByTagName("RuleSet"); |
|
90 |
foreach (XmlElement element in nodes) { |
|
91 |
highlighter.AddRuleSet(new HighlightRuleSet(element)); |
|
92 |
} |
|
93 |
||
94 |
xmlReader.Close(); |
|
95 |
||
96 |
if (errors != null) { |
|
97 |
StringBuilder msg = new StringBuilder(); |
|
98 |
foreach (ValidationEventArgs args in errors) { |
|
99 |
msg.AppendLine(args.Message); |
|
100 |
} |
|
101 |
throw new HighlightingDefinitionInvalidException(msg.ToString()); |
|
102 |
} else { |
|
103 |
return highlighter; |
|
104 |
} |
|
105 |
} catch (Exception e) { |
|
106 |
throw new HighlightingDefinitionInvalidException("Could not load mode definition file '" + syntaxMode.FileName + "'.\n", e); |
|
107 |
} |
|
108 |
} |
|
109 |
} |
|
110 |
} |