2010-04-07 Jb Evain <jbevain@novell.com>
[mcs.git] / class / System.XML / System.Xml / XmlAttribute.cs
blob4a79204ceb21e4af69c04d09a884db942f36a71a
1 //
2 // System.Xml.XmlAttribute
3 //
4 // Authors:
5 // Jason Diamond (jason@injektilo.org)
6 // Atsushi Enomoto (ginga@kit.hi-ho.ne.jp)
7 //
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:
20 //
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
23 //
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.
33 using System;
34 using System.Text;
35 using System.Xml.XPath;
36 using Mono.Xml;
37 #if NET_2_0
38 using System.Xml.Schema;
39 #endif
41 namespace System.Xml
43 public class XmlAttribute : XmlNode, IHasXmlChildNode
45 #region Fields
47 private XmlNameEntry name;
48 internal bool isDefault;
49 XmlLinkedNode lastLinkedChild;
50 #if NET_2_0
51 private IXmlSchemaInfo schemaInfo;
52 #endif
54 #endregion
56 #region Constructor
58 protected internal XmlAttribute (
59 string prefix,
60 string localName,
61 string namespaceURI,
62 XmlDocument doc) : this (prefix, localName, namespaceURI, doc, false, true)
66 internal XmlAttribute (
67 string prefix,
68 string localName,
69 string namespaceURI,
70 XmlDocument doc,
71 bool atomizedNames, bool checkNamespace) : base (doc)
73 if (!atomizedNames) {
74 if (prefix == null)
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
82 if (checkNamespace) {
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.");
90 if (!atomizedNames) {
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);
106 #endregion
108 #region Properties
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 {
120 #if !(NET_2_0)
121 get {
122 return base.InnerText;
124 #endif
126 set {
127 Value = value;
131 public override string InnerXml {
132 #if !(NET_2_0)
133 get {
134 // Not sure why this is an override. Passing through for now.
135 return base.InnerXml;
137 #endif
139 set {
140 RemoveAll ();
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;
147 xtr.Read ();
148 OwnerDocument.ReadAttributeNodeValue (xtr, this);
152 public override string LocalName {
153 get {
154 return name.LocalName;
158 public override string Name {
159 get { return name.GetPrefixedName (OwnerDocument.NameCache); }
162 public override string NamespaceURI {
163 get {
164 return name.NS;
168 public override XmlNodeType NodeType {
169 get {
170 return XmlNodeType.Attribute;
174 internal override XPathNodeType XPathNodeType {
175 get {
176 return XPathNodeType.Attribute;
180 public override XmlDocument OwnerDocument {
181 get {
182 return base.OwnerDocument;
186 public virtual XmlElement OwnerElement {
187 get { return AttributeOwnerElement; }
190 public override XmlNode ParentNode {
191 get {
192 // It always returns null (by specification).
193 return null;
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 {
204 set {
205 if (IsReadOnly)
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);
215 get {
216 return name.Prefix;
220 #if NET_2_0
221 public override IXmlSchemaInfo SchemaInfo {
222 get { return schemaInfo; }
223 internal set { schemaInfo = value; }
225 #endif
227 public virtual bool Specified {
228 get {
229 return !isDefault;
233 public override string Value {
234 get { return InnerText; }
236 set {
237 if (this.IsReadOnly)
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) {
243 this.RemoveAll ();
244 AppendChild (OwnerDocument.CreateTextNode (value), false);
246 else if (FirstChild.NextSibling != null) {
247 this.RemoveAll ();
248 AppendChild (OwnerDocument.CreateTextNode (value), false);
250 else
251 textChild.Value = value;
252 isDefault = false;
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; }
264 #endregion
266 #region Methods
268 #if NET_2_0
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);
298 #endif
300 public override XmlNode CloneNode (bool deep)
302 XmlNode node = OwnerDocument.CreateAttribute (name.Prefix, name.LocalName, name.NS, true, false);
303 if (deep) {
304 for (XmlNode n = FirstChild; n != null; n = n.NextSibling)
305 node.AppendChild (n.CloneNode (deep), false);
308 return node;
311 internal void SetDefault ()
313 isDefault = true;
316 public override void WriteContentTo (XmlWriter w)
318 for (XmlNode n = FirstChild; n != null; n = n.NextSibling)
319 n.WriteTo (w);
322 public override void WriteTo (XmlWriter w)
324 if (isDefault)
325 return; // Write nothing.
326 w.WriteStartAttribute (name.NS.Length > 0 ? name.Prefix : String.Empty, name.LocalName, name.NS);
327 WriteContentTo (w);
328 w.WriteEndAttribute ();
331 internal DTDAttributeDefinition GetAttributeDefinition ()
333 if (OwnerElement == null)
334 return 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;
340 #endregion