2 // InferredXmlCompletionProvider.cs
5 // Michael Hutchinson <mhutchinson@novell.com>
7 // Copyright (c) 2009 Novell, Inc. (http://www.novell.com)
9 // Permission is hereby granted, free of charge, to any person obtaining a copy
10 // of this software and associated documentation files (the "Software"), to deal
11 // in the Software without restriction, including without limitation the rights
12 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 // copies of the Software, and to permit persons to whom the Software is
14 // furnished to do so, subject to the following conditions:
16 // The above copyright notice and this permission notice shall be included in
17 // all copies or substantial portions of the Software.
19 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
28 using System
.Collections
.Generic
;
29 using MonoDevelop
.Projects
.Gui
.Completion
;
30 using MonoDevelop
.Xml
.StateEngine
;
32 namespace MonoDevelop
.XmlEditor
.Completion
36 public class InferredXmlCompletionProvider
: IXmlCompletionProvider
38 Dictionary
<string,HashSet
<string>> elementCompletions
= new Dictionary
<string,HashSet
<string>> ();
39 Dictionary
<string,HashSet
<string>> attributeCompletions
= new Dictionary
<string,HashSet
<string>> ();
41 public DateTime TimeStamp { get; set; }
42 public int ErrorCount { get; set; }
44 public InferredXmlCompletionProvider ()
48 //TODO: respect namespaces
49 public void Populate (XDocument doc
)
51 foreach (XNode node
in doc
.AllDescendentNodes
) {
52 XElement el
= node
as XElement
;
55 string parentName
= "";
56 XElement parentEl
= el
.Parent
as XElement
;
58 parentName
= parentEl
.Name
.Name
;
61 if (!elementCompletions
.TryGetValue (parentName
, out map
)) {
62 map
= new HashSet
<string> ();
63 elementCompletions
.Add (parentName
, map
);
65 map
.Add (el
.Name
.Name
);
67 if (!attributeCompletions
.TryGetValue (el
.Name
.Name
, out map
)) {
68 map
= new HashSet
<string> ();
69 attributeCompletions
.Add (el
.Name
.Name
, map
);
71 foreach (XAttribute att
in el
.Attributes
)
72 map
.Add (att
.Name
.Name
);
76 public ICompletionData
[] GetElementCompletionData ()
78 return GetChildElementCompletionData ("");
81 public ICompletionData
[] GetElementCompletionData (string namespacePrefix
)
83 return new ICompletionData
[0];
86 public ICompletionData
[] GetChildElementCompletionData (XmlElementPath path
)
88 return GetCompletions (elementCompletions
, path
, XmlCompletionData
.DataType
.XmlElement
);
91 public ICompletionData
[] GetAttributeCompletionData (XmlElementPath path
)
93 return GetCompletions (attributeCompletions
, path
, XmlCompletionData
.DataType
.XmlAttribute
);
96 public ICompletionData
[] GetAttributeValueCompletionData (XmlElementPath path
, string name
)
98 return new ICompletionData
[0];
101 public ICompletionData
[] GetChildElementCompletionData (string tagName
)
103 return GetCompletions (elementCompletions
, tagName
, XmlCompletionData
.DataType
.XmlElement
);
106 public ICompletionData
[] GetAttributeCompletionData (string tagName
)
108 return GetCompletions (attributeCompletions
, tagName
, XmlCompletionData
.DataType
.XmlAttribute
);
111 public ICompletionData
[] GetAttributeValueCompletionData (string tagName
, string name
)
113 return new ICompletionData
[0];
116 static ICompletionData
[] GetCompletions (Dictionary
<string,HashSet
<string>> map
, string tagName
, XmlCompletionData
.DataType type
)
118 HashSet
<string> values
;
119 if (!map
.TryGetValue (tagName
, out values
))
120 return new ICompletionData
[0];
121 ICompletionData
[] data
= new ICompletionData
[values
.Count
];
123 foreach (string s
in values
)
124 data
[i
++] = new XmlCompletionData (s
, type
);
128 static ICompletionData
[] GetCompletions (Dictionary
<string,HashSet
<string>> map
, XmlElementPath path
, XmlCompletionData
.DataType type
)
130 if (path
== null || path
.Elements
.Count
== 0)
131 return new ICompletionData
[0];
132 return GetCompletions (map
, path
.Elements
[path
.Elements
.Count
- 1].Name
, type
);