tizen beta release
[profile/ivi/webkit-efl.git] / Source / WebCore / xml / XSLTProcessor.cpp
1 /*
2  * This file is part of the XSL implementation.
3  *
4  * Copyright (C) 2004, 2005, 2006, 2007, 2008 Apple, Inc. All rights reserved.
5  * Copyright (C) 2005, 2006 Alexey Proskuryakov <ap@webkit.org>
6  *
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.
11  *
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.
16  *
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.
21  */
22
23 #include "config.h"
24
25 #if ENABLE(XSLT)
26
27 #include "XSLTProcessor.h"
28
29 #include "DOMImplementation.h"
30 #include "CachedResourceLoader.h"
31 #include "DocumentFragment.h"
32 #include "Frame.h"
33 #include "FrameLoader.h"
34 #include "FrameView.h"
35 #include "HTMLBodyElement.h"
36 #include "HTMLDocument.h"
37 #include "Page.h"
38 #include "SecurityOrigin.h"
39 #include "Text.h"
40 #include "TextResourceDecoder.h"
41 #include "markup.h"
42
43 #include <wtf/Assertions.h>
44 #include <wtf/Vector.h>
45
46 namespace WebCore {
47
48 static inline void transformTextStringToXHTMLDocumentString(String& text)
49 {
50     // Modify the output so that it is a well-formed XHTML document with a <pre> tag enclosing the text.
51     text.replace('&', "&amp;");
52     text.replace('<', "&lt;");
53     text = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
54         "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n"
55         "<html xmlns=\"http://www.w3.org/1999/xhtml\">\n"
56         "<head><title/></head>\n"
57         "<body>\n"
58         "<pre>" + text + "</pre>\n"
59         "</body>\n"
60         "</html>\n";
61 }
62
63 XSLTProcessor::~XSLTProcessor()
64 {
65     // Stylesheet shouldn't outlive its root node.
66     ASSERT(!m_stylesheetRootNode || !m_stylesheet || m_stylesheet->hasOneRef());
67 }
68
69 PassRefPtr<Document> XSLTProcessor::createDocumentFromSource(const String& sourceString,
70     const String& sourceEncoding, const String& sourceMIMEType, Node* sourceNode, Frame* frame)
71 {
72     RefPtr<Document> ownerDocument = sourceNode->document();
73     bool sourceIsDocument = (sourceNode == ownerDocument.get());
74     String documentSource = sourceString;
75
76     RefPtr<Document> result;
77     if (sourceMIMEType == "text/plain") {
78         result = Document::create(frame, sourceIsDocument ? ownerDocument->url() : KURL());
79         transformTextStringToXHTMLDocumentString(documentSource);
80     } else
81         result = DOMImplementation::createDocument(sourceMIMEType, frame, sourceIsDocument ? ownerDocument->url() : KURL(), false);
82
83     // Before parsing, we need to save & detach the old document and get the new document
84     // in place. We have to do this only if we're rendering the result document.
85     if (frame) {
86         if (FrameView* view = frame->view())
87             view->clear();
88
89         if (Document* oldDocument = frame->document()) {
90             result->setTransformSourceDocument(oldDocument);
91             result->setSecurityOrigin(oldDocument->securityOrigin());
92             result->setCookieURL(oldDocument->cookieURL());
93         }
94
95         frame->setDocument(result);
96     }
97
98     RefPtr<TextResourceDecoder> decoder = TextResourceDecoder::create(sourceMIMEType);
99     decoder->setEncoding(sourceEncoding.isEmpty() ? UTF8Encoding() : TextEncoding(sourceEncoding), TextResourceDecoder::EncodingFromXMLHeader);
100     result->setDecoder(decoder.release());
101
102     result->setContent(documentSource);
103
104     return result.release();
105 }
106
107 static inline RefPtr<DocumentFragment> createFragmentFromSource(const String& sourceString, const String& sourceMIMEType, Document* outputDoc)
108 {
109     RefPtr<DocumentFragment> fragment = outputDoc->createDocumentFragment();
110
111     if (sourceMIMEType == "text/html") {
112         // As far as I can tell, there isn't a spec for how transformToFragment
113         // is supposed to work.  Based on the documentation I can find, it looks
114         // like we want to start parsing the fragment in the InBody insertion
115         // mode.  Unfortunately, that's an implementation detail of the parser.
116         // We achieve that effect here by passing in a fake body element as
117         // context for the fragment.
118         RefPtr<HTMLBodyElement> fakeBody = HTMLBodyElement::create(outputDoc);
119         fragment->parseHTML(sourceString, fakeBody.get());
120     } else if (sourceMIMEType == "text/plain")
121         fragment->parserAddChild(Text::create(outputDoc, sourceString));
122     else {
123         bool successfulParse = fragment->parseXML(sourceString, 0);
124         if (!successfulParse)
125             return 0;
126     }
127
128     // FIXME: Do we need to mess with URLs here?
129
130     return fragment;
131 }
132
133 PassRefPtr<Document> XSLTProcessor::transformToDocument(Node* sourceNode)
134 {
135     String resultMIMEType;
136     String resultString;
137     String resultEncoding;
138     if (!transformToString(sourceNode, resultMIMEType, resultString, resultEncoding))
139         return 0;
140     return createDocumentFromSource(resultString, resultEncoding, resultMIMEType, sourceNode, 0);
141 }
142
143 PassRefPtr<DocumentFragment> XSLTProcessor::transformToFragment(Node* sourceNode, Document* outputDoc)
144 {
145     String resultMIMEType;
146     String resultString;
147     String resultEncoding;
148
149     // If the output document is HTML, default to HTML method.
150     if (outputDoc->isHTMLDocument())
151         resultMIMEType = "text/html";
152
153     if (!transformToString(sourceNode, resultMIMEType, resultString, resultEncoding))
154         return 0;
155     return createFragmentFromSource(resultString, resultMIMEType, outputDoc);
156 }
157
158 void XSLTProcessor::setParameter(const String& /*namespaceURI*/, const String& localName, const String& value)
159 {
160     // FIXME: namespace support?
161     // should make a QualifiedName here but we'd have to expose the impl
162     m_parameters.set(localName, value);
163 }
164
165 String XSLTProcessor::getParameter(const String& /*namespaceURI*/, const String& localName) const
166 {
167     // FIXME: namespace support?
168     // should make a QualifiedName here but we'd have to expose the impl
169     return m_parameters.get(localName);
170 }
171
172 void XSLTProcessor::removeParameter(const String& /*namespaceURI*/, const String& localName)
173 {
174     // FIXME: namespace support?
175     m_parameters.remove(localName);
176 }
177
178 void XSLTProcessor::reset()
179 {
180     m_stylesheet.clear();
181     m_stylesheetRootNode.clear();
182     m_parameters.clear();
183 }
184
185 } // namespace WebCore
186
187 #endif // ENABLE(XSLT)