2 * This file is part of the XSL implementation.
4 * Copyright (C) 2004, 2005, 2006, 2007, 2008 Apple, Inc. All rights reserved.
5 * Copyright (C) 2005, 2006 Alexey Proskuryakov <ap@webkit.org>
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 #include "core/xml/XSLTProcessor.h"
26 #include "core/FetchInitiatorTypeNames.h"
27 #include "core/dom/Document.h"
28 #include "core/dom/TransformSource.h"
29 #include "core/editing/markup.h"
30 #include "core/fetch/Resource.h"
31 #include "core/fetch/ResourceFetcher.h"
32 #include "core/frame/FrameConsole.h"
33 #include "core/frame/FrameHost.h"
34 #include "core/frame/LocalFrame.h"
35 #include "core/xml/XSLStyleSheet.h"
36 #include "core/xml/XSLTExtensions.h"
37 #include "core/xml/XSLTUnicodeSort.h"
38 #include "core/xml/parser/XMLDocumentParser.h"
39 #include "platform/SharedBuffer.h"
40 #include "platform/network/ResourceError.h"
41 #include "platform/network/ResourceRequest.h"
42 #include "platform/network/ResourceResponse.h"
43 #include "platform/weborigin/SecurityOrigin.h"
44 #include "wtf/Assertions.h"
45 #include "wtf/Vector.h"
46 #include "wtf/text/CString.h"
47 #include "wtf/text/StringBuffer.h"
48 #include "wtf/unicode/UTF8.h"
49 #include <libxslt/imports.h>
50 #include <libxslt/security.h>
51 #include <libxslt/variables.h>
52 #include <libxslt/xsltutils.h>
56 void XSLTProcessor::genericErrorFunc(void*, const char*, ...)
58 // It would be nice to do something with this error message.
61 void XSLTProcessor::parseErrorFunc(void* userData, xmlError* error)
63 FrameConsole* console = static_cast<FrameConsole*>(userData);
68 switch (error->level) {
70 level = DebugMessageLevel;
73 level = WarningMessageLevel;
78 level = ErrorMessageLevel;
82 console->addMessage(XMLMessageSource, level, error->message, error->file, error->line);
85 // FIXME: There seems to be no way to control the ctxt pointer for loading here, thus we have globals.
86 static XSLTProcessor* globalProcessor = 0;
87 static ResourceFetcher* globalResourceFetcher = 0;
89 static xmlDocPtr docLoaderFunc(
90 const xmlChar* uri, xmlDictPtr, int options, void* ctxt, xsltLoadType type)
96 case XSLT_LOAD_DOCUMENT: {
97 xsltTransformContextPtr context = (xsltTransformContextPtr)ctxt;
98 xmlChar* base = xmlNodeGetBase(context->document->doc, context->node);
99 KURL url(KURL(ParsedURLString, reinterpret_cast<const char*>(base)), reinterpret_cast<const char*>(uri));
102 ResourceLoaderOptions fetchOptions(ResourceFetcher::defaultResourceOptions());
103 FetchRequest request(ResourceRequest(url), FetchInitiatorTypeNames::xml, fetchOptions);
104 request.setOriginRestriction(FetchRequest::RestrictToSameOrigin);
105 ResourcePtr<Resource> resource = globalResourceFetcher->fetchSynchronously(request);
106 if (!resource || !globalProcessor)
109 FrameConsole* console = 0;
110 LocalFrame* frame = globalProcessor->xslStylesheet()->ownerDocument()->frame();
112 console = &frame->console();
113 xmlSetStructuredErrorFunc(console, XSLTProcessor::parseErrorFunc);
114 xmlSetGenericErrorFunc(console, XSLTProcessor::genericErrorFunc);
116 // We don't specify an encoding here. Neither Gecko nor WinIE respects
117 // the encoding specified in the HTTP headers.
118 SharedBuffer* data = resource->resourceBuffer();
119 xmlDocPtr doc = data ? xmlReadMemory(data->data(), data->size(), (const char*)uri, 0, options) : 0;
121 xmlSetStructuredErrorFunc(0, 0);
122 xmlSetGenericErrorFunc(0, 0);
126 case XSLT_LOAD_STYLESHEET:
127 return globalProcessor->xslStylesheet()->locateStylesheetSubResource(((xsltStylesheetPtr)ctxt)->doc, uri);
135 static inline void setXSLTLoadCallBack(xsltDocLoaderFunc func, XSLTProcessor* processor, ResourceFetcher* fetcher)
137 xsltSetLoaderFunc(func);
138 globalProcessor = processor;
139 globalResourceFetcher = fetcher;
142 static int writeToStringBuilder(void* context, const char* buffer, int len)
144 StringBuilder& resultOutput = *static_cast<StringBuilder*>(context);
149 StringBuffer<UChar> stringBuffer(len);
150 UChar* bufferUChar = stringBuffer.characters();
151 UChar* bufferUCharEnd = bufferUChar + len;
153 const char* stringCurrent = buffer;
154 WTF::Unicode::ConversionResult result = WTF::Unicode::convertUTF8ToUTF16(&stringCurrent, buffer + len, &bufferUChar, bufferUCharEnd);
155 if (result != WTF::Unicode::conversionOK && result != WTF::Unicode::sourceExhausted) {
156 ASSERT_NOT_REACHED();
160 int utf16Length = bufferUChar - stringBuffer.characters();
161 resultOutput.append(stringBuffer.characters(), utf16Length);
162 return stringCurrent - buffer;
165 static bool saveResultToString(xmlDocPtr resultDoc, xsltStylesheetPtr sheet, String& resultString)
167 xmlOutputBufferPtr outputBuf = xmlAllocOutputBuffer(0);
171 StringBuilder resultBuilder;
172 outputBuf->context = &resultBuilder;
173 outputBuf->writecallback = writeToStringBuilder;
175 int retval = xsltSaveResultTo(outputBuf, resultDoc, sheet);
176 xmlOutputBufferClose(outputBuf);
180 // Workaround for <http://bugzilla.gnome.org/show_bug.cgi?id=495668>:
181 // libxslt appends an extra line feed to the result.
182 if (resultBuilder.length() > 0 && resultBuilder[resultBuilder.length() - 1] == '\n')
183 resultBuilder.resize(resultBuilder.length() - 1);
185 resultString = resultBuilder.toString();
190 static const char** xsltParamArrayFromParameterMap(XSLTProcessor::ParameterMap& parameters)
192 if (parameters.isEmpty())
195 const char** parameterArray = static_cast<const char**>(fastMalloc(((parameters.size() * 2) + 1) * sizeof(char*)));
197 XSLTProcessor::ParameterMap::iterator end = parameters.end();
199 for (XSLTProcessor::ParameterMap::iterator it = parameters.begin(); it != end; ++it) {
200 parameterArray[index++] = fastStrDup(it->key.utf8().data());
201 parameterArray[index++] = fastStrDup(it->value.utf8().data());
203 parameterArray[index] = 0;
205 return parameterArray;
208 static void freeXsltParamArray(const char** params)
210 const char** temp = params;
215 fastFree(const_cast<char*>(*(temp++)));
216 fastFree(const_cast<char*>(*(temp++)));
221 static xsltStylesheetPtr xsltStylesheetPointer(RefPtrWillBeMember<XSLStyleSheet>& cachedStylesheet, Node* stylesheetRootNode)
223 if (!cachedStylesheet && stylesheetRootNode) {
224 cachedStylesheet = XSLStyleSheet::createForXSLTProcessor(
225 stylesheetRootNode->parentNode() ? stylesheetRootNode->parentNode() : stylesheetRootNode,
226 stylesheetRootNode->document().url().string(),
227 stylesheetRootNode->document().url()); // FIXME: Should we use baseURL here?
229 // According to Mozilla documentation, the node must be a Document node,
230 // an xsl:stylesheet or xsl:transform element. But we just use text
231 // content regardless of node type.
232 cachedStylesheet->parseString(createMarkup(stylesheetRootNode));
235 if (!cachedStylesheet || !cachedStylesheet->document())
238 return cachedStylesheet->compileStyleSheet();
241 static inline xmlDocPtr xmlDocPtrFromNode(Node* sourceNode, bool& shouldDelete)
243 RefPtrWillBeRawPtr<Document> ownerDocument(sourceNode->document());
244 bool sourceIsDocument = (sourceNode == ownerDocument.get());
246 xmlDocPtr sourceDoc = 0;
247 if (sourceIsDocument && ownerDocument->transformSource())
248 sourceDoc = (xmlDocPtr)ownerDocument->transformSource()->platformSource();
250 sourceDoc = (xmlDocPtr)xmlDocPtrForString(ownerDocument->fetcher(), createMarkup(sourceNode),
251 sourceIsDocument ? ownerDocument->url().string() : String());
252 shouldDelete = sourceDoc;
257 static inline String resultMIMEType(xmlDocPtr resultDoc, xsltStylesheetPtr sheet)
259 // There are three types of output we need to be able to deal with:
260 // HTML (create an HTML document), XML (create an XML document),
261 // and text (wrap in a <pre> and create an XML document).
263 const xmlChar* resultType = 0;
264 XSLT_GET_IMPORT_PTR(resultType, sheet, method);
265 if (!resultType && resultDoc->type == XML_HTML_DOCUMENT_NODE)
266 resultType = (const xmlChar*)"html";
268 if (xmlStrEqual(resultType, (const xmlChar*)"html"))
270 if (xmlStrEqual(resultType, (const xmlChar*)"text"))
273 return "application/xml";
276 bool XSLTProcessor::transformToString(Node* sourceNode, String& mimeType, String& resultString, String& resultEncoding)
278 RefPtrWillBeRawPtr<Document> ownerDocument(sourceNode->document());
280 setXSLTLoadCallBack(docLoaderFunc, this, ownerDocument->fetcher());
281 xsltStylesheetPtr sheet = xsltStylesheetPointer(m_stylesheet, m_stylesheetRootNode.get());
283 setXSLTLoadCallBack(0, 0, 0);
284 m_stylesheet = nullptr;
287 m_stylesheet->clearDocuments();
289 xmlChar* origMethod = sheet->method;
290 if (!origMethod && mimeType == "text/html")
291 sheet->method = (xmlChar*)"html";
293 bool success = false;
294 bool shouldFreeSourceDoc = false;
295 if (xmlDocPtr sourceDoc = xmlDocPtrFromNode(sourceNode, shouldFreeSourceDoc)) {
296 // The XML declaration would prevent parsing the result as a fragment,
297 // and it's not needed even for documents, as the result of this
298 // function is always immediately parsed.
299 sheet->omitXmlDeclaration = true;
301 xsltTransformContextPtr transformContext = xsltNewTransformContext(sheet, sourceDoc);
302 registerXSLTExtensions(transformContext);
304 xsltSecurityPrefsPtr securityPrefs = xsltNewSecurityPrefs();
305 // Read permissions are checked by docLoaderFunc.
306 if (0 != xsltSetSecurityPrefs(securityPrefs, XSLT_SECPREF_WRITE_FILE, xsltSecurityForbid))
308 if (0 != xsltSetSecurityPrefs(securityPrefs, XSLT_SECPREF_CREATE_DIRECTORY, xsltSecurityForbid))
310 if (0 != xsltSetSecurityPrefs(securityPrefs, XSLT_SECPREF_WRITE_NETWORK, xsltSecurityForbid))
312 if (0 != xsltSetCtxtSecurityPrefs(securityPrefs, transformContext))
315 // <http://bugs.webkit.org/show_bug.cgi?id=16077>: XSLT processor
316 // <xsl:sort> algorithm only compares by code point.
317 xsltSetCtxtSortFunc(transformContext, xsltUnicodeSortFunction);
319 // This is a workaround for a bug in libxslt.
320 // The bug has been fixed in version 1.1.13, so once we ship that this
322 if (!transformContext->globalVars)
323 transformContext->globalVars = xmlHashCreate(20);
325 const char** params = xsltParamArrayFromParameterMap(m_parameters);
326 xsltQuoteUserParams(transformContext, params);
327 xmlDocPtr resultDoc = xsltApplyStylesheetUser(sheet, sourceDoc, 0, 0, 0, transformContext);
329 xsltFreeTransformContext(transformContext);
330 xsltFreeSecurityPrefs(securityPrefs);
331 freeXsltParamArray(params);
333 if (shouldFreeSourceDoc)
334 xmlFreeDoc(sourceDoc);
336 if ((success = saveResultToString(resultDoc, sheet, resultString))) {
337 mimeType = resultMIMEType(resultDoc, sheet);
338 resultEncoding = (char*)resultDoc->encoding;
340 xmlFreeDoc(resultDoc);
343 sheet->method = origMethod;
344 setXSLTLoadCallBack(0, 0, 0);
345 xsltFreeStylesheet(sheet);
346 m_stylesheet = nullptr;
351 } // namespace WebCore