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>
36 #include <QtXml/qxmlstream.h>
38 #include <libxml/tree.h>
39 #include <libxml/xmlstring.h>
47 class DocumentFragment
;
51 class PendingCallbacks
;
54 class XMLTokenizer
: public Tokenizer
, public CachedResourceClient
{
56 XMLTokenizer(Document
*, FrameView
* = 0);
57 XMLTokenizer(DocumentFragment
*, Element
*);
60 enum ErrorType
{ warning
, nonFatal
, fatal
};
63 virtual bool write(const SegmentedString
&, bool appendData
);
64 virtual void finish();
65 virtual bool isWaitingForScripts() const;
66 virtual void stopParsing();
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;
91 void parseStartElement();
92 void parseEndElement();
93 void parseCharacters();
94 void parseProcessingInstruction();
99 bool hasError() const;
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
);
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
);
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();
127 void doWrite(const String
&);
133 String m_originalSourceForTransform
;
136 QXmlStreamReader m_stream
;
139 xmlParserCtxtPtr m_context
;
140 OwnPtr
<PendingCallbacks
> m_pendingCallbacks
;
141 Vector
<xmlChar
> m_bufferedText
;
144 bool m_currentNodeIsReferenced
;
147 bool m_sawXSLTransform
;
148 bool m_sawFirstElement
;
149 bool m_isXHTMLDocument
;
152 bool m_requestingScript
;
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
;
173 void* xmlDocPtrForString(DocLoader
*, const String
& source
, const String
& url
);
174 void setLoaderForLibXMLCallbacks(DocLoader
*);
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