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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// Copyright (c) 2009-2013 AlphaSierraPapa for the SharpDevelop Team
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;

namespace ICSharpCode.NRefactory.Xml
{
	/// <summary>
	/// XML element.
	/// </summary>
	public class AXmlElement : AXmlObject
	{
		internal AXmlElement(AXmlObject parent, int startOffset, InternalElement internalObject)
			: base(parent, startOffset, internalObject)
		{
			Log.Assert(internalObject.NestedObjects[0] is InternalTag, "First child of element must be start tag");
		}
		
		/// <summary> No tags are missing anywhere within this element (recursive) </summary>
		public bool IsProperlyNested {
			get { return ((InternalElement)internalObject).IsPropertyNested; }
		}
		
		/// <summary>The start or empty-element tag for this element.</summary>
		public AXmlTag StartTag {
			get { return (AXmlTag)this.Children[0]; }
		}
		
		/// <summary>Name with namespace prefix - exactly as in source</summary>
		public string Name {
			get { return ((InternalTag)internalObject.NestedObjects[0]).Name; }
		}
		
		/// <summary>Gets whether an end tag exists for this node.</summary>
		public bool HasEndTag {
			get { return ((InternalElement)internalObject).HasEndTag; }
		}
		
		/// <summary> The end tag, if there is any. Returns null for empty elements "&lt;Element/>" and missing end tags in malformed XML.</summary>
		public AXmlTag EndTag {
			get {
				if (HasEndTag)
					return (AXmlTag)this.Children[this.Children.Count - 1];
				else
					return null;
			}
		}
		
		/// <summary>
		/// Gets the attributes.
		/// </summary>
		public IEnumerable<AXmlAttribute> Attributes {
			get {
				return ((AXmlTag)this.Children[0]).Children.OfType<AXmlAttribute>();
			}
		}
		
		/// <summary>
		/// Gets the content (all children except for the start and end tags)
		/// </summary>
		public IEnumerable<AXmlObject> Content {
			get {
				int end = this.Children.Count;
				if (HasEndTag)
					end--;
				for (int i = 1; i < end; i++) {
					yield return this.Children[i];
				}
			}
		}
		
		/// <summary> The part of name before ":" </summary>
		/// <returns> Empty string if not found </returns>
		public string Prefix {
	 		get { return ((InternalElement)internalObject).Prefix; }
		}
		
		/// <summary> The part of name after ":" </summary>
		/// <returns> Empty string if not found </returns>
		public string LocalName {
			get { return ((InternalElement)internalObject).LocalName; }
		}
		
		/// <summary> Resolved namespace of the name </summary>
		/// <returns> Empty string if prefix is not found </returns>
		public string Namespace {
			get {
				string prefix = this.Prefix;
				return ResolvePrefix(prefix);
			}
		}
		
		/// <summary> Find the defualt namespace for this context </summary>
		public string FindDefaultNamespace()
		{
			return ResolvePrefix(string.Empty);
		}
		
		/// <summary>
		/// Recursively resolve given prefix in this context.  Prefix must have some value.
		/// </summary>
		/// <returns> Empty string if prefix is not found </returns>
		public string ResolvePrefix(string prefix)
		{
			if (prefix == null)
				throw new ArgumentNullException("prefix");
			
			// Implicit namesapces
			if (prefix == "xml") return XmlNamespace;
			if (prefix == "xmlns") return XmlnsNamespace;
			
			string lookFor = (prefix.Length > 0 ? "xmlns:" + prefix : "xmlns");
			for (AXmlElement current = this; current != null; current = current.Parent as AXmlElement) {
				foreach (var attr in current.Attributes) {
					if (attr.Name == lookFor)
						return attr.Value;
				}
			}
			return NoNamespace; // Can not find prefix
		}
		
		/// <summary>
		/// Get unquoted value of attribute.
		/// It looks in the no namespace (empty string).
		/// </summary>
		/// <returns>Null if not found</returns>
		public string GetAttributeValue(string localName)
		{
			return GetAttributeValue(NoNamespace, localName);
		}
		
		/// <summary>
		/// Get unquoted value of attribute
		/// </summary>
		/// <param name="namespace">Namespace.  Can be no namepace (empty string), which is the default for attributes.</param>
		/// <param name="localName">Local name - text after ":"</param>
		/// <returns>Null if not found</returns>
		public string GetAttributeValue(string @namespace, string localName)
		{
			@namespace = @namespace ?? string.Empty;
			foreach (AXmlAttribute attr in this.Attributes) {
				if (attr.LocalName == localName && attr.Namespace == @namespace)
					return attr.Value;
			}
			return null;
		}
		
		/// <inheritdoc/>
		public override void AcceptVisitor(AXmlVisitor visitor)
		{
			visitor.VisitElement(this);
		}
		
		/// <inheritdoc/>
		public override string ToString()
		{
			return string.Format(CultureInfo.InvariantCulture, "[{0} '{1}' Attr:{2} Chld:{3} Nest:{4}]", base.ToString(), this.Name, this.StartTag.Children.Count, this.Children.Count, this.IsProperlyNested ? "Ok" : "Bad");
		}
	}
}