1
ļ»æ// Copyright (c) 2009-2013 AlphaSierraPapa for the SharpDevelop Team
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:
9
// The above copyright notice and this permission notice shall be included in all copies or
10
// substantial portions of the Software.
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.
20
using System.Collections.Generic;
23
using ICSharpCode.NRefactory.Editor;
24
using ICSharpCode.NRefactory.Utils;
26
namespace ICSharpCode.NRefactory.Xml
29
/// XML object. Base class for all nodes in the XML document.
31
public abstract class AXmlObject : ISegment
33
/// <summary> Empty string. The namespace used if there is no "xmlns" specified </summary>
34
public static readonly string NoNamespace = string.Empty;
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";
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/";
42
readonly AXmlObject parent;
43
internal readonly int startOffset;
44
internal readonly InternalObject internalObject;
45
IList<AXmlObject> children;
47
internal AXmlObject(AXmlObject parent, int startOffset, InternalObject internalObject)
50
this.startOffset = startOffset;
51
this.internalObject = internalObject;
55
/// Creates an XML reader that reads from this document.
57
public virtual XmlReader CreateReader()
59
return new AXmlReader(new[] { internalObject });
63
/// Creates an XML reader that reads from this document.
65
public virtual XmlReader CreateReader(Func<int, TextLocation> offsetToTextLocation)
67
return new AXmlReader(new[] { internalObject }, startOffset, offsetToTextLocation);
71
/// Gets the parent node.
73
public AXmlObject Parent {
74
get { return parent; }
78
/// Gets the list of child objects.
80
public IList<AXmlObject> Children {
82
var result = LazyInit.VolatileRead(ref this.children);
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);
91
result = Array.AsReadOnly(array);
93
result = EmptyList<AXmlObject>.Instance;
95
return LazyInit.GetOrSet(ref this.children, result);
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
105
public AXmlObject GetChildAtOffset(int offset)
107
foreach(AXmlObject child in this.Children) {
108
if (offset == child.EndOffset && (child is AXmlAttribute || child is AXmlText))
110
if (child.StartOffset < offset && offset < child.EndOffset) {
111
return child.GetChildAtOffset(offset);
114
return this; // No children at offset
118
/// The error that occured in the context of this node (excluding nested nodes)
120
public IEnumerable<SyntaxError> MySyntaxErrors {
122
if (internalObject.SyntaxErrors != null) {
123
return internalObject.SyntaxErrors.Select(e => new SyntaxError(startOffset + e.RelativeStart, startOffset + e.RelativeEnd, e.Description));
125
return EmptyList<SyntaxError>.Instance;
131
/// The error that occured in the context of this node and all nested nodes.
132
/// It has O(n) cost.
134
public IEnumerable<SyntaxError> SyntaxErrors {
136
return TreeTraversal.PreOrder(this, n => n.Children).SelectMany(obj => obj.MySyntaxErrors);
140
/// <summary> Get all ancestors of this node </summary>
141
public IEnumerable<AXmlObject> Ancestors {
143
AXmlObject curr = this.Parent;
144
while(curr != null) {
151
#region Helper methods
153
/// <summary> The part of name before ":" </summary>
154
/// <returns> Empty string if not found </returns>
155
internal static string GetNamespacePrefix(string name)
157
if (string.IsNullOrEmpty(name)) return string.Empty;
158
int colonIndex = name.IndexOf(':');
159
if (colonIndex != -1) {
160
return name.Substring(0, colonIndex);
166
/// <summary> The part of name after ":" </summary>
167
/// <returns> Whole name if ":" not found </returns>
168
internal static string GetLocalName(string name)
170
if (string.IsNullOrEmpty(name)) return string.Empty;
171
int colonIndex = name.IndexOf(':');
172
if (colonIndex != -1) {
173
return name.Remove(0, colonIndex + 1);
175
return name ?? string.Empty;
181
/// <summary> Call appropriate visit method on the given visitor </summary>
182
public abstract void AcceptVisitor(AXmlVisitor visitor);
185
/// Gets the start offset of the segment.
187
public int StartOffset {
188
get { return startOffset; }
191
int ISegment.Offset {
192
get { return startOffset; }
197
get { return internalObject.Length; }
201
public int EndOffset {
202
get { return startOffset + internalObject.Length; }