libxml2reader.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. #include "libxml2reader.h"
  2. #include <cstring>
  3. #include <libxml/tree.h>
  4. #include <libxml/parser.h>
  5. #include <libxml/HTMLparser.h>
  6. class LibXml2ReaderLocator : public QXmlLocator {
  7. public:
  8. LibXml2ReaderLocator(LibXml2Reader* r) : reader(r) {}
  9. virtual int columnNumber(void) const;
  10. virtual int lineNumber(void) const;
  11. private:
  12. LibXml2Reader* reader;
  13. };
  14. class LibXml2ReaderPrivate {
  15. public:
  16. ~LibXml2ReaderPrivate(void) {}
  17. private:
  18. LibXml2ReaderPrivate(LibXml2Reader* reader);
  19. static void startDocument(void* c);
  20. static void endDocument(void* c);
  21. static void startElement(void* c, const xmlChar* name, const xmlChar** attrs);
  22. static void endElement(void* c, const xmlChar* name);
  23. static void comment(void* c, const xmlChar* value);
  24. static void cdataBlock(void* c, const xmlChar* value, int len);
  25. static void processingInstruction(void* c, const xmlChar* target, const xmlChar* data);
  26. static void characters(void* c, const xmlChar* ch, int len);
  27. static void ignorableWhitespace(void* c, const xmlChar* ch, int len);
  28. static void internalSubset(void* c, const xmlChar* name, const xmlChar* publicId, const xmlChar* systemId);
  29. void parse(const QXmlInputSource* input);
  30. QScopedPointer<LibXml2ReaderLocator> locator;
  31. Q_DECLARE_PUBLIC(LibXml2Reader)
  32. LibXml2Reader* q_ptr;
  33. QXmlEntityResolver* entityresolver;
  34. QXmlDTDHandler* dtdhandler;
  35. QXmlContentHandler* contenthandler;
  36. QXmlErrorHandler* errorhandler;
  37. QXmlLexicalHandler* lexicalhandler;
  38. QXmlDeclHandler* declhandler;
  39. xmlParserCtxt* context;
  40. friend class LibXml2ReaderLocator;
  41. };
  42. LibXml2ReaderPrivate::LibXml2ReaderPrivate(LibXml2Reader* reader)
  43. : q_ptr(reader), entityresolver(0), dtdhandler(0), contenthandler(0), errorhandler(0), lexicalhandler(0), declhandler(0), context(0)
  44. {
  45. this->locator.reset(new LibXml2ReaderLocator(reader));
  46. }
  47. void LibXml2ReaderPrivate::parse(const QXmlInputSource* input)
  48. {
  49. htmlSAXHandler handler;
  50. QByteArray arr = input->data().toLocal8Bit();
  51. const char* data = arr.data();
  52. std::memset(&handler, 0, sizeof(handler));
  53. handler.startDocument = &LibXml2ReaderPrivate::startDocument;
  54. handler.endDocument = &LibXml2ReaderPrivate::endDocument;
  55. handler.startElement = &LibXml2ReaderPrivate::startElement;
  56. handler.endElement = &LibXml2ReaderPrivate::endElement;
  57. handler.comment = &LibXml2ReaderPrivate::comment;
  58. handler.cdataBlock = &LibXml2ReaderPrivate::cdataBlock;
  59. handler.processingInstruction = &LibXml2ReaderPrivate::processingInstruction;
  60. handler.characters = &LibXml2ReaderPrivate::characters;
  61. handler.ignorableWhitespace = &LibXml2ReaderPrivate::ignorableWhitespace;
  62. handler.internalSubset = &LibXml2ReaderPrivate::internalSubset;
  63. this->context = htmlCreatePushParserCtxt(&handler, this, data, xmlStrlen(reinterpret_cast<const xmlChar*>(data)), "", XML_CHAR_ENCODING_NONE);
  64. htmlParseChunk(this->context, NULL, 0, 1);
  65. htmlFreeParserCtxt(this->context);
  66. xmlCleanupParser();
  67. }
  68. void LibXml2ReaderPrivate::startDocument(void* c)
  69. {
  70. LibXml2ReaderPrivate* r = reinterpret_cast<LibXml2ReaderPrivate*>(c);
  71. if (r->contenthandler) {
  72. r->contenthandler->startDocument();
  73. }
  74. }
  75. void LibXml2ReaderPrivate::endDocument(void* c)
  76. {
  77. LibXml2ReaderPrivate* r = reinterpret_cast<LibXml2ReaderPrivate*>(c);
  78. if (r->contenthandler) {
  79. r->contenthandler->endDocument();
  80. }
  81. }
  82. void LibXml2ReaderPrivate::startElement(void* c, const xmlChar* name, const xmlChar** attrs)
  83. {
  84. LibXml2ReaderPrivate* r = reinterpret_cast<LibXml2ReaderPrivate*>(c);
  85. if (r->contenthandler) {
  86. QXmlAttributes a;
  87. int i = 0;
  88. if (attrs) {
  89. while (attrs[i]) {
  90. const char* name = reinterpret_cast<const char*>(attrs[i]);
  91. const char* value = reinterpret_cast<const char*>(attrs[i+1]);
  92. i += 2;
  93. a.append(name, "", "", value ? value : name);
  94. }
  95. }
  96. QString uri = "";
  97. QString localName = "";
  98. QString qName = reinterpret_cast<const char*>(name);
  99. r->contenthandler->startElement(uri, localName, qName, a);
  100. }
  101. }
  102. void LibXml2ReaderPrivate::endElement(void* c, const xmlChar* name)
  103. {
  104. LibXml2ReaderPrivate* r = reinterpret_cast<LibXml2ReaderPrivate*>(c);
  105. if (r->contenthandler) {
  106. r->contenthandler->endElement(QString(""), QString(""), QString(reinterpret_cast<const char*>(name)));
  107. }
  108. }
  109. void LibXml2ReaderPrivate::comment(void* c, const xmlChar* value)
  110. {
  111. LibXml2ReaderPrivate* r = reinterpret_cast<LibXml2ReaderPrivate*>(c);
  112. if (r->lexicalhandler) {
  113. r->lexicalhandler->comment(QString::fromLocal8Bit(reinterpret_cast<const char*>(value)));
  114. }
  115. }
  116. void LibXml2ReaderPrivate::cdataBlock(void* c, const xmlChar* value, int len)
  117. {
  118. LibXml2ReaderPrivate* r = reinterpret_cast<LibXml2ReaderPrivate*>(c);
  119. if (r->lexicalhandler) {
  120. r->lexicalhandler->startCDATA();
  121. if (r->contenthandler) {
  122. QByteArray arr(reinterpret_cast<const char*>(value), len);
  123. r->contenthandler->characters(arr);
  124. }
  125. r->lexicalhandler->endCDATA();
  126. }
  127. }
  128. void LibXml2ReaderPrivate::processingInstruction(void* c, const xmlChar* target, const xmlChar* data)
  129. {
  130. LibXml2ReaderPrivate* r = reinterpret_cast<LibXml2ReaderPrivate*>(c);
  131. if (r->contenthandler) {
  132. r->contenthandler->processingInstruction(reinterpret_cast<const char*>(target), reinterpret_cast<const char*>(data));
  133. }
  134. }
  135. void LibXml2ReaderPrivate::characters(void* c, const xmlChar* ch, int len)
  136. {
  137. LibXml2ReaderPrivate* r = reinterpret_cast<LibXml2ReaderPrivate*>(c);
  138. if (r->contenthandler) {
  139. r->contenthandler->characters(QString::fromLocal8Bit(reinterpret_cast<const char*>(ch), len));
  140. }
  141. }
  142. void LibXml2ReaderPrivate::ignorableWhitespace(void* c, const xmlChar* ch, int len)
  143. {
  144. LibXml2ReaderPrivate* r = reinterpret_cast<LibXml2ReaderPrivate*>(c);
  145. if (r->contenthandler) {
  146. r->contenthandler->ignorableWhitespace(QString::fromLocal8Bit(reinterpret_cast<const char*>(ch), len));
  147. }
  148. }
  149. void LibXml2ReaderPrivate::internalSubset(void* c, const xmlChar* name, const xmlChar* publicId, const xmlChar* systemId)
  150. {
  151. LibXml2ReaderPrivate* r = reinterpret_cast<LibXml2ReaderPrivate*>(c);
  152. if (r->lexicalhandler) {
  153. QString n(QString::fromLocal8Bit(reinterpret_cast<const char*>(name)));
  154. QString p(QString::fromLocal8Bit(reinterpret_cast<const char*>(publicId)));
  155. QString s(QString::fromLocal8Bit(reinterpret_cast<const char*>(systemId)));
  156. r->lexicalhandler->startDTD(n, p, s);
  157. r->lexicalhandler->endDTD();
  158. }
  159. }
  160. LibXml2Reader::LibXml2Reader(void)
  161. : d_ptr(new LibXml2ReaderPrivate(this))
  162. {
  163. }
  164. LibXml2Reader::~LibXml2Reader(void)
  165. {
  166. }
  167. bool LibXml2Reader::feature(const QString&, bool* ok) const
  168. {
  169. if (ok) {
  170. *ok = false;
  171. }
  172. return false;
  173. }
  174. void LibXml2Reader::setFeature(const QString&, bool)
  175. {
  176. }
  177. bool LibXml2Reader::hasFeature(const QString&) const
  178. {
  179. return false;
  180. }
  181. void* LibXml2Reader::property(const QString&, bool* ok) const
  182. {
  183. if (ok) {
  184. *ok = false;
  185. }
  186. return 0;
  187. }
  188. void LibXml2Reader::setProperty(const QString&, void*)
  189. {
  190. }
  191. bool LibXml2Reader::hasProperty(const QString&) const
  192. {
  193. return false;
  194. }
  195. void LibXml2Reader::setEntityResolver(QXmlEntityResolver* handler)
  196. {
  197. Q_D(LibXml2Reader);
  198. d->entityresolver = handler;
  199. }
  200. QXmlEntityResolver* LibXml2Reader::entityResolver(void) const
  201. {
  202. const LibXml2ReaderPrivate* d = this->d_func();
  203. return d->entityresolver;
  204. }
  205. void LibXml2Reader::setDTDHandler(QXmlDTDHandler* handler)
  206. {
  207. Q_D(LibXml2Reader);
  208. d->dtdhandler = handler;
  209. }
  210. QXmlDTDHandler* LibXml2Reader::DTDHandler(void) const
  211. {
  212. const LibXml2ReaderPrivate* d = this->d_func();
  213. return d->dtdhandler;
  214. }
  215. void LibXml2Reader::setContentHandler(QXmlContentHandler* handler)
  216. {
  217. Q_D(LibXml2Reader);
  218. d->contenthandler = handler;
  219. }
  220. QXmlContentHandler* LibXml2Reader::contentHandler(void) const
  221. {
  222. const LibXml2ReaderPrivate* d = this->d_func();
  223. return d->contenthandler;
  224. }
  225. void LibXml2Reader::setErrorHandler(QXmlErrorHandler* handler)
  226. {
  227. Q_D(LibXml2Reader);
  228. d->errorhandler = handler;
  229. }
  230. QXmlErrorHandler* LibXml2Reader::errorHandler(void) const
  231. {
  232. const LibXml2ReaderPrivate* d = this->d_func();
  233. return d->errorhandler;
  234. }
  235. void LibXml2Reader::setLexicalHandler(QXmlLexicalHandler* handler)
  236. {
  237. Q_D(LibXml2Reader);
  238. d->lexicalhandler = handler;
  239. }
  240. QXmlLexicalHandler* LibXml2Reader::lexicalHandler(void) const
  241. {
  242. const LibXml2ReaderPrivate* d = this->d_func();
  243. return d->lexicalhandler;
  244. }
  245. void LibXml2Reader::setDeclHandler(QXmlDeclHandler* handler)
  246. {
  247. Q_D(LibXml2Reader);
  248. d->declhandler = handler;
  249. }
  250. QXmlDeclHandler* LibXml2Reader::declHandler(void) const
  251. {
  252. const LibXml2ReaderPrivate* d = this->d_func();
  253. return d->declhandler;
  254. }
  255. bool LibXml2Reader::parse(const QXmlInputSource& input)
  256. {
  257. return this->parse(&input);
  258. }
  259. bool LibXml2Reader::parse(const QXmlInputSource* input)
  260. {
  261. Q_D(LibXml2Reader);
  262. if (d->contenthandler) {
  263. d->contenthandler->setDocumentLocator(d->locator.data());
  264. }
  265. d->parse(input);
  266. return true;
  267. }
  268. int LibXml2ReaderLocator::columnNumber(void) const
  269. {
  270. return this->reader->d_func()->context->input->col;
  271. }
  272. int LibXml2ReaderLocator::lineNumber(void) const
  273. {
  274. return this->reader->d_func()->context->input->line;
  275. }