- add third_party src.
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / xml / parser / XMLDocumentParser.h
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)
6  * Copyright (C) 2008, 2009 Torch Mobile Inc. All rights reserved. (http://www.torchmobile.com/)
7  *
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.
12  *
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.
17  *
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.
22  *
23  */
24
25 #ifndef XMLDocumentParser_h
26 #define XMLDocumentParser_h
27
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>
39
40 namespace WebCore {
41
42 class ContainerNode;
43 class ScriptResource;
44 class ResourceFetcher;
45 class DocumentFragment;
46 class Document;
47 class Element;
48 class FrameView;
49 class Text;
50
51     class XMLParserContext : public RefCounted<XMLParserContext> {
52     public:
53         static PassRefPtr<XMLParserContext> createMemoryParser(xmlSAXHandlerPtr, void* userData, const CString& chunk);
54         static PassRefPtr<XMLParserContext> createStringParser(xmlSAXHandlerPtr, void* userData);
55         ~XMLParserContext();
56         xmlParserCtxtPtr context() const { return m_context; }
57
58     private:
59         XMLParserContext(xmlParserCtxtPtr context)
60             : m_context(context)
61         {
62         }
63         xmlParserCtxtPtr m_context;
64     };
65
66     class XMLDocumentParser : public ScriptableDocumentParser, public ResourceClient {
67         WTF_MAKE_FAST_ALLOCATED;
68     public:
69         static PassRefPtr<XMLDocumentParser> create(Document* document, FrameView* view)
70         {
71             return adoptRef(new XMLDocumentParser(document, view));
72         }
73         static PassRefPtr<XMLDocumentParser> create(DocumentFragment* fragment, Element* element, ParserContentPolicy parserContentPolicy)
74         {
75             return adoptRef(new XMLDocumentParser(fragment, element, parserContentPolicy));
76         }
77
78         ~XMLDocumentParser();
79
80         // Exposed for callbacks:
81         void handleError(XMLErrors::ErrorType, const char* message, TextPosition);
82
83         void setIsXHTMLDocument(bool isXHTML) { m_isXHTMLDocument = isXHTML; }
84         bool isXHTMLDocument() const { return m_isXHTMLDocument; }
85
86         bool isCurrentlyParsing8BitChunk() { return m_isCurrentlyParsing8BitChunk; }
87
88         static bool parseDocumentFragment(const String&, DocumentFragment*, Element* parent = 0, ParserContentPolicy = AllowScriptingContent);
89
90         // Used by the XMLHttpRequest to check if the responseXML was well formed.
91         virtual bool wellFormed() const { return !m_sawError; }
92
93         TextPosition textPosition() const;
94
95         static bool supportsXMLVersion(const String&);
96
97         class PendingCallback {
98         public:
99             virtual ~PendingCallback() { }
100             virtual void call(XMLDocumentParser*) = 0;
101         };
102
103     private:
104         XMLDocumentParser(Document*, FrameView* = 0);
105         XMLDocumentParser(DocumentFragment*, Element*, ParserContentPolicy);
106
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;
116
117         // from ResourceClient
118         virtual void notifyFinished(Resource*);
119
120         void end();
121
122         void pauseParsing();
123         void resumeParsing();
124
125         bool appendFragmentSource(const String&);
126
127     public:
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);
132         void endElementNs();
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);
139         void endDocument();
140
141     private:
142         void initializeParserContext(const CString& chunk = CString());
143
144         void pushCurrentNode(ContainerNode*);
145         void popCurrentNode();
146         void clearCurrentNodeStack();
147
148         void insertErrorMessageBlock();
149
150         void enterText();
151         void exitText();
152
153         void doWrite(const String&);
154         void doEnd();
155
156         FrameView* m_view;
157
158         SegmentedString m_originalSourceForTransform;
159
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;
164
165         ContainerNode* m_currentNode;
166         Vector<ContainerNode*> m_currentNodeStack;
167
168         RefPtr<Text> m_leafTextNode;
169
170         bool m_isCurrentlyParsing8BitChunk;
171         bool m_sawError;
172         bool m_sawCSS;
173         bool m_sawXSLTransform;
174         bool m_sawFirstElement;
175         bool m_isXHTMLDocument;
176         bool m_parserPaused;
177         bool m_requestingScript;
178         bool m_finishCalled;
179
180         XMLErrors m_xmlErrors;
181
182         ResourcePtr<ScriptResource> m_pendingScript;
183         RefPtr<Element> m_scriptElement;
184         TextPosition m_scriptStartPosition;
185
186         bool m_parsingFragment;
187         AtomicString m_defaultNamespaceURI;
188
189         typedef HashMap<AtomicString, AtomicString> PrefixForNamespaceMap;
190         PrefixForNamespaceMap m_prefixToNamespaceMap;
191         SegmentedString m_pendingSrc;
192     };
193
194 xmlDocPtr xmlDocPtrForString(ResourceFetcher*, const String& source, const String& url);
195
196 HashMap<String, String> parseAttributes(const String&, bool& attrsOK);
197
198 } // namespace WebCore
199
200 #endif // XMLDocumentParser_h