XmlAttribute.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. //
  2. // System.Xml.XmlAttribute
  3. //
  4. // Author:
  5. // Jason Diamond ([email protected])
  6. //
  7. // (C) 2002 Jason Diamond http://injektilo.org/
  8. //
  9. using System;
  10. using System.Text;
  11. using System.Xml.XPath;
  12. namespace System.Xml
  13. {
  14. public class XmlAttribute : XmlNode
  15. {
  16. #region Fields
  17. private XmlLinkedNode lastChild;
  18. private string localName;
  19. private string namespaceURI;
  20. private string prefix;
  21. internal bool isDefault;
  22. private XmlElement ownerElement;
  23. #endregion
  24. #region Constructor
  25. [MonoTODO("need to set namespaceURI if prefix is recognized built-in ones like xmlns")]
  26. protected internal XmlAttribute (
  27. string prefix,
  28. string localName,
  29. string namespaceURI,
  30. XmlDocument doc) : base (doc)
  31. {
  32. // What to be recognized is: xml:space, xml:lang, xml:base, and
  33. // xmlns and xmlns:* (when XmlDocument.Namespaces = true only)
  34. this.prefix = prefix;
  35. this.localName = localName;
  36. this.namespaceURI = namespaceURI;
  37. }
  38. #endregion
  39. #region Properties
  40. public override string BaseURI {
  41. get {
  42. return OwnerElement.BaseURI;
  43. }
  44. }
  45. public override string InnerText {
  46. get {
  47. StringBuilder builder = new StringBuilder ();
  48. AppendChildValues (this, builder);
  49. return builder.ToString ();
  50. }
  51. set {
  52. Value = value;
  53. }
  54. }
  55. private void AppendChildValues (XmlNode parent, StringBuilder builder)
  56. {
  57. XmlNode node = parent.FirstChild;
  58. while (node != null) {
  59. builder.Append (node.Value);
  60. AppendChildValues (node, builder);
  61. node = node.NextSibling;
  62. }
  63. }
  64. [MonoTODO ("Setter is incomplete(XmlTextReader.ReadAttribute is incomplete;No resolution for xml:lang/space")]
  65. public override string InnerXml {
  66. get {
  67. // Not sure why this is an override. Passing through for now.
  68. return base.InnerXml;
  69. }
  70. set {
  71. XmlNamespaceManager nsmgr = ConstructNamespaceManager ();
  72. XmlParserContext ctx = new XmlParserContext (OwnerDocument.NameTable, nsmgr, XmlLang, this.XmlSpace);
  73. XmlTextReader xtr = OwnerDocument.ReusableReader;
  74. xtr.SetReaderContext (BaseURI, ctx);
  75. xtr.SetReaderFragment (new System.IO.StringReader ("'" + value.Replace ("'", "'") + "'"), XmlNodeType.Attribute);
  76. OwnerDocument.ReadAttributeNodeValue (xtr, this);
  77. }
  78. }
  79. public override string LocalName {
  80. get {
  81. return localName;
  82. }
  83. }
  84. public override string Name {
  85. get {
  86. return prefix != String.Empty ? prefix + ":" + localName : localName;
  87. }
  88. }
  89. public override string NamespaceURI {
  90. get {
  91. return namespaceURI;
  92. }
  93. }
  94. public override XmlNodeType NodeType {
  95. get {
  96. return XmlNodeType.Attribute;
  97. }
  98. }
  99. internal override XPathNodeType XPathNodeType {
  100. get {
  101. return XPathNodeType.Attribute;
  102. }
  103. }
  104. public override XmlDocument OwnerDocument {
  105. get {
  106. return base.OwnerDocument;
  107. }
  108. }
  109. public virtual XmlElement OwnerElement {
  110. get {
  111. return ownerElement;
  112. }
  113. }
  114. public override XmlNode ParentNode {
  115. get {
  116. // It always returns null (by specification).
  117. return null;
  118. }
  119. }
  120. [MonoTODO("setter incomplete (name character check, format check, wrong prefix&nsURI)")]
  121. // We gotta do more in the set block here
  122. // We need to do the proper tests and throw
  123. // the correct Exceptions
  124. //
  125. // Wrong cases are: (1)check readonly, (2)check character validity,
  126. // (3)check format validity, (4)this is attribute and qualifiedName != "xmlns"
  127. // (5)when argument is 'xml' or 'xmlns' and namespaceURI doesn't match
  128. public override string Prefix {
  129. set {
  130. if(IsReadOnly)
  131. throw new XmlException ("This node is readonly.");
  132. XmlNamespaceManager nsmgr = ConstructNamespaceManager ();
  133. string nsuri = nsmgr.LookupNamespace (value);
  134. if(nsuri == null)
  135. throw new XmlException ("Namespace URI not found for this prefix");
  136. prefix = value;
  137. }
  138. get {
  139. return prefix;
  140. }
  141. }
  142. [MonoTODO("There are no code which sets 'specified = true', so this logic is without checking.")]
  143. public virtual bool Specified {
  144. get {
  145. return !isDefault;
  146. }
  147. }
  148. public override string Value {
  149. get {
  150. XmlNode firstChild = FirstChild;
  151. if (firstChild == null)
  152. return String.Empty;
  153. return firstChild.Value;
  154. }
  155. set {
  156. XmlNode firstChild = FirstChild;
  157. if (firstChild == null)
  158. AppendChild (OwnerDocument.CreateTextNode (value));
  159. else
  160. firstChild.Value = value;
  161. }
  162. }
  163. internal override string XmlLang {
  164. get { return OwnerElement.XmlLang; }
  165. }
  166. internal override XmlSpace XmlSpace {
  167. get { return OwnerElement.XmlSpace; }
  168. }
  169. #endregion
  170. #region Methods
  171. public override XmlNode CloneNode (bool deep)
  172. {
  173. XmlNode node = new XmlAttribute (prefix, localName, namespaceURI,
  174. OwnerDocument);
  175. if (deep) {
  176. while ((node != null) && (node.HasChildNodes)) {
  177. AppendChild (node.NextSibling.CloneNode (true));
  178. node = node.NextSibling;
  179. }
  180. }
  181. return node;
  182. }
  183. // Parent of XmlAttribute must be null
  184. internal void SetOwnerElement (XmlElement el) {
  185. ownerElement = el;
  186. }
  187. public override void WriteContentTo (XmlWriter w)
  188. {
  189. w.WriteString (Value);
  190. }
  191. public override void WriteTo (XmlWriter w)
  192. {
  193. w.WriteAttributeString (prefix, localName, namespaceURI, Value);
  194. }
  195. #endregion
  196. internal override XmlLinkedNode LastLinkedChild {
  197. get { return lastChild; }
  198. set { lastChild = value; }
  199. }
  200. }
  201. }