2 // System.Xml.XmlAttribute
5 // Jason Diamond (jason@injektilo.org)
6 // Atsushi Enomoto (ginga@kit.hi-ho.ne.jp)
8 // (C) 2002 Jason Diamond http://injektilo.org/
9 // (C) 2003 Atsushi Enomoto
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35 using System
.Xml
.XPath
;
38 using System
.Xml
.Schema
;
43 public class XmlAttribute
: XmlNode
, IHasXmlChildNode
47 private XmlNameEntry name
;
48 internal bool isDefault
;
49 XmlLinkedNode lastLinkedChild
;
51 private IXmlSchemaInfo schemaInfo
;
58 protected internal XmlAttribute (
62 XmlDocument doc
) : this (prefix
, localName
, namespaceURI
, doc
, false, true)
66 internal XmlAttribute (
71 bool atomizedNames
, bool checkNamespace
) : base (doc
)
75 prefix
= String
.Empty
;
76 if (namespaceURI
== null)
77 namespaceURI
= String
.Empty
;
80 // Prefix "xml" should be also checked (http://www.w3.org/XML/xml-names-19990114-errata#NE05)
81 // but MS.NET ignores such case
83 if (prefix
== "xmlns" || (prefix
== "" && localName
== "xmlns"))
84 if (namespaceURI
!= XmlNamespaceManager
.XmlnsXmlns
)
85 throw new ArgumentException ("Invalid attribute namespace for namespace declaration.");
86 else if (prefix
== "xml" && namespaceURI
!= XmlNamespaceManager
.XmlnsXml
)
87 throw new ArgumentException ("Invalid attribute namespace for namespace declaration.");
91 // There are no means to identify the DOM is
92 // namespace-aware or not, so we can only
93 // check Name validity.
94 if (prefix
!= "" && !XmlChar
.IsName (prefix
))
95 throw new ArgumentException ("Invalid attribute prefix.");
96 else if (!XmlChar
.IsName (localName
))
97 throw new ArgumentException ("Invalid attribute local name.");
99 prefix
= doc
.NameTable
.Add (prefix
);
100 localName
= doc
.NameTable
.Add (localName
);
101 namespaceURI
= doc
.NameTable
.Add (namespaceURI
);
103 name
= doc
.NameCache
.Add (prefix
, localName
, namespaceURI
, true);
110 XmlLinkedNode IHasXmlChildNode
.LastLinkedChild
{
111 get { return lastLinkedChild; }
112 set { lastLinkedChild = value; }
115 public override string BaseURI
{
116 get { return OwnerElement != null ? OwnerElement.BaseURI : String.Empty; }
119 public override string InnerText
{
122 return base.InnerText
;
131 public override string InnerXml
{
134 // Not sure why this is an override. Passing through for now.
135 return base.InnerXml
;
141 XmlNamespaceManager nsmgr
= ConstructNamespaceManager ();
142 XmlParserContext ctx
= new XmlParserContext (OwnerDocument
.NameTable
, nsmgr
,
143 OwnerDocument
.DocumentType
!= null ? OwnerDocument
.DocumentType
.DTD
: null,
144 BaseURI
, XmlLang
, XmlSpace
, null);
145 XmlTextReader xtr
= new XmlTextReader (value, XmlNodeType
.Attribute
, ctx
);
146 xtr
.XmlResolver
= OwnerDocument
.Resolver
;
148 OwnerDocument
.ReadAttributeNodeValue (xtr
, this);
152 public override string LocalName
{
154 return name
.LocalName
;
158 public override string Name
{
159 get { return name.GetPrefixedName (OwnerDocument.NameCache); }
162 public override string NamespaceURI
{
168 public override XmlNodeType NodeType
{
170 return XmlNodeType
.Attribute
;
174 internal override XPathNodeType XPathNodeType
{
176 return XPathNodeType
.Attribute
;
180 public override XmlDocument OwnerDocument
{
182 return base.OwnerDocument
;
186 public virtual XmlElement OwnerElement
{
187 get { return AttributeOwnerElement; }
190 public override XmlNode ParentNode
{
192 // It always returns null (by specification).
197 // We gotta do more in the set block here
198 // We need to do the proper tests and throw
199 // the correct Exceptions
201 // Wrong cases are: (1)check readonly, (2)check character validity,
202 // (3)check format validity, (4)this is attribute and qualifiedName != "xmlns"
203 public override string Prefix
{
206 throw new XmlException ("This node is readonly.");
207 if (name
.Prefix
== "xmlns" && value != "xmlns")
208 throw new ArgumentException ("Cannot bind to the reserved namespace.");
210 value = OwnerDocument
.NameTable
.Add (value);
211 name
= OwnerDocument
.NameCache
.Add (value,
212 name
.LocalName
, name
.NS
, true);
221 public override IXmlSchemaInfo SchemaInfo
{
222 get { return schemaInfo; }
223 internal set { schemaInfo = value; }
227 public virtual bool Specified
{
233 public override string Value
{
234 get { return InnerText; }
238 throw new ArgumentException ("Attempt to modify a read-only node.");
239 OwnerDocument
.CheckIdTableUpdate (this, InnerText
, value);
241 XmlNode textChild
= FirstChild
as XmlCharacterData
;
242 if (textChild
== null) {
244 AppendChild (OwnerDocument
.CreateTextNode (value), false);
246 else if (FirstChild
.NextSibling
!= null) {
248 AppendChild (OwnerDocument
.CreateTextNode (value), false);
251 textChild
.Value
= value;
256 internal override string XmlLang
{
257 get { return OwnerElement != null ? OwnerElement.XmlLang : String.Empty; }
260 internal override XmlSpace XmlSpace
{
261 get { return OwnerElement != null ? OwnerElement.XmlSpace : XmlSpace.None; }
269 public override XmlNode
AppendChild (XmlNode child
)
271 return base.AppendChild (child
);
274 public override XmlNode
InsertBefore (XmlNode newChild
, XmlNode refChild
)
276 return base.InsertBefore (newChild
, refChild
);
279 public override XmlNode
InsertAfter (XmlNode newChild
, XmlNode refChild
)
281 return base.InsertAfter (newChild
, refChild
);
284 public override XmlNode
PrependChild (XmlNode node
)
286 return base.PrependChild (node
);
289 public override XmlNode
RemoveChild (XmlNode node
)
291 return base.RemoveChild (node
);
294 public override XmlNode
ReplaceChild (XmlNode newChild
, XmlNode oldChild
)
296 return base.ReplaceChild (newChild
, oldChild
);
300 public override XmlNode
CloneNode (bool deep
)
302 XmlNode node
= OwnerDocument
.CreateAttribute (name
.Prefix
, name
.LocalName
, name
.NS
, true, false);
304 for (XmlNode n
= FirstChild
; n
!= null; n
= n
.NextSibling
)
305 node
.AppendChild (n
.CloneNode (deep
), false);
311 internal void SetDefault ()
316 public override void WriteContentTo (XmlWriter w
)
318 for (XmlNode n
= FirstChild
; n
!= null; n
= n
.NextSibling
)
322 public override void WriteTo (XmlWriter w
)
325 return; // Write nothing.
326 w
.WriteStartAttribute (name
.NS
.Length
> 0 ? name
.Prefix
: String
.Empty
, name
.LocalName
, name
.NS
);
328 w
.WriteEndAttribute ();
331 internal DTDAttributeDefinition
GetAttributeDefinition ()
333 if (OwnerElement
== null)
336 // If it is default, then directly create new attribute.
337 DTDAttListDeclaration attList
= OwnerDocument
.DocumentType
!= null ? OwnerDocument
.DocumentType
.DTD
.AttListDecls
[OwnerElement
.Name
] : null;
338 return attList
!= null ? attList
[Name
] : null;