2
// MonoDevelop XML Editor
4
// Copyright (C) 2006 Matthew Ward
6
// Permission is hereby granted, free of charge, to any person obtaining
7
// a copy of this software and associated documentation files (the
8
// "Software"), to deal in the Software without restriction, including
9
// without limitation the rights to use, copy, modify, merge, publish,
10
// distribute, sublicense, and/or sell copies of the Software, and to
11
// permit persons to whom the Software is furnished to do so, subject to
12
// the following conditions:
14
// The above copyright notice and this permission notice shall be
15
// included in all copies or substantial portions of the Software.
17
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27
using System.Xml.XPath;
29
namespace MonoDevelop.XmlEditor
32
/// Stores an XmlNode and its associated line number and position after an
33
/// XPath query has been evaluated.
35
public class XPathNodeMatch : IXmlLineInfo
41
XPathNodeType nodeType;
44
/// Creates an XPathNodeMatch from the navigator which should be position on the
48
/// We deliberately use the OuterXml when we find a Namespace since the
49
/// navigator location returned starts from the xmlns attribute.
51
public XPathNodeMatch(XPathNavigator currentNavigator)
53
SetLineNumbers(currentNavigator as IXmlLineInfo);
54
nodeType = currentNavigator.NodeType;
56
case XPathNodeType.Text:
57
SetTextValue(currentNavigator);
59
case XPathNodeType.Comment:
60
SetCommentValue(currentNavigator);
62
case XPathNodeType.Namespace:
63
SetNamespaceValue(currentNavigator);
65
case XPathNodeType.Element:
66
SetElementValue(currentNavigator);
68
case XPathNodeType.ProcessingInstruction:
69
SetProcessingInstructionValue(currentNavigator);
71
case XPathNodeType.Attribute:
72
SetAttributeValue(currentNavigator);
75
value = currentNavigator.LocalName;
82
/// Line numbers are zero based.
84
public int LineNumber {
86
return lineNumber.GetValueOrDefault(0);
91
/// Line positions are zero based.
93
public int LinePosition {
99
public bool HasLineInfo()
101
return lineNumber.HasValue;
105
/// Gets the text value of the node.
107
public string Value {
114
/// Gets the node display value. This includes the angle brackets if it is
115
/// an element, for example.
117
public string DisplayValue {
123
public XPathNodeType NodeType {
129
void SetElementValue(XPathNavigator navigator)
131
value = navigator.Name;
132
if (navigator.IsEmptyElement) {
133
displayValue = String.Concat("<", value, "/>");
135
displayValue = String.Concat("<", value, ">");
139
void SetTextValue(XPathNavigator navigator)
141
value = navigator.Value;
142
displayValue = value;
145
void SetCommentValue(XPathNavigator navigator)
147
value = navigator.Value;
148
displayValue = navigator.OuterXml;
151
void SetNamespaceValue(XPathNavigator navigator)
153
value = navigator.OuterXml;
154
displayValue = value;
157
void SetProcessingInstructionValue(XPathNavigator navigator)
159
value = navigator.Name;
160
displayValue = navigator.OuterXml;
163
void SetAttributeValue(XPathNavigator navigator)
165
value = navigator.Name;
166
displayValue = String.Concat("@", value);
170
/// Takes one of the xml line number so the numbers are now zero
171
/// based instead of one based.
173
/// <remarks>A namespace query (e.g. //namespace::*) will return
174
/// a line info of -1, -1 for the xml namespace. Which looks like
175
/// a bug in the XPathDocument class.</remarks>
176
void SetLineNumbers(IXmlLineInfo lineInfo)
178
if (lineInfo.HasLineInfo() && lineInfo.LineNumber > 0) {
179
lineNumber = lineInfo.LineNumber - 1;
180
linePosition = lineInfo.LinePosition - 1;