XmlWriter.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623
  1. //
  2. // System.Xml.XmlWriter
  3. //
  4. // Authors:
  5. // Kral Ferch <[email protected]>
  6. // Atsushi Enomoto <[email protected]>
  7. //
  8. // (C) 2002 Kral Ferch
  9. // (C) 2002-2003 Atsushi Enomoto
  10. //
  11. //
  12. // Permission is hereby granted, free of charge, to any person obtaining
  13. // a copy of this software and associated documentation files (the
  14. // "Software"), to deal in the Software without restriction, including
  15. // without limitation the rights to use, copy, modify, merge, publish,
  16. // distribute, sublicense, and/or sell copies of the Software, and to
  17. // permit persons to whom the Software is furnished to do so, subject to
  18. // the following conditions:
  19. //
  20. // The above copyright notice and this permission notice shall be
  21. // included in all copies or substantial portions of the Software.
  22. //
  23. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  27. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  28. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  29. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  30. //
  31. using System;
  32. using System.IO;
  33. using System.Text;
  34. #if NET_2_0
  35. using System.Xml.XPath;
  36. #endif
  37. namespace System.Xml
  38. {
  39. #if NET_2_0
  40. public abstract class XmlWriter : IDisposable
  41. #else
  42. public abstract class XmlWriter
  43. #endif
  44. {
  45. #if NET_2_0
  46. XmlWriterSettings settings;
  47. #endif
  48. #region Constructors
  49. protected XmlWriter () { }
  50. #endregion
  51. #region Properties
  52. #if NET_2_0
  53. public XmlWriterSettings Settings {
  54. get {
  55. if (settings == null)
  56. settings = new XmlWriterSettings ();
  57. return settings;
  58. }
  59. }
  60. #endif
  61. public abstract WriteState WriteState { get; }
  62. #if NET_2_0
  63. public virtual string XmlLang {
  64. get { return null; }
  65. }
  66. public virtual XmlSpace XmlSpace {
  67. get { return XmlSpace.None; }
  68. }
  69. #else
  70. public abstract string XmlLang { get; }
  71. public abstract XmlSpace XmlSpace { get; }
  72. #endif
  73. #endregion
  74. #region Methods
  75. public abstract void Close ();
  76. #if NET_2_0
  77. public static XmlWriter Create (Stream stream)
  78. {
  79. return Create (stream, null);
  80. }
  81. public static XmlWriter Create (string file)
  82. {
  83. return Create (file, null);
  84. }
  85. public static XmlWriter Create (TextWriter writer)
  86. {
  87. return Create (writer, null);
  88. }
  89. public static XmlWriter Create (StringBuilder builder)
  90. {
  91. return Create (builder, null);
  92. }
  93. public static XmlWriter Create (Stream stream, XmlWriterSettings settings)
  94. {
  95. // FIXME: this might result in encoding null reference
  96. Encoding enc = settings != null ? settings.Encoding : Encoding.UTF8;
  97. return Create (new StreamWriter (stream, enc), settings);
  98. }
  99. public static XmlWriter Create (string file, XmlWriterSettings settings)
  100. {
  101. // FIXME: this might result in encoding null reference
  102. Encoding enc = settings != null ? settings.Encoding : Encoding.UTF8;
  103. return Create (new StreamWriter (file, false, enc), settings);
  104. }
  105. [MonoTODO]
  106. public static XmlWriter Create (StringBuilder builder, XmlWriterSettings settings)
  107. {
  108. return Create (new StringWriter (builder), null);
  109. }
  110. [MonoTODO]
  111. public static XmlWriter Create (TextWriter writer, XmlWriterSettings settings)
  112. {
  113. return CreateTextWriter (writer, settings);
  114. }
  115. [MonoTODO]
  116. public static XmlWriter Create (XmlWriter writer, XmlWriterSettings settings)
  117. {
  118. if (settings == null)
  119. settings = new XmlWriterSettings ();
  120. writer.settings = settings;
  121. return writer;
  122. }
  123. public static XmlWriter CreateTextWriter (TextWriter writer, XmlWriterSettings settings)
  124. {
  125. if (settings == null)
  126. settings = new XmlWriterSettings ();
  127. XmlTextWriter xtw = new XmlTextWriter (writer);
  128. // Indent, IndentChars
  129. if (settings.Indent) {
  130. xtw.Formatting = Formatting.Indented;
  131. xtw.IndentChars = settings.IndentChars;
  132. }
  133. // NewLineChars
  134. xtw.NewLineChars = settings.NewLineChars;
  135. // CloseOutput
  136. xtw.CloseOutput = settings.CloseOutput;
  137. // NewLineOnAttributes
  138. xtw.NewLineOnAttributes = settings.NewLineOnAttributes;
  139. // ConformanceLevel
  140. xtw.ConformanceLevel = settings.ConformanceLevel;
  141. // OmitXmlDeclaration
  142. xtw.OmitXmlDeclaration = settings.OmitXmlDeclaration;
  143. // CheckCharacters
  144. xtw.CheckCharacters = settings.CheckCharacters;
  145. return Create (xtw, settings);
  146. }
  147. [MonoTODO]
  148. public virtual void Dispose ()
  149. {
  150. Close ();
  151. }
  152. #endif
  153. public abstract void Flush ();
  154. public abstract string LookupPrefix (string ns);
  155. private void WriteAttribute (XmlReader reader, bool defattr)
  156. {
  157. if (!defattr && reader.IsDefault)
  158. return;
  159. WriteStartAttribute (reader.Prefix, reader.LocalName, reader.NamespaceURI);
  160. while (reader.ReadAttributeValue ()) {
  161. switch (reader.NodeType) {
  162. case XmlNodeType.Text:
  163. WriteString (reader.Value);
  164. break;
  165. case XmlNodeType.EntityReference:
  166. WriteEntityRef (reader.Name);
  167. break;
  168. }
  169. }
  170. WriteEndAttribute ();
  171. }
  172. public virtual void WriteAttributes (XmlReader reader, bool defattr)
  173. {
  174. if(reader == null)
  175. throw new ArgumentException("null XmlReader specified.", "reader");
  176. switch (reader.NodeType) {
  177. case XmlNodeType.XmlDeclaration:
  178. WriteAttributeString ("version", reader ["version"]);
  179. if (reader ["encoding"] != null)
  180. WriteAttributeString ("encoding", reader ["encoding"]);
  181. if (reader ["standalone"] != null)
  182. WriteAttributeString ("standalone", reader ["standalone"]);
  183. break;
  184. case XmlNodeType.Element:
  185. if (reader.MoveToFirstAttribute ())
  186. goto case XmlNodeType.Attribute;
  187. break;
  188. case XmlNodeType.Attribute:
  189. do {
  190. WriteAttribute (reader, defattr);
  191. } while (reader.MoveToNextAttribute ());
  192. break;
  193. default:
  194. throw new XmlException("NodeType is not one of Element, Attribute, nor XmlDeclaration.");
  195. }
  196. }
  197. public void WriteAttributeString (string localName, string value)
  198. {
  199. WriteAttributeString ("", localName, null, value);
  200. }
  201. public void WriteAttributeString (string localName, string ns, string value)
  202. {
  203. WriteAttributeString ("", localName, ns, value);
  204. }
  205. public void WriteAttributeString (string prefix, string localName, string ns, string value)
  206. {
  207. // In MS.NET (1.0), this check is done *here*, not at WriteStartAttribute.
  208. // (XmlTextWriter.WriteStartAttribute("xmlns", "anyname", null) throws an exception.
  209. #if NET_1_0
  210. if ((prefix == "xmlns" || (prefix == "" && localName == "xmlns")) && ns == null)
  211. ns = "http://www.w3.org/2000/xmlns/";
  212. #endif
  213. WriteStartAttribute (prefix, localName, ns);
  214. WriteString (value);
  215. WriteEndAttribute ();
  216. }
  217. public abstract void WriteBase64 (byte[] buffer, int index, int count);
  218. #if NET_2_0
  219. public virtual void WriteBinHex (byte [] buffer, int index, int count)
  220. {
  221. StringWriter sw = new StringWriter ();
  222. XmlConvert.WriteBinHex (buffer, index, count, sw);
  223. WriteString (sw.ToString ());
  224. }
  225. #else
  226. public abstract void WriteBinHex (byte[] buffer, int index, int count);
  227. #endif
  228. public abstract void WriteCData (string text);
  229. public abstract void WriteCharEntity (char ch);
  230. public abstract void WriteChars (char[] buffer, int index, int count);
  231. public abstract void WriteComment (string text);
  232. public abstract void WriteDocType (string name, string pubid, string sysid, string subset);
  233. public void WriteElementString (string localName, string value)
  234. {
  235. WriteStartElement(localName);
  236. WriteString(value);
  237. WriteEndElement();
  238. }
  239. public void WriteElementString (string localName, string ns, string value)
  240. {
  241. WriteStartElement(localName, ns);
  242. WriteString(value);
  243. WriteEndElement();
  244. }
  245. #if NET_2_0
  246. public void WriteElementString (string prefix, string localName, string ns, string value)
  247. {
  248. WriteStartElement(prefix, localName, ns);
  249. WriteString(value);
  250. WriteEndElement();
  251. }
  252. #endif
  253. public abstract void WriteEndAttribute ();
  254. public abstract void WriteEndDocument ();
  255. public abstract void WriteEndElement ();
  256. public abstract void WriteEntityRef (string name);
  257. public abstract void WriteFullEndElement ();
  258. #if NET_2_0
  259. public virtual void WriteName (string name)
  260. {
  261. WriteNameInternal (name);
  262. }
  263. public virtual void WriteNmToken (string name)
  264. {
  265. WriteNmTokenInternal (name);
  266. }
  267. public virtual void WriteQualifiedName (string localName, string ns)
  268. {
  269. WriteQualifiedNameInternal (localName, ns);
  270. }
  271. #else
  272. public abstract void WriteName (string name);
  273. public abstract void WriteNmToken (string name);
  274. public abstract void WriteQualifiedName (string localName, string ns);
  275. #endif
  276. internal void WriteNameInternal (string name)
  277. {
  278. #if NET_2_0
  279. switch (Settings.ConformanceLevel) {
  280. case ConformanceLevel.Document:
  281. case ConformanceLevel.Fragment:
  282. XmlConvert.VerifyName (name);
  283. break;
  284. }
  285. #else
  286. XmlConvert.VerifyName (name);
  287. #endif
  288. WriteString (name);
  289. }
  290. internal virtual void WriteNmTokenInternal (string name)
  291. {
  292. #if NET_2_0
  293. switch (Settings.ConformanceLevel) {
  294. case ConformanceLevel.Document:
  295. case ConformanceLevel.Fragment:
  296. XmlConvert.VerifyNMTOKEN (name);
  297. break;
  298. }
  299. #else
  300. XmlConvert.VerifyNMTOKEN (name);
  301. #endif
  302. WriteString (name);
  303. }
  304. internal void WriteQualifiedNameInternal (string localName, string ns)
  305. {
  306. if (localName == null || localName == String.Empty)
  307. throw new ArgumentException ();
  308. #if NET_2_0
  309. switch (Settings.ConformanceLevel) {
  310. case ConformanceLevel.Document:
  311. case ConformanceLevel.Fragment:
  312. XmlConvert.VerifyNCName (localName);
  313. break;
  314. }
  315. #else
  316. XmlConvert.VerifyNCName (localName);
  317. #endif
  318. string prefix = LookupPrefix (ns);
  319. if (prefix != String.Empty) {
  320. WriteString (prefix);
  321. WriteString (":");
  322. WriteString (localName);
  323. }
  324. else
  325. WriteString (localName);
  326. }
  327. #if NET_2_0
  328. [MonoTODO ("defattr handling")]
  329. public virtual void WriteNode (XPathNavigator navigator, bool defattr)
  330. {
  331. WriteNode (navigator.ReadSubtree (), defattr);
  332. }
  333. #endif
  334. public virtual void WriteNode (XmlReader reader, bool defattr)
  335. {
  336. if (reader == null)
  337. throw new ArgumentException ();
  338. if (reader.ReadState == ReadState.Initial) {
  339. reader.Read ();
  340. do {
  341. WriteNode (reader, defattr);
  342. } while (!reader.EOF);
  343. return;
  344. }
  345. switch (reader.NodeType) {
  346. case XmlNodeType.Element:
  347. WriteStartElement (reader.Prefix, reader.LocalName, reader.NamespaceURI);
  348. #if false
  349. WriteAttributes (reader, defattr);
  350. reader.MoveToElement ();
  351. #else
  352. // Well, I found that MS.NET took this way, since
  353. // there was a error-prone SgmlReader that fails
  354. // MoveToNextAttribute().
  355. if (reader.HasAttributes) {
  356. for (int i = 0; i < reader.AttributeCount; i++) {
  357. reader.MoveToAttribute (i);
  358. WriteAttribute (reader, defattr);
  359. }
  360. reader.MoveToElement ();
  361. }
  362. #endif
  363. if (reader.IsEmptyElement)
  364. WriteEndElement ();
  365. else {
  366. int depth = reader.Depth;
  367. reader.Read ();
  368. if (reader.NodeType != XmlNodeType.EndElement) {
  369. do {
  370. WriteNode (reader, defattr);
  371. } while (depth < reader.Depth);
  372. }
  373. WriteFullEndElement ();
  374. }
  375. break;
  376. // In case of XmlAttribute, don't proceed reader.
  377. case XmlNodeType.Attribute:
  378. return;
  379. case XmlNodeType.Text:
  380. WriteString (reader.Value);
  381. break;
  382. case XmlNodeType.CDATA:
  383. WriteCData (reader.Value);
  384. break;
  385. case XmlNodeType.EntityReference:
  386. WriteEntityRef (reader.Name);
  387. break;
  388. case XmlNodeType.XmlDeclaration:
  389. // LAMESPEC: It means that XmlWriter implementation _must not_ check
  390. // whether PI name is "xml" (it is XML error) or not.
  391. case XmlNodeType.ProcessingInstruction:
  392. WriteProcessingInstruction (reader.Name, reader.Value);
  393. break;
  394. case XmlNodeType.Comment:
  395. WriteComment (reader.Value);
  396. break;
  397. case XmlNodeType.DocumentType:
  398. WriteDocType (reader.Name,
  399. reader ["PUBLIC"], reader ["SYSTEM"], reader.Value);
  400. break;
  401. case XmlNodeType.SignificantWhitespace:
  402. goto case XmlNodeType.Whitespace;
  403. case XmlNodeType.Whitespace:
  404. WriteWhitespace (reader.Value);
  405. break;
  406. case XmlNodeType.EndElement:
  407. WriteFullEndElement ();
  408. break;
  409. case XmlNodeType.EndEntity:
  410. break;
  411. case XmlNodeType.None:
  412. break; // Do nothing, nor reporting errors.
  413. default:
  414. throw new XmlException ("Unexpected node " + reader.Name + " of type " + reader.NodeType);
  415. }
  416. reader.Read ();
  417. }
  418. public abstract void WriteProcessingInstruction (string name, string text);
  419. public abstract void WriteRaw (string data);
  420. public abstract void WriteRaw (char[] buffer, int index, int count);
  421. #if NET_2_0
  422. public void WriteStartAttribute (string localName)
  423. {
  424. WriteStartAttribute (null, localName, null);
  425. }
  426. #endif
  427. public void WriteStartAttribute (string localName, string ns)
  428. {
  429. WriteStartAttribute (null, localName, ns);
  430. }
  431. public abstract void WriteStartAttribute (string prefix, string localName, string ns);
  432. public abstract void WriteStartDocument ();
  433. public abstract void WriteStartDocument (bool standalone);
  434. public void WriteStartElement (string localName)
  435. {
  436. WriteStartElement (null, localName, null);
  437. }
  438. public void WriteStartElement (string localName, string ns)
  439. {
  440. WriteStartElement (null, localName, ns);
  441. }
  442. public abstract void WriteStartElement (string prefix, string localName, string ns);
  443. public abstract void WriteString (string text);
  444. public abstract void WriteSurrogateCharEntity (char lowChar, char highChar);
  445. public abstract void WriteWhitespace (string ws);
  446. #if NET_2_0
  447. [MonoTODO]
  448. public virtual void WriteFromObject (object value)
  449. {
  450. throw new NotImplementedException ();
  451. }
  452. [MonoTODO]
  453. public virtual void WriteValue (bool value)
  454. {
  455. WriteString (XQueryConvert.BooleanToString (value));
  456. }
  457. [MonoTODO]
  458. public virtual void WriteValue (DateTime value)
  459. {
  460. WriteString (XmlConvert.ToString (value));
  461. }
  462. [MonoTODO]
  463. public virtual void WriteValue (Decimal value)
  464. {
  465. WriteString (XQueryConvert.DecimalToString (value));
  466. }
  467. [MonoTODO]
  468. public virtual void WriteValue (double value)
  469. {
  470. WriteString (XQueryConvert.DoubleToString (value));
  471. }
  472. [MonoTODO]
  473. public virtual void WriteValue (int value)
  474. {
  475. WriteString (XQueryConvert.IntToString (value));
  476. }
  477. [MonoTODO]
  478. public virtual void WriteValue (long value)
  479. {
  480. WriteString (XQueryConvert.IntegerToString (value));
  481. }
  482. [MonoTODO]
  483. public virtual void WriteValue (Stream value)
  484. {
  485. throw new NotImplementedException ();
  486. }
  487. [MonoTODO]
  488. public virtual void WriteValue (TextReader value)
  489. {
  490. throw new NotImplementedException ();
  491. }
  492. [MonoTODO]
  493. public virtual void WriteValue (object value)
  494. {
  495. if (value is string)
  496. WriteString ((string) value);
  497. else if (value is bool)
  498. WriteValue ((bool) value);
  499. else if (value is DateTime)
  500. WriteValue ((DateTime) value);
  501. else if (value is decimal)
  502. WriteValue ((decimal) value);
  503. else if (value is double)
  504. WriteValue ((double) value);
  505. else if (value is int)
  506. WriteValue ((int) value);
  507. else if (value is long)
  508. WriteValue ((long) value);
  509. else if (value is XmlQualifiedName) {
  510. XmlQualifiedName qname = (XmlQualifiedName) value;
  511. WriteQualifiedName (qname.Name, qname.Namespace);
  512. }
  513. else
  514. throw new NotImplementedException ("Argument value is " + value);
  515. }
  516. [MonoTODO]
  517. public virtual void WriteValue (float value)
  518. {
  519. WriteString (XQueryConvert.FloatToString (value));
  520. }
  521. [MonoTODO]
  522. public virtual void WriteValue (string value)
  523. {
  524. WriteString (value);
  525. }
  526. #endif
  527. #endregion
  528. }
  529. }