fix logic
[personal-kdelibs.git] / khtml / xml / xml_tokenizer.h
blobcf7534d7e0e2ac4d3c54f80a6d842ef55e0b23ac
1 /*
2 * This file is part of the DOM implementation for KDE.
4 * Copyright (C) 2000 Peter Kelly (pmk@post.com)
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.
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.
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.
23 #ifndef _XML_Tokenizer_h_
24 #define _XML_Tokenizer_h_
26 #include <QtXml/qxml.h>
27 #include <QtCore/QStack>
28 #include <QtCore/QLinkedList>
29 #include <QtCore/QMap>
30 #include <QtCore/QObject>
31 #include "misc/loader_client.h"
32 #include "misc/stringit.h"
34 class KHTMLView;
36 namespace khtml {
37 class CachedObject;
38 class CachedScript;
41 namespace DOM {
42 class DocumentImpl;
43 class NodeImpl;
44 class HTMLScriptElementImpl;
45 class DocumentImpl;
46 class HTMLScriptElementImpl;
49 namespace khtml {
51 class XMLHandler : public QXmlDefaultHandler
53 public:
54 XMLHandler(DOM::DocumentImpl *_doc, KHTMLView *_view);
55 virtual ~XMLHandler();
57 // return the error protocol if parsing failed
58 QString errorProtocol();
60 // overloaded handler functions
61 bool startDocument();
62 bool startElement(const QString& namespaceURI, const QString& localName, const QString& qName, const QXmlAttributes& atts);
63 bool endElement(const QString& namespaceURI, const QString& localName, const QString& qName);
64 bool startCDATA();
65 bool endCDATA();
66 bool characters(const QString& ch);
67 bool comment(const QString & ch);
68 bool processingInstruction(const QString &target, const QString &data);
70 // namespace handling, to workaround problem in QXML where some attributes
71 // do not get the namespace resolved properly
72 bool startPrefixMapping(const QString& prefix, const QString& uri);
73 bool endPrefixMapping(const QString& prefix);
74 void fixUpNSURI(QString& uri, const QString& qname);
75 QMap<QString, QStack<QString> > namespaceInfo;
78 // from QXmlDeclHandler
79 bool attributeDecl(const QString &eName, const QString &aName, const QString &type, const QString &valueDefault, const QString &value);
80 bool externalEntityDecl(const QString &name, const QString &publicId, const QString &systemId);
81 bool internalEntityDecl(const QString &name, const QString &value);
83 // from QXmlDTDHandler
84 bool notationDecl(const QString &name, const QString &publicId, const QString &systemId);
85 bool unparsedEntityDecl(const QString &name, const QString &publicId, const QString &systemId, const QString &notationName);
87 bool enterText();
88 void exitText();
90 QString errorString() const;
92 bool fatalError( const QXmlParseException& exception );
94 unsigned long errorLine;
95 unsigned long errorCol;
97 private:
98 void pushNode( DOM::NodeImpl *node );
99 DOM::NodeImpl *popNode();
100 DOM::NodeImpl *currentNode() const;
101 private:
102 QString errorProt;
103 DOM::DocumentImpl *m_doc;
104 KHTMLView *m_view;
105 QStack<DOM::NodeImpl*> m_nodes;
106 DOM::NodeImpl *m_rootNode;
108 enum State {
109 StateInit,
110 StateDocument,
111 StateQuote,
112 StateLine,
113 StateHeading,
114 StateP
116 State state;
119 class Tokenizer : public QObject
121 Q_OBJECT
122 public:
123 virtual void begin() = 0;
124 // script output must be prepended, while new data
125 // received during executing a script must be appended, hence the
126 // extra bool to be able to distinguish between both cases. document.write()
127 // always uses false, while khtmlpart uses true
128 virtual void write( const TokenizerString &str, bool appendData) = 0;
129 virtual void end() = 0;
130 virtual void finish() = 0;
131 virtual void setOnHold(bool /*_onHold*/) {}
132 virtual bool isWaitingForScripts() const = 0;
133 virtual bool isExecutingScript() const = 0;
134 virtual void setNormalYeldDelay() {}
135 virtual void abort() {}
136 virtual void setAutoClose(bool b=true) = 0;
138 Q_SIGNALS:
139 void finishedParsing();
143 class XMLIncrementalSource : public QXmlInputSource
145 public:
146 XMLIncrementalSource();
147 virtual void fetchData();
148 virtual QChar next();
149 virtual void setData( const QString& str );
150 virtual void setData( const QByteArray& data );
151 virtual QString data() const;
153 void appendXML( const QString& str );
154 void setFinished( bool );
156 inline void setPaused(bool paused = true) { m_paused = paused; }
158 private:
159 QString m_data;
160 int m_pos;
161 const QChar *m_unicode;
162 bool m_finished;
163 bool m_paused; // if waiting for scripts
166 class XMLTokenizer : public Tokenizer, public khtml::CachedObjectClient
168 public:
169 XMLTokenizer(DOM::DocumentImpl *, KHTMLView * = 0);
170 virtual ~XMLTokenizer();
171 virtual void begin();
172 virtual void write( const TokenizerString &str, bool );
173 virtual void end();
174 virtual void finish();
175 virtual void setAutoClose(bool b=true) { qWarning("XMLTokenizer::setAutoClose: stub."); (void)b; }
177 // from CachedObjectClient
178 void notifyFinished(khtml::CachedObject *finishedObj);
180 virtual bool isWaitingForScripts() const;
181 virtual bool isExecutingScript() const { return m_executingScript; }
183 // execute script in place, if it contains src attribute we stop parsing till it's downloaded
184 void executeScript(DOM::NodeImpl *n);
186 protected:
187 DOM::DocumentImpl *m_doc;
188 KHTMLView *m_view;
190 khtml::CachedScript *m_cachedScript;
192 QString m_bufferedData;
194 XMLHandler m_handler;
195 QXmlSimpleReader m_reader;
196 XMLIncrementalSource m_source;
197 bool m_noErrors;
198 bool m_executingScript;
199 bool m_explicitFinishParsingNeeded;
200 bool m_insideWrite;
203 } // end namespace
205 #endif