KHtml

xml_tokenizer.h
1 /*
2  * This file is part of the DOM implementation for KDE.
3  *
4  * Copyright (C) 2000 Peter Kelly ([email protected])
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public License
17  * along with this library; see the file COPYING.LIB. If not, write to
18  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19  * Boston, MA 02110-1301, USA.
20  *
21  */
22 
23 #ifndef _XML_Tokenizer_h_
24 #define _XML_Tokenizer_h_
25 
26 #include <qxml.h>
27 #include <QStack>
28 #include <QLinkedList>
29 #include <QMap>
30 #include <QObject>
31 #include "misc/loader_client.h"
32 #include "misc/stringit.h"
33 
34 class KHTMLView;
35 
36 namespace khtml
37 {
38 class CachedObject;
39 class CachedScript;
40 }
41 
42 namespace DOM
43 {
44 class DocumentImpl;
45 class NodeImpl;
46 class HTMLScriptElementImpl;
47 class DocumentImpl;
48 class HTMLScriptElementImpl;
49 }
50 
51 namespace khtml
52 {
53 
54 class XMLHandler : public QXmlDefaultHandler
55 {
56 public:
57  XMLHandler(DOM::DocumentImpl *_doc, KHTMLView *_view);
58  virtual ~XMLHandler();
59 
60  // return the error protocol if parsing failed
61  QString errorProtocol();
62 
63  // overloaded handler functions
64  bool startDocument() override;
65  bool startElement(const QString &namespaceURI, const QString &localName, const QString &qName, const QXmlAttributes &atts) override;
66  bool endElement(const QString &namespaceURI, const QString &localName, const QString &qName) override;
67  bool startCDATA() override;
68  bool endCDATA() override;
69  bool characters(const QString &ch) override;
70  bool comment(const QString &ch) override;
71  bool processingInstruction(const QString &target, const QString &data) override;
72  bool startDTD(const QString &name, const QString &publicId, const QString &systemId) override;
73  bool endDTD() override;
74 
75  // namespace handling, to workaround problem in QXML where some attributes
76  // do not get the namespace resolved properly
77  bool startPrefixMapping(const QString &prefix, const QString &uri) override;
78  bool endPrefixMapping(const QString &prefix) override;
79  void fixUpNSURI(QString &uri, const QString &qname);
80  QMap<QString, QStack<QString> > namespaceInfo;
81 
82  // from QXmlDeclHandler
83  bool attributeDecl(const QString &eName, const QString &aName, const QString &type, const QString &valueDefault, const QString &value) override;
84  bool externalEntityDecl(const QString &name, const QString &publicId, const QString &systemId) override;
85  bool internalEntityDecl(const QString &name, const QString &value) override;
86 
87  // from QXmlDTDHandler
88  bool notationDecl(const QString &name, const QString &publicId, const QString &systemId) override;
89  bool unparsedEntityDecl(const QString &name, const QString &publicId, const QString &systemId, const QString &notationName) override;
90 
91  bool enterText();
92  void exitText();
93 
94  QString errorString() const override;
95 
96  bool fatalError(const QXmlParseException &exception) override;
97 
98  int errorLine;
99  int errorCol;
100 
101 private:
102  void pushNode(DOM::NodeImpl *node);
103  DOM::NodeImpl *popNode();
104  DOM::NodeImpl *currentNode() const;
105 private:
106  QString errorProt;
107  DOM::DocumentImpl *m_doc;
108  KHTMLView *m_view;
109  QStack<DOM::NodeImpl *> m_nodes;
110  DOM::NodeImpl *m_rootNode;
111 
112  enum State {
113  StateInit,
114  StateDocument,
115  StateQuote,
116  StateLine,
117  StateHeading,
118  StateP
119  };
120  State state;
121 };
122 
123 class Tokenizer : public QObject
124 {
125  Q_OBJECT
126 public:
127  virtual void begin() = 0;
128  // script output must be prepended, while new data
129  // received during executing a script must be appended, hence the
130  // extra bool to be able to distinguish between both cases. document.write()
131  // always uses false, while khtmlpart uses true
132  virtual void write(const TokenizerString &str, bool appendData) = 0;
133  virtual void end() = 0;
134  virtual void finish() = 0;
135  virtual void setOnHold(bool /*_onHold*/) {}
136  virtual bool isWaitingForScripts() const = 0;
137  virtual bool isExecutingScript() const = 0;
138  virtual void setNormalYieldDelay() {}
139  virtual void abort() {}
140 
141  virtual void executeScriptsWaitingForStylesheets() = 0;
142 
143 Q_SIGNALS:
144  void finishedParsing();
145 
146 };
147 
148 class XMLIncrementalSource : public QXmlInputSource
149 {
150 public:
151  XMLIncrementalSource();
152  void fetchData() override;
153  QChar next() override;
154  void setData(const QString &str) override;
155  void setData(const QByteArray &data) override;
156  QString data() const override;
157 
158  void appendXML(const QString &str);
159  void setFinished(bool);
160 
161  inline void setPaused(bool paused = true)
162  {
163  m_paused = paused;
164  }
165 
166 private:
167  QString m_data;
168  int m_pos;
169  const QChar *m_unicode;
170  bool m_finished;
171  bool m_paused; // if waiting for scripts
172 };
173 
174 class XMLTokenizer : public Tokenizer, public khtml::CachedObjectClient
175 {
176 public:
177  XMLTokenizer(DOM::DocumentImpl *, KHTMLView * = nullptr);
178  virtual ~XMLTokenizer();
179  void begin() override;
180  void write(const TokenizerString &str, bool) override;
181  void end() override;
182  void finish() override;
183 
184  // from CachedObjectClient
185  void notifyFinished(khtml::CachedObject *finishedObj) override;
186 
187  void executeScriptsWaitingForStylesheets() override {}
188 
189  bool isWaitingForScripts() const override;
190  bool isExecutingScript() const override
191  {
192  return m_executingScript;
193  }
194 
195  // execute script in place, if it contains src attribute we stop parsing till it's downloaded
196  void executeScript(DOM::NodeImpl *n);
197 
198 protected:
199  DOM::DocumentImpl *m_doc;
200  KHTMLView *m_view;
201 
202  khtml::CachedScript *m_cachedScript;
203 
204  QString m_bufferedData;
205 
206  XMLHandler m_handler;
207  QXmlSimpleReader m_reader;
208  XMLIncrementalSource m_source;
209  bool m_noErrors;
210  bool m_executingScript;
211  bool m_explicitFinishParsingNeeded;
212  bool m_insideWrite;
213 };
214 
215 } // end namespace
216 
217 #endif
This file is part of the HTML rendering engine for KDE.
MESSAGECORE_EXPORT KMime::Content * next(KMime::Content *node, bool allowChildren=true)
Renders and displays HTML in a QScrollArea.
Definition: khtmlview.h:97
const QList< QKeySequence > & begin()
This library provides a full-featured HTML parser and widget.
const QList< QKeySequence > & end()
State
a cached script
Definition: loader.h:322
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Tue Oct 26 2021 22:48:10 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.