XmlWriter.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. //
  2. // System.Xml.XmlTextWriter
  3. //
  4. // Author:
  5. // Kral Ferch <[email protected]>
  6. //
  7. // (C) 2002 Kral Ferch
  8. //
  9. using System;
  10. namespace System.Xml
  11. {
  12. public abstract class XmlWriter
  13. {
  14. #region Fields
  15. protected WriteState ws = WriteState.Start;
  16. protected XmlNamespaceManager namespaceManager = new XmlNamespaceManager (new NameTable ());
  17. #endregion
  18. #region Constructors
  19. protected XmlWriter () { }
  20. #endregion
  21. #region Properties
  22. public abstract WriteState WriteState { get; }
  23. public abstract string XmlLang { get; }
  24. public abstract XmlSpace XmlSpace { get; }
  25. #endregion
  26. #region Methods
  27. public abstract void Close ();
  28. public abstract void Flush ();
  29. public abstract string LookupPrefix (string ns);
  30. [MonoTODO("DTDs must be implemented to use 'defattr' parameter.")]
  31. public virtual void WriteAttributes (XmlReader reader, bool defattr)
  32. {
  33. if(reader == null)
  34. throw new ArgumentException("null XmlReader specified.", "reader");
  35. switch (reader.NodeType) {
  36. case XmlNodeType.XmlDeclaration:
  37. case XmlNodeType.Element:
  38. if (reader.MoveToFirstAttribute ())
  39. goto case XmlNodeType.Attribute;
  40. break;
  41. case XmlNodeType.Attribute:
  42. do {
  43. // FIXME: use ReadAttributeValue () for strictly write EntityReference.
  44. WriteAttributeString (reader.Prefix, reader.LocalName, reader.NamespaceURI, reader.Value);
  45. } while (reader.MoveToNextAttribute ());
  46. break;
  47. default:
  48. throw new XmlException("NodeType is not one of Element, Attribute, nor XmlDeclaration.");
  49. }
  50. }
  51. public void WriteAttributeString (string localName, string value)
  52. {
  53. WriteAttributeString ("", localName, "", value);
  54. }
  55. public void WriteAttributeString (string localName, string ns, string value)
  56. {
  57. WriteAttributeString ("", localName, ns, value);
  58. }
  59. public void WriteAttributeString (string prefix, string localName, string ns, string value)
  60. {
  61. if ((prefix == "xmlns") || (localName == "xmlns"))
  62. {
  63. ns = value;
  64. if (prefix == "xmlns" && namespaceManager.HasNamespace (localName))
  65. return;
  66. /* Users need to be able to re-declare the default namespace for subnodes
  67. else if (localName == "xmlns" && namespaceManager.HasNamespace (String.Empty))
  68. return;
  69. */
  70. }
  71. WriteStartAttribute (prefix, localName, ns);
  72. WriteString (value);
  73. WriteEndAttribute ();
  74. if ((prefix == "xmlns") || (localName == "xmlns"))
  75. {
  76. if (prefix == "xmlns")
  77. namespaceManager.AddNamespace (localName, ns);
  78. else
  79. namespaceManager.AddNamespace ("", ns);
  80. }
  81. }
  82. public abstract void WriteBase64 (byte[] buffer, int index, int count);
  83. public abstract void WriteBinHex (byte[] buffer, int index, int count);
  84. public abstract void WriteCData (string text);
  85. public abstract void WriteCharEntity (char ch);
  86. public abstract void WriteChars (char[] buffer, int index, int count);
  87. public abstract void WriteComment (string text);
  88. public abstract void WriteDocType (string name, string pubid, string sysid, string subset);
  89. public void WriteElementString (string localName, string value)
  90. {
  91. WriteStartElement(localName);
  92. WriteString(value);
  93. WriteEndElement();
  94. }
  95. public void WriteElementString (string localName, string ns, string value)
  96. {
  97. WriteStartElement(localName, ns);
  98. WriteString(value);
  99. WriteEndElement();
  100. }
  101. public abstract void WriteEndAttribute ();
  102. public abstract void WriteEndDocument ();
  103. public abstract void WriteEndElement ();
  104. public abstract void WriteEntityRef (string name);
  105. public abstract void WriteFullEndElement ();
  106. public abstract void WriteName (string name);
  107. public abstract void WriteNmToken (string name);
  108. [MonoTODO("needs to test")]
  109. public virtual void WriteNode (XmlReader reader, bool defattr)
  110. {
  111. if (reader == null)
  112. throw new ArgumentException ();
  113. if (reader.ReadState == ReadState.Initial) {
  114. reader.Read ();
  115. do {
  116. WriteNode (reader, defattr);
  117. } while (!reader.EOF);
  118. return;
  119. }
  120. switch (reader.NodeType) {
  121. case XmlNodeType.Element:
  122. WriteStartElement (reader.Prefix, reader.LocalName, reader.NamespaceURI);
  123. WriteAttributes (reader, defattr);
  124. reader.MoveToElement ();
  125. if (reader.IsEmptyElement)
  126. WriteEndElement ();
  127. else {
  128. int depth = reader.Depth;
  129. reader.Read ();
  130. do {
  131. WriteNode (reader, defattr);
  132. } while (depth < reader.Depth);
  133. WriteFullEndElement ();
  134. }
  135. break;
  136. // In case of XmlAttribute, don't proceed reader.
  137. case XmlNodeType.Attribute:
  138. return;
  139. case XmlNodeType.Text:
  140. WriteString (reader.Value);
  141. break;
  142. case XmlNodeType.CDATA:
  143. WriteCData (reader.Value);
  144. break;
  145. case XmlNodeType.EntityReference:
  146. WriteEntityRef (reader.Name);
  147. break;
  148. case XmlNodeType.ProcessingInstruction:
  149. WriteProcessingInstruction (reader.Name, reader.Value);
  150. break;
  151. case XmlNodeType.Comment:
  152. WriteComment (reader.Value);
  153. break;
  154. case XmlNodeType.DocumentType:
  155. WriteDocType (reader.Name,
  156. reader ["PUBLIC"], reader ["SYSTEM"], reader.Value);
  157. break;
  158. case XmlNodeType.SignificantWhitespace:
  159. goto case XmlNodeType.Whitespace;
  160. case XmlNodeType.Whitespace:
  161. WriteWhitespace (reader.Value);
  162. break;
  163. case XmlNodeType.EndElement:
  164. WriteEndElement ();
  165. break;
  166. case XmlNodeType.EndEntity:
  167. break;
  168. case XmlNodeType.XmlDeclaration:
  169. string st = reader.GetAttribute ("standalone");
  170. if (st != String.Empty)
  171. WriteStartDocument (st.ToLower () == "yes");
  172. else
  173. WriteStartDocument ();
  174. break;
  175. default:
  176. throw new NotImplementedException ();
  177. }
  178. reader.Read ();
  179. }
  180. public abstract void WriteProcessingInstruction (string name, string text);
  181. public abstract void WriteQualifiedName (string localName, string ns);
  182. public abstract void WriteRaw (string data);
  183. public abstract void WriteRaw (char[] buffer, int index, int count);
  184. public void WriteStartAttribute (string localName, string ns)
  185. {
  186. WriteStartAttribute (null, localName, ns);
  187. }
  188. public abstract void WriteStartAttribute (string prefix, string localName, string ns);
  189. public abstract void WriteStartDocument ();
  190. public abstract void WriteStartDocument (bool standalone);
  191. public void WriteStartElement (string localName)
  192. {
  193. WriteStartElement (null, localName, null);
  194. }
  195. public void WriteStartElement (string localName, string ns)
  196. {
  197. WriteStartElement (null, localName, ns);
  198. }
  199. public abstract void WriteStartElement (string prefix, string localName, string ns);
  200. public abstract void WriteString (string text);
  201. public abstract void WriteSurrogateCharEntity (char lowChar, char highChar);
  202. public abstract void WriteWhitespace (string ws);
  203. #endregion
  204. }
  205. }