1
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
/* ***** BEGIN LICENSE BLOCK *****
3
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
5
* The contents of this file are subject to the Mozilla Public License Version
6
* 1.1 (the "License"); you may not use this file except in compliance with
7
* the License. You may obtain a copy of the License at
8
* http://www.mozilla.org/MPL/
10
* Software distributed under the License is distributed on an "AS IS" basis,
11
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12
* for the specific language governing rights and limitations under the
15
* The Original Code is TransforMiiX XSLT Processor.
17
* The Initial Developer of the Original Code is
19
* Portions created by the Initial Developer are Copyright (C) 2003
20
* the Initial Developer. All Rights Reserved.
23
* Axel Hecht <axel@pike.org>
25
* Alternatively, the contents of this file may be used under the terms of
26
* either the GNU General Public License Version 2 or later (the "GPL"), or
27
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28
* in which case the provisions of the GPL or the LGPL are applicable instead
29
* of those above. If you wish to allow use of your version of this file only
30
* under the terms of either the GPL or the LGPL, and not to allow others to
31
* use your version of this file under the terms of the MPL, indicate your
32
* decision by deleting the provisions above and replace them with the notice
33
* and other provisions required by the GPL or the LGPL. If you do not delete
34
* the provisions above, a recipient may use your version of this file under
35
* the terms of any one of the MPL, the GPL or the LGPL.
37
* ***** END LICENSE BLOCK ***** */
39
#include "txStandaloneStylesheetCompiler.h"
40
#include "txStylesheetCompiler.h"
41
#include "txURIUtils.h"
45
* Implementation of an In-Memory DOM based XML parser. The actual XML
46
* parsing is provided by EXPAT.
48
class txDriver : public txACompileObserver
51
nsresult parse(istream& aInputStream, const nsAString& aUri);
52
const nsAString& getErrorString();
57
void StartElement(const XML_Char *aName, const XML_Char **aAtts);
58
void EndElement(const XML_Char* aName);
59
void CharacterData(const XML_Char* aChars, int aLength);
60
int ExternalEntityRef(const XML_Char *aContext, const XML_Char *aBase,
61
const XML_Char *aSystemId,
62
const XML_Char *aPublicId);
64
TX_DECL_ACOMPILEOBSERVER;
66
nsRefPtr<txStylesheetCompiler> mCompiler;
68
void createErrorString();
69
nsString mErrorString;
70
// keep track of the nsresult returned by the handlers, expat forgets them
72
XML_Parser mExpatParser;
77
TX_CompileStylesheetPath(const txParsedURL& aURL, txStylesheet** aResult)
80
nsAutoString errMsg, filePath;
82
aURL.getFile(filePath);
83
PR_LOG(txLog::xslt, PR_LOG_ALWAYS,
84
("TX_CompileStylesheetPath: %s\n",
85
NS_LossyConvertUCS2toASCII(filePath).get()));
86
istream* xslInput = URIUtils::getInputStream(filePath, errMsg);
88
return NS_ERROR_FAILURE;
90
nsRefPtr<txDriver> driver = new txDriver();
92
return NS_ERROR_OUT_OF_MEMORY;
94
nsAutoString spec = filePath;
95
if (!aURL.mRef.IsEmpty()) {
96
spec.Append(PRUnichar('#'));
97
spec.Append(aURL.mRef);
99
driver->mCompiler = new txStylesheetCompiler(spec, driver);
100
if (!driver->mCompiler) {
101
return NS_ERROR_OUT_OF_MEMORY;
103
nsresult rv = driver->parse(*xslInput, filePath);
107
*aResult = driver->mCompiler->getStylesheet();
113
* expat C stub handlers
116
// shortcut macro for redirection into txDriver method calls
117
#define TX_DRIVER(_userData) NS_STATIC_CAST(txDriver*, _userData)
119
PR_STATIC_CALLBACK(void)
120
startElement(void *aUserData, const XML_Char *aName, const XML_Char **aAtts)
123
NS_WARNING("no userData in startElement handler");
126
TX_DRIVER(aUserData)->StartElement(aName, aAtts);
129
PR_STATIC_CALLBACK(void)
130
endElement(void *aUserData, const XML_Char* aName)
133
NS_WARNING("no userData in endElement handler");
136
TX_DRIVER(aUserData)->EndElement(aName);
139
PR_STATIC_CALLBACK(void)
140
charData(void* aUserData, const XML_Char* aChars, int aLength)
143
NS_WARNING("no userData in charData handler");
146
TX_DRIVER(aUserData)->CharacterData(aChars, aLength);
149
PR_STATIC_CALLBACK(int)
150
externalEntityRefHandler(XML_Parser aParser,
151
const XML_Char *aContext,
152
const XML_Char *aBase,
153
const XML_Char *aSystemId,
154
const XML_Char *aPublicId)
156
// aParser is aUserData is the txDriver,
157
// we set that in txDriver::parse
158
NS_ENSURE_TRUE(aParser, XML_ERROR_NONE);
159
return TX_DRIVER(aParser)->ExternalEntityRef(aContext, aBase,
160
aSystemId, aPublicId);
165
* Parses the given input stream and returns a DOM Document.
166
* A NULL pointer will be returned if errors occurred
169
txDriver::parse(istream& aInputStream, const nsAString& aUri)
171
mErrorString.Truncate();
173
mErrorString.Append(NS_LITERAL_STRING("unable to parse xml: invalid or unopen stream encountered."));
174
return NS_ERROR_FAILURE;
176
mExpatParser = XML_ParserCreate(nsnull);
178
return NS_ERROR_OUT_OF_MEMORY;
181
XML_SetUserData(mExpatParser, this);
182
XML_SetElementHandler(mExpatParser, startElement, endElement);
183
XML_SetCharacterDataHandler(mExpatParser, charData);
185
XML_SetParamEntityParsing(mExpatParser, XML_PARAM_ENTITY_PARSING_ALWAYS);
187
XML_SetExternalEntityRefHandler(mExpatParser, externalEntityRefHandler);
188
XML_SetExternalEntityRefHandlerArg(mExpatParser, this);
189
XML_SetBase(mExpatParser,
190
(const XML_Char*)(PromiseFlatString(aUri).get()));
192
const int bufferSize = 1024;
193
char buf[bufferSize];
198
aInputStream.read(buf, bufferSize);
199
done = aInputStream.eof();
200
success = XML_Parse(mExpatParser, buf, aInputStream.gcount(), done);
201
// mRV is set in onDoneCompiling in case of an error
202
if (!success || NS_FAILED(mRV)) {
207
aInputStream.clear();
210
XML_ParserFree(mExpatParser);
211
mCompiler->doneLoading();
213
return NS_ERROR_FAILURE;
219
txDriver::getErrorString()
225
txDriver::StartElement(const XML_Char *aName, const XML_Char **aAtts)
227
PRInt32 attcount = 0;
228
const XML_Char** atts = aAtts;
233
PRInt32 idOffset = XML_GetIdAttributeIndex(mExpatParser);
235
mCompiler->startElement(NS_STATIC_CAST(const PRUnichar*, aName),
236
NS_STATIC_CAST(const PRUnichar**, aAtts),
237
attcount/2, idOffset);
239
PR_LOG(txLog::xslt, PR_LOG_ALWAYS,
240
("compile failed at %i with %x\n",
241
XML_GetCurrentLineNumber(mExpatParser), rv));
242
mCompiler->cancel(rv);
247
txDriver::EndElement(const XML_Char* aName)
249
nsresult rv = mCompiler->endElement();
251
mCompiler->cancel(rv);
256
txDriver::CharacterData(const XML_Char* aChars, int aLength)
258
const PRUnichar* pChars = NS_STATIC_CAST(const PRUnichar*, aChars);
259
// ignore rv, as this expat handler returns void
260
nsresult rv = mCompiler->characters(Substring(pChars, pChars + aLength));
262
mCompiler->cancel(rv);
267
txDriver::ExternalEntityRef(const XML_Char *aContext, const XML_Char *aBase,
268
const XML_Char *aSystemId,
269
const XML_Char *aPublicId)
272
// not supported, this is "http://some.site.net/foo.dtd" stuff
273
return XML_ERROR_EXTERNAL_ENTITY_HANDLING;
276
URIUtils::resolveHref(nsDependentString((PRUnichar*)aSystemId),
277
nsDependentString((PRUnichar*)aBase), absUrl);
278
istream* extInput = URIUtils::getInputStream(absUrl, mErrorString);
280
return XML_ERROR_EXTERNAL_ENTITY_HANDLING;
282
XML_Parser parent = mExpatParser;
284
XML_ExternalEntityParserCreate(mExpatParser, aContext, nsnull);
286
mExpatParser = parent;
288
return XML_ERROR_EXTERNAL_ENTITY_HANDLING;
290
XML_SetBase(mExpatParser, absUrl.get());
292
const int bufSize = 1024;
293
char buffer[bufSize];
297
extInput->read(buffer, bufSize);
298
done = extInput->eof();
300
XML_Parse(mExpatParser, buffer, extInput->gcount(), done))) {
302
mErrorString.Append(PRUnichar('\n'));
308
XML_ParserFree(mExpatParser);
310
mExpatParser = parent;
316
txDriver::createErrorString()
318
XML_Error errCode = XML_GetErrorCode(mExpatParser);
319
mErrorString.AppendWithConversion(XML_ErrorString(errCode));
320
mErrorString.Append(NS_LITERAL_STRING(" at line "));
321
mErrorString.AppendInt(XML_GetCurrentLineNumber(mExpatParser));
322
mErrorString.Append(NS_LITERAL_STRING(" in "));
323
mErrorString.Append((const PRUnichar*)XML_GetBase(mExpatParser));
327
* txACompileObserver implementation
339
if (--mRefCnt == 0) {
340
mRefCnt = 1; //stabilize
348
txDriver::onDoneCompiling(txStylesheetCompiler* aCompiler, nsresult aResult,
349
const PRUnichar *aErrorText, const PRUnichar *aParam)
351
// store the nsresult as expat forgets about it
356
txDriver::loadURI(const nsAString& aUri, txStylesheetCompiler* aCompiler)
359
istream* xslInput = URIUtils::getInputStream(aUri, errMsg);
361
return NS_ERROR_FAILURE;
363
nsRefPtr<txDriver> driver = new txDriver();
365
return NS_ERROR_OUT_OF_MEMORY;
367
driver->mCompiler = aCompiler;
368
return driver->parse(*xslInput, aUri);