node-expat.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. 'use strict'
  2. var util = require('util')
  3. var expat = require('bindings')('node_expat')
  4. var Stream = require('stream').Stream
  5. var Parser = function (encoding) {
  6. this.encoding = encoding
  7. this._getNewParser()
  8. this.parser.emit = this.emit.bind(this)
  9. // Stream API
  10. this.writable = true
  11. this.readable = true
  12. }
  13. util.inherits(Parser, Stream)
  14. Parser.prototype._getNewParser = function () {
  15. this.parser = new expat.Parser(this.encoding)
  16. }
  17. Parser.prototype.parse = function (buf, isFinal) {
  18. return this.parser.parse(buf, isFinal)
  19. }
  20. Parser.prototype.setEncoding = function (encoding) {
  21. this.encoding = encoding
  22. return this.parser.setEncoding(this.encoding)
  23. }
  24. Parser.prototype.setUnknownEncoding = function (map, convert) {
  25. return this.parser.setUnknownEncoding(map, convert)
  26. }
  27. Parser.prototype.getError = function () {
  28. return this.parser.getError()
  29. }
  30. Parser.prototype.stop = function () {
  31. return this.parser.stop()
  32. }
  33. Parser.prototype.pause = function () {
  34. return this.stop()
  35. }
  36. Parser.prototype.resume = function () {
  37. return this.parser.resume()
  38. }
  39. Parser.prototype.destroy = function () {
  40. this.parser.stop()
  41. this.end()
  42. }
  43. Parser.prototype.destroySoon = function () {
  44. this.destroy()
  45. }
  46. Parser.prototype.write = function (data) {
  47. var error, result
  48. try {
  49. result = this.parse(data)
  50. if (!result) {
  51. error = this.getError()
  52. }
  53. } catch (e) {
  54. error = e
  55. }
  56. if (error) {
  57. this.emit('error', error)
  58. this.emit('close')
  59. }
  60. return result
  61. }
  62. Parser.prototype.end = function (data) {
  63. var error, result
  64. try {
  65. result = this.parse(data || '', true)
  66. if (!result) {
  67. error = this.getError()
  68. }
  69. } catch (e) {
  70. error = e
  71. }
  72. if (!error) {
  73. this.emit('end')
  74. } else {
  75. this.emit('error', error)
  76. }
  77. this.emit('close')
  78. }
  79. Parser.prototype.reset = function () {
  80. return this.parser.reset()
  81. }
  82. Parser.prototype.getCurrentLineNumber = function () {
  83. return this.parser.getCurrentLineNumber()
  84. }
  85. Parser.prototype.getCurrentColumnNumber = function () {
  86. return this.parser.getCurrentColumnNumber()
  87. }
  88. Parser.prototype.getCurrentByteIndex = function () {
  89. return this.parser.getCurrentByteIndex()
  90. }
  91. exports.Parser = Parser
  92. exports.createParser = function (cb) {
  93. var parser = new Parser()
  94. if (cb) {
  95. parser.on('startElement', cb)
  96. }
  97. return parser
  98. }