~ubuntu-branches/ubuntu/precise/kompozer/precise

« back to all changes in this revision

Viewing changes to mozilla/extensions/transformiix/source/xslt/txStandaloneStylesheetCompiler.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Anthony Yarusso
  • Date: 2007-08-27 01:11:03 UTC
  • Revision ID: james.westby@ubuntu.com-20070827011103-2jgf4s6532gqu2ka
Tags: upstream-0.7.10
ImportĀ upstreamĀ versionĀ 0.7.10

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
4
 *
 
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/
 
9
 *
 
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
 
13
 * License.
 
14
 *
 
15
 * The Original Code is TransforMiiX XSLT Processor.
 
16
 *
 
17
 * The Initial Developer of the Original Code is
 
18
 * Axel Hecht.
 
19
 * Portions created by the Initial Developer are Copyright (C) 2003
 
20
 * the Initial Developer. All Rights Reserved.
 
21
 *
 
22
 * Contributor(s):
 
23
 *  Axel Hecht <axel@pike.org>
 
24
 *
 
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.
 
36
 *
 
37
 * ***** END LICENSE BLOCK ***** */
 
38
 
 
39
#include "txStandaloneStylesheetCompiler.h"
 
40
#include "txStylesheetCompiler.h"
 
41
#include "txURIUtils.h"
 
42
#include "xmlparse.h"
 
43
 
 
44
/**
 
45
 *  Implementation of an In-Memory DOM based XML parser.  The actual XML
 
46
 *  parsing is provided by EXPAT.
 
47
 */
 
48
class txDriver : public txACompileObserver
 
49
{
 
50
  public:
 
51
    nsresult parse(istream& aInputStream, const nsAString& aUri);
 
52
    const nsAString& getErrorString();
 
53
 
 
54
    /**
 
55
     * Expat handlers
 
56
     */
 
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);
 
63
 
 
64
    TX_DECL_ACOMPILEOBSERVER;
 
65
 
 
66
    nsRefPtr<txStylesheetCompiler> mCompiler;
 
67
  protected:
 
68
    void createErrorString();
 
69
    nsString  mErrorString;
 
70
    // keep track of the nsresult returned by the handlers, expat forgets them
 
71
    nsresult mRV;
 
72
    XML_Parser mExpatParser;
 
73
    nsAutoRefCnt mRefCnt;
 
74
};
 
75
 
 
76
nsresult
 
77
TX_CompileStylesheetPath(const txParsedURL& aURL, txStylesheet** aResult)
 
78
{
 
79
    *aResult = nsnull;
 
80
    nsAutoString errMsg, filePath;
 
81
 
 
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);
 
87
    if (!xslInput) {
 
88
        return NS_ERROR_FAILURE;
 
89
    }
 
90
    nsRefPtr<txDriver> driver = new txDriver();
 
91
    if (!driver) {
 
92
        return NS_ERROR_OUT_OF_MEMORY;
 
93
    }
 
94
    nsAutoString spec = filePath;
 
95
    if (!aURL.mRef.IsEmpty()) {
 
96
        spec.Append(PRUnichar('#'));
 
97
        spec.Append(aURL.mRef);
 
98
    }
 
99
    driver->mCompiler =  new txStylesheetCompiler(spec, driver);
 
100
    if (!driver->mCompiler) {
 
101
        return NS_ERROR_OUT_OF_MEMORY;
 
102
    }
 
103
    nsresult rv = driver->parse(*xslInput, filePath);
 
104
    if (NS_FAILED(rv)) {
 
105
        return rv;
 
106
    };
 
107
    *aResult = driver->mCompiler->getStylesheet();
 
108
    NS_ADDREF(*aResult);
 
109
    return NS_OK;
 
110
}
 
111
 
 
112
/**
 
113
 * expat C stub handlers
 
114
 */
 
115
 
 
116
// shortcut macro for redirection into txDriver method calls
 
117
#define TX_DRIVER(_userData) NS_STATIC_CAST(txDriver*, _userData)
 
118
 
 
119
PR_STATIC_CALLBACK(void)
 
120
startElement(void *aUserData, const XML_Char *aName, const XML_Char **aAtts)
 
121
{
 
122
    if (!aUserData) {
 
123
        NS_WARNING("no userData in startElement handler");
 
124
        return;
 
125
    }
 
126
    TX_DRIVER(aUserData)->StartElement(aName, aAtts);
 
127
}
 
128
 
 
129
PR_STATIC_CALLBACK(void)
 
130
endElement(void *aUserData, const XML_Char* aName)
 
131
{
 
132
    if (!aUserData) {
 
133
        NS_WARNING("no userData in endElement handler");
 
134
        return;
 
135
    }
 
136
    TX_DRIVER(aUserData)->EndElement(aName);
 
137
}
 
138
 
 
139
PR_STATIC_CALLBACK(void)
 
140
charData(void* aUserData, const XML_Char* aChars, int aLength)
 
141
{
 
142
    if (!aUserData) {
 
143
        NS_WARNING("no userData in charData handler");
 
144
        return;
 
145
    }
 
146
    TX_DRIVER(aUserData)->CharacterData(aChars, aLength);
 
147
}
 
148
 
 
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)
 
155
{
 
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);
 
161
}
 
162
 
 
163
 
 
164
/**
 
165
 *  Parses the given input stream and returns a DOM Document.
 
166
 *  A NULL pointer will be returned if errors occurred
 
167
 */
 
168
nsresult
 
169
txDriver::parse(istream& aInputStream, const nsAString& aUri)
 
170
{
 
171
    mErrorString.Truncate();
 
172
    if (!aInputStream) {
 
173
        mErrorString.Append(NS_LITERAL_STRING("unable to parse xml: invalid or unopen stream encountered."));
 
174
        return NS_ERROR_FAILURE;
 
175
    }
 
176
    mExpatParser = XML_ParserCreate(nsnull);
 
177
    if (!mExpatParser) {
 
178
        return NS_ERROR_OUT_OF_MEMORY;
 
179
    }
 
180
 
 
181
    XML_SetUserData(mExpatParser, this);
 
182
    XML_SetElementHandler(mExpatParser, startElement, endElement);
 
183
    XML_SetCharacterDataHandler(mExpatParser, charData);
 
184
#ifdef XML_DTD
 
185
    XML_SetParamEntityParsing(mExpatParser, XML_PARAM_ENTITY_PARSING_ALWAYS);
 
186
#endif
 
187
    XML_SetExternalEntityRefHandler(mExpatParser, externalEntityRefHandler);
 
188
    XML_SetExternalEntityRefHandlerArg(mExpatParser, this);
 
189
    XML_SetBase(mExpatParser,
 
190
                (const XML_Char*)(PromiseFlatString(aUri).get()));
 
191
 
 
192
    const int bufferSize = 1024;
 
193
    char buf[bufferSize];
 
194
    PRBool done;
 
195
    int success;
 
196
    mRV = NS_OK;
 
197
    do {
 
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)) {
 
203
            createErrorString();
 
204
            done = MB_TRUE;
 
205
        }
 
206
    } while (!done);
 
207
    aInputStream.clear();
 
208
 
 
209
    // clean up
 
210
    XML_ParserFree(mExpatParser);
 
211
    mCompiler->doneLoading();
 
212
    if (!success) {
 
213
        return NS_ERROR_FAILURE;
 
214
    }
 
215
    return mRV;
 
216
}
 
217
 
 
218
const nsAString&
 
219
txDriver::getErrorString()
 
220
{
 
221
    return mErrorString;
 
222
}
 
223
 
 
224
void
 
225
txDriver::StartElement(const XML_Char *aName, const XML_Char **aAtts)
 
226
{
 
227
    PRInt32 attcount = 0;
 
228
    const XML_Char** atts = aAtts;
 
229
    while (*atts) {
 
230
        ++atts;
 
231
        ++attcount;
 
232
    }
 
233
    PRInt32 idOffset = XML_GetIdAttributeIndex(mExpatParser);
 
234
    nsresult rv =
 
235
        mCompiler->startElement(NS_STATIC_CAST(const PRUnichar*, aName), 
 
236
                                NS_STATIC_CAST(const PRUnichar**, aAtts),
 
237
                                attcount/2, idOffset);
 
238
    if (NS_FAILED(rv)) {
 
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);
 
243
    }
 
244
}
 
245
 
 
246
void
 
247
txDriver::EndElement(const XML_Char* aName)
 
248
{
 
249
    nsresult rv = mCompiler->endElement();
 
250
    if (NS_FAILED(rv)) {
 
251
        mCompiler->cancel(rv);
 
252
    }
 
253
}
 
254
 
 
255
void
 
256
txDriver::CharacterData(const XML_Char* aChars, int aLength)
 
257
{
 
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));
 
261
    if (NS_FAILED(rv)) {
 
262
        mCompiler->cancel(rv);
 
263
    }
 
264
}
 
265
 
 
266
int
 
267
txDriver::ExternalEntityRef(const XML_Char *aContext, const XML_Char *aBase,
 
268
                            const XML_Char *aSystemId,
 
269
                            const XML_Char *aPublicId)
 
270
{
 
271
    if (aPublicId) {
 
272
        // not supported, this is "http://some.site.net/foo.dtd" stuff
 
273
        return XML_ERROR_EXTERNAL_ENTITY_HANDLING;
 
274
    }
 
275
    nsAutoString absUrl;
 
276
    URIUtils::resolveHref(nsDependentString((PRUnichar*)aSystemId),
 
277
                          nsDependentString((PRUnichar*)aBase), absUrl);
 
278
    istream* extInput = URIUtils::getInputStream(absUrl, mErrorString);
 
279
    if (!extInput) {
 
280
        return XML_ERROR_EXTERNAL_ENTITY_HANDLING;
 
281
    }
 
282
    XML_Parser parent = mExpatParser;
 
283
    mExpatParser = 
 
284
        XML_ExternalEntityParserCreate(mExpatParser, aContext, nsnull);
 
285
    if (!mExpatParser) {
 
286
        mExpatParser = parent;
 
287
        delete extInput;
 
288
        return XML_ERROR_EXTERNAL_ENTITY_HANDLING;
 
289
    }
 
290
    XML_SetBase(mExpatParser, absUrl.get());
 
291
 
 
292
    const int bufSize = 1024;
 
293
    char buffer[bufSize];
 
294
    int result;
 
295
    PRBool done;
 
296
    do {
 
297
        extInput->read(buffer, bufSize);
 
298
        done = extInput->eof();
 
299
        if (!(result =
 
300
              XML_Parse(mExpatParser, buffer,  extInput->gcount(), done))) {
 
301
            createErrorString();
 
302
            mErrorString.Append(PRUnichar('\n'));
 
303
            done = MB_TRUE;
 
304
        }
 
305
    } while (!done);
 
306
 
 
307
    delete extInput;
 
308
    XML_ParserFree(mExpatParser);
 
309
 
 
310
    mExpatParser = parent;
 
311
 
 
312
    return result;
 
313
}
 
314
 
 
315
void
 
316
txDriver::createErrorString()
 
317
{
 
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));
 
324
}
 
325
 
 
326
/**
 
327
 * txACompileObserver implementation
 
328
 */
 
329
 
 
330
nsrefcnt
 
331
txDriver::AddRef()
 
332
{
 
333
    return ++mRefCnt;
 
334
}
 
335
 
 
336
nsrefcnt
 
337
txDriver::Release()
 
338
{
 
339
    if (--mRefCnt == 0) {
 
340
        mRefCnt = 1; //stabilize
 
341
        delete this;
 
342
        return 0;
 
343
    }
 
344
    return mRefCnt;
 
345
}
 
346
 
 
347
void
 
348
txDriver::onDoneCompiling(txStylesheetCompiler* aCompiler, nsresult aResult,
 
349
                          const PRUnichar *aErrorText, const PRUnichar *aParam)
 
350
{
 
351
    // store the nsresult as expat forgets about it
 
352
    mRV = aResult;
 
353
}
 
354
 
 
355
nsresult
 
356
txDriver::loadURI(const nsAString& aUri, txStylesheetCompiler* aCompiler)
 
357
{
 
358
    nsAutoString errMsg;
 
359
    istream* xslInput = URIUtils::getInputStream(aUri, errMsg);
 
360
    if (!xslInput) {
 
361
        return NS_ERROR_FAILURE;
 
362
    }
 
363
    nsRefPtr<txDriver> driver = new txDriver();
 
364
    if (!driver) {
 
365
        return NS_ERROR_OUT_OF_MEMORY;
 
366
    }
 
367
    driver->mCompiler = aCompiler;
 
368
    return driver->parse(*xslInput, aUri);
 
369
}