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 code.
16 : *
17 : * The Initial Developer of the Original Code is
18 : * Jonas Sicking.
19 : * Portions created by the Initial Developer are Copyright (C) 2003
20 : * the Initial Developer. All Rights Reserved.
21 : *
22 : * Contributor(s):
23 : * Jonas Sicking <jonas@sicking.cc>
24 : * Peter Van der Beken <peterv@propagandism.org>
25 : *
26 : * Alternatively, the contents of this file may be used under the terms of
27 : * either the GNU General Public License Version 2 or later (the "GPL"), or
28 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 : * in which case the provisions of the GPL or the LGPL are applicable instead
30 : * of those above. If you wish to allow use of your version of this file only
31 : * under the terms of either the GPL or the LGPL, and not to allow others to
32 : * use your version of this file under the terms of the MPL, indicate your
33 : * decision by deleting the provisions above and replace them with the notice
34 : * and other provisions required by the GPL or the LGPL. If you do not delete
35 : * the provisions above, a recipient may use your version of this file under
36 : * the terms of any one of the MPL, the GPL or the LGPL.
37 : *
38 : * ***** END LICENSE BLOCK ***** */
39 :
40 : #include "txBufferingHandler.h"
41 :
42 : class txOutputTransaction
43 : {
44 : public:
45 : enum txTransactionType {
46 : eAttributeTransaction,
47 : eAttributeAtomTransaction,
48 : eCharacterTransaction,
49 : eCharacterNoOETransaction,
50 : eCommentTransaction,
51 : eEndDocumentTransaction,
52 : eEndElementTransaction,
53 : ePITransaction,
54 : eStartDocumentTransaction,
55 : eStartElementAtomTransaction,
56 : eStartElementTransaction
57 : };
58 0 : txOutputTransaction(txTransactionType aType)
59 0 : : mType(aType)
60 : {
61 0 : MOZ_COUNT_CTOR(txOutputTransaction);
62 0 : }
63 0 : virtual ~txOutputTransaction()
64 0 : {
65 0 : MOZ_COUNT_DTOR(txOutputTransaction);
66 0 : }
67 : txTransactionType mType;
68 : };
69 :
70 : class txCharacterTransaction : public txOutputTransaction
71 : {
72 : public:
73 0 : txCharacterTransaction(txTransactionType aType, PRUint32 aLength)
74 : : txOutputTransaction(aType),
75 0 : mLength(aLength)
76 : {
77 0 : MOZ_COUNT_CTOR_INHERITED(txCharacterTransaction, txOutputTransaction);
78 0 : }
79 0 : virtual ~txCharacterTransaction()
80 0 : {
81 0 : MOZ_COUNT_DTOR_INHERITED(txCharacterTransaction, txOutputTransaction);
82 0 : }
83 : PRUint32 mLength;
84 : };
85 :
86 : class txCommentTransaction : public txOutputTransaction
87 : {
88 : public:
89 0 : txCommentTransaction(const nsAString& aValue)
90 : : txOutputTransaction(eCommentTransaction),
91 0 : mValue(aValue)
92 : {
93 0 : MOZ_COUNT_CTOR_INHERITED(txCommentTransaction, txOutputTransaction);
94 0 : }
95 0 : virtual ~txCommentTransaction()
96 0 : {
97 0 : MOZ_COUNT_DTOR_INHERITED(txCommentTransaction, txOutputTransaction);
98 0 : }
99 : nsString mValue;
100 : };
101 :
102 : class txPITransaction : public txOutputTransaction
103 : {
104 : public:
105 0 : txPITransaction(const nsAString& aTarget, const nsAString& aData)
106 : : txOutputTransaction(ePITransaction),
107 : mTarget(aTarget),
108 0 : mData(aData)
109 : {
110 0 : MOZ_COUNT_CTOR_INHERITED(txPITransaction, txOutputTransaction);
111 0 : }
112 0 : virtual ~txPITransaction()
113 0 : {
114 0 : MOZ_COUNT_DTOR_INHERITED(txPITransaction, txOutputTransaction);
115 0 : }
116 : nsString mTarget;
117 : nsString mData;
118 : };
119 :
120 : class txStartElementAtomTransaction : public txOutputTransaction
121 : {
122 : public:
123 0 : txStartElementAtomTransaction(nsIAtom* aPrefix, nsIAtom* aLocalName,
124 : nsIAtom* aLowercaseLocalName, PRInt32 aNsID)
125 : : txOutputTransaction(eStartElementAtomTransaction),
126 : mPrefix(aPrefix),
127 : mLocalName(aLocalName),
128 : mLowercaseLocalName(aLowercaseLocalName),
129 0 : mNsID(aNsID)
130 : {
131 0 : MOZ_COUNT_CTOR_INHERITED(txStartElementAtomTransaction, txOutputTransaction);
132 0 : }
133 0 : virtual ~txStartElementAtomTransaction()
134 0 : {
135 0 : MOZ_COUNT_DTOR_INHERITED(txStartElementAtomTransaction, txOutputTransaction);
136 0 : }
137 : nsCOMPtr<nsIAtom> mPrefix;
138 : nsCOMPtr<nsIAtom> mLocalName;
139 : nsCOMPtr<nsIAtom> mLowercaseLocalName;
140 : PRInt32 mNsID;
141 : };
142 :
143 : class txStartElementTransaction : public txOutputTransaction
144 : {
145 : public:
146 0 : txStartElementTransaction(nsIAtom* aPrefix,
147 : const nsSubstring& aLocalName, PRInt32 aNsID)
148 : : txOutputTransaction(eStartElementTransaction),
149 : mPrefix(aPrefix),
150 : mLocalName(aLocalName),
151 0 : mNsID(aNsID)
152 : {
153 0 : MOZ_COUNT_CTOR_INHERITED(txStartElementTransaction, txOutputTransaction);
154 0 : }
155 0 : virtual ~txStartElementTransaction()
156 0 : {
157 0 : MOZ_COUNT_DTOR_INHERITED(txStartElementTransaction, txOutputTransaction);
158 0 : }
159 : nsCOMPtr<nsIAtom> mPrefix;
160 : nsString mLocalName;
161 : PRInt32 mNsID;
162 : };
163 :
164 : class txAttributeTransaction : public txOutputTransaction
165 : {
166 : public:
167 0 : txAttributeTransaction(nsIAtom* aPrefix,
168 : const nsSubstring& aLocalName, PRInt32 aNsID,
169 : const nsString& aValue)
170 : : txOutputTransaction(eAttributeTransaction),
171 : mPrefix(aPrefix),
172 : mLocalName(aLocalName),
173 : mNsID(aNsID),
174 0 : mValue(aValue)
175 : {
176 0 : MOZ_COUNT_CTOR_INHERITED(txAttributeTransaction, txOutputTransaction);
177 0 : }
178 0 : virtual ~txAttributeTransaction()
179 0 : {
180 0 : MOZ_COUNT_DTOR_INHERITED(txAttributeTransaction, txOutputTransaction);
181 0 : }
182 : nsCOMPtr<nsIAtom> mPrefix;
183 : nsString mLocalName;
184 : PRInt32 mNsID;
185 : nsString mValue;
186 : };
187 :
188 : class txAttributeAtomTransaction : public txOutputTransaction
189 : {
190 : public:
191 0 : txAttributeAtomTransaction(nsIAtom* aPrefix, nsIAtom* aLocalName,
192 : nsIAtom* aLowercaseLocalName,
193 : PRInt32 aNsID, const nsString& aValue)
194 : : txOutputTransaction(eAttributeAtomTransaction),
195 : mPrefix(aPrefix),
196 : mLocalName(aLocalName),
197 : mLowercaseLocalName(aLowercaseLocalName),
198 : mNsID(aNsID),
199 0 : mValue(aValue)
200 : {
201 0 : MOZ_COUNT_CTOR_INHERITED(txAttributeAtomTransaction, txOutputTransaction);
202 0 : }
203 0 : virtual ~txAttributeAtomTransaction()
204 0 : {
205 0 : MOZ_COUNT_DTOR_INHERITED(txAttributeAtomTransaction, txOutputTransaction);
206 0 : }
207 : nsCOMPtr<nsIAtom> mPrefix;
208 : nsCOMPtr<nsIAtom> mLocalName;
209 : nsCOMPtr<nsIAtom> mLowercaseLocalName;
210 : PRInt32 mNsID;
211 : nsString mValue;
212 : };
213 :
214 0 : txBufferingHandler::txBufferingHandler() : mCanAddAttribute(false)
215 : {
216 0 : MOZ_COUNT_CTOR(txBufferingHandler);
217 0 : mBuffer = new txResultBuffer();
218 0 : }
219 :
220 0 : txBufferingHandler::~txBufferingHandler()
221 : {
222 0 : MOZ_COUNT_DTOR(txBufferingHandler);
223 0 : }
224 :
225 : nsresult
226 0 : txBufferingHandler::attribute(nsIAtom* aPrefix, nsIAtom* aLocalName,
227 : nsIAtom* aLowercaseLocalName, PRInt32 aNsID,
228 : const nsString& aValue)
229 : {
230 0 : NS_ENSURE_TRUE(mBuffer, NS_ERROR_OUT_OF_MEMORY);
231 :
232 0 : if (!mCanAddAttribute) {
233 : // XXX ErrorReport: Can't add attributes without element
234 0 : return NS_OK;
235 : }
236 :
237 : txOutputTransaction* transaction =
238 : new txAttributeAtomTransaction(aPrefix, aLocalName,
239 : aLowercaseLocalName, aNsID,
240 0 : aValue);
241 0 : NS_ENSURE_TRUE(transaction, NS_ERROR_OUT_OF_MEMORY);
242 :
243 0 : return mBuffer->addTransaction(transaction);
244 : }
245 :
246 : nsresult
247 0 : txBufferingHandler::attribute(nsIAtom* aPrefix, const nsSubstring& aLocalName,
248 : const PRInt32 aNsID, const nsString& aValue)
249 : {
250 0 : NS_ENSURE_TRUE(mBuffer, NS_ERROR_OUT_OF_MEMORY);
251 :
252 0 : if (!mCanAddAttribute) {
253 : // XXX ErrorReport: Can't add attributes without element
254 0 : return NS_OK;
255 : }
256 :
257 : txOutputTransaction* transaction =
258 0 : new txAttributeTransaction(aPrefix, aLocalName, aNsID, aValue);
259 0 : NS_ENSURE_TRUE(transaction, NS_ERROR_OUT_OF_MEMORY);
260 :
261 0 : return mBuffer->addTransaction(transaction);
262 : }
263 :
264 : nsresult
265 0 : txBufferingHandler::characters(const nsSubstring& aData, bool aDOE)
266 : {
267 0 : NS_ENSURE_TRUE(mBuffer, NS_ERROR_OUT_OF_MEMORY);
268 :
269 0 : mCanAddAttribute = false;
270 :
271 : txOutputTransaction::txTransactionType type =
272 : aDOE ? txOutputTransaction::eCharacterNoOETransaction
273 0 : : txOutputTransaction::eCharacterTransaction;
274 :
275 0 : txOutputTransaction* transaction = mBuffer->getLastTransaction();
276 0 : if (transaction && transaction->mType == type) {
277 0 : mBuffer->mStringValue.Append(aData);
278 : static_cast<txCharacterTransaction*>(transaction)->mLength +=
279 0 : aData.Length();
280 0 : return NS_OK;
281 : }
282 :
283 0 : transaction = new txCharacterTransaction(type, aData.Length());
284 0 : NS_ENSURE_TRUE(transaction, NS_ERROR_OUT_OF_MEMORY);
285 :
286 0 : mBuffer->mStringValue.Append(aData);
287 0 : return mBuffer->addTransaction(transaction);
288 : }
289 :
290 : nsresult
291 0 : txBufferingHandler::comment(const nsString& aData)
292 : {
293 0 : NS_ENSURE_TRUE(mBuffer, NS_ERROR_OUT_OF_MEMORY);
294 :
295 0 : mCanAddAttribute = false;
296 :
297 0 : txOutputTransaction* transaction = new txCommentTransaction(aData);
298 0 : NS_ENSURE_TRUE(transaction, NS_ERROR_OUT_OF_MEMORY);
299 :
300 0 : return mBuffer->addTransaction(transaction);
301 : }
302 :
303 : nsresult
304 0 : txBufferingHandler::endDocument(nsresult aResult)
305 : {
306 0 : NS_ENSURE_TRUE(mBuffer, NS_ERROR_OUT_OF_MEMORY);
307 :
308 : txOutputTransaction* transaction =
309 0 : new txOutputTransaction(txOutputTransaction::eEndDocumentTransaction);
310 0 : NS_ENSURE_TRUE(transaction, NS_ERROR_OUT_OF_MEMORY);
311 :
312 0 : return mBuffer->addTransaction(transaction);
313 : }
314 :
315 : nsresult
316 0 : txBufferingHandler::endElement()
317 : {
318 0 : NS_ENSURE_TRUE(mBuffer, NS_ERROR_OUT_OF_MEMORY);
319 :
320 0 : mCanAddAttribute = false;
321 :
322 : txOutputTransaction* transaction =
323 0 : new txOutputTransaction(txOutputTransaction::eEndElementTransaction);
324 0 : NS_ENSURE_TRUE(transaction, NS_ERROR_OUT_OF_MEMORY);
325 :
326 0 : return mBuffer->addTransaction(transaction);
327 : }
328 :
329 : nsresult
330 0 : txBufferingHandler::processingInstruction(const nsString& aTarget,
331 : const nsString& aData)
332 : {
333 0 : NS_ENSURE_TRUE(mBuffer, NS_ERROR_OUT_OF_MEMORY);
334 :
335 0 : mCanAddAttribute = false;
336 :
337 : txOutputTransaction* transaction =
338 0 : new txPITransaction(aTarget, aData);
339 0 : NS_ENSURE_TRUE(transaction, NS_ERROR_OUT_OF_MEMORY);
340 :
341 0 : return mBuffer->addTransaction(transaction);
342 : }
343 :
344 : nsresult
345 0 : txBufferingHandler::startDocument()
346 : {
347 0 : NS_ENSURE_TRUE(mBuffer, NS_ERROR_OUT_OF_MEMORY);
348 :
349 : txOutputTransaction* transaction =
350 0 : new txOutputTransaction(txOutputTransaction::eStartDocumentTransaction);
351 0 : NS_ENSURE_TRUE(transaction, NS_ERROR_OUT_OF_MEMORY);
352 :
353 0 : return mBuffer->addTransaction(transaction);
354 : }
355 :
356 : nsresult
357 0 : txBufferingHandler::startElement(nsIAtom* aPrefix, nsIAtom* aLocalName,
358 : nsIAtom* aLowercaseLocalName,
359 : PRInt32 aNsID)
360 : {
361 0 : NS_ENSURE_TRUE(mBuffer, NS_ERROR_OUT_OF_MEMORY);
362 :
363 0 : mCanAddAttribute = true;
364 :
365 : txOutputTransaction* transaction =
366 : new txStartElementAtomTransaction(aPrefix, aLocalName,
367 0 : aLowercaseLocalName, aNsID);
368 0 : NS_ENSURE_TRUE(transaction, NS_ERROR_OUT_OF_MEMORY);
369 :
370 0 : return mBuffer->addTransaction(transaction);
371 : }
372 :
373 : nsresult
374 0 : txBufferingHandler::startElement(nsIAtom* aPrefix,
375 : const nsSubstring& aLocalName,
376 : const PRInt32 aNsID)
377 : {
378 0 : NS_ENSURE_TRUE(mBuffer, NS_ERROR_OUT_OF_MEMORY);
379 :
380 0 : mCanAddAttribute = true;
381 :
382 : txOutputTransaction* transaction =
383 0 : new txStartElementTransaction(aPrefix, aLocalName, aNsID);
384 0 : NS_ENSURE_TRUE(transaction, NS_ERROR_OUT_OF_MEMORY);
385 :
386 0 : return mBuffer->addTransaction(transaction);
387 : }
388 :
389 0 : txResultBuffer::txResultBuffer()
390 : {
391 0 : MOZ_COUNT_CTOR(txResultBuffer);
392 0 : }
393 :
394 0 : txResultBuffer::~txResultBuffer()
395 : {
396 0 : MOZ_COUNT_DTOR(txResultBuffer);
397 0 : for (PRUint32 i = 0, len = mTransactions.Length(); i < len; ++i) {
398 0 : delete mTransactions[i];
399 : }
400 0 : }
401 :
402 : nsresult
403 0 : txResultBuffer::addTransaction(txOutputTransaction* aTransaction)
404 : {
405 0 : if (mTransactions.AppendElement(aTransaction) == nsnull) {
406 0 : return NS_ERROR_OUT_OF_MEMORY;
407 : }
408 0 : return NS_OK;
409 : }
410 :
411 : static nsresult
412 0 : flushTransaction(txOutputTransaction* aTransaction,
413 : txAXMLEventHandler* aHandler,
414 : nsAFlatString::const_char_iterator& aIter)
415 : {
416 0 : switch (aTransaction->mType) {
417 : case txOutputTransaction::eAttributeAtomTransaction:
418 : {
419 : txAttributeAtomTransaction* transaction =
420 0 : static_cast<txAttributeAtomTransaction*>(aTransaction);
421 : return aHandler->attribute(transaction->mPrefix,
422 : transaction->mLocalName,
423 : transaction->mLowercaseLocalName,
424 : transaction->mNsID,
425 0 : transaction->mValue);
426 : }
427 : case txOutputTransaction::eAttributeTransaction:
428 : {
429 : txAttributeTransaction* attrTransaction =
430 0 : static_cast<txAttributeTransaction*>(aTransaction);
431 : return aHandler->attribute(attrTransaction->mPrefix,
432 : attrTransaction->mLocalName,
433 : attrTransaction->mNsID,
434 0 : attrTransaction->mValue);
435 : }
436 : case txOutputTransaction::eCharacterTransaction:
437 : case txOutputTransaction::eCharacterNoOETransaction:
438 : {
439 : txCharacterTransaction* charTransaction =
440 0 : static_cast<txCharacterTransaction*>(aTransaction);
441 0 : nsAFlatString::const_char_iterator start = aIter;
442 : nsAFlatString::const_char_iterator end =
443 0 : start + charTransaction->mLength;
444 0 : aIter = end;
445 0 : return aHandler->characters(Substring(start, end),
446 : aTransaction->mType ==
447 0 : txOutputTransaction::eCharacterNoOETransaction);
448 : }
449 : case txOutputTransaction::eCommentTransaction:
450 : {
451 : txCommentTransaction* commentTransaction =
452 0 : static_cast<txCommentTransaction*>(aTransaction);
453 0 : return aHandler->comment(commentTransaction->mValue);
454 : }
455 : case txOutputTransaction::eEndElementTransaction:
456 : {
457 0 : return aHandler->endElement();
458 : }
459 : case txOutputTransaction::ePITransaction:
460 : {
461 : txPITransaction* piTransaction =
462 0 : static_cast<txPITransaction*>(aTransaction);
463 : return aHandler->processingInstruction(piTransaction->mTarget,
464 0 : piTransaction->mData);
465 : }
466 : case txOutputTransaction::eStartDocumentTransaction:
467 : {
468 0 : return aHandler->startDocument();
469 : }
470 : case txOutputTransaction::eStartElementAtomTransaction:
471 : {
472 : txStartElementAtomTransaction* transaction =
473 0 : static_cast<txStartElementAtomTransaction*>(aTransaction);
474 : return aHandler->startElement(transaction->mPrefix,
475 : transaction->mLocalName,
476 : transaction->mLowercaseLocalName,
477 0 : transaction->mNsID);
478 : }
479 : case txOutputTransaction::eStartElementTransaction:
480 : {
481 : txStartElementTransaction* transaction =
482 0 : static_cast<txStartElementTransaction*>(aTransaction);
483 : return aHandler->startElement(transaction->mPrefix,
484 : transaction->mLocalName,
485 0 : transaction->mNsID);
486 : }
487 : default:
488 : {
489 0 : NS_NOTREACHED("Unexpected transaction type");
490 : }
491 : }
492 :
493 0 : return NS_ERROR_UNEXPECTED;
494 : }
495 :
496 : nsresult
497 0 : txResultBuffer::flushToHandler(txAXMLEventHandler* aHandler)
498 : {
499 : nsAFlatString::const_char_iterator iter;
500 0 : mStringValue.BeginReading(iter);
501 :
502 0 : for (PRUint32 i = 0, len = mTransactions.Length(); i < len; ++i) {
503 0 : nsresult rv = flushTransaction(mTransactions[i], aHandler, iter);
504 0 : NS_ENSURE_SUCCESS(rv, rv);
505 : }
506 :
507 0 : return NS_OK;
508 : }
509 :
510 : txOutputTransaction*
511 0 : txResultBuffer::getLastTransaction()
512 : {
513 0 : PRInt32 last = mTransactions.Length() - 1;
514 0 : if (last < 0) {
515 0 : return nsnull;
516 : }
517 0 : return mTransactions[last];
518 : }
|