~halega/+junk/sharpdevelop

« back to all changes in this revision

Viewing changes to src/AddIns/DisplayBindings/AvalonEdit.AddIn/Src/SyntaxModeDoozer.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.Collections;
 
6
using System.IO;
 
7
using System.Linq;
 
8
using System.Reflection;
 
9
using System.Xml;
 
10
 
 
11
using ICSharpCode.AvalonEdit.Highlighting;
 
12
using ICSharpCode.AvalonEdit.Highlighting.Xshd;
 
13
using ICSharpCode.Core;
 
14
 
 
15
namespace ICSharpCode.AvalonEdit.AddIn
 
16
{
 
17
        public class AddInTreeSyntaxMode
 
18
        {
 
19
                ICSharpCode.Core.AddIn addin;
 
20
                string resourceName;
 
21
                
 
22
                public string Name { get; private set; }
 
23
                public string[] Extensions { get; private set; }
 
24
                
 
25
                public AddInTreeSyntaxMode(ICSharpCode.Core.AddIn addin, string resourceName, string name, string[] extensions)
 
26
                {
 
27
                        if (addin == null)
 
28
                                throw new ArgumentNullException("addin");
 
29
                        if (resourceName == null)
 
30
                                throw new ArgumentNullException("resourceName");
 
31
                        if (name == null)
 
32
                                throw new ArgumentNullException("name");
 
33
                        if (extensions == null)
 
34
                                throw new ArgumentNullException("extensions");
 
35
                        
 
36
                        this.addin = addin;
 
37
                        this.resourceName = resourceName;
 
38
                        this.Name = name;
 
39
                        this.Extensions = extensions;
 
40
                }
 
41
                
 
42
                public XmlReader CreateXmlReader()
 
43
                {
 
44
                        Stream stream = addin.GetManifestResourceStream(resourceName);
 
45
                        if (stream != null) {
 
46
                                return new XmlTextReader(stream);
 
47
                        } else {
 
48
                                throw new InvalidOperationException("Could not find resource '" + resourceName + "' in any of the AddIn runtime assemblies.");
 
49
                        }
 
50
                }
 
51
                
 
52
                public XshdSyntaxDefinition LoadXshd()
 
53
                {
 
54
                        using (XmlReader reader = CreateXmlReader()) {
 
55
                                XshdSyntaxDefinition xshd = HighlightingLoader.LoadXshd(reader);
 
56
                                if (xshd.Name != this.Name)
 
57
                                        throw new InvalidOperationException("Loaded XSHD has name '" + xshd.Name + "', but expected was '" + this.Name + "'.");
 
58
                                if (!Enumerable.SequenceEqual(xshd.Extensions, this.Extensions))
 
59
                                        throw new InvalidOperationException("Loaded XSHD has extensions '" + string.Join(";", xshd.Extensions) + "', but expected was '" + string.Join(";", this.Extensions) + "'.");
 
60
                                return xshd;
 
61
                        }
 
62
                }
 
63
                
 
64
                public void Register(HighlightingManager manager)
 
65
                {
 
66
                        manager.RegisterHighlighting(
 
67
                                this.Name, this.Extensions, delegate {
 
68
                                        return HighlightingLoader.Load(LoadXshd(), manager);
 
69
                                });
 
70
                }
 
71
        }
 
72
        
 
73
        /// <summary>
 
74
        /// Creates AddInTreeSyntaxMode objects that wrap a .xshd syntax mode stored as resource in the
 
75
        /// addin assembly.
 
76
        /// </summary>
 
77
        /// <attribute name="name" use="required">
 
78
        /// Name of the language for which the syntax mode is used.
 
79
        /// </attribute>
 
80
        /// <attribute name="extensions" use="required">
 
81
        /// Semicolon-separated list of file extensions for which the syntax mode is used.
 
82
        /// </attribute>
 
83
        /// <attribute name="resource" use="required">
 
84
        /// Fully qualified name of the resource file.
 
85
        /// </attribute>
 
86
        /// <usage>Only in /SharpDevelop/ViewContent/AvalonEdit/SyntaxModes</usage>
 
87
        /// <returns>
 
88
        /// An AddInTreeSyntaxMode object that loads the resource from the addin assembly when
 
89
        /// its CreateTextReader method is called.
 
90
        /// </returns>
 
91
        public class SyntaxModeDoozer : IDoozer
 
92
        {
 
93
                public const string Path = "/SharpDevelop/ViewContent/AvalonEdit/SyntaxModes";
 
94
                
 
95
                /// <summary>
 
96
                /// Gets if the doozer handles codon conditions on its own.
 
97
                /// If this property return false, the item is excluded when the condition is not met.
 
98
                /// </summary>
 
99
                public bool HandleConditions {
 
100
                        get { return false; }
 
101
                }
 
102
                
 
103
                public object BuildItem(BuildItemArgs args)
 
104
                {
 
105
                        Codon codon = args.Codon;
 
106
                        string   highlightingName = codon.Properties["name"];
 
107
                        string[] extensions       = codon.Properties["extensions"].Split(';');
 
108
                        string   resource         = codon.Properties["resource"];
 
109
                        
 
110
                        return new AddInTreeSyntaxMode(codon.AddIn, resource, highlightingName, extensions);
 
111
                }
 
112
        }
 
113
}