~ubuntu-branches/ubuntu/oneiric/monodevelop/oneiric-updates

« back to all changes in this revision

Viewing changes to src/addins/AspNetAddIn/MonoDevelop.Html/HtmlSchemaService.cs

  • Committer: Bazaar Package Importer
  • Author(s): Jo Shields
  • Date: 2009-02-18 08:40:51 UTC
  • mfrom: (1.2.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20090218084051-gh8m6ukvokbwj7cf
Tags: 1.9.2+dfsg-1ubuntu1
* Merge from Debian Experimental (LP: #330519), remaining Ubuntu changes:
  + debian/control:
    - Update for Gnome# 2.24
    - Add libmono-cairo1.0-cil to build-deps to fool pkg-config check

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// 
 
2
// HtmlSchemaService.cs
 
3
// 
 
4
// Author:
 
5
//   Michael Hutchinson <mhutchinson@novell.com>
 
6
// 
 
7
// Copyright (C) 2008 Novell, Inc (http://www.novell.com)
 
8
// 
 
9
// Permission is hereby granted, free of charge, to any person obtaining
 
10
// a copy of this software and associated documentation files (the
 
11
// "Software"), to deal in the Software without restriction, including
 
12
// without limitation the rights to use, copy, modify, merge, publish,
 
13
// distribute, sublicense, and/or sell copies of the Software, and to
 
14
// permit persons to whom the Software is furnished to do so, subject to
 
15
// the following conditions:
 
16
// 
 
17
// The above copyright notice and this permission notice shall be
 
18
// included in all copies or substantial portions of the Software.
 
19
// 
 
20
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 
21
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 
22
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 
23
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 
24
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 
25
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 
26
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 
27
//
 
28
 
 
29
using System;
 
30
using System.Collections.Generic;
 
31
using System.IO;
 
32
 
 
33
using MonoDevelop.Core;
 
34
using MonoDevelop.Projects.Gui.Completion;
 
35
using MonoDevelop.XmlEditor.Completion;
 
36
 
 
37
namespace MonoDevelop.Html
 
38
{
 
39
        
 
40
        public static class HtmlSchemaService
 
41
        {
 
42
                static Dictionary<string, HtmlSchema> schemas;
 
43
                
 
44
                #region Service loading
 
45
                
 
46
                static object initLock = new object ();
 
47
                static System.Threading.Thread loadingThread;
 
48
                
 
49
                public static void Initialise ()
 
50
                {
 
51
                        if (schemas != null)
 
52
                                return;
 
53
                        
 
54
                        schemas = new Dictionary<string, HtmlSchema> ();
 
55
                        
 
56
                        //load all the schemas from addin points
 
57
                        //NOTE: the first ([0]) schema must be the default schema (HTML4 transitional)
 
58
                        string schemaDir = Path.GetDirectoryName (System.Reflection.Assembly.GetExecutingAssembly ().Location);
 
59
                        schemaDir = Path.Combine (schemaDir, "Schemas");
 
60
                        
 
61
                        foreach (DocTypeExtensionNode node in Mono.Addins.AddinManager.GetExtensionNodes ("/MonoDevelop/Html/DocTypes")) {
 
62
                                if (schemas.ContainsKey (node.Name))
 
63
                                        LoggingService.LogWarning (
 
64
                                            "HtmlSchemaService cannot register duplicate doctype with the name '{0}'", node.Name);
 
65
                                
 
66
                                if (!string.IsNullOrEmpty (node.XsdFile)) {
 
67
                                        string path = Path.Combine (schemaDir, node.XsdFile);
 
68
                                        try {
 
69
                                                IXmlCompletionProvider provider = new XmlSchemaCompletionData (path);
 
70
                                                schemas.Add (node.Name, new HtmlSchema (node.Name, node.FullName, provider));
 
71
                                        } catch (Exception ex) {
 
72
                                                LoggingService.LogWarning (
 
73
                                                    "HtmlSchemaService encountered an error registering the schema '" + path + "'", ex);
 
74
                                        }
 
75
                                } else {
 
76
                                        schemas.Add (node.Name, new HtmlSchema (node.Name, node.FullName, node.CompletionDocTypeName));
 
77
                                }
 
78
                        }
 
79
                        
 
80
                        //initialise the default backup schema if it doesn't exist already
 
81
                        if (!schemas.ContainsKey (DefaultDocTypeName)) {
 
82
                                HtmlSchema defaultSubstProvider = schemas["XHTML 1.0 Transitional"];
 
83
                                IXmlCompletionProvider provider;
 
84
                                if (defaultSubstProvider != null) {
 
85
                                        //start the threaded schema loading
 
86
                                        LoadSchema (defaultSubstProvider, true);
 
87
                                        provider = defaultSubstProvider.CompletionProvider;
 
88
                                } else {
 
89
                                        LoggingService.LogWarning ("Completion schema for default HTML doctype not found.");
 
90
                                        provider = new EmptyXmlCompletionProvider ();
 
91
                                }
 
92
                                
 
93
                                schemas[DefaultDocTypeName] = new HtmlSchema ("HTML 4.01 Transitional",
 
94
                                    "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
 
95
                                    + "\"http://www.w3.org/TR/html4/loose.dtd\">",
 
96
                                    provider);
 
97
                        }
 
98
                        
 
99
                        MonoDevelop.Core.LoggingService.LogDebug ("HtmlSchemaService initialised");
 
100
                }
 
101
                
 
102
                #endregion
 
103
                
 
104
                public static string DefaultDocTypeName {
 
105
                        get { return "HTML 4.01 Transitional"; }
 
106
                }
 
107
                
 
108
                public static HtmlSchema DefaultDocType {
 
109
                        get {
 
110
                                if (schemas == null)
 
111
                                        Initialise ();
 
112
                                return schemas[DefaultDocTypeName];
 
113
                        }
 
114
                }
 
115
                
 
116
                public static HtmlSchema GetSchema (string docType)
 
117
                {
 
118
                        return GetSchema (docType, false);
 
119
                }
 
120
                
 
121
                //if lazy==true, then if the schema is lazily compiled, it gets force-compiled in a thread and null is returned
 
122
                public static HtmlSchema GetSchema (string docType, bool lazy)
 
123
                {
 
124
                        if (schemas == null)
 
125
                                Initialise ();
 
126
                        
 
127
                        if (!string.IsNullOrEmpty (docType))
 
128
                                foreach (HtmlSchema schema in schemas.Values)
 
129
                                        if (docType.Contains (schema.Name))
 
130
                                                return LoadSchema (schema, lazy);
 
131
                        return null;
 
132
                }
 
133
                
 
134
                //if lazy == true, returns null if the schema isn't loaded yet
 
135
                static HtmlSchema LoadSchema (HtmlSchema schema, bool lazy)
 
136
                {
 
137
                        ILazilyLoadedProvider lazyProv = schema.CompletionProvider as ILazilyLoadedProvider;
 
138
                        if (lazyProv == null || lazyProv.IsLoaded) {
 
139
                                return schema;
 
140
                        } else {
 
141
                                //FIXME: actually implement threaded loading and return null if loading && lazy
 
142
                                lazyProv.EnsureLoaded ();
 
143
                                return schema;
 
144
                        }
 
145
                }
 
146
                
 
147
                public static IXmlCompletionProvider LazyGetProvider (HtmlSchema schema)
 
148
                {
 
149
                        //get the provided schema, if it's loaded
 
150
                        HtmlSchema hs = LoadSchema (schema, true);
 
151
                        //fall back to the defaul provider
 
152
                        if (hs == null)
 
153
                                hs = LoadSchema (DefaultDocType, true);
 
154
                        //fall back to a blank provider
 
155
                        return new EmptyXmlCompletionProvider ();
 
156
                }
 
157
                
 
158
                public static IEnumerable<DocTypeCompletionData> DocTypeCompletionData {
 
159
                        get {
 
160
                                if (schemas == null)
 
161
                                        Initialise ();
 
162
                                
 
163
                                foreach (HtmlSchema item in schemas.Values)
 
164
                                        yield return new DocTypeCompletionData (item.Name, item.DocType);
 
165
                        }
 
166
                }
 
167
        }
 
168
}