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)
6 * Copyright (C) 2008, 2009 Torch Mobile Inc. All rights reserved. (http://www.torchmobile.com/)
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
18 * You should have received a copy of the GNU Library General Public License
19 * along with this library; see the file COPYING.LIB. If not, write to
20 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 * Boston, MA 02110-1301, USA.
25 #ifndef XMLDocumentParser_h
26 #define XMLDocumentParser_h
28 #include "core/dom/ParserContentPolicy.h"
29 #include "core/dom/ScriptableDocumentParser.h"
30 #include "core/fetch/ResourceClient.h"
31 #include "core/fetch/ResourcePtr.h"
32 #include "core/xml/XMLErrors.h"
33 #include "platform/text/SegmentedString.h"
34 #include "wtf/HashMap.h"
35 #include "wtf/OwnPtr.h"
36 #include "wtf/text/CString.h"
37 #include "wtf/text/StringHash.h"
38 #include <libxml/tree.h>
44 class ResourceFetcher;
45 class DocumentFragment;
51 class XMLParserContext : public RefCounted<XMLParserContext> {
53 static PassRefPtr<XMLParserContext> createMemoryParser(xmlSAXHandlerPtr, void* userData, const CString& chunk);
54 static PassRefPtr<XMLParserContext> createStringParser(xmlSAXHandlerPtr, void* userData);
56 xmlParserCtxtPtr context() const { return m_context; }
59 XMLParserContext(xmlParserCtxtPtr context)
63 xmlParserCtxtPtr m_context;
66 class XMLDocumentParser : public ScriptableDocumentParser, public ResourceClient {
67 WTF_MAKE_FAST_ALLOCATED;
69 static PassRefPtr<XMLDocumentParser> create(Document* document, FrameView* view)
71 return adoptRef(new XMLDocumentParser(document, view));
73 static PassRefPtr<XMLDocumentParser> create(DocumentFragment* fragment, Element* element, ParserContentPolicy parserContentPolicy)
75 return adoptRef(new XMLDocumentParser(fragment, element, parserContentPolicy));
80 // Exposed for callbacks:
81 void handleError(XMLErrors::ErrorType, const char* message, TextPosition);
83 void setIsXHTMLDocument(bool isXHTML) { m_isXHTMLDocument = isXHTML; }
84 bool isXHTMLDocument() const { return m_isXHTMLDocument; }
86 bool isCurrentlyParsing8BitChunk() { return m_isCurrentlyParsing8BitChunk; }
88 static bool parseDocumentFragment(const String&, DocumentFragment*, Element* parent = 0, ParserContentPolicy = AllowScriptingContent);
90 // Used by the XMLHttpRequest to check if the responseXML was well formed.
91 virtual bool wellFormed() const { return !m_sawError; }
93 TextPosition textPosition() const;
95 static bool supportsXMLVersion(const String&);
97 class PendingCallback {
99 virtual ~PendingCallback() { }
100 virtual void call(XMLDocumentParser*) = 0;
104 XMLDocumentParser(Document*, FrameView* = 0);
105 XMLDocumentParser(DocumentFragment*, Element*, ParserContentPolicy);
107 // From DocumentParser
108 virtual void insert(const SegmentedString&);
109 virtual void append(PassRefPtr<StringImpl>);
110 virtual void finish();
111 virtual bool isWaitingForScripts() const;
112 virtual void stopParsing();
113 virtual void detach();
114 virtual OrdinalNumber lineNumber() const;
115 OrdinalNumber columnNumber() const;
117 // from ResourceClient
118 virtual void notifyFinished(Resource*);
123 void resumeParsing();
125 bool appendFragmentSource(const String&);
128 // callbacks from parser SAX
129 void error(XMLErrors::ErrorType, const char* message, va_list args) WTF_ATTRIBUTE_PRINTF(3, 0);
130 void startElementNs(const AtomicString& localName, const AtomicString& prefix, const AtomicString& uri, int nb_namespaces,
131 const xmlChar** namespaces, int nb_attributes, int nb_defaulted, const xmlChar** libxmlAttributes);
133 void characters(const xmlChar* chars, int length);
134 void processingInstruction(const String& target, const String& data);
135 void cdataBlock(const String&);
136 void comment(const String&);
137 void startDocument(const String& version, const String& encoding, int standalone);
138 void internalSubset(const String& name, const String& externalID, const String& systemID);
142 void initializeParserContext(const CString& chunk = CString());
144 void pushCurrentNode(ContainerNode*);
145 void popCurrentNode();
146 void clearCurrentNodeStack();
148 void insertErrorMessageBlock();
153 void doWrite(const String&);
158 SegmentedString m_originalSourceForTransform;
160 xmlParserCtxtPtr context() const { return m_context ? m_context->context() : 0; };
161 RefPtr<XMLParserContext> m_context;
162 Deque<OwnPtr<PendingCallback> > m_pendingCallbacks;
163 Vector<xmlChar> m_bufferedText;
165 ContainerNode* m_currentNode;
166 Vector<ContainerNode*> m_currentNodeStack;
168 RefPtr<Text> m_leafTextNode;
170 bool m_isCurrentlyParsing8BitChunk;
173 bool m_sawXSLTransform;
174 bool m_sawFirstElement;
175 bool m_isXHTMLDocument;
177 bool m_requestingScript;
180 XMLErrors m_xmlErrors;
182 ResourcePtr<ScriptResource> m_pendingScript;
183 RefPtr<Element> m_scriptElement;
184 TextPosition m_scriptStartPosition;
186 bool m_parsingFragment;
187 AtomicString m_defaultNamespaceURI;
189 typedef HashMap<AtomicString, AtomicString> PrefixForNamespaceMap;
190 PrefixForNamespaceMap m_prefixToNamespaceMap;
191 SegmentedString m_pendingSrc;
194 xmlDocPtr xmlDocPtrForString(ResourceFetcher*, const String& source, const String& url);
196 HashMap<String, String> parseAttributes(const String&, bool& attrsOK);
198 } // namespace WebCore
200 #endif // XMLDocumentParser_h