QHtmlParser  0.0.1
A Qt/C++ library for parsing and traversing/searching HTML documents.
qhtmlparser.h
Go to the documentation of this file.
1 
19 #ifndef QHTMLPARSER_H
20 #define QHTMLPARSER_H
21 
22 #include <QList>
23 #include <QString>
24 
25 #if defined(QHTMLPARSER_LIBRARY)
26 #define QHTMLPARSER_EXPORT Q_DECL_EXPORT
27 #elif defined(QHTMLPARSER_STATIC_LIBRARY)
28 #define QHTMLPARSER_EXPORT
29 #else
30 #define QHTMLPARSER_EXPORT Q_DECL_IMPORT
31 #endif
32 
110 namespace QHtmlParser
111 {
115  enum MatchFlag {
119  MatchExactly = 0x0000,
120 
124  MatchContains = 0x0001,
125 
129  MatchStartsWith = 0x0002,
130 
134  MatchEndsWith = 0x0004,
135 
139  MatchRegExp = 0x0008,
140 
144  MatchWildcard = 0x0010,
145 
150  };
151 
155  typedef QFlags<MatchFlag> MatchFlags;
156 
160  enum MatchType {
164  MatchAll = 0,
165 
170  };
171 }
172 
173 Q_DECLARE_OPERATORS_FOR_FLAGS(QHtmlParser::MatchFlags)
174 
175 
178 class QHTMLPARSER_EXPORT QHtmlAttribute
179 {
180 
181 public:
185  QHtmlAttribute();
186 
190  explicit QHtmlAttribute(const QString &name, const QString &value);
191 
195  const QString& name() const;
196 
200  void setName(const QString &name);
201 
205  const QString& value() const;
206 
210  void setValue(const QString &value);
211 
215  bool operator==(const QHtmlAttribute &other) const;
216 
220  bool operator!=(const QHtmlAttribute &other) const;
221 
222 private:
223  QString m_name;
224  QString m_value;
225 };
226 
257 class QHTMLPARSER_EXPORT QHtmlAttributeMatch : public QHtmlAttribute
258 {
259 
260 public:
265 
269  explicit QHtmlAttributeMatch(const QString &name, const QString &value,
271 
277  QHtmlParser::MatchFlags flags() const;
278 
284  void setFlags(QHtmlParser::MatchFlags flags);
285 
291  void setFlag(QHtmlParser::MatchFlag flag, bool on = true);
292 
298  bool testFlag(QHtmlParser::MatchFlag flag) const;
299 
303  bool operator==(const QHtmlAttributeMatch &other) const;
304 
308  bool operator!=(const QHtmlAttributeMatch &other) const;
309 
310 private:
311  QHtmlParser::MatchFlags m_flags;
312 };
313 
314 class QHtmlElement;
315 class QHtmlElementPrivate;
316 
320 typedef QList<QHtmlAttribute> QHtmlAttributes;
321 
325 typedef QList<QHtmlAttributeMatch> QHtmlAttributeMatches;
326 
330 typedef QList<QHtmlElement> QHtmlElementList;
331 
335 class QHTMLPARSER_EXPORT QHtmlElement
336 {
337 
338 public:
342  QHtmlElement();
343 
347  QHtmlElement(const QHtmlElement &other);
348 
352  ~QHtmlElement();
353 
357  QHtmlAttributes attributes() const;
358 
364  QString attribute(const QString &name) const;
365 
369  QHtmlElement parentElement() const;
370 
376  QHtmlElement nextSibling() const;
377 
383  QHtmlElement previousSibling() const;
384 
388  QHtmlElementList childElements() const;
389 
395  QHtmlElement firstChildElement() const;
396 
402  QHtmlElement lastChildElement() const;
403 
409  QHtmlElement nthChildElement(int n) const;
410 
416  QHtmlElement elementById(const QString &id) const;
417 
421  QHtmlElementList elementsByTagName(const QString &name) const;
422 
428  QHtmlElementList elementsByTagName(const QString &name, const QHtmlAttributeMatch &match) const;
429 
435  QHtmlElementList elementsByTagName(const QString &name, const QHtmlAttributeMatches &matches,
437 
443  QHtmlElement firstElementByTagName(const QString &name) const;
444 
450  QHtmlElement firstElementByTagName(const QString &name, const QHtmlAttributeMatch &match) const;
451 
457  QHtmlElement firstElementByTagName(const QString &name, const QHtmlAttributeMatches &matches,
459 
465  QHtmlElement lastElementByTagName(const QString &name) const;
466 
472  QHtmlElement lastElementByTagName(const QString &name, const QHtmlAttributeMatch &match) const;
473 
479  QHtmlElement lastElementByTagName(const QString &name, const QHtmlAttributeMatches &matches,
481 
487  QHtmlElement nthElementByTagName(int n, const QString &name) const;
488 
494  QHtmlElement nthElementByTagName(int n, const QString &name, const QHtmlAttributeMatch &match) const;
495 
501  QHtmlElement nthElementByTagName(int n, const QString &name, const QHtmlAttributeMatches &matches,
502  QHtmlParser::MatchType matchType = QHtmlParser::MatchAll) const;
503 
509  QString tagName() const;
510 
516  QString text(bool includeChildElements = false) const;
517 
523  QString toString() const;
524 
530  bool isNull() const;
531 
532  QHtmlElement& operator=(const QHtmlElement &other);
533 
537  bool operator==(const QHtmlElement &other) const;
538 
542  bool operator!=(const QHtmlElement &other) const;
543 
544 private:
545  QHtmlElementPrivate *d;
546 
547  friend class QHtmlDocument;
548 };
549 
550 class QHtmlDocumentPrivate;
551 class QIODevice;
552 
577 class QHTMLPARSER_EXPORT QHtmlDocument
578 {
579 
580 public:
584  QHtmlDocument();
585 
591  explicit QHtmlDocument(const QString &content);
592 
598  explicit QHtmlDocument(const QByteArray &content);
599 
607  explicit QHtmlDocument(QIODevice *device);
608 
615  ~QHtmlDocument();
616 
625  bool setContent(const QString &content);
626 
630  bool setContent(const QByteArray &content);
631 
639  bool setContent(QIODevice *device);
640 
646  QHtmlElement documentElement() const;
647 
653  QHtmlElement htmlElement() const;
654 
660  QHtmlElement headElement() const;
661 
667  QHtmlElement bodyElement() const;
668 
674  QString toString() const;
675 
679  bool hasError() const;
680 
686  QString errorString() const;
687 
693  bool isNull() const;
694 
695 private:
696  QHtmlDocumentPrivate *d;
697  Q_DISABLE_COPY(QHtmlDocument)
698 };
699 
700 #endif // QHTMLPARSER_H
The attribute value ends with the specified string.
Definition: qhtmlparser.h:134
Only one match in the list must be successful.
Definition: qhtmlparser.h:169
QList< QHtmlAttributeMatch > QHtmlAttributeMatches
Typedef for QList<QHtmlAttributeMatch>.
Definition: qhtmlparser.h:325
QList< QHtmlAttribute > QHtmlAttributes
Typedef for QList<QHtmlAttribute>.
Definition: qhtmlparser.h:315
The attribute value is equal to the specified string.
Definition: qhtmlparser.h:119
All matches in the list must be successful.
Definition: qhtmlparser.h:164
Represents a HTML document.
Definition: qhtmlparser.h:577
Represents a HTML attribute with a name and value.
Definition: qhtmlparser.h:178
Represents a HTML element/tag.
Definition: qhtmlparser.h:335
MatchType
Specifies the criteria applied when matching a list of attributes.
Definition: qhtmlparser.h:160
The attribute value matches the specified regular expression pattern.
Definition: qhtmlparser.h:139
The attribute matches the specified wildcard.
Definition: qhtmlparser.h:144
QFlags< MatchFlag > MatchFlags
Typedef for QFlags<MatchFlag>.
Definition: qhtmlparser.h:155
The attribute value contains the specified string.
Definition: qhtmlparser.h:124
The attribute value starts with the specified string.
Definition: qhtmlparser.h:129
The QHtmlParser namespace.
Definition: qhtmlparser.h:110
Defines the criteria used for performing a match against the attributes of an element.
Definition: qhtmlparser.h:257
QList< QHtmlElement > QHtmlElementList
Typedef for QList<QHtmlElement>.
Definition: qhtmlparser.h:330
MatchFlag
Specifies the criteria applied when matching attribute values.
Definition: qhtmlparser.h:115
The match is case sensitive.
Definition: qhtmlparser.h:149