This source file includes following definitions.
- genericErrorFunc
- parseErrorFunc
- docLoaderFunc
- setXSLTLoadCallBack
- writeToStringBuilder
- saveResultToString
- xsltParamArrayFromParameterMap
- freeXsltParamArray
- xsltStylesheetPointer
- xmlDocPtrFromNode
- resultMIMEType
- transformToString
#include "config.h"
#include "core/xml/XSLTProcessor.h"
#include <libxslt/imports.h>
#include <libxslt/security.h>
#include <libxslt/variables.h>
#include <libxslt/xsltutils.h>
#include "FetchInitiatorTypeNames.h"
#include "core/dom/Document.h"
#include "core/dom/TransformSource.h"
#include "core/editing/markup.h"
#include "core/fetch/Resource.h"
#include "core/fetch/ResourceFetcher.h"
#include "core/frame/FrameHost.h"
#include "core/frame/LocalFrame.h"
#include "core/frame/PageConsole.h"
#include "core/xml/XSLStyleSheet.h"
#include "core/xml/XSLTExtensions.h"
#include "core/xml/XSLTUnicodeSort.h"
#include "core/xml/parser/XMLDocumentParser.h"
#include "platform/SharedBuffer.h"
#include "platform/network/ResourceError.h"
#include "platform/network/ResourceRequest.h"
#include "platform/network/ResourceResponse.h"
#include "platform/weborigin/SecurityOrigin.h"
#include "wtf/Assertions.h"
#include "wtf/Vector.h"
#include "wtf/text/CString.h"
#include "wtf/text/StringBuffer.h"
#include "wtf/unicode/UTF8.h"
namespace WebCore {
void XSLTProcessor::genericErrorFunc(void*, const char*, ...)
{
}
void XSLTProcessor::parseErrorFunc(void* userData, xmlError* error)
{
PageConsole* console = static_cast<PageConsole*>(userData);
if (!console)
return;
MessageLevel level;
switch (error->level) {
case XML_ERR_NONE:
level = DebugMessageLevel;
break;
case XML_ERR_WARNING:
level = WarningMessageLevel;
break;
case XML_ERR_ERROR:
case XML_ERR_FATAL:
default:
level = ErrorMessageLevel;
break;
}
console->addMessage(XMLMessageSource, level, error->message, error->file, error->line);
}
static XSLTProcessor* globalProcessor = 0;
static ResourceFetcher* globalResourceFetcher = 0;
static xmlDocPtr docLoaderFunc(const xmlChar* uri,
xmlDictPtr,
int options,
void* ctxt,
xsltLoadType type)
{
if (!globalProcessor)
return 0;
switch (type) {
case XSLT_LOAD_DOCUMENT: {
xsltTransformContextPtr context = (xsltTransformContextPtr)ctxt;
xmlChar* base = xmlNodeGetBase(context->document->doc, context->node);
KURL url(KURL(ParsedURLString, reinterpret_cast<const char*>(base)), reinterpret_cast<const char*>(uri));
xmlFree(base);
ResourceLoaderOptions fetchOptions(ResourceFetcher::defaultResourceOptions());
FetchRequest request(ResourceRequest(url), FetchInitiatorTypeNames::xml, fetchOptions);
request.setOriginRestriction(FetchRequest::RestrictToSameOrigin);
ResourcePtr<Resource> resource = globalResourceFetcher->fetchSynchronously(request);
if (!resource || !globalProcessor)
return 0;
PageConsole* console = 0;
LocalFrame* frame = globalProcessor->xslStylesheet()->ownerDocument()->frame();
if (frame && frame->host())
console = &frame->host()->console();
xmlSetStructuredErrorFunc(console, XSLTProcessor::parseErrorFunc);
xmlSetGenericErrorFunc(console, XSLTProcessor::genericErrorFunc);
SharedBuffer* data = resource->resourceBuffer();
xmlDocPtr doc = data ? xmlReadMemory(data->data(), data->size(), (const char*)uri, 0, options) : 0;
xmlSetStructuredErrorFunc(0, 0);
xmlSetGenericErrorFunc(0, 0);
return doc;
}
case XSLT_LOAD_STYLESHEET:
return globalProcessor->xslStylesheet()->locateStylesheetSubResource(((xsltStylesheetPtr)ctxt)->doc, uri);
default:
break;
}
return 0;
}
static inline void setXSLTLoadCallBack(xsltDocLoaderFunc func, XSLTProcessor* processor, ResourceFetcher* fetcher)
{
xsltSetLoaderFunc(func);
globalProcessor = processor;
globalResourceFetcher = fetcher;
}
static int writeToStringBuilder(void* context, const char* buffer, int len)
{
StringBuilder& resultOutput = *static_cast<StringBuilder*>(context);
if (!len)
return 0;
StringBuffer<UChar> stringBuffer(len);
UChar* bufferUChar = stringBuffer.characters();
UChar* bufferUCharEnd = bufferUChar + len;
const char* stringCurrent = buffer;
WTF::Unicode::ConversionResult result = WTF::Unicode::convertUTF8ToUTF16(&stringCurrent, buffer + len, &bufferUChar, bufferUCharEnd);
if (result != WTF::Unicode::conversionOK && result != WTF::Unicode::sourceExhausted) {
ASSERT_NOT_REACHED();
return -1;
}
int utf16Length = bufferUChar - stringBuffer.characters();
resultOutput.append(stringBuffer.characters(), utf16Length);
return stringCurrent - buffer;
}
static bool saveResultToString(xmlDocPtr resultDoc, xsltStylesheetPtr sheet, String& resultString)
{
xmlOutputBufferPtr outputBuf = xmlAllocOutputBuffer(0);
if (!outputBuf)
return false;
StringBuilder resultBuilder;
outputBuf->context = &resultBuilder;
outputBuf->writecallback = writeToStringBuilder;
int retval = xsltSaveResultTo(outputBuf, resultDoc, sheet);
xmlOutputBufferClose(outputBuf);
if (retval < 0)
return false;
if (resultBuilder.length() > 0 && resultBuilder[resultBuilder.length() - 1] == '\n')
resultBuilder.resize(resultBuilder.length() - 1);
resultString = resultBuilder.toString();
return true;
}
static const char** xsltParamArrayFromParameterMap(XSLTProcessor::ParameterMap& parameters)
{
if (parameters.isEmpty())
return 0;
const char** parameterArray = (const char**)fastMalloc(((parameters.size() * 2) + 1) * sizeof(char*));
XSLTProcessor::ParameterMap::iterator end = parameters.end();
unsigned index = 0;
for (XSLTProcessor::ParameterMap::iterator it = parameters.begin(); it != end; ++it) {
parameterArray[index++] = fastStrDup(it->key.utf8().data());
parameterArray[index++] = fastStrDup(it->value.utf8().data());
}
parameterArray[index] = 0;
return parameterArray;
}
static void freeXsltParamArray(const char** params)
{
const char** temp = params;
if (!params)
return;
while (*temp) {
fastFree((void*)*(temp++));
fastFree((void*)*(temp++));
}
fastFree(params);
}
static xsltStylesheetPtr xsltStylesheetPointer(RefPtrWillBeMember<XSLStyleSheet>& cachedStylesheet, Node* stylesheetRootNode)
{
if (!cachedStylesheet && stylesheetRootNode) {
cachedStylesheet = XSLStyleSheet::createForXSLTProcessor(stylesheetRootNode->parentNode() ? stylesheetRootNode->parentNode() : stylesheetRootNode,
stylesheetRootNode->document().url().string(),
stylesheetRootNode->document().url());
cachedStylesheet->parseString(createMarkup(stylesheetRootNode));
}
if (!cachedStylesheet || !cachedStylesheet->document())
return 0;
return cachedStylesheet->compileStyleSheet();
}
static inline xmlDocPtr xmlDocPtrFromNode(Node* sourceNode, bool& shouldDelete)
{
RefPtr<Document> ownerDocument(sourceNode->document());
bool sourceIsDocument = (sourceNode == ownerDocument.get());
xmlDocPtr sourceDoc = 0;
if (sourceIsDocument && ownerDocument->transformSource())
sourceDoc = (xmlDocPtr)ownerDocument->transformSource()->platformSource();
if (!sourceDoc) {
sourceDoc = (xmlDocPtr)xmlDocPtrForString(ownerDocument->fetcher(), createMarkup(sourceNode),
sourceIsDocument ? ownerDocument->url().string() : String());
shouldDelete = sourceDoc;
}
return sourceDoc;
}
static inline String resultMIMEType(xmlDocPtr resultDoc, xsltStylesheetPtr sheet)
{
const xmlChar* resultType = 0;
XSLT_GET_IMPORT_PTR(resultType, sheet, method);
if (!resultType && resultDoc->type == XML_HTML_DOCUMENT_NODE)
resultType = (const xmlChar*)"html";
if (xmlStrEqual(resultType, (const xmlChar*)"html"))
return "text/html";
if (xmlStrEqual(resultType, (const xmlChar*)"text"))
return "text/plain";
return "application/xml";
}
bool XSLTProcessor::transformToString(Node* sourceNode, String& mimeType, String& resultString, String& resultEncoding)
{
RefPtr<Document> ownerDocument(sourceNode->document());
setXSLTLoadCallBack(docLoaderFunc, this, ownerDocument->fetcher());
xsltStylesheetPtr sheet = xsltStylesheetPointer(m_stylesheet, m_stylesheetRootNode.get());
if (!sheet) {
setXSLTLoadCallBack(0, 0, 0);
m_stylesheet = nullptr;
return false;
}
m_stylesheet->clearDocuments();
xmlChar* origMethod = sheet->method;
if (!origMethod && mimeType == "text/html")
sheet->method = (xmlChar*)"html";
bool success = false;
bool shouldFreeSourceDoc = false;
if (xmlDocPtr sourceDoc = xmlDocPtrFromNode(sourceNode, shouldFreeSourceDoc)) {
sheet->omitXmlDeclaration = true;
xsltTransformContextPtr transformContext = xsltNewTransformContext(sheet, sourceDoc);
registerXSLTExtensions(transformContext);
xsltSecurityPrefsPtr securityPrefs = xsltNewSecurityPrefs();
if (0 != xsltSetSecurityPrefs(securityPrefs, XSLT_SECPREF_WRITE_FILE, xsltSecurityForbid))
CRASH();
if (0 != xsltSetSecurityPrefs(securityPrefs, XSLT_SECPREF_CREATE_DIRECTORY, xsltSecurityForbid))
CRASH();
if (0 != xsltSetSecurityPrefs(securityPrefs, XSLT_SECPREF_WRITE_NETWORK, xsltSecurityForbid))
CRASH();
if (0 != xsltSetCtxtSecurityPrefs(securityPrefs, transformContext))
CRASH();
xsltSetCtxtSortFunc(transformContext, xsltUnicodeSortFunction);
if (!transformContext->globalVars)
transformContext->globalVars = xmlHashCreate(20);
const char** params = xsltParamArrayFromParameterMap(m_parameters);
xsltQuoteUserParams(transformContext, params);
xmlDocPtr resultDoc = xsltApplyStylesheetUser(sheet, sourceDoc, 0, 0, 0, transformContext);
xsltFreeTransformContext(transformContext);
xsltFreeSecurityPrefs(securityPrefs);
freeXsltParamArray(params);
if (shouldFreeSourceDoc)
xmlFreeDoc(sourceDoc);
if ((success = saveResultToString(resultDoc, sheet, resultString))) {
mimeType = resultMIMEType(resultDoc, sheet);
resultEncoding = (char*)resultDoc->encoding;
}
xmlFreeDoc(resultDoc);
}
sheet->method = origMethod;
setXSLTLoadCallBack(0, 0, 0);
xsltFreeStylesheet(sheet);
m_stylesheet = nullptr;
return success;
}
}