~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to src/addins/MonoDevelop.XmlEditor/MonoDevelop.XmlEditor.XPath/XPathNodeMatch.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
// MonoDevelop XML Editor
 
3
//
 
4
// Copyright (C) 2006 Matthew Ward
 
5
//
 
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:
 
13
// 
 
14
// The above copyright notice and this permission notice shall be
 
15
// included in all copies or substantial portions of the Software.
 
16
// 
 
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.
 
24
 
 
25
using System;
 
26
using System.Xml;
 
27
using System.Xml.XPath;
 
28
 
 
29
namespace MonoDevelop.XmlEditor
 
30
{
 
31
        /// <summary>
 
32
        /// Stores an XmlNode and its associated line number and position after an 
 
33
        /// XPath query has been evaluated.
 
34
        /// </summary>
 
35
        public class XPathNodeMatch : IXmlLineInfo
 
36
        {
 
37
                int? lineNumber;
 
38
                int linePosition;
 
39
                string value;
 
40
                string displayValue;
 
41
                XPathNodeType nodeType;
 
42
                
 
43
                /// <summary>
 
44
                /// Creates an XPathNodeMatch from the navigator which should be position on the
 
45
                /// node.
 
46
                /// </summary>
 
47
                /// <remarks>
 
48
                /// We deliberately use the OuterXml when we find a Namespace since the
 
49
                /// navigator location returned starts from the xmlns attribute.
 
50
                /// </remarks>
 
51
                public XPathNodeMatch(XPathNavigator currentNavigator)
 
52
                {
 
53
                        SetLineNumbers(currentNavigator as IXmlLineInfo);
 
54
                        nodeType = currentNavigator.NodeType;
 
55
                        switch (nodeType) {
 
56
                                case XPathNodeType.Text:
 
57
                                        SetTextValue(currentNavigator);
 
58
                                        break;
 
59
                                case XPathNodeType.Comment:
 
60
                                        SetCommentValue(currentNavigator);
 
61
                                        break;
 
62
                                case XPathNodeType.Namespace:
 
63
                                        SetNamespaceValue(currentNavigator);
 
64
                                        break;
 
65
                                case XPathNodeType.Element:
 
66
                                        SetElementValue(currentNavigator);
 
67
                                        break;
 
68
                                case XPathNodeType.ProcessingInstruction:
 
69
                                        SetProcessingInstructionValue(currentNavigator);
 
70
                                        break;
 
71
                                case XPathNodeType.Attribute:
 
72
                                        SetAttributeValue(currentNavigator);
 
73
                                        break;
 
74
                                default:
 
75
                                        value = currentNavigator.LocalName;
 
76
                                        displayValue = value;
 
77
                                        break;
 
78
                        }
 
79
                }
 
80
                
 
81
                /// <summary>
 
82
                /// Line numbers are zero based.
 
83
                /// </summary>
 
84
                public int LineNumber {
 
85
                        get {
 
86
                                return lineNumber.GetValueOrDefault(0);
 
87
                        }
 
88
                }
 
89
                
 
90
                /// <summary>
 
91
                /// Line positions are zero based.
 
92
                /// </summary>
 
93
                public int LinePosition {
 
94
                        get {
 
95
                                return linePosition;
 
96
                        }
 
97
                }
 
98
                
 
99
                public bool HasLineInfo()
 
100
                {
 
101
                        return lineNumber.HasValue;
 
102
                }
 
103
                
 
104
                /// <summary>
 
105
                /// Gets the text value of the node.
 
106
                /// </summary>
 
107
                public string Value {
 
108
                        get {
 
109
                                return value;
 
110
                        }
 
111
                }
 
112
                
 
113
                /// <summary>
 
114
                /// Gets the node display value. This includes the angle brackets if it is
 
115
                /// an element, for example.
 
116
                /// </summary>
 
117
                public string DisplayValue {
 
118
                        get {
 
119
                                return displayValue;
 
120
                        }
 
121
                }
 
122
                
 
123
                public XPathNodeType NodeType {
 
124
                        get {
 
125
                                return nodeType;
 
126
                        }
 
127
                }
 
128
                
 
129
                void SetElementValue(XPathNavigator navigator)
 
130
                {
 
131
                        value = navigator.Name;
 
132
                        if (navigator.IsEmptyElement) {
 
133
                                displayValue = String.Concat("<", value, "/>");
 
134
                        } else {
 
135
                                displayValue = String.Concat("<", value, ">");
 
136
                        }
 
137
                }
 
138
                
 
139
                void SetTextValue(XPathNavigator navigator)
 
140
                {
 
141
                        value = navigator.Value;
 
142
                        displayValue = value;
 
143
                }
 
144
                
 
145
                void SetCommentValue(XPathNavigator navigator)
 
146
                {
 
147
                        value = navigator.Value;
 
148
                        displayValue = navigator.OuterXml;
 
149
                }
 
150
                
 
151
                void SetNamespaceValue(XPathNavigator navigator)
 
152
                {
 
153
                        value = navigator.OuterXml;
 
154
                        displayValue = value;
 
155
                }
 
156
                
 
157
                void SetProcessingInstructionValue(XPathNavigator navigator)
 
158
                {
 
159
                        value = navigator.Name;
 
160
                        displayValue = navigator.OuterXml;
 
161
                }
 
162
                
 
163
                void SetAttributeValue(XPathNavigator navigator)
 
164
                {
 
165
                        value = navigator.Name;
 
166
                        displayValue = String.Concat("@", value);
 
167
                }
 
168
                
 
169
                /// <summary>
 
170
                /// Takes one of the xml line number so the numbers are now zero
 
171
                /// based instead of one based.
 
172
                /// </summary>
 
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)
 
177
                {
 
178
                        if (lineInfo.HasLineInfo() && lineInfo.LineNumber > 0) {
 
179
                                lineNumber = lineInfo.LineNumber - 1;
 
180
                                linePosition = lineInfo.LinePosition - 1;
 
181
                        }
 
182
                }
 
183
        }
 
184
}