Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /* This Source Code Form is subject to the terms of the Mozilla Public
3 : * License, v. 2.0. If a copy of the MPL was not distributed with this
4 : * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 :
6 : #include "txBufferingHandler.h"
7 :
8 : class txOutputTransaction
9 : {
10 : public:
11 : enum txTransactionType {
12 : eAttributeTransaction,
13 : eAttributeAtomTransaction,
14 : eCharacterTransaction,
15 : eCharacterNoOETransaction,
16 : eCommentTransaction,
17 : eEndDocumentTransaction,
18 : eEndElementTransaction,
19 : ePITransaction,
20 : eStartDocumentTransaction,
21 : eStartElementAtomTransaction,
22 : eStartElementTransaction
23 : };
24 0 : explicit txOutputTransaction(txTransactionType aType)
25 0 : : mType(aType)
26 : {
27 0 : MOZ_COUNT_CTOR(txOutputTransaction);
28 0 : }
29 0 : virtual ~txOutputTransaction()
30 0 : {
31 0 : MOZ_COUNT_DTOR(txOutputTransaction);
32 0 : }
33 : txTransactionType mType;
34 : };
35 :
36 : class txCharacterTransaction : public txOutputTransaction
37 : {
38 : public:
39 0 : txCharacterTransaction(txTransactionType aType, uint32_t aLength)
40 0 : : txOutputTransaction(aType),
41 0 : mLength(aLength)
42 : {
43 0 : MOZ_COUNT_CTOR_INHERITED(txCharacterTransaction, txOutputTransaction);
44 0 : }
45 0 : virtual ~txCharacterTransaction()
46 0 : {
47 0 : MOZ_COUNT_DTOR_INHERITED(txCharacterTransaction, txOutputTransaction);
48 0 : }
49 : uint32_t mLength;
50 : };
51 :
52 : class txCommentTransaction : public txOutputTransaction
53 : {
54 : public:
55 0 : explicit txCommentTransaction(const nsAString& aValue)
56 0 : : txOutputTransaction(eCommentTransaction),
57 0 : mValue(aValue)
58 : {
59 0 : MOZ_COUNT_CTOR_INHERITED(txCommentTransaction, txOutputTransaction);
60 0 : }
61 0 : virtual ~txCommentTransaction()
62 0 : {
63 0 : MOZ_COUNT_DTOR_INHERITED(txCommentTransaction, txOutputTransaction);
64 0 : }
65 : nsString mValue;
66 : };
67 :
68 : class txPITransaction : public txOutputTransaction
69 : {
70 : public:
71 0 : txPITransaction(const nsAString& aTarget, const nsAString& aData)
72 0 : : txOutputTransaction(ePITransaction),
73 : mTarget(aTarget),
74 0 : mData(aData)
75 : {
76 0 : MOZ_COUNT_CTOR_INHERITED(txPITransaction, txOutputTransaction);
77 0 : }
78 0 : virtual ~txPITransaction()
79 0 : {
80 0 : MOZ_COUNT_DTOR_INHERITED(txPITransaction, txOutputTransaction);
81 0 : }
82 : nsString mTarget;
83 : nsString mData;
84 : };
85 :
86 : class txStartElementAtomTransaction : public txOutputTransaction
87 : {
88 : public:
89 0 : txStartElementAtomTransaction(nsIAtom* aPrefix, nsIAtom* aLocalName,
90 : nsIAtom* aLowercaseLocalName, int32_t aNsID)
91 0 : : txOutputTransaction(eStartElementAtomTransaction),
92 : mPrefix(aPrefix),
93 : mLocalName(aLocalName),
94 : mLowercaseLocalName(aLowercaseLocalName),
95 0 : mNsID(aNsID)
96 : {
97 0 : MOZ_COUNT_CTOR_INHERITED(txStartElementAtomTransaction, txOutputTransaction);
98 0 : }
99 0 : virtual ~txStartElementAtomTransaction()
100 0 : {
101 0 : MOZ_COUNT_DTOR_INHERITED(txStartElementAtomTransaction, txOutputTransaction);
102 0 : }
103 : nsCOMPtr<nsIAtom> mPrefix;
104 : nsCOMPtr<nsIAtom> mLocalName;
105 : nsCOMPtr<nsIAtom> mLowercaseLocalName;
106 : int32_t mNsID;
107 : };
108 :
109 : class txStartElementTransaction : public txOutputTransaction
110 : {
111 : public:
112 0 : txStartElementTransaction(nsIAtom* aPrefix,
113 : const nsAString& aLocalName, int32_t aNsID)
114 0 : : txOutputTransaction(eStartElementTransaction),
115 : mPrefix(aPrefix),
116 : mLocalName(aLocalName),
117 0 : mNsID(aNsID)
118 : {
119 0 : MOZ_COUNT_CTOR_INHERITED(txStartElementTransaction, txOutputTransaction);
120 0 : }
121 0 : virtual ~txStartElementTransaction()
122 0 : {
123 0 : MOZ_COUNT_DTOR_INHERITED(txStartElementTransaction, txOutputTransaction);
124 0 : }
125 : nsCOMPtr<nsIAtom> mPrefix;
126 : nsString mLocalName;
127 : int32_t mNsID;
128 : };
129 :
130 : class txAttributeTransaction : public txOutputTransaction
131 : {
132 : public:
133 0 : txAttributeTransaction(nsIAtom* aPrefix,
134 : const nsAString& aLocalName, int32_t aNsID,
135 : const nsString& aValue)
136 0 : : txOutputTransaction(eAttributeTransaction),
137 : mPrefix(aPrefix),
138 : mLocalName(aLocalName),
139 : mNsID(aNsID),
140 0 : mValue(aValue)
141 : {
142 0 : MOZ_COUNT_CTOR_INHERITED(txAttributeTransaction, txOutputTransaction);
143 0 : }
144 0 : virtual ~txAttributeTransaction()
145 0 : {
146 0 : MOZ_COUNT_DTOR_INHERITED(txAttributeTransaction, txOutputTransaction);
147 0 : }
148 : nsCOMPtr<nsIAtom> mPrefix;
149 : nsString mLocalName;
150 : int32_t mNsID;
151 : nsString mValue;
152 : };
153 :
154 : class txAttributeAtomTransaction : public txOutputTransaction
155 : {
156 : public:
157 0 : txAttributeAtomTransaction(nsIAtom* aPrefix, nsIAtom* aLocalName,
158 : nsIAtom* aLowercaseLocalName,
159 : int32_t aNsID, const nsString& aValue)
160 0 : : txOutputTransaction(eAttributeAtomTransaction),
161 : mPrefix(aPrefix),
162 : mLocalName(aLocalName),
163 : mLowercaseLocalName(aLowercaseLocalName),
164 : mNsID(aNsID),
165 0 : mValue(aValue)
166 : {
167 0 : MOZ_COUNT_CTOR_INHERITED(txAttributeAtomTransaction, txOutputTransaction);
168 0 : }
169 0 : virtual ~txAttributeAtomTransaction()
170 0 : {
171 0 : MOZ_COUNT_DTOR_INHERITED(txAttributeAtomTransaction, txOutputTransaction);
172 0 : }
173 : nsCOMPtr<nsIAtom> mPrefix;
174 : nsCOMPtr<nsIAtom> mLocalName;
175 : nsCOMPtr<nsIAtom> mLowercaseLocalName;
176 : int32_t mNsID;
177 : nsString mValue;
178 : };
179 :
180 0 : txBufferingHandler::txBufferingHandler() : mCanAddAttribute(false)
181 : {
182 0 : MOZ_COUNT_CTOR(txBufferingHandler);
183 0 : mBuffer = new txResultBuffer();
184 0 : }
185 :
186 0 : txBufferingHandler::~txBufferingHandler()
187 : {
188 0 : MOZ_COUNT_DTOR(txBufferingHandler);
189 0 : }
190 :
191 : nsresult
192 0 : txBufferingHandler::attribute(nsIAtom* aPrefix, nsIAtom* aLocalName,
193 : nsIAtom* aLowercaseLocalName, int32_t aNsID,
194 : const nsString& aValue)
195 : {
196 0 : NS_ENSURE_TRUE(mBuffer, NS_ERROR_OUT_OF_MEMORY);
197 :
198 0 : if (!mCanAddAttribute) {
199 : // XXX ErrorReport: Can't add attributes without element
200 0 : return NS_OK;
201 : }
202 :
203 : txOutputTransaction* transaction =
204 : new txAttributeAtomTransaction(aPrefix, aLocalName,
205 : aLowercaseLocalName, aNsID,
206 0 : aValue);
207 0 : return mBuffer->addTransaction(transaction);
208 : }
209 :
210 : nsresult
211 0 : txBufferingHandler::attribute(nsIAtom* aPrefix, const nsAString& aLocalName,
212 : const int32_t aNsID, const nsString& aValue)
213 : {
214 0 : NS_ENSURE_TRUE(mBuffer, NS_ERROR_OUT_OF_MEMORY);
215 :
216 0 : if (!mCanAddAttribute) {
217 : // XXX ErrorReport: Can't add attributes without element
218 0 : return NS_OK;
219 : }
220 :
221 : txOutputTransaction* transaction =
222 0 : new txAttributeTransaction(aPrefix, aLocalName, aNsID, aValue);
223 0 : return mBuffer->addTransaction(transaction);
224 : }
225 :
226 : nsresult
227 0 : txBufferingHandler::characters(const nsAString& aData, bool aDOE)
228 : {
229 0 : NS_ENSURE_TRUE(mBuffer, NS_ERROR_OUT_OF_MEMORY);
230 :
231 0 : mCanAddAttribute = false;
232 :
233 : txOutputTransaction::txTransactionType type =
234 0 : aDOE ? txOutputTransaction::eCharacterNoOETransaction
235 0 : : txOutputTransaction::eCharacterTransaction;
236 :
237 0 : txOutputTransaction* transaction = mBuffer->getLastTransaction();
238 0 : if (transaction && transaction->mType == type) {
239 0 : mBuffer->mStringValue.Append(aData);
240 0 : static_cast<txCharacterTransaction*>(transaction)->mLength +=
241 0 : aData.Length();
242 0 : return NS_OK;
243 : }
244 :
245 0 : transaction = new txCharacterTransaction(type, aData.Length());
246 0 : mBuffer->mStringValue.Append(aData);
247 0 : return mBuffer->addTransaction(transaction);
248 : }
249 :
250 : nsresult
251 0 : txBufferingHandler::comment(const nsString& aData)
252 : {
253 0 : NS_ENSURE_TRUE(mBuffer, NS_ERROR_OUT_OF_MEMORY);
254 :
255 0 : mCanAddAttribute = false;
256 :
257 0 : txOutputTransaction* transaction = new txCommentTransaction(aData);
258 0 : return mBuffer->addTransaction(transaction);
259 : }
260 :
261 : nsresult
262 0 : txBufferingHandler::endDocument(nsresult aResult)
263 : {
264 0 : NS_ENSURE_TRUE(mBuffer, NS_ERROR_OUT_OF_MEMORY);
265 :
266 : txOutputTransaction* transaction =
267 0 : new txOutputTransaction(txOutputTransaction::eEndDocumentTransaction);
268 0 : return mBuffer->addTransaction(transaction);
269 : }
270 :
271 : nsresult
272 0 : txBufferingHandler::endElement()
273 : {
274 0 : NS_ENSURE_TRUE(mBuffer, NS_ERROR_OUT_OF_MEMORY);
275 :
276 0 : mCanAddAttribute = false;
277 :
278 : txOutputTransaction* transaction =
279 0 : new txOutputTransaction(txOutputTransaction::eEndElementTransaction);
280 0 : return mBuffer->addTransaction(transaction);
281 : }
282 :
283 : nsresult
284 0 : txBufferingHandler::processingInstruction(const nsString& aTarget,
285 : const nsString& aData)
286 : {
287 0 : NS_ENSURE_TRUE(mBuffer, NS_ERROR_OUT_OF_MEMORY);
288 :
289 0 : mCanAddAttribute = false;
290 :
291 : txOutputTransaction* transaction =
292 0 : new txPITransaction(aTarget, aData);
293 0 : return mBuffer->addTransaction(transaction);
294 : }
295 :
296 : nsresult
297 0 : txBufferingHandler::startDocument()
298 : {
299 0 : NS_ENSURE_TRUE(mBuffer, NS_ERROR_OUT_OF_MEMORY);
300 :
301 : txOutputTransaction* transaction =
302 0 : new txOutputTransaction(txOutputTransaction::eStartDocumentTransaction);
303 0 : return mBuffer->addTransaction(transaction);
304 : }
305 :
306 : nsresult
307 0 : txBufferingHandler::startElement(nsIAtom* aPrefix, nsIAtom* aLocalName,
308 : nsIAtom* aLowercaseLocalName,
309 : int32_t aNsID)
310 : {
311 0 : NS_ENSURE_TRUE(mBuffer, NS_ERROR_OUT_OF_MEMORY);
312 :
313 0 : mCanAddAttribute = true;
314 :
315 : txOutputTransaction* transaction =
316 : new txStartElementAtomTransaction(aPrefix, aLocalName,
317 0 : aLowercaseLocalName, aNsID);
318 0 : return mBuffer->addTransaction(transaction);
319 : }
320 :
321 : nsresult
322 0 : txBufferingHandler::startElement(nsIAtom* aPrefix,
323 : const nsAString& aLocalName,
324 : const int32_t aNsID)
325 : {
326 0 : NS_ENSURE_TRUE(mBuffer, NS_ERROR_OUT_OF_MEMORY);
327 :
328 0 : mCanAddAttribute = true;
329 :
330 : txOutputTransaction* transaction =
331 0 : new txStartElementTransaction(aPrefix, aLocalName, aNsID);
332 0 : return mBuffer->addTransaction(transaction);
333 : }
334 :
335 0 : txResultBuffer::txResultBuffer()
336 : {
337 0 : MOZ_COUNT_CTOR(txResultBuffer);
338 0 : }
339 :
340 0 : txResultBuffer::~txResultBuffer()
341 : {
342 0 : MOZ_COUNT_DTOR(txResultBuffer);
343 0 : for (uint32_t i = 0, len = mTransactions.Length(); i < len; ++i) {
344 0 : delete mTransactions[i];
345 : }
346 0 : }
347 :
348 : nsresult
349 0 : txResultBuffer::addTransaction(txOutputTransaction* aTransaction)
350 : {
351 0 : if (mTransactions.AppendElement(aTransaction) == nullptr) {
352 0 : return NS_ERROR_OUT_OF_MEMORY;
353 : }
354 0 : return NS_OK;
355 : }
356 :
357 : static nsresult
358 0 : flushTransaction(txOutputTransaction* aTransaction,
359 : txAXMLEventHandler* aHandler,
360 : nsString::const_char_iterator& aIter)
361 : {
362 0 : switch (aTransaction->mType) {
363 : case txOutputTransaction::eAttributeAtomTransaction:
364 : {
365 : txAttributeAtomTransaction* transaction =
366 0 : static_cast<txAttributeAtomTransaction*>(aTransaction);
367 0 : return aHandler->attribute(transaction->mPrefix,
368 : transaction->mLocalName,
369 : transaction->mLowercaseLocalName,
370 : transaction->mNsID,
371 0 : transaction->mValue);
372 : }
373 : case txOutputTransaction::eAttributeTransaction:
374 : {
375 : txAttributeTransaction* attrTransaction =
376 0 : static_cast<txAttributeTransaction*>(aTransaction);
377 0 : return aHandler->attribute(attrTransaction->mPrefix,
378 : attrTransaction->mLocalName,
379 : attrTransaction->mNsID,
380 0 : attrTransaction->mValue);
381 : }
382 : case txOutputTransaction::eCharacterTransaction:
383 : case txOutputTransaction::eCharacterNoOETransaction:
384 : {
385 : txCharacterTransaction* charTransaction =
386 0 : static_cast<txCharacterTransaction*>(aTransaction);
387 0 : nsString::const_char_iterator start = aIter;
388 : nsString::const_char_iterator end =
389 0 : start + charTransaction->mLength;
390 0 : aIter = end;
391 0 : return aHandler->characters(Substring(start, end),
392 0 : aTransaction->mType ==
393 0 : txOutputTransaction::eCharacterNoOETransaction);
394 : }
395 : case txOutputTransaction::eCommentTransaction:
396 : {
397 : txCommentTransaction* commentTransaction =
398 0 : static_cast<txCommentTransaction*>(aTransaction);
399 0 : return aHandler->comment(commentTransaction->mValue);
400 : }
401 : case txOutputTransaction::eEndElementTransaction:
402 : {
403 0 : return aHandler->endElement();
404 : }
405 : case txOutputTransaction::ePITransaction:
406 : {
407 : txPITransaction* piTransaction =
408 0 : static_cast<txPITransaction*>(aTransaction);
409 0 : return aHandler->processingInstruction(piTransaction->mTarget,
410 0 : piTransaction->mData);
411 : }
412 : case txOutputTransaction::eStartDocumentTransaction:
413 : {
414 0 : return aHandler->startDocument();
415 : }
416 : case txOutputTransaction::eStartElementAtomTransaction:
417 : {
418 : txStartElementAtomTransaction* transaction =
419 0 : static_cast<txStartElementAtomTransaction*>(aTransaction);
420 0 : return aHandler->startElement(transaction->mPrefix,
421 : transaction->mLocalName,
422 : transaction->mLowercaseLocalName,
423 0 : transaction->mNsID);
424 : }
425 : case txOutputTransaction::eStartElementTransaction:
426 : {
427 : txStartElementTransaction* transaction =
428 0 : static_cast<txStartElementTransaction*>(aTransaction);
429 0 : return aHandler->startElement(transaction->mPrefix,
430 : transaction->mLocalName,
431 0 : transaction->mNsID);
432 : }
433 : default:
434 : {
435 0 : NS_NOTREACHED("Unexpected transaction type");
436 : }
437 : }
438 :
439 0 : return NS_ERROR_UNEXPECTED;
440 : }
441 :
442 : nsresult
443 0 : txResultBuffer::flushToHandler(txAXMLEventHandler* aHandler)
444 : {
445 : nsString::const_char_iterator iter;
446 0 : mStringValue.BeginReading(iter);
447 :
448 0 : for (uint32_t i = 0, len = mTransactions.Length(); i < len; ++i) {
449 0 : nsresult rv = flushTransaction(mTransactions[i], aHandler, iter);
450 0 : NS_ENSURE_SUCCESS(rv, rv);
451 : }
452 :
453 0 : return NS_OK;
454 : }
455 :
456 : txOutputTransaction*
457 0 : txResultBuffer::getLastTransaction()
458 : {
459 0 : int32_t last = mTransactions.Length() - 1;
460 0 : if (last < 0) {
461 0 : return nullptr;
462 : }
463 0 : return mTransactions[last];
464 : }
|