qxml.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. /****************************************************************************
  2. **
  3. ** Copyright (C) 2016 The Qt Company Ltd.
  4. ** Contact: https://www.qt.io/licensing/
  5. **
  6. ** This file is part of the QtXml module of the Qt Toolkit.
  7. **
  8. ** $QT_BEGIN_LICENSE:LGPL$
  9. ** Commercial License Usage
  10. ** Licensees holding valid commercial Qt licenses may use this file in
  11. ** accordance with the commercial license agreement provided with the
  12. ** Software or, alternatively, in accordance with the terms contained in
  13. ** a written agreement between you and The Qt Company. For licensing terms
  14. ** and conditions see https://www.qt.io/terms-conditions. For further
  15. ** information use the contact form at https://www.qt.io/contact-us.
  16. **
  17. ** GNU Lesser General Public License Usage
  18. ** Alternatively, this file may be used under the terms of the GNU Lesser
  19. ** General Public License version 3 as published by the Free Software
  20. ** Foundation and appearing in the file LICENSE.LGPL3 included in the
  21. ** packaging of this file. Please review the following information to
  22. ** ensure the GNU Lesser General Public License version 3 requirements
  23. ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
  24. **
  25. ** GNU General Public License Usage
  26. ** Alternatively, this file may be used under the terms of the GNU
  27. ** General Public License version 2.0 or (at your option) the GNU General
  28. ** Public license version 3 or any later version approved by the KDE Free
  29. ** Qt Foundation. The licenses are as published by the Free Software
  30. ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
  31. ** included in the packaging of this file. Please review the following
  32. ** information to ensure the GNU General Public License requirements will
  33. ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
  34. ** https://www.gnu.org/licenses/gpl-3.0.html.
  35. **
  36. ** $QT_END_LICENSE$
  37. **
  38. ****************************************************************************/
  39. #ifndef QXML_H
  40. #define QXML_H
  41. #include <QtXml/qtxmlglobal.h>
  42. #include <QtCore/qtextstream.h>
  43. #include <QtCore/qfile.h>
  44. #include <QtCore/qstring.h>
  45. #include <QtCore/qstringlist.h>
  46. #include <QtCore/qlist.h>
  47. #include <QtCore/qscopedpointer.h>
  48. QT_BEGIN_NAMESPACE
  49. class QXmlNamespaceSupport;
  50. class QXmlAttributes;
  51. class QXmlContentHandler;
  52. class QXmlDefaultHandler;
  53. class QXmlDTDHandler;
  54. class QXmlEntityResolver;
  55. class QXmlErrorHandler;
  56. class QXmlLexicalHandler;
  57. class QXmlDeclHandler;
  58. class QXmlInputSource;
  59. class QXmlLocator;
  60. class QXmlNamespaceSupport;
  61. class QXmlParseException;
  62. class QXmlReader;
  63. class QXmlSimpleReader;
  64. class QXmlSimpleReaderPrivate;
  65. class QXmlNamespaceSupportPrivate;
  66. class QXmlAttributesPrivate;
  67. class QXmlInputSourcePrivate;
  68. class QXmlParseExceptionPrivate;
  69. class QXmlLocatorPrivate;
  70. class QXmlDefaultHandlerPrivate;
  71. //
  72. // SAX Namespace Support
  73. //
  74. class Q_XML_EXPORT QXmlNamespaceSupport
  75. {
  76. public:
  77. QXmlNamespaceSupport();
  78. ~QXmlNamespaceSupport();
  79. void setPrefix(const QString&, const QString&);
  80. QString prefix(const QString&) const;
  81. QString uri(const QString&) const;
  82. void splitName(const QString&, QString&, QString&) const;
  83. void processName(const QString&, bool, QString&, QString&) const;
  84. QStringList prefixes() const;
  85. QStringList prefixes(const QString&) const;
  86. void pushContext();
  87. void popContext();
  88. void reset();
  89. private:
  90. QXmlNamespaceSupportPrivate *d;
  91. friend class QXmlSimpleReaderPrivate;
  92. Q_DISABLE_COPY(QXmlNamespaceSupport)
  93. };
  94. //
  95. // SAX Attributes
  96. //
  97. class Q_XML_EXPORT QXmlAttributes
  98. {
  99. public:
  100. QXmlAttributes();
  101. virtual ~QXmlAttributes();
  102. int index(const QString& qName) const;
  103. int index(QLatin1String qName) const;
  104. int index(const QString& uri, const QString& localPart) const;
  105. int length() const;
  106. int count() const;
  107. QString localName(int index) const;
  108. QString qName(int index) const;
  109. QString uri(int index) const;
  110. QString type(int index) const;
  111. QString type(const QString& qName) const;
  112. QString type(const QString& uri, const QString& localName) const;
  113. QString value(int index) const;
  114. QString value(const QString& qName) const;
  115. QString value(QLatin1String qName) const;
  116. QString value(const QString& uri, const QString& localName) const;
  117. void clear();
  118. void append(const QString &qName, const QString &uri, const QString &localPart, const QString &value);
  119. private:
  120. struct Attribute {
  121. QString qname, uri, localname, value;
  122. };
  123. typedef QList<Attribute> AttributeList;
  124. AttributeList attList;
  125. QXmlAttributesPrivate *d;
  126. };
  127. //
  128. // SAX Input Source
  129. //
  130. class Q_XML_EXPORT QXmlInputSource
  131. {
  132. public:
  133. QXmlInputSource();
  134. explicit QXmlInputSource(QIODevice *dev);
  135. virtual ~QXmlInputSource();
  136. virtual void setData(const QString& dat);
  137. virtual void setData(const QByteArray& dat);
  138. virtual void fetchData();
  139. virtual QString data() const;
  140. virtual QChar next();
  141. virtual void reset();
  142. static const ushort EndOfData;
  143. static const ushort EndOfDocument;
  144. protected:
  145. virtual QString fromRawData(const QByteArray &data, bool beginning = false);
  146. private:
  147. void init();
  148. QXmlInputSourcePrivate *d;
  149. };
  150. //
  151. // SAX Exception Classes
  152. //
  153. class Q_XML_EXPORT QXmlParseException
  154. {
  155. public:
  156. explicit QXmlParseException(const QString &name = QString(), int c = -1, int l = -1,
  157. const QString &p = QString(), const QString &s = QString());
  158. QXmlParseException(const QXmlParseException &other);
  159. ~QXmlParseException();
  160. int columnNumber() const;
  161. int lineNumber() const;
  162. QString publicId() const;
  163. QString systemId() const;
  164. QString message() const;
  165. private:
  166. QScopedPointer<QXmlParseExceptionPrivate> d;
  167. };
  168. //
  169. // XML Reader
  170. //
  171. class Q_XML_EXPORT QXmlReader
  172. {
  173. public:
  174. virtual ~QXmlReader() {}
  175. virtual bool feature(const QString& name, bool *ok = Q_NULLPTR) const = 0;
  176. virtual void setFeature(const QString& name, bool value) = 0;
  177. virtual bool hasFeature(const QString& name) const = 0;
  178. virtual void* property(const QString& name, bool *ok = Q_NULLPTR) const = 0;
  179. virtual void setProperty(const QString& name, void* value) = 0;
  180. virtual bool hasProperty(const QString& name) const = 0;
  181. virtual void setEntityResolver(QXmlEntityResolver* handler) = 0;
  182. virtual QXmlEntityResolver* entityResolver() const = 0;
  183. virtual void setDTDHandler(QXmlDTDHandler* handler) = 0;
  184. virtual QXmlDTDHandler* DTDHandler() const = 0;
  185. virtual void setContentHandler(QXmlContentHandler* handler) = 0;
  186. virtual QXmlContentHandler* contentHandler() const = 0;
  187. virtual void setErrorHandler(QXmlErrorHandler* handler) = 0;
  188. virtual QXmlErrorHandler* errorHandler() const = 0;
  189. virtual void setLexicalHandler(QXmlLexicalHandler* handler) = 0;
  190. virtual QXmlLexicalHandler* lexicalHandler() const = 0;
  191. virtual void setDeclHandler(QXmlDeclHandler* handler) = 0;
  192. virtual QXmlDeclHandler* declHandler() const = 0;
  193. virtual bool parse(const QXmlInputSource& input) = 0;
  194. virtual bool parse(const QXmlInputSource* input) = 0;
  195. };
  196. class Q_XML_EXPORT QXmlSimpleReader : public QXmlReader
  197. {
  198. public:
  199. QXmlSimpleReader();
  200. virtual ~QXmlSimpleReader();
  201. bool feature(const QString& name, bool *ok = Q_NULLPTR) const Q_DECL_OVERRIDE;
  202. void setFeature(const QString& name, bool value) Q_DECL_OVERRIDE;
  203. bool hasFeature(const QString& name) const Q_DECL_OVERRIDE;
  204. void* property(const QString& name, bool *ok = Q_NULLPTR) const Q_DECL_OVERRIDE;
  205. void setProperty(const QString& name, void* value) Q_DECL_OVERRIDE;
  206. bool hasProperty(const QString& name) const Q_DECL_OVERRIDE;
  207. void setEntityResolver(QXmlEntityResolver* handler) Q_DECL_OVERRIDE;
  208. QXmlEntityResolver* entityResolver() const Q_DECL_OVERRIDE;
  209. void setDTDHandler(QXmlDTDHandler* handler) Q_DECL_OVERRIDE;
  210. QXmlDTDHandler* DTDHandler() const Q_DECL_OVERRIDE;
  211. void setContentHandler(QXmlContentHandler* handler) Q_DECL_OVERRIDE;
  212. QXmlContentHandler* contentHandler() const Q_DECL_OVERRIDE;
  213. void setErrorHandler(QXmlErrorHandler* handler) Q_DECL_OVERRIDE;
  214. QXmlErrorHandler* errorHandler() const Q_DECL_OVERRIDE;
  215. void setLexicalHandler(QXmlLexicalHandler* handler) Q_DECL_OVERRIDE;
  216. QXmlLexicalHandler* lexicalHandler() const Q_DECL_OVERRIDE;
  217. void setDeclHandler(QXmlDeclHandler* handler) Q_DECL_OVERRIDE;
  218. QXmlDeclHandler* declHandler() const Q_DECL_OVERRIDE;
  219. bool parse(const QXmlInputSource& input) Q_DECL_OVERRIDE;
  220. bool parse(const QXmlInputSource* input) Q_DECL_OVERRIDE;
  221. virtual bool parse(const QXmlInputSource* input, bool incremental);
  222. virtual bool parseContinue();
  223. private:
  224. Q_DISABLE_COPY(QXmlSimpleReader)
  225. Q_DECLARE_PRIVATE(QXmlSimpleReader)
  226. QScopedPointer<QXmlSimpleReaderPrivate> d_ptr;
  227. friend class QXmlSimpleReaderLocator;
  228. friend class QDomHandler;
  229. };
  230. //
  231. // SAX Locator
  232. //
  233. class Q_XML_EXPORT QXmlLocator
  234. {
  235. public:
  236. QXmlLocator();
  237. virtual ~QXmlLocator();
  238. virtual int columnNumber() const = 0;
  239. virtual int lineNumber() const = 0;
  240. // QString getPublicId() const
  241. // QString getSystemId() const
  242. };
  243. //
  244. // SAX handler classes
  245. //
  246. class Q_XML_EXPORT QXmlContentHandler
  247. {
  248. public:
  249. virtual ~QXmlContentHandler() {}
  250. virtual void setDocumentLocator(QXmlLocator* locator) = 0;
  251. virtual bool startDocument() = 0;
  252. virtual bool endDocument() = 0;
  253. virtual bool startPrefixMapping(const QString& prefix, const QString& uri) = 0;
  254. virtual bool endPrefixMapping(const QString& prefix) = 0;
  255. virtual bool startElement(const QString& namespaceURI, const QString& localName, const QString& qName, const QXmlAttributes& atts) = 0;
  256. virtual bool endElement(const QString& namespaceURI, const QString& localName, const QString& qName) = 0;
  257. virtual bool characters(const QString& ch) = 0;
  258. virtual bool ignorableWhitespace(const QString& ch) = 0;
  259. virtual bool processingInstruction(const QString& target, const QString& data) = 0;
  260. virtual bool skippedEntity(const QString& name) = 0;
  261. virtual QString errorString() const = 0;
  262. };
  263. class Q_XML_EXPORT QXmlErrorHandler
  264. {
  265. public:
  266. virtual ~QXmlErrorHandler() {}
  267. virtual bool warning(const QXmlParseException& exception) = 0;
  268. virtual bool error(const QXmlParseException& exception) = 0;
  269. virtual bool fatalError(const QXmlParseException& exception) = 0;
  270. virtual QString errorString() const = 0;
  271. };
  272. class Q_XML_EXPORT QXmlDTDHandler
  273. {
  274. public:
  275. virtual ~QXmlDTDHandler() {}
  276. virtual bool notationDecl(const QString& name, const QString& publicId, const QString& systemId) = 0;
  277. virtual bool unparsedEntityDecl(const QString& name, const QString& publicId, const QString& systemId, const QString& notationName) = 0;
  278. virtual QString errorString() const = 0;
  279. };
  280. class Q_XML_EXPORT QXmlEntityResolver
  281. {
  282. public:
  283. virtual ~QXmlEntityResolver() {}
  284. virtual bool resolveEntity(const QString& publicId, const QString& systemId, QXmlInputSource*& ret) = 0;
  285. virtual QString errorString() const = 0;
  286. };
  287. class Q_XML_EXPORT QXmlLexicalHandler
  288. {
  289. public:
  290. virtual ~QXmlLexicalHandler() {}
  291. virtual bool startDTD(const QString& name, const QString& publicId, const QString& systemId) = 0;
  292. virtual bool endDTD() = 0;
  293. virtual bool startEntity(const QString& name) = 0;
  294. virtual bool endEntity(const QString& name) = 0;
  295. virtual bool startCDATA() = 0;
  296. virtual bool endCDATA() = 0;
  297. virtual bool comment(const QString& ch) = 0;
  298. virtual QString errorString() const = 0;
  299. };
  300. class Q_XML_EXPORT QXmlDeclHandler
  301. {
  302. public:
  303. virtual ~QXmlDeclHandler() {}
  304. virtual bool attributeDecl(const QString& eName, const QString& aName, const QString& type, const QString& valueDefault, const QString& value) = 0;
  305. virtual bool internalEntityDecl(const QString& name, const QString& value) = 0;
  306. virtual bool externalEntityDecl(const QString& name, const QString& publicId, const QString& systemId) = 0;
  307. virtual QString errorString() const = 0;
  308. // ### Conform to SAX by adding elementDecl
  309. };
  310. class Q_XML_EXPORT QXmlDefaultHandler : public QXmlContentHandler, public QXmlErrorHandler, public QXmlDTDHandler, public QXmlEntityResolver, public QXmlLexicalHandler, public QXmlDeclHandler
  311. {
  312. public:
  313. QXmlDefaultHandler();
  314. virtual ~QXmlDefaultHandler();
  315. void setDocumentLocator(QXmlLocator* locator) Q_DECL_OVERRIDE;
  316. bool startDocument() Q_DECL_OVERRIDE;
  317. bool endDocument() Q_DECL_OVERRIDE;
  318. bool startPrefixMapping(const QString& prefix, const QString& uri) Q_DECL_OVERRIDE;
  319. bool endPrefixMapping(const QString& prefix) Q_DECL_OVERRIDE;
  320. bool startElement(const QString& namespaceURI, const QString& localName, const QString& qName, const QXmlAttributes& atts) Q_DECL_OVERRIDE;
  321. bool endElement(const QString& namespaceURI, const QString& localName, const QString& qName) Q_DECL_OVERRIDE;
  322. bool characters(const QString& ch) Q_DECL_OVERRIDE;
  323. bool ignorableWhitespace(const QString& ch) Q_DECL_OVERRIDE;
  324. bool processingInstruction(const QString& target, const QString& data) Q_DECL_OVERRIDE;
  325. bool skippedEntity(const QString& name) Q_DECL_OVERRIDE;
  326. bool warning(const QXmlParseException& exception) Q_DECL_OVERRIDE;
  327. bool error(const QXmlParseException& exception) Q_DECL_OVERRIDE;
  328. bool fatalError(const QXmlParseException& exception) Q_DECL_OVERRIDE;
  329. bool notationDecl(const QString& name, const QString& publicId, const QString& systemId) Q_DECL_OVERRIDE;
  330. bool unparsedEntityDecl(const QString& name, const QString& publicId, const QString& systemId, const QString& notationName) Q_DECL_OVERRIDE;
  331. bool resolveEntity(const QString& publicId, const QString& systemId, QXmlInputSource*& ret) Q_DECL_OVERRIDE;
  332. bool startDTD(const QString& name, const QString& publicId, const QString& systemId) Q_DECL_OVERRIDE;
  333. bool endDTD() Q_DECL_OVERRIDE;
  334. bool startEntity(const QString& name) Q_DECL_OVERRIDE;
  335. bool endEntity(const QString& name) Q_DECL_OVERRIDE;
  336. bool startCDATA() Q_DECL_OVERRIDE;
  337. bool endCDATA() Q_DECL_OVERRIDE;
  338. bool comment(const QString& ch) Q_DECL_OVERRIDE;
  339. bool attributeDecl(const QString& eName, const QString& aName, const QString& type, const QString& valueDefault, const QString& value) Q_DECL_OVERRIDE;
  340. bool internalEntityDecl(const QString& name, const QString& value) Q_DECL_OVERRIDE;
  341. bool externalEntityDecl(const QString& name, const QString& publicId, const QString& systemId) Q_DECL_OVERRIDE;
  342. QString errorString() const Q_DECL_OVERRIDE;
  343. private:
  344. QXmlDefaultHandlerPrivate *d;
  345. Q_DISABLE_COPY(QXmlDefaultHandler)
  346. };
  347. // inlines
  348. inline int QXmlAttributes::count() const
  349. { return length(); }
  350. QT_END_NAMESPACE
  351. #endif // QXML_H