XmlWriter.cs 14 KB

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