XmlWriter.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  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. [MonoTODO]
  78. public static XmlWriter Create (Stream stream)
  79. {
  80. throw new NotImplementedException ();
  81. }
  82. [MonoTODO]
  83. public static XmlWriter Create (string file)
  84. {
  85. return new XmlTextWriter (file, null);
  86. }
  87. [MonoTODO]
  88. public static XmlWriter Create (TextWriter writer)
  89. {
  90. return new XmlTextWriter (writer);
  91. }
  92. [MonoTODO]
  93. public static XmlWriter Create (StringBuilder builder)
  94. {
  95. throw new NotImplementedException ();
  96. }
  97. [MonoTODO]
  98. public static XmlWriter Create (Stream stream, XmlWriterSettings settings)
  99. {
  100. throw new NotImplementedException ();
  101. }
  102. [MonoTODO]
  103. public static XmlWriter Create (string file, XmlWriterSettings settings)
  104. {
  105. throw new NotImplementedException ();
  106. }
  107. [MonoTODO]
  108. public static XmlWriter Create (StringBuilder builder, XmlWriterSettings settings)
  109. {
  110. throw new NotImplementedException ();
  111. }
  112. [MonoTODO]
  113. public static XmlWriter Create (TextWriter writer, XmlWriterSettings settings)
  114. {
  115. throw new NotImplementedException ();
  116. }
  117. [MonoTODO]
  118. public static XmlWriter Create (XmlWriter writer, XmlWriterSettings settings)
  119. {
  120. throw new NotImplementedException ();
  121. }
  122. [MonoTODO]
  123. public virtual void Dispose ()
  124. {
  125. Close ();
  126. }
  127. #endif
  128. public abstract void Flush ();
  129. public abstract string LookupPrefix (string ns);
  130. private void WriteAttribute (XmlReader reader, bool defattr)
  131. {
  132. if (!defattr && reader.IsDefault)
  133. return;
  134. WriteStartAttribute (reader.Prefix, reader.LocalName, reader.NamespaceURI);
  135. while (reader.ReadAttributeValue ()) {
  136. switch (reader.NodeType) {
  137. case XmlNodeType.Text:
  138. WriteString (reader.Value);
  139. break;
  140. case XmlNodeType.EntityReference:
  141. WriteEntityRef (reader.Name);
  142. break;
  143. }
  144. }
  145. WriteEndAttribute ();
  146. }
  147. public virtual void WriteAttributes (XmlReader reader, bool defattr)
  148. {
  149. if(reader == null)
  150. throw new ArgumentException("null XmlReader specified.", "reader");
  151. switch (reader.NodeType) {
  152. case XmlNodeType.XmlDeclaration:
  153. WriteAttributeString ("version", reader ["version"]);
  154. if (reader ["encoding"] != null)
  155. WriteAttributeString ("encoding", reader ["encoding"]);
  156. if (reader ["standalone"] != null)
  157. WriteAttributeString ("standalone", reader ["standalone"]);
  158. break;
  159. case XmlNodeType.Element:
  160. if (reader.MoveToFirstAttribute ())
  161. goto case XmlNodeType.Attribute;
  162. break;
  163. case XmlNodeType.Attribute:
  164. do {
  165. WriteAttribute (reader, defattr);
  166. } while (reader.MoveToNextAttribute ());
  167. break;
  168. default:
  169. throw new XmlException("NodeType is not one of Element, Attribute, nor XmlDeclaration.");
  170. }
  171. }
  172. public void WriteAttributeString (string localName, string value)
  173. {
  174. WriteAttributeString ("", localName, null, value);
  175. }
  176. public void WriteAttributeString (string localName, string ns, string value)
  177. {
  178. WriteAttributeString ("", localName, ns, value);
  179. }
  180. public void WriteAttributeString (string prefix, string localName, string ns, string value)
  181. {
  182. // In MS.NET (1.0), this check is done *here*, not at WriteStartAttribute.
  183. // (XmlTextWriter.WriteStartAttribute("xmlns", "anyname", null) throws an exception.
  184. #if NET_1_0
  185. if ((prefix == "xmlns" || (prefix == "" && localName == "xmlns")) && ns == null)
  186. ns = "http://www.w3.org/2000/xmlns/";
  187. #endif
  188. WriteStartAttribute (prefix, localName, ns);
  189. WriteString (value);
  190. WriteEndAttribute ();
  191. }
  192. public abstract void WriteBase64 (byte[] buffer, int index, int count);
  193. #if NET_2_0
  194. public virtual void WriteBinHex (byte [] buffer, int index, int count)
  195. {
  196. StringWriter sw = new StringWriter ();
  197. XmlConvert.WriteBinHex (buffer, index, count, sw);
  198. WriteString (sw.ToString ());
  199. }
  200. #else
  201. public abstract void WriteBinHex (byte[] buffer, int index, int count);
  202. #endif
  203. public abstract void WriteCData (string text);
  204. public abstract void WriteCharEntity (char ch);
  205. public abstract void WriteChars (char[] buffer, int index, int count);
  206. public abstract void WriteComment (string text);
  207. public abstract void WriteDocType (string name, string pubid, string sysid, string subset);
  208. public void WriteElementString (string localName, string value)
  209. {
  210. WriteStartElement(localName);
  211. WriteString(value);
  212. WriteEndElement();
  213. }
  214. public void WriteElementString (string localName, string ns, string value)
  215. {
  216. WriteStartElement(localName, ns);
  217. WriteString(value);
  218. WriteEndElement();
  219. }
  220. #if NET_2_0
  221. public void WriteElementString (string prefix, string localName, string ns, string value)
  222. {
  223. WriteStartElement(prefix, localName, ns);
  224. WriteString(value);
  225. WriteEndElement();
  226. }
  227. #endif
  228. public abstract void WriteEndAttribute ();
  229. public abstract void WriteEndDocument ();
  230. public abstract void WriteEndElement ();
  231. public abstract void WriteEntityRef (string name);
  232. public abstract void WriteFullEndElement ();
  233. #if NET_2_0
  234. public virtual void WriteName (string name)
  235. {
  236. WriteNameInternal (name);
  237. }
  238. public virtual void WriteNmToken (string name)
  239. {
  240. WriteNmTokenInternal (name);
  241. }
  242. public virtual void WriteQualifiedName (string localName, string ns)
  243. {
  244. WriteQualifiedNameInternal (localName, ns);
  245. }
  246. #else
  247. public abstract void WriteName (string name);
  248. public abstract void WriteNmToken (string name);
  249. public abstract void WriteQualifiedName (string localName, string ns);
  250. #endif
  251. internal void WriteNameInternal (string name)
  252. {
  253. #if NET_2_0
  254. switch (Settings.ConformanceLevel) {
  255. case ConformanceLevel.Document:
  256. case ConformanceLevel.Fragment:
  257. XmlConvert.VerifyName (name);
  258. break;
  259. }
  260. #else
  261. XmlConvert.VerifyName (name);
  262. #endif
  263. WriteString (name);
  264. }
  265. internal virtual void WriteNmTokenInternal (string name)
  266. {
  267. #if NET_2_0
  268. switch (Settings.ConformanceLevel) {
  269. case ConformanceLevel.Document:
  270. case ConformanceLevel.Fragment:
  271. XmlConvert.VerifyNMTOKEN (name);
  272. break;
  273. }
  274. #else
  275. XmlConvert.VerifyNMTOKEN (name);
  276. #endif
  277. WriteString (name);
  278. }
  279. internal void WriteQualifiedNameInternal (string localName, string ns)
  280. {
  281. if (localName == null || localName == String.Empty)
  282. throw new ArgumentException ();
  283. #if NET_2_0
  284. switch (Settings.ConformanceLevel) {
  285. case ConformanceLevel.Document:
  286. case ConformanceLevel.Fragment:
  287. XmlConvert.VerifyNCName (localName);
  288. break;
  289. }
  290. #else
  291. XmlConvert.VerifyNCName (localName);
  292. #endif
  293. string prefix = LookupPrefix (ns);
  294. if (prefix != String.Empty) {
  295. WriteString (prefix);
  296. WriteString (":");
  297. WriteString (localName);
  298. }
  299. else
  300. WriteString (localName);
  301. }
  302. #if NET_2_0
  303. [MonoTODO ("defattr handling")]
  304. public virtual void WriteNode (XPathNavigator navigator, bool defattr)
  305. {
  306. WriteNode (navigator.ReadSubtree (), defattr);
  307. }
  308. #endif
  309. public virtual void WriteNode (XmlReader reader, bool defattr)
  310. {
  311. if (reader == null)
  312. throw new ArgumentException ();
  313. if (reader.ReadState == ReadState.Initial) {
  314. reader.Read ();
  315. do {
  316. WriteNode (reader, defattr);
  317. } while (!reader.EOF);
  318. return;
  319. }
  320. switch (reader.NodeType) {
  321. case XmlNodeType.Element:
  322. WriteStartElement (reader.Prefix, reader.LocalName, reader.NamespaceURI);
  323. #if false
  324. WriteAttributes (reader, defattr);
  325. reader.MoveToElement ();
  326. #else
  327. // Well, I found that MS.NET took this way, since
  328. // there was a error-prone SgmlReader that fails
  329. // MoveToNextAttribute().
  330. if (reader.HasAttributes) {
  331. for (int i = 0; i < reader.AttributeCount; i++) {
  332. reader.MoveToAttribute (i);
  333. WriteAttribute (reader, defattr);
  334. }
  335. reader.MoveToElement ();
  336. }
  337. #endif
  338. if (reader.IsEmptyElement)
  339. WriteEndElement ();
  340. else {
  341. int depth = reader.Depth;
  342. reader.Read ();
  343. if (reader.NodeType != XmlNodeType.EndElement) {
  344. do {
  345. WriteNode (reader, defattr);
  346. } while (depth < reader.Depth);
  347. }
  348. WriteFullEndElement ();
  349. }
  350. break;
  351. // In case of XmlAttribute, don't proceed reader.
  352. case XmlNodeType.Attribute:
  353. return;
  354. case XmlNodeType.Text:
  355. WriteString (reader.Value);
  356. break;
  357. case XmlNodeType.CDATA:
  358. WriteCData (reader.Value);
  359. break;
  360. case XmlNodeType.EntityReference:
  361. WriteEntityRef (reader.Name);
  362. break;
  363. case XmlNodeType.XmlDeclaration:
  364. // LAMESPEC: It means that XmlWriter implementation _must not_ check
  365. // whether PI name is "xml" (it is XML error) or not.
  366. case XmlNodeType.ProcessingInstruction:
  367. WriteProcessingInstruction (reader.Name, reader.Value);
  368. break;
  369. case XmlNodeType.Comment:
  370. WriteComment (reader.Value);
  371. break;
  372. case XmlNodeType.DocumentType:
  373. WriteDocType (reader.Name,
  374. reader ["PUBLIC"], reader ["SYSTEM"], reader.Value);
  375. break;
  376. case XmlNodeType.SignificantWhitespace:
  377. goto case XmlNodeType.Whitespace;
  378. case XmlNodeType.Whitespace:
  379. WriteWhitespace (reader.Value);
  380. break;
  381. case XmlNodeType.EndElement:
  382. WriteFullEndElement ();
  383. break;
  384. case XmlNodeType.EndEntity:
  385. break;
  386. case XmlNodeType.None:
  387. return; // Do nothing, nor reporting errors.
  388. default:
  389. throw new XmlException ("Unexpected node " + reader.Name + " of type " + reader.NodeType);
  390. }
  391. reader.Read ();
  392. }
  393. public abstract void WriteProcessingInstruction (string name, string text);
  394. public abstract void WriteRaw (string data);
  395. public abstract void WriteRaw (char[] buffer, int index, int count);
  396. #if NET_2_0
  397. public void WriteStartAttribute (string localName)
  398. {
  399. WriteStartAttribute (null, localName, null);
  400. }
  401. #endif
  402. public void WriteStartAttribute (string localName, string ns)
  403. {
  404. WriteStartAttribute (null, localName, ns);
  405. }
  406. public abstract void WriteStartAttribute (string prefix, string localName, string ns);
  407. public abstract void WriteStartDocument ();
  408. public abstract void WriteStartDocument (bool standalone);
  409. public void WriteStartElement (string localName)
  410. {
  411. WriteStartElement (null, localName, null);
  412. }
  413. public void WriteStartElement (string localName, string ns)
  414. {
  415. WriteStartElement (null, localName, ns);
  416. }
  417. public abstract void WriteStartElement (string prefix, string localName, string ns);
  418. public abstract void WriteString (string text);
  419. public abstract void WriteSurrogateCharEntity (char lowChar, char highChar);
  420. public abstract void WriteWhitespace (string ws);
  421. #if NET_2_0
  422. [MonoTODO]
  423. public virtual void WriteFromObject (object value)
  424. {
  425. throw new NotImplementedException ();
  426. }
  427. [MonoTODO]
  428. public virtual void WriteValue (bool value)
  429. {
  430. WriteString (XQueryConvert.BooleanToString (value));
  431. }
  432. [MonoTODO]
  433. public virtual void WriteValue (DateTime value)
  434. {
  435. WriteString (XmlConvert.ToString (value));
  436. }
  437. [MonoTODO]
  438. public virtual void WriteValue (Decimal value)
  439. {
  440. WriteString (XQueryConvert.DecimalToString (value));
  441. }
  442. [MonoTODO]
  443. public virtual void WriteValue (double value)
  444. {
  445. WriteString (XQueryConvert.DoubleToString (value));
  446. }
  447. [MonoTODO]
  448. public virtual void WriteValue (int value)
  449. {
  450. WriteString (XQueryConvert.IntToString (value));
  451. }
  452. [MonoTODO]
  453. public virtual void WriteValue (long value)
  454. {
  455. WriteString (XQueryConvert.IntegerToString (value));
  456. }
  457. [MonoTODO]
  458. public virtual void WriteValue (Stream value)
  459. {
  460. throw new NotImplementedException ();
  461. }
  462. [MonoTODO]
  463. public virtual void WriteValue (TextReader value)
  464. {
  465. throw new NotImplementedException ();
  466. }
  467. [MonoTODO]
  468. public virtual void WriteValue (object value)
  469. {
  470. if (value is string)
  471. WriteString ((string) value);
  472. else if (value is bool)
  473. WriteValue ((bool) value);
  474. else if (value is DateTime)
  475. WriteValue ((DateTime) value);
  476. else if (value is decimal)
  477. WriteValue ((decimal) value);
  478. else if (value is double)
  479. WriteValue ((double) value);
  480. else if (value is int)
  481. WriteValue ((int) value);
  482. else if (value is long)
  483. WriteValue ((long) value);
  484. else if (value is XmlQualifiedName) {
  485. XmlQualifiedName qname = (XmlQualifiedName) value;
  486. WriteQualifiedName (qname.Name, qname.Namespace);
  487. }
  488. else
  489. throw new NotImplementedException ("Argument value is " + value);
  490. }
  491. [MonoTODO]
  492. public virtual void WriteValue (float value)
  493. {
  494. WriteString (XQueryConvert.FloatToString (value));
  495. }
  496. [MonoTODO]
  497. public virtual void WriteValue (string value)
  498. {
  499. WriteString (value);
  500. }
  501. #endif
  502. #endregion
  503. }
  504. }