// 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.Globalization;
using ICSharpCode.NRefactory.Editor;
namespace ICSharpCode.NRefactory.Xml
{
///
/// Name-value pair in a tag
///
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1711:IdentifiersShouldNotHaveIncorrectSuffix")]
public class AXmlAttribute : AXmlObject
{
internal AXmlAttribute(AXmlObject parent, int startOffset, InternalAttribute internalObject)
: base(parent, startOffset, internalObject)
{
}
internal InternalAttribute InternalAttribute {
get { return (InternalAttribute)internalObject; }
}
/// Name with namespace prefix - exactly as in source file
public string Name { get { return InternalAttribute.Name; } }
/// Unquoted and dereferenced value of the attribute
public string Value { get { return InternalAttribute.Value; } }
/// Gets the segment for the attribute name
public ISegment NameSegment {
get { return new XmlSegment(startOffset, startOffset + Name.Length); }
}
/// Gets the segment for the attribute value, including the quotes
public ISegment ValueSegment {
get { return new XmlSegment(startOffset + Name.Length + InternalAttribute.EqualsSignLength, this.EndOffset); }
}
/// The element containing this attribute
/// Null if orphaned
public AXmlElement ParentElement {
get {
AXmlTag tag = this.Parent as AXmlTag;
if (tag != null) {
return tag.Parent as AXmlElement;
}
return null;
}
}
/// The part of name before ":"
/// Empty string if not found
public string Prefix {
get {
return GetNamespacePrefix(this.Name);
}
}
/// The part of name after ":"
/// Whole name if ":" not found
public string LocalName {
get {
return GetLocalName(this.Name);
}
}
///
/// Resolved namespace of the name. Empty string if not found
/// From the specification: "The namespace name for an unprefixed attribute name always has no value."
///
public string Namespace {
get {
if (string.IsNullOrEmpty(this.Prefix)) return NoNamespace;
AXmlElement elem = this.ParentElement;
if (elem != null) {
return elem.LookupNamespace(this.Prefix) ?? NoNamespace;
}
return NoNamespace; // Orphaned attribute
}
}
/// Attribute is declaring namespace ("xmlns" or "xmlns:*")
public bool IsNamespaceDeclaration {
get {
return this.Name == "xmlns" || this.Prefix == "xmlns";
}
}
///
public override void AcceptVisitor(AXmlVisitor visitor)
{
visitor.VisitAttribute(this);
}
///
public override string ToString()
{
return string.Format(CultureInfo.InvariantCulture, "[{0} '{1}={2}']", base.ToString(), this.Name, this.Value);
}
}
}