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"
44 class HTMLScriptElementImpl
;
46 class HTMLScriptElementImpl
;
51 class XMLHandler
: public QXmlDefaultHandler
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
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
);
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
¬ationName
);
90 QString
errorString() const;
92 bool fatalError( const QXmlParseException
& exception
);
94 unsigned long errorLine
;
95 unsigned long errorCol
;
98 void pushNode( DOM::NodeImpl
*node
);
99 DOM::NodeImpl
*popNode();
100 DOM::NodeImpl
*currentNode() const;
103 DOM::DocumentImpl
*m_doc
;
105 QStack
<DOM::NodeImpl
*> m_nodes
;
106 DOM::NodeImpl
*m_rootNode
;
119 class Tokenizer
: public QObject
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;
139 void finishedParsing();
143 class XMLIncrementalSource
: public QXmlInputSource
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
; }
161 const QChar
*m_unicode
;
163 bool m_paused
; // if waiting for scripts
166 class XMLTokenizer
: public Tokenizer
, public khtml::CachedObjectClient
169 XMLTokenizer(DOM::DocumentImpl
*, KHTMLView
* = 0);
170 virtual ~XMLTokenizer();
171 virtual void begin();
172 virtual void write( const TokenizerString
&str
, bool );
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
);
187 DOM::DocumentImpl
*m_doc
;
190 khtml::CachedScript
*m_cachedScript
;
192 QString m_bufferedData
;
194 XMLHandler m_handler
;
195 QXmlSimpleReader m_reader
;
196 XMLIncrementalSource m_source
;
198 bool m_executingScript
;
199 bool m_explicitFinishParsingNeeded
;