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 bool isWMLDocument() const;
79 // from CachedResourceClient
80 virtual void notifyFinished(CachedResource
* finishedObj
);
83 void handleError(ErrorType type
, const char* m
, int lineNumber
, int columnNumber
);
85 virtual bool wellFormed() const { return !m_sawError
; }
87 int lineNumber() const;
88 int columnNumber() const;
94 void parseStartElement();
95 void parseEndElement();
96 void parseCharacters();
97 void parseProcessingInstruction();
102 bool hasError() const;
105 // callbacks from parser SAX
106 void error(ErrorType
, const char* message
, va_list args
) WTF_ATTRIBUTE_PRINTF(3, 0);
107 void startElementNs(const xmlChar
* xmlLocalName
, const xmlChar
* xmlPrefix
, const xmlChar
* xmlURI
, int nb_namespaces
,
108 const xmlChar
** namespaces
, int nb_attributes
, int nb_defaulted
, const xmlChar
** libxmlAttributes
);
110 void characters(const xmlChar
* s
, int len
);
111 void processingInstruction(const xmlChar
* target
, const xmlChar
* data
);
112 void cdataBlock(const xmlChar
* s
, int len
);
113 void comment(const xmlChar
* s
);
114 void startDocument(const xmlChar
* version
, const xmlChar
* encoding
, int standalone
);
115 void internalSubset(const xmlChar
* name
, const xmlChar
* externalID
, const xmlChar
* systemID
);
119 friend bool parseXMLDocumentFragment(const String
& chunk
, DocumentFragment
* fragment
, Element
* parent
);
121 void initializeParserContext(const char* chunk
= 0);
122 void setCurrentNode(Node
*);
124 void insertErrorMessageBlock();
129 void doWrite(const String
&);
135 String m_originalSourceForTransform
;
138 QXmlStreamReader m_stream
;
141 xmlParserCtxtPtr m_context
;
142 OwnPtr
<PendingCallbacks
> m_pendingCallbacks
;
143 Vector
<xmlChar
> m_bufferedText
;
146 bool m_currentNodeIsReferenced
;
149 bool m_sawXSLTransform
;
150 bool m_sawFirstElement
;
151 bool m_isXHTMLDocument
;
154 bool m_requestingScript
;
159 int m_lastErrorColumn
;
160 String m_errorMessages
;
162 CachedResourceHandle
<CachedScript
> m_pendingScript
;
163 RefPtr
<Element
> m_scriptElement
;
164 int m_scriptStartLine
;
166 bool m_parsingFragment
;
167 String m_defaultNamespaceURI
;
169 typedef HashMap
<String
, String
> PrefixForNamespaceMap
;
170 PrefixForNamespaceMap m_prefixToNamespaceMap
;
171 SegmentedString m_pendingSrc
;
175 void* xmlDocPtrForString(DocLoader
*, const String
& source
, const String
& url
);
178 HashMap
<String
, String
> parseAttributes(const String
&, bool& attrsOK
);
179 bool parseXMLDocumentFragment(const String
&, DocumentFragment
*, Element
* parent
= 0);
181 } // namespace WebCore
183 #endif // XMLTokenizer_h