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

« back to all changes in this revision

Viewing changes to external/nrefactory/ICSharpCode.NRefactory.Xml/AXmlObject.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
ļ»æ// Copyright (c) 2009-2013 AlphaSierraPapa for the SharpDevelop Team
 
2
// 
 
3
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
 
4
// software and associated documentation files (the "Software"), to deal in the Software
 
5
// without restriction, including without limitation the rights to use, copy, modify, merge,
 
6
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
 
7
// to whom the Software is furnished to do so, subject to the following conditions:
 
8
// 
 
9
// The above copyright notice and this permission notice shall be included in all copies or
 
10
// substantial portions of the Software.
 
11
// 
 
12
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
 
13
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
 
14
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
 
15
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 
16
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 
17
// DEALINGS IN THE SOFTWARE.
 
18
 
 
19
using System;
 
20
using System.Collections.Generic;
 
21
using System.Linq;
 
22
using System.Xml;
 
23
using ICSharpCode.NRefactory.Editor;
 
24
using ICSharpCode.NRefactory.Utils;
 
25
 
 
26
namespace ICSharpCode.NRefactory.Xml
 
27
{
 
28
        /// <summary>
 
29
        /// XML object. Base class for all nodes in the XML document.
 
30
        /// </summary>
 
31
        public abstract class AXmlObject : ISegment
 
32
        {
 
33
                /// <summary> Empty string.  The namespace used if there is no "xmlns" specified </summary>
 
34
                public static readonly string NoNamespace = string.Empty;
 
35
                
 
36
                /// <summary> Namespace for "xml:" prefix: "http://www.w3.org/XML/1998/namespace" </summary>
 
37
                public static readonly string XmlNamespace = "http://www.w3.org/XML/1998/namespace";
 
38
                
 
39
                /// <summary> Namesapce for "xmlns:" prefix: "http://www.w3.org/2000/xmlns/" </summary>
 
40
                public static readonly string XmlnsNamespace = "http://www.w3.org/2000/xmlns/";
 
41
                
 
42
                readonly AXmlObject parent;
 
43
                internal readonly int startOffset;
 
44
                internal readonly InternalObject internalObject;
 
45
                IList<AXmlObject> children;
 
46
                
 
47
                internal AXmlObject(AXmlObject parent, int startOffset, InternalObject internalObject)
 
48
                {
 
49
                        this.parent = parent;
 
50
                        this.startOffset = startOffset;
 
51
                        this.internalObject = internalObject;
 
52
                }
 
53
                
 
54
                /// <summary>
 
55
                /// Creates an XML reader that reads from this document.
 
56
                /// </summary>
 
57
                public virtual XmlReader CreateReader()
 
58
                {
 
59
                        return new AXmlReader(new[] { internalObject });
 
60
                }
 
61
                
 
62
                /// <summary>
 
63
                /// Creates an XML reader that reads from this document.
 
64
                /// </summary>
 
65
                public virtual XmlReader CreateReader(Func<int, TextLocation> offsetToTextLocation)
 
66
                {
 
67
                        return new AXmlReader(new[] { internalObject }, startOffset, offsetToTextLocation);
 
68
                }
 
69
                
 
70
                /// <summary>
 
71
                /// Gets the parent node.
 
72
                /// </summary>
 
73
                public AXmlObject Parent {
 
74
                        get { return parent; }
 
75
                }
 
76
                
 
77
                /// <summary>
 
78
                /// Gets the list of child objects.
 
79
                /// </summary>
 
80
                public IList<AXmlObject> Children {
 
81
                        get {
 
82
                                var result = LazyInit.VolatileRead(ref this.children);
 
83
                                if (result != null) {
 
84
                                        return result;
 
85
                                } else {
 
86
                                        if (internalObject.NestedObjects != null) {
 
87
                                                var array = new AXmlObject[internalObject.NestedObjects.Length];
 
88
                                                for (int i = 0; i < array.Length; i++) {
 
89
                                                        array[i] = internalObject.NestedObjects[i].CreatePublicObject(this, startOffset);
 
90
                                                }
 
91
                                                result = Array.AsReadOnly(array);
 
92
                                        } else {
 
93
                                                result = EmptyList<AXmlObject>.Instance;
 
94
                                        }
 
95
                                        return LazyInit.GetOrSet(ref this.children, result);
 
96
                                }
 
97
                        }
 
98
                }
 
99
                
 
100
                /// <summary>
 
101
                /// Gets a child fully containg the given offset.
 
102
                /// Goes recursively down the tree.
 
103
                /// Specail case if at the end of attribute or text
 
104
                /// </summary>
 
105
                public AXmlObject GetChildAtOffset(int offset)
 
106
                {
 
107
                        foreach(AXmlObject child in this.Children) {
 
108
                                if (offset == child.EndOffset && (child is AXmlAttribute || child is AXmlText))
 
109
                                        return child;
 
110
                                if (child.StartOffset < offset && offset < child.EndOffset) {
 
111
                                        return child.GetChildAtOffset(offset);
 
112
                                }
 
113
                        }
 
114
                        return this; // No children at offset
 
115
                }
 
116
                
 
117
                /// <summary>
 
118
                /// The error that occured in the context of this node (excluding nested nodes)
 
119
                /// </summary>
 
120
                public IEnumerable<SyntaxError> MySyntaxErrors {
 
121
                        get {
 
122
                                if (internalObject.SyntaxErrors != null) {
 
123
                                        return internalObject.SyntaxErrors.Select(e => new SyntaxError(startOffset + e.RelativeStart, startOffset + e.RelativeEnd, e.Description));
 
124
                                } else {
 
125
                                        return EmptyList<SyntaxError>.Instance;
 
126
                                }
 
127
                        }
 
128
                }
 
129
                
 
130
                /// <summary>
 
131
                /// The error that occured in the context of this node and all nested nodes.
 
132
                /// It has O(n) cost.
 
133
                /// </summary>
 
134
                public IEnumerable<SyntaxError> SyntaxErrors {
 
135
                        get {
 
136
                                return TreeTraversal.PreOrder(this, n => n.Children).SelectMany(obj => obj.MySyntaxErrors);
 
137
                        }
 
138
                }
 
139
                
 
140
                /// <summary> Get all ancestors of this node </summary>
 
141
                public IEnumerable<AXmlObject> Ancestors {
 
142
                        get {
 
143
                                AXmlObject curr = this.Parent;
 
144
                                while(curr != null) {
 
145
                                        yield return curr;
 
146
                                        curr = curr.Parent;
 
147
                                }
 
148
                        }
 
149
                }
 
150
                
 
151
                #region Helper methods
 
152
                
 
153
                /// <summary> The part of name before ":" </summary>
 
154
                /// <returns> Empty string if not found </returns>
 
155
                internal static string GetNamespacePrefix(string name)
 
156
                {
 
157
                        if (string.IsNullOrEmpty(name)) return string.Empty;
 
158
                        int colonIndex = name.IndexOf(':');
 
159
                        if (colonIndex != -1) {
 
160
                                return name.Substring(0, colonIndex);
 
161
                        } else {
 
162
                                return string.Empty;
 
163
                        }
 
164
                }
 
165
                
 
166
                /// <summary> The part of name after ":" </summary>
 
167
                /// <returns> Whole name if ":" not found </returns>
 
168
                internal static string GetLocalName(string name)
 
169
                {
 
170
                        if (string.IsNullOrEmpty(name)) return string.Empty;
 
171
                        int colonIndex = name.IndexOf(':');
 
172
                        if (colonIndex != -1) {
 
173
                                return name.Remove(0, colonIndex + 1);
 
174
                        } else {
 
175
                                return name ?? string.Empty;
 
176
                        }
 
177
                }
 
178
                
 
179
                #endregion
 
180
                
 
181
                /// <summary> Call appropriate visit method on the given visitor </summary>
 
182
                public abstract void AcceptVisitor(AXmlVisitor visitor);
 
183
                
 
184
                /// <summary>
 
185
                /// Gets the start offset of the segment.
 
186
                /// </summary>
 
187
                public int StartOffset {
 
188
                        get { return startOffset; }
 
189
                }
 
190
                
 
191
                int ISegment.Offset {
 
192
                        get { return startOffset; }
 
193
                }
 
194
                
 
195
                /// <inheritdoc/>
 
196
                public int Length {
 
197
                        get { return internalObject.Length; }
 
198
                }
 
199
                
 
200
                /// <inheritdoc/>
 
201
                public int EndOffset {
 
202
                        get { return startOffset + internalObject.Length; }
 
203
                }
 
204
        }
 
205
}