2
0

JsonReader.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840
  1. //
  2. // JsonWriter.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  6. //
  7. // Copyright (C) 2007 Novell, Inc (http://www.novell.com)
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System;
  29. using System.Collections.Generic;
  30. using System.Globalization;
  31. using System.IO;
  32. using System.Text;
  33. using System.Xml;
  34. namespace System.Runtime.Serialization.Json
  35. {
  36. class PushbackReader : StreamReader
  37. {
  38. Stack<int> pushback;
  39. public PushbackReader (Stream stream, Encoding encoding) : base (stream, encoding)
  40. {
  41. pushback = new Stack<int>();
  42. }
  43. public PushbackReader (Stream stream) : base (stream, true)
  44. {
  45. pushback = new Stack<int>();
  46. }
  47. public override void Close ()
  48. {
  49. pushback.Clear ();
  50. }
  51. public override int Peek ()
  52. {
  53. if (pushback.Count > 0) {
  54. return pushback.Peek ();
  55. }
  56. else {
  57. return base.Peek ();
  58. }
  59. }
  60. public override int Read ()
  61. {
  62. if (pushback.Count > 0) {
  63. return pushback.Pop ();
  64. }
  65. else {
  66. return base.Read ();
  67. }
  68. }
  69. public void Pushback (int ch)
  70. {
  71. pushback.Push (ch);
  72. }
  73. }
  74. // FIXME: quotas check
  75. class JsonReader : XmlDictionaryReader, IXmlJsonReaderInitializer, IXmlLineInfo
  76. {
  77. class ElementInfo
  78. {
  79. public readonly string Name;
  80. public readonly string Type;
  81. public bool HasContent;
  82. public ElementInfo (string name, string type)
  83. {
  84. this.Name = name;
  85. this.Type = type;
  86. }
  87. }
  88. enum AttributeState
  89. {
  90. None,
  91. Type,
  92. TypeValue,
  93. RuntimeType,
  94. RuntimeTypeValue
  95. }
  96. PushbackReader reader;
  97. XmlDictionaryReaderQuotas quotas;
  98. OnXmlDictionaryReaderClose on_close;
  99. XmlNameTable name_table = new NameTable ();
  100. XmlNodeType current_node;
  101. AttributeState attr_state;
  102. string simple_value;
  103. string next_element;
  104. string current_runtime_type, next_object_content_name;
  105. ReadState read_state = ReadState.Initial;
  106. bool content_stored;
  107. bool finished;
  108. Stack<ElementInfo> elements = new Stack<ElementInfo> ();
  109. int line = 1, column = 0;
  110. // Constructors
  111. public JsonReader (byte [] buffer, int offset, int count, Encoding encoding, XmlDictionaryReaderQuotas quotas, OnXmlDictionaryReaderClose onClose)
  112. {
  113. SetInput (buffer, offset, count, encoding, quotas, onClose);
  114. }
  115. public JsonReader (Stream stream, Encoding encoding, XmlDictionaryReaderQuotas quotas, OnXmlDictionaryReaderClose onClose)
  116. {
  117. SetInput (stream, encoding, quotas, onClose);
  118. }
  119. internal bool LameSilverlightLiteralParser { get; set; }
  120. // IXmlLineInfo
  121. public bool HasLineInfo ()
  122. {
  123. return true;
  124. }
  125. public int LineNumber {
  126. get { return line; }
  127. }
  128. public int LinePosition {
  129. get { return column; }
  130. }
  131. // IXmlJsonReaderInitializer
  132. public void SetInput (byte [] buffer, int offset, int count, Encoding encoding, XmlDictionaryReaderQuotas quotas, OnXmlDictionaryReaderClose onClose)
  133. {
  134. SetInput (new MemoryStream (buffer, offset, count), encoding, quotas, onClose);
  135. }
  136. public void SetInput (Stream stream, Encoding encoding, XmlDictionaryReaderQuotas quotas, OnXmlDictionaryReaderClose onClose)
  137. {
  138. if (encoding != null)
  139. reader = new PushbackReader (stream, encoding);
  140. else
  141. reader = new PushbackReader (stream);
  142. if (quotas == null)
  143. throw new ArgumentNullException ("quotas");
  144. this.quotas = quotas;
  145. this.on_close = onClose;
  146. }
  147. // XmlDictionaryReader
  148. public override int AttributeCount {
  149. get { return current_node != XmlNodeType.Element ? 0 : current_runtime_type != null ? 2 : 1; }
  150. }
  151. public override string BaseURI {
  152. get { return String.Empty; }
  153. }
  154. public override int Depth {
  155. get {
  156. int mod = 0;
  157. switch (attr_state) {
  158. case AttributeState.Type:
  159. case AttributeState.RuntimeType:
  160. mod++;
  161. break;
  162. case AttributeState.TypeValue:
  163. case AttributeState.RuntimeTypeValue:
  164. mod += 2;
  165. break;
  166. case AttributeState.None:
  167. if (NodeType == XmlNodeType.Text)
  168. mod++;
  169. break;
  170. }
  171. return read_state != ReadState.Interactive ? 0 : elements.Count - 1 + mod;
  172. }
  173. }
  174. public override bool EOF {
  175. get {
  176. switch (read_state) {
  177. case ReadState.Closed:
  178. case ReadState.EndOfFile:
  179. return true;
  180. default:
  181. return false;
  182. }
  183. }
  184. }
  185. public override bool HasValue {
  186. get {
  187. switch (NodeType) {
  188. case XmlNodeType.Attribute:
  189. case XmlNodeType.Text:
  190. return true;
  191. default:
  192. return false;
  193. }
  194. }
  195. }
  196. public override bool IsEmptyElement {
  197. get { return false; }
  198. }
  199. public override string LocalName {
  200. get {
  201. switch (attr_state) {
  202. case AttributeState.Type:
  203. return "type";
  204. case AttributeState.RuntimeType:
  205. return "__type";
  206. }
  207. switch (NodeType) {
  208. case XmlNodeType.Element:
  209. case XmlNodeType.EndElement:
  210. return elements.Peek ().Name;
  211. default:
  212. return String.Empty;
  213. }
  214. }
  215. }
  216. public override string NamespaceURI {
  217. get { return String.Empty; }
  218. }
  219. public override XmlNameTable NameTable {
  220. get { return name_table; }
  221. }
  222. public override XmlNodeType NodeType {
  223. get {
  224. switch (attr_state) {
  225. case AttributeState.Type:
  226. case AttributeState.RuntimeType:
  227. return XmlNodeType.Attribute;
  228. case AttributeState.TypeValue:
  229. case AttributeState.RuntimeTypeValue:
  230. return XmlNodeType.Text;
  231. default:
  232. return current_node;
  233. }
  234. }
  235. }
  236. public override string Prefix {
  237. get { return String.Empty; }
  238. }
  239. public override ReadState ReadState {
  240. get { return read_state; }
  241. }
  242. public override string Value {
  243. get {
  244. switch (attr_state) {
  245. case AttributeState.Type:
  246. case AttributeState.TypeValue:
  247. return elements.Peek ().Type;
  248. case AttributeState.RuntimeType:
  249. case AttributeState.RuntimeTypeValue:
  250. return current_runtime_type;
  251. default:
  252. return current_node == XmlNodeType.Text ? simple_value : String.Empty;
  253. }
  254. }
  255. }
  256. public override void Close ()
  257. {
  258. if (on_close != null) {
  259. on_close (this);
  260. on_close = null;
  261. }
  262. read_state = ReadState.Closed;
  263. }
  264. public override string GetAttribute (int index)
  265. {
  266. if (index == 0 && current_node == XmlNodeType.Element)
  267. return elements.Peek ().Type;
  268. else if (index == 1 && current_runtime_type != null)
  269. return current_runtime_type;
  270. throw new ArgumentOutOfRangeException ("index", "Index is must be either 0 or 1 when there is an explicit __type in the object, and only valid on an element on this XmlDictionaryReader");
  271. }
  272. public override string GetAttribute (string name)
  273. {
  274. if (current_node != XmlNodeType.Element)
  275. return null;
  276. switch (name) {
  277. case "type":
  278. return elements.Peek ().Type;
  279. case "__type":
  280. return current_runtime_type;
  281. default:
  282. return null;
  283. }
  284. }
  285. public override string GetAttribute (string localName, string ns)
  286. {
  287. if (ns == String.Empty)
  288. return GetAttribute (localName);
  289. else
  290. return null;
  291. }
  292. public override string LookupNamespace (string prefix)
  293. {
  294. if (prefix == null)
  295. throw new ArgumentNullException ("prefix");
  296. else if (prefix.Length == 0)
  297. return String.Empty;
  298. return null;
  299. }
  300. public override bool MoveToAttribute (string name)
  301. {
  302. if (current_node != XmlNodeType.Element)
  303. return false;
  304. switch (name) {
  305. case "type":
  306. attr_state = AttributeState.Type;
  307. return true;
  308. case "__type":
  309. if (current_runtime_type == null)
  310. return false;
  311. attr_state = AttributeState.RuntimeType;
  312. return true;
  313. default:
  314. return false;
  315. }
  316. }
  317. public override bool MoveToAttribute (string localName, string ns)
  318. {
  319. if (ns != String.Empty)
  320. return false;
  321. return MoveToAttribute (localName);
  322. }
  323. public override bool MoveToElement ()
  324. {
  325. if (attr_state == AttributeState.None)
  326. return false;
  327. attr_state = AttributeState.None;
  328. return true;
  329. }
  330. public override bool MoveToFirstAttribute ()
  331. {
  332. if (current_node != XmlNodeType.Element)
  333. return false;
  334. attr_state = AttributeState.Type;
  335. return true;
  336. }
  337. public override bool MoveToNextAttribute ()
  338. {
  339. if (attr_state == AttributeState.None)
  340. return MoveToFirstAttribute ();
  341. else
  342. return MoveToAttribute ("__type");
  343. }
  344. public override bool ReadAttributeValue ()
  345. {
  346. switch (attr_state) {
  347. case AttributeState.Type:
  348. attr_state = AttributeState.TypeValue;
  349. return true;
  350. case AttributeState.RuntimeType:
  351. attr_state = AttributeState.RuntimeTypeValue;
  352. return true;
  353. }
  354. return false;
  355. }
  356. public override void ResolveEntity ()
  357. {
  358. throw new NotSupportedException ();
  359. }
  360. public override bool Read ()
  361. {
  362. switch (read_state) {
  363. case ReadState.EndOfFile:
  364. case ReadState.Closed:
  365. case ReadState.Error:
  366. return false;
  367. case ReadState.Initial:
  368. read_state = ReadState.Interactive;
  369. next_element = "root";
  370. current_node = XmlNodeType.Element;
  371. break;
  372. }
  373. if (content_stored) {
  374. if (current_node == XmlNodeType.Element) {
  375. if (elements.Peek ().Type == "null") {
  376. // since null is not consumed as text content, it skips Text state.
  377. current_node = XmlNodeType.EndElement;
  378. content_stored = false;
  379. }
  380. else
  381. current_node = XmlNodeType.Text;
  382. return true;
  383. } else if (current_node == XmlNodeType.Text) {
  384. current_node = XmlNodeType.EndElement;
  385. content_stored = false;
  386. return true;
  387. }
  388. }
  389. else if (current_node == XmlNodeType.EndElement) {
  390. // clear EndElement state
  391. elements.Pop ();
  392. if (elements.Count > 0)
  393. elements.Peek ().HasContent = true;
  394. else
  395. finished = true;
  396. }
  397. SkipWhitespaces ();
  398. attr_state = AttributeState.None;
  399. // Default. May be overriden only as EndElement or None.
  400. current_node = XmlNodeType.Element;
  401. if (!ReadContent (false))
  402. return false;
  403. if (finished)
  404. throw XmlError ("Multiple top-level content is not allowed");
  405. return true;
  406. }
  407. bool TryReadString (string str)
  408. {
  409. for (int i = 0; i < str.Length; i ++) {
  410. int ch = ReadChar ();
  411. if (ch != str[i]) {
  412. for (int j = i; j >= 0; j--)
  413. PushbackChar (j);
  414. return false;
  415. }
  416. }
  417. return true;
  418. }
  419. bool ReadContent (bool objectValue)
  420. {
  421. int ch = ReadChar ();
  422. if (ch < 0) {
  423. ReadEndOfStream ();
  424. return false;
  425. }
  426. bool itemMustFollow = false;
  427. if (!objectValue && elements.Count > 0 && elements.Peek ().HasContent) {
  428. if (ch == ',') {
  429. switch (elements.Peek ().Type) {
  430. case "object":
  431. case "array":
  432. SkipWhitespaces ();
  433. ch = ReadChar ();
  434. itemMustFollow = true;
  435. break;
  436. }
  437. }
  438. else if (ch != '}' && ch != ']')
  439. throw XmlError ("Comma is required unless an array or object is at the end");
  440. }
  441. if (elements.Count > 0 && elements.Peek ().Type == "array")
  442. next_element = "item";
  443. else if (next_object_content_name != null) {
  444. next_element = next_object_content_name;
  445. next_object_content_name = null;
  446. if (ch != ':')
  447. throw XmlError ("':' is expected after a name of an object content");
  448. SkipWhitespaces ();
  449. ReadContent (true);
  450. return true;
  451. }
  452. switch (ch) {
  453. case '{':
  454. ReadStartObject ();
  455. return true;
  456. case '[':
  457. ReadStartArray ();
  458. return true;
  459. case '}':
  460. if (itemMustFollow)
  461. throw XmlError ("Invalid comma before an end of object");
  462. if (objectValue)
  463. throw XmlError ("Invalid end of object as an object content");
  464. ReadEndObject ();
  465. return true;
  466. case ']':
  467. if (itemMustFollow)
  468. throw XmlError ("Invalid comma before an end of array");
  469. if (objectValue)
  470. throw XmlError ("Invalid end of array as an object content");
  471. ReadEndArray ();
  472. return true;
  473. case '"':
  474. bool lame = LameSilverlightLiteralParser && ch != '"';
  475. string s = ReadStringLiteral (lame);
  476. if (!objectValue && elements.Count > 0 && elements.Peek ().Type == "object") {
  477. next_element = s;
  478. SkipWhitespaces ();
  479. if (!lame)
  480. Expect (':');
  481. SkipWhitespaces ();
  482. ReadContent (true);
  483. }
  484. else
  485. ReadAsSimpleContent ("string", s);
  486. return true;
  487. case '-':
  488. ReadNumber (ch);
  489. return true;
  490. case 'n':
  491. if (TryReadString("ull")) {
  492. ReadAsSimpleContent ("null", "null");
  493. return true;
  494. }
  495. else {
  496. // the pushback for 'n' is taken care of by the
  497. // default case if we're in lame silverlight literal
  498. // mode
  499. goto default;
  500. }
  501. case 't':
  502. if (TryReadString ("rue")) {
  503. ReadAsSimpleContent ("boolean", "true");
  504. return true;
  505. }
  506. else {
  507. // the pushback for 't' is taken care of by the
  508. // default case if we're in lame silverlight literal
  509. // mode
  510. goto default;
  511. }
  512. case 'f':
  513. if (TryReadString ("alse")) {
  514. ReadAsSimpleContent ("boolean", "false");
  515. return true;
  516. }
  517. else {
  518. // the pushback for 'f' is taken care of by the
  519. // default case if we're in lame silverlight literal
  520. // mode
  521. goto default;
  522. }
  523. default:
  524. if ('0' <= ch && ch <= '9') {
  525. ReadNumber (ch);
  526. return true;
  527. }
  528. if (LameSilverlightLiteralParser) {
  529. PushbackChar (ch);
  530. goto case '"';
  531. }
  532. throw XmlError (String.Format ("Unexpected token: '{0}' ({1:X04})", (char) ch, (int) ch));
  533. }
  534. }
  535. void ReadStartObject ()
  536. {
  537. ElementInfo ei = new ElementInfo (next_element, "object");
  538. elements.Push (ei);
  539. SkipWhitespaces ();
  540. if (PeekChar () == '"') { // it isn't premise: the object might be empty
  541. ReadChar ();
  542. string s = ReadStringLiteral ();
  543. if (s == "__type") {
  544. SkipWhitespaces ();
  545. Expect (':');
  546. SkipWhitespaces ();
  547. Expect ('"');
  548. current_runtime_type = ReadStringLiteral ();
  549. SkipWhitespaces ();
  550. ei.HasContent = true;
  551. }
  552. else
  553. next_object_content_name = s;
  554. }
  555. }
  556. void ReadStartArray ()
  557. {
  558. elements.Push (new ElementInfo (next_element, "array"));
  559. }
  560. void ReadEndObject ()
  561. {
  562. if (elements.Count == 0 || elements.Peek ().Type != "object")
  563. throw XmlError ("Unexpected end of object");
  564. current_node = XmlNodeType.EndElement;
  565. }
  566. void ReadEndArray ()
  567. {
  568. if (elements.Count == 0 || elements.Peek ().Type != "array")
  569. throw XmlError ("Unexpected end of array");
  570. current_node = XmlNodeType.EndElement;
  571. }
  572. void ReadEndOfStream ()
  573. {
  574. if (elements.Count > 0)
  575. throw XmlError (String.Format ("{0} missing end of arrays or objects", elements.Count));
  576. read_state = ReadState.EndOfFile;
  577. current_node = XmlNodeType.None;
  578. }
  579. void ReadAsSimpleContent (string type, string value)
  580. {
  581. elements.Push (new ElementInfo (next_element, type));
  582. simple_value = value;
  583. content_stored = true;
  584. }
  585. void ReadNumber (int ch)
  586. {
  587. elements.Push (new ElementInfo (next_element, "number"));
  588. content_stored = true;
  589. int init = ch;
  590. int prev;
  591. bool floating = false, exp = false;
  592. StringBuilder sb = new StringBuilder ();
  593. bool cont = true;
  594. do {
  595. sb.Append ((char) ch);
  596. prev = ch;
  597. ch = ReadChar ();
  598. if (prev == '-' && !IsNumber (ch)) // neither '.', '-' or '+' nor anything else is valid
  599. throw XmlError ("Invalid JSON number");
  600. switch (ch) {
  601. case 'e':
  602. case 'E':
  603. if (exp)
  604. throw XmlError ("Invalid JSON number token. Either 'E' or 'e' must not occur more than once");
  605. if (!IsNumber (prev))
  606. throw XmlError ("Invalid JSON number token. only a number is valid before 'E' or 'e'");
  607. exp = true;
  608. break;
  609. case '.':
  610. if (floating)
  611. throw XmlError ("Invalid JSON number token. '.' must not occur twice");
  612. if (exp)
  613. throw XmlError ("Invalid JSON number token. '.' must not occur after 'E' or 'e'");
  614. floating = true;
  615. break;
  616. case '+':
  617. case '-':
  618. if (prev == 'E' || prev == 'e')
  619. break;
  620. goto default;
  621. default:
  622. if (!IsNumber (ch)) {
  623. PushbackChar (ch);
  624. cont = false;
  625. }
  626. break;
  627. }
  628. } while (cont);
  629. if (!IsNumber (prev)) // only number is valid at the end
  630. throw XmlError ("Invalid JSON number");
  631. simple_value = sb.ToString ();
  632. if (init == '0' && !floating && !exp && simple_value != "0")
  633. throw XmlError ("Invalid JSON number");
  634. }
  635. bool IsNumber (int c)
  636. {
  637. return '0' <= c && c <= '9';
  638. }
  639. StringBuilder vb = new StringBuilder ();
  640. string ReadStringLiteral ()
  641. {
  642. return ReadStringLiteral (false);
  643. }
  644. string ReadStringLiteral (bool endWithColon)
  645. {
  646. vb.Length = 0;
  647. while (true) {
  648. int c = ReadChar ();
  649. if (c < 0)
  650. throw XmlError ("JSON string is not closed");
  651. if (c == '"' && !endWithColon)
  652. return vb.ToString ();
  653. else if (c == ':' && endWithColon)
  654. return vb.ToString ();
  655. else if (c != '\\') {
  656. vb.Append ((char) c);
  657. continue;
  658. }
  659. // escaped expression
  660. c = ReadChar ();
  661. if (c < 0)
  662. throw XmlError ("Invalid JSON string literal; incomplete escape sequence");
  663. switch (c) {
  664. case '"':
  665. case '\\':
  666. case '/':
  667. vb.Append ((char) c);
  668. break;
  669. case 'b':
  670. vb.Append ('\x8');
  671. break;
  672. case 'f':
  673. vb.Append ('\f');
  674. break;
  675. case 'n':
  676. vb.Append ('\n');
  677. break;
  678. case 'r':
  679. vb.Append ('\r');
  680. break;
  681. case 't':
  682. vb.Append ('\t');
  683. break;
  684. case 'u':
  685. ushort cp = 0;
  686. for (int i = 0; i < 4; i++) {
  687. if ((c = ReadChar ()) < 0)
  688. throw XmlError ("Incomplete unicode character escape literal");
  689. cp *= 16;
  690. if ('0' <= c && c <= '9')
  691. cp += (ushort) (c - '0');
  692. if ('A' <= c && c <= 'F')
  693. cp += (ushort) (c - 'A' + 10);
  694. if ('a' <= c && c <= 'f')
  695. cp += (ushort) (c - 'a' + 10);
  696. }
  697. vb.Append ((char) cp);
  698. break;
  699. default:
  700. throw XmlError ("Invalid JSON string literal; unexpected escape character");
  701. }
  702. }
  703. }
  704. int PeekChar ()
  705. {
  706. return reader.Peek ();
  707. }
  708. int ReadChar ()
  709. {
  710. int v = reader.Read ();
  711. if (v == '\n') {
  712. line++;
  713. column = 0;
  714. }
  715. else
  716. column++;
  717. return v;
  718. }
  719. void PushbackChar (int ch)
  720. {
  721. // FIXME handle lines (and columns? ugh, how?)
  722. reader.Pushback (ch);
  723. }
  724. void SkipWhitespaces ()
  725. {
  726. do {
  727. switch (PeekChar ()) {
  728. case ' ':
  729. case '\t':
  730. case '\r':
  731. case '\n':
  732. ReadChar ();
  733. continue;
  734. default:
  735. return;
  736. }
  737. } while (true);
  738. }
  739. void Expect (char c)
  740. {
  741. int v = ReadChar ();
  742. if (v < 0)
  743. throw XmlError (String.Format ("Expected '{0}' but got EOF", c));
  744. if (v != c)
  745. throw XmlError (String.Format ("Expected '{0}' but got '{1}'", c, (char) v));
  746. }
  747. Exception XmlError (string s)
  748. {
  749. return new XmlException (String.Format ("{0} ({1},{2})", s, line, column));
  750. }
  751. }
  752. }