2
// HtmlSchemaService.cs
5
// Michael Hutchinson <mhutchinson@novell.com>
7
// Copyright (C) 2008 Novell, Inc (http://www.novell.com)
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:
17
// The above copyright notice and this permission notice shall be
18
// included in all copies or substantial portions of the Software.
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.
30
using System.Collections.Generic;
33
using MonoDevelop.Core;
34
using MonoDevelop.Projects.Gui.Completion;
35
using MonoDevelop.XmlEditor.Completion;
37
namespace MonoDevelop.Html
40
public static class HtmlSchemaService
42
static Dictionary<string, HtmlSchema> schemas;
44
#region Service loading
46
static object initLock = new object ();
47
static System.Threading.Thread loadingThread;
49
public static void Initialise ()
54
schemas = new Dictionary<string, HtmlSchema> ();
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");
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);
66
if (!string.IsNullOrEmpty (node.XsdFile)) {
67
string path = Path.Combine (schemaDir, node.XsdFile);
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);
76
schemas.Add (node.Name, new HtmlSchema (node.Name, node.FullName, node.CompletionDocTypeName));
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;
89
LoggingService.LogWarning ("Completion schema for default HTML doctype not found.");
90
provider = new EmptyXmlCompletionProvider ();
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\">",
99
MonoDevelop.Core.LoggingService.LogDebug ("HtmlSchemaService initialised");
104
public static string DefaultDocTypeName {
105
get { return "HTML 4.01 Transitional"; }
108
public static HtmlSchema DefaultDocType {
112
return schemas[DefaultDocTypeName];
116
public static HtmlSchema GetSchema (string docType)
118
return GetSchema (docType, false);
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)
127
if (!string.IsNullOrEmpty (docType))
128
foreach (HtmlSchema schema in schemas.Values)
129
if (docType.Contains (schema.Name))
130
return LoadSchema (schema, lazy);
134
//if lazy == true, returns null if the schema isn't loaded yet
135
static HtmlSchema LoadSchema (HtmlSchema schema, bool lazy)
137
ILazilyLoadedProvider lazyProv = schema.CompletionProvider as ILazilyLoadedProvider;
138
if (lazyProv == null || lazyProv.IsLoaded) {
141
//FIXME: actually implement threaded loading and return null if loading && lazy
142
lazyProv.EnsureLoaded ();
147
public static IXmlCompletionProvider LazyGetProvider (HtmlSchema schema)
149
//get the provided schema, if it's loaded
150
HtmlSchema hs = LoadSchema (schema, true);
151
//fall back to the defaul provider
153
hs = LoadSchema (DefaultDocType, true);
154
//fall back to a blank provider
155
return new EmptyXmlCompletionProvider ();
158
public static IEnumerable<DocTypeCompletionData> DocTypeCompletionData {
163
foreach (HtmlSchema item in schemas.Values)
164
yield return new DocTypeCompletionData (item.Name, item.DocType);