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.Globalization; |
---|
21 | using ICSharpCode.NRefactory.Editor; |
---|
22 | |
---|
23 | namespace ICSharpCode.NRefactory.Xml |
---|
24 | { |
---|
25 | /// <summary> |
---|
26 | /// Name-value pair in a tag |
---|
27 | /// </summary> |
---|
28 | [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1711:IdentifiersShouldNotHaveIncorrectSuffix")] |
---|
29 | public class AXmlAttribute : AXmlObject |
---|
30 | { |
---|
31 | internal AXmlAttribute(AXmlObject parent, int startOffset, InternalAttribute internalObject) |
---|
32 | : base(parent, startOffset, internalObject) |
---|
33 | { |
---|
34 | } |
---|
35 | |
---|
36 | internal InternalAttribute InternalAttribute { |
---|
37 | get { return (InternalAttribute)internalObject; } |
---|
38 | } |
---|
39 | |
---|
40 | /// <summary> Name with namespace prefix - exactly as in source file </summary> |
---|
41 | public string Name { get { return InternalAttribute.Name; } } |
---|
42 | |
---|
43 | /// <summary> Unquoted and dereferenced value of the attribute </summary> |
---|
44 | public string Value { get { return InternalAttribute.Value; } } |
---|
45 | |
---|
46 | /// <summary>Gets the segment for the attribute name</summary> |
---|
47 | public ISegment NameSegment { |
---|
48 | get { return new XmlSegment(startOffset, startOffset + Name.Length); } |
---|
49 | } |
---|
50 | |
---|
51 | /// <summary>Gets the segment for the attribute value, including the quotes</summary> |
---|
52 | public ISegment ValueSegment { |
---|
53 | get { return new XmlSegment(startOffset + Name.Length + InternalAttribute.EqualsSignLength, this.EndOffset); } |
---|
54 | } |
---|
55 | /// <summary> The element containing this attribute </summary> |
---|
56 | /// <returns> Null if orphaned </returns> |
---|
57 | public AXmlElement ParentElement { |
---|
58 | get { |
---|
59 | AXmlTag tag = this.Parent as AXmlTag; |
---|
60 | if (tag != null) { |
---|
61 | return tag.Parent as AXmlElement; |
---|
62 | } |
---|
63 | return null; |
---|
64 | } |
---|
65 | } |
---|
66 | |
---|
67 | /// <summary> The part of name before ":"</summary> |
---|
68 | /// <returns> Empty string if not found </returns> |
---|
69 | public string Prefix { |
---|
70 | get { |
---|
71 | return GetNamespacePrefix(this.Name); |
---|
72 | } |
---|
73 | } |
---|
74 | |
---|
75 | /// <summary> The part of name after ":" </summary> |
---|
76 | /// <returns> Whole name if ":" not found </returns> |
---|
77 | public string LocalName { |
---|
78 | get { |
---|
79 | return GetLocalName(this.Name); |
---|
80 | } |
---|
81 | } |
---|
82 | |
---|
83 | /// <summary> |
---|
84 | /// Resolved namespace of the name. Empty string if not found |
---|
85 | /// From the specification: "The namespace name for an unprefixed attribute name always has no value." |
---|
86 | /// </summary> |
---|
87 | public string Namespace { |
---|
88 | get { |
---|
89 | if (string.IsNullOrEmpty(this.Prefix)) return NoNamespace; |
---|
90 | |
---|
91 | AXmlElement elem = this.ParentElement; |
---|
92 | if (elem != null) { |
---|
93 | return elem.LookupNamespace(this.Prefix) ?? NoNamespace; |
---|
94 | } |
---|
95 | return NoNamespace; // Orphaned attribute |
---|
96 | } |
---|
97 | } |
---|
98 | |
---|
99 | /// <summary> Attribute is declaring namespace ("xmlns" or "xmlns:*") </summary> |
---|
100 | public bool IsNamespaceDeclaration { |
---|
101 | get { |
---|
102 | return this.Name == "xmlns" || this.Prefix == "xmlns"; |
---|
103 | } |
---|
104 | } |
---|
105 | |
---|
106 | /// <inheritdoc/> |
---|
107 | public override void AcceptVisitor(AXmlVisitor visitor) |
---|
108 | { |
---|
109 | visitor.VisitAttribute(this); |
---|
110 | } |
---|
111 | |
---|
112 | /// <inheritdoc/> |
---|
113 | public override string ToString() |
---|
114 | { |
---|
115 | return string.Format(CultureInfo.InvariantCulture, "[{0} '{1}={2}']", base.ToString(), this.Name, this.Value); |
---|
116 | } |
---|
117 | } |
---|
118 | } |
---|