2008-11-04 Anders Carlsson <andersca@apple.com>
[webkit/qt.git] / WebCore / dom / XMLTokenizer.h
blob822ab2dca6160253a3606639bf7eca6cea12fa7a
1 /*
2 * Copyright (C) 2000 Peter Kelly (pmk@post.com)
3 * Copyright (C) 2005, 2006, 2007 Apple Inc. All rights reserved.
4 * Copyright (C) 2007 Samuel Weinig (sam@webkit.org)
5 * Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies)
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public License
18 * along with this library; see the file COPYING.LIB. If not, write to
19 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
24 #ifndef XMLTokenizer_h
25 #define XMLTokenizer_h
27 #include "CachedResourceClient.h"
28 #include "CachedResourceHandle.h"
29 #include "SegmentedString.h"
30 #include "StringHash.h"
31 #include "Tokenizer.h"
32 #include <wtf/HashMap.h>
33 #include <wtf/OwnPtr.h>
35 #if USE(QXMLSTREAM)
36 #include <QtXml/qxmlstream.h>
37 #else
38 #include <libxml/tree.h>
39 #include <libxml/xmlstring.h>
40 #endif
42 namespace WebCore {
44 class Node;
45 class CachedScript;
46 class DocLoader;
47 class DocumentFragment;
48 class Document;
49 class Element;
50 class FrameView;
51 class PendingCallbacks;
52 class ScriptElement;
54 class XMLTokenizer : public Tokenizer, public CachedResourceClient {
55 public:
56 XMLTokenizer(Document*, FrameView* = 0);
57 XMLTokenizer(DocumentFragment*, Element*);
58 ~XMLTokenizer();
60 enum ErrorType { warning, nonFatal, fatal };
62 // from Tokenizer
63 virtual bool write(const SegmentedString&, bool appendData);
64 virtual void finish();
65 virtual bool isWaitingForScripts() const;
66 virtual void stopParsing();
68 void end();
70 void pauseParsing();
71 void resumeParsing();
73 void setIsXHTMLDocument(bool isXHTML) { m_isXHTMLDocument = isXHTML; }
74 bool isXHTMLDocument() const { return m_isXHTMLDocument; }
76 // from CachedResourceClient
77 virtual void notifyFinished(CachedResource* finishedObj);
80 void handleError(ErrorType type, const char* m, int lineNumber, int columnNumber);
82 virtual bool wellFormed() const { return !m_sawError; }
84 int lineNumber() const;
85 int columnNumber() const;
87 #if USE(QXMLSTREAM)
88 private:
89 void parse();
90 void startDocument();
91 void parseStartElement();
92 void parseEndElement();
93 void parseCharacters();
94 void parseProcessingInstruction();
95 void parseCdata();
96 void parseComment();
97 void endDocument();
98 void parseDtd();
99 bool hasError() const;
100 #else
101 public:
102 // callbacks from parser SAX
103 void error(ErrorType, const char* message, va_list args) WTF_ATTRIBUTE_PRINTF(3, 0);
104 void startElementNs(const xmlChar* xmlLocalName, const xmlChar* xmlPrefix, const xmlChar* xmlURI, int nb_namespaces,
105 const xmlChar** namespaces, int nb_attributes, int nb_defaulted, const xmlChar** libxmlAttributes);
106 void endElementNs();
107 void characters(const xmlChar* s, int len);
108 void processingInstruction(const xmlChar* target, const xmlChar* data);
109 void cdataBlock(const xmlChar* s, int len);
110 void comment(const xmlChar* s);
111 void startDocument(const xmlChar* version, const xmlChar* encoding, int standalone);
112 void internalSubset(const xmlChar* name, const xmlChar* externalID, const xmlChar* systemID);
113 void endDocument();
114 #endif
115 private:
116 friend bool parseXMLDocumentFragment(const String& chunk, DocumentFragment* fragment, Element* parent);
118 static void eventuallyMarkAsParserCreated(Element* element);
119 void initializeParserContext(const char* chunk = 0);
120 void setCurrentNode(Node*);
122 void insertErrorMessageBlock();
124 bool enterText();
125 void exitText();
127 void doWrite(const String&);
128 void doEnd();
130 Document* m_doc;
131 FrameView* m_view;
133 String m_originalSourceForTransform;
135 #if USE(QXMLSTREAM)
136 QXmlStreamReader m_stream;
137 bool m_wroteText;
138 #else
139 xmlParserCtxtPtr m_context;
140 OwnPtr<PendingCallbacks> m_pendingCallbacks;
141 Vector<xmlChar> m_bufferedText;
142 #endif
143 Node* m_currentNode;
144 bool m_currentNodeIsReferenced;
146 bool m_sawError;
147 bool m_sawXSLTransform;
148 bool m_sawFirstElement;
149 bool m_isXHTMLDocument;
151 bool m_parserPaused;
152 bool m_requestingScript;
153 bool m_finishCalled;
155 int m_errorCount;
156 int m_lastErrorLine;
157 int m_lastErrorColumn;
158 String m_errorMessages;
160 CachedResourceHandle<CachedScript> m_pendingScript;
161 RefPtr<Element> m_scriptElement;
162 int m_scriptStartLine;
164 bool m_parsingFragment;
165 String m_defaultNamespaceURI;
167 typedef HashMap<String, String> PrefixForNamespaceMap;
168 PrefixForNamespaceMap m_prefixToNamespaceMap;
169 SegmentedString m_pendingSrc;
172 #if ENABLE(XSLT)
173 void* xmlDocPtrForString(DocLoader*, const String& source, const String& url);
174 void setLoaderForLibXMLCallbacks(DocLoader*);
175 #endif
177 HashMap<String, String> parseAttributes(const String&, bool& attrsOK);
178 bool parseXMLDocumentFragment(const String&, DocumentFragment*, Element* parent = 0);
180 bool isScriptElement(Element*);
181 ScriptElement* castToScriptElement(Element*);
183 } // namespace WebCore
185 #endif // XMLTokenizer_h