1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
#include "fastserializer.hxx"
#include <com/sun/star/xml/sax/FastTokenHandler.hpp>
#include <rtl/math.h>
#include <sal/log.hxx>
#include <comphelper/processfactory.hxx>
#include <comphelper/sequence.hxx>
#include <cassert>
#include <optional>
#include <string.h>
#include <string_view>
#include <utility>
#if OSL_DEBUG_LEVEL > 0
#include <iostream>
#include <set>
#endif
using ::std::vector;
using ::com::sun::star::uno::Sequence;
using ::com::sun::star::io::XOutputStream;
#define HAS_NAMESPACE(x) ((x & 0xffff0000) != 0)
#define NAMESPACE(x) (x >> 16)
#define TOKEN(x) (x & 0xffff)
// number of characters without terminating 0
#define N_CHARS(string) (SAL_N_ELEMENTS(string) - 1)
const char sClosingBracket[] = ">";
const char sSlashAndClosingBracket[] = "/>";
constexpr OStringLiteral sColon = ":";
const char sOpeningBracket[] = "<";
const char sOpeningBracketAndSlash[] = "</";
const char sQuote[] = "\"";
const char sEqualSignAndQuote[] = "=\"";
const char sSpace[] = " ";
const char sXmlHeader[] = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n";
namespace sax_fastparser {
FastSaxSerializer::FastSaxSerializer( const css::uno::Reference< css::io::XOutputStream >& xOutputStream )
: mbMarkStackEmpty(true)
, mpDoubleStr(nullptr)
, mnDoubleStrCapacity(RTL_STR_MAX_VALUEOFDOUBLE)
, mbXescape(true)
{
rtl_string_new_WithLength(&mpDoubleStr, mnDoubleStrCapacity);
mxFastTokenHandler = css::xml::sax::FastTokenHandler::create(
::comphelper::getProcessComponentContext());
assert(xOutputStream.is()); // cannot do anything without that
maCachedOutputStream.setOutputStream( xOutputStream );
}
FastSaxSerializer::~FastSaxSerializer()
{
rtl_string_release(mpDoubleStr);
}
void FastSaxSerializer::startDocument()
{
writeBytes(sXmlHeader, N_CHARS(sXmlHeader));
}
void FastSaxSerializer::write( double value )
{
rtl_math_doubleToString(
&mpDoubleStr, &mnDoubleStrCapacity, 0, value, rtl_math_StringFormat_G,
RTL_STR_MAX_VALUEOFDOUBLE - RTL_CONSTASCII_LENGTH("-x.E-xxx"), '.', nullptr,
0, true);
write(mpDoubleStr->buffer, mpDoubleStr->length);
// and "clear" the string
mpDoubleStr->length = 0;
mnDoubleStrCapacity = RTL_STR_MAX_VALUEOFDOUBLE;
}
void FastSaxSerializer::write( std::u16string_view sOutput, bool bEscape )
{
write( OUStringToOString(sOutput, RTL_TEXTENCODING_UTF8), bEscape );
}
void FastSaxSerializer::write( const OString& sOutput, bool bEscape )
{
write( sOutput.getStr(), sOutput.getLength(), bEscape );
}
/** Characters not allowed in XML 1.0
XML 1.1 would exclude only U+0000
This assumes that `string` is UTF-8, but which appears to generally be the case: The only
user of this FastSaxSerializer code is FastSerializerHelper, and when its constructor
(sax/source/tools/fshelper.cxx) is called with bWriteHeader being true, it calls
FastSaxSerializer::startDocument, which writes sXmlHeader claiming encoding="UTF-8". The
only place that appears to construct FastSerializerHelper appears to be
XmlFilterBase::openFragmentStreamWithSerializer (oox/source/core/xmlfilterbase.cxx), and it
only passes false for bWriteHeader when the given rMediaType contains "vml" but not "+xml"
(see <https://git.libreoffice.org/core/+/6a11add2c4ea975356cfb7bab02301788c79c904%5E!/>
"XLSX VML Export fixes", stating "Don't write xml headers for vml files"). But lets assume
that even such Vector Markup Language files are written as UTF-8.
*/
template<typename Int> static std::optional<std::pair<unsigned, Int>> invalidChar(
char const * string, Int length, Int index )
{
assert(index < length);
auto const c = string[index];
if (static_cast<unsigned char>(c) >= 0x20 && c != '\xEF')
return {};
switch (c)
{
case 0x09:
case 0x0a:
case 0x0d:
return {};
case '\xEF': // U+FFFE, U+FFFF:
if (length - index >= 3 && string[index + 1] == '\xBF') {
switch (string[index + 2]) {
case '\xBE':
return std::pair(0xFFFE, 3);
case '\xBF':
return std::pair(0xFFFF, 3);
}
}
return {};
}
return std::pair(static_cast<unsigned char>(c), 1);
}
static bool isHexDigit( char c )
{
return ('0' <= c && c <= '9') || ('A' <= c && c <= 'F') || ('a' <= c && c <= 'f');
}
void FastSaxSerializer::write( const char* pStr, sal_Int32 nLen, bool bEscape )
{
if (nLen == -1)
nLen = pStr ? strlen(pStr) : 0;
if (!bEscape)
{
writeBytes( pStr, nLen );
return;
}
bool bGood = true;
const sal_Int32 kXescapeLen = 7;
char bufXescape[kXescapeLen+1];
sal_Int32 nNextXescape = 0;
for (sal_Int32 i = 0; i < nLen;)
{
char c = pStr[ i ];
switch( c )
{
case '<': writeBytes( "<", 4 ); break;
case '>': writeBytes( ">", 4 ); break;
case '&': writeBytes( "&", 5 ); break;
case '\'': writeBytes( "'", 6 ); break;
case '"': writeBytes( """, 6 ); break;
case '\t':
#if 0
// Seems OOXML prefers the _xHHHH_ escape over the
// entity in *some* cases, apparently in attribute
// values but not in element data.
// Would need to distinguish at a higher level.
if (mbXescape)
{
snprintf( bufXescape, kXescapeLen+1, "_x%04x_",
static_cast<unsigned int>(static_cast<unsigned char>(c)));
writeBytes( bufXescape, kXescapeLen);
}
else
#endif
{
writeBytes( "	", 4 );
}
break;
case '\n':
#if 0
if (mbXescape)
{
snprintf( bufXescape, kXescapeLen+1, "_x%04x_",
static_cast<unsigned int>(static_cast<unsigned char>(c)));
writeBytes( bufXescape, kXescapeLen);
}
else
#endif
{
writeBytes( " ", 5 );
}
break;
case '\r':
#if 0
if (mbXescape)
{
snprintf( bufXescape, kXescapeLen+1, "_x%04x_",
static_cast<unsigned int>(static_cast<unsigned char>(c)));
writeBytes( bufXescape, kXescapeLen);
}
else
#endif
{
writeBytes( " ", 5 );
}
break;
default:
if (mbXescape)
{
char c1, c2, c3, c4;
// Escape characters not valid in XML 1.0 as
// _xHHHH_. A literal "_xHHHH_" has to be
// escaped as _x005F_xHHHH_ (effectively
// escaping the leading '_').
// See ECMA-376-1:2016 page 3736,
// 22.4.2.4 bstr (Basic String)
// for reference.
if (c == '_' && i >= nNextXescape && i <= nLen - kXescapeLen &&
pStr[i+6] == '_' &&
((pStr[i+1] | 0x20) == 'x') &&
isHexDigit( c1 = pStr[i+2] ) &&
isHexDigit( c2 = pStr[i+3] ) &&
isHexDigit( c3 = pStr[i+4] ) &&
isHexDigit( c4 = pStr[i+5] ))
{
// OOXML has the odd habit to write some
// names using this that when re-saving
// should *not* be escaped, specifically
// _x0020_ for blanks in w:xpath values.
if (!(c1 == '0' && c2 == '0' && c3 == '2' && c4 == '0'))
{
// When encountering "_x005F_xHHHH_"
// assume that is an already escaped
// sequence that was not unescaped and
// shall be written as is, to not end
// up with "_x005F_x005F_xHHHH_" and
// repeated...
if (c1 == '0' && c2 == '0' && c3 == '5' && (c4 | 0x20) == 'f' &&
i + kXescapeLen <= nLen - 6 &&
pStr[i+kXescapeLen+5] == '_' &&
((pStr[i+kXescapeLen+0] | 0x20) == 'x') &&
isHexDigit( pStr[i+kXescapeLen+1] ) &&
isHexDigit( pStr[i+kXescapeLen+2] ) &&
isHexDigit( pStr[i+kXescapeLen+3] ) &&
isHexDigit( pStr[i+kXescapeLen+4] ))
{
writeBytes( &c, 1 );
// Remember this fake escapement.
nNextXescape = i + kXescapeLen + 6;
}
else
{
writeBytes( "_x005F_", kXescapeLen);
// Remember this escapement so in
// _xHHHH_xHHHH_ only the first '_'
// is escaped.
nNextXescape = i + kXescapeLen;
}
break;
}
}
if (auto const inv = invalidChar(pStr, nLen, i))
{
snprintf( bufXescape, kXescapeLen+1, "_x%04x_",
inv->first);
writeBytes( bufXescape, kXescapeLen);
i += inv->second;
continue;
}
}
#if OSL_DEBUG_LEVEL > 0
else
{
if (bGood && invalidChar(pStr, nLen, i))
{
bGood = false;
// The SAL_WARN() for the single character is
// issued in writeBytes(), just gather for the
// SAL_WARN_IF() below.
}
}
#endif
writeBytes( &c, 1 );
break;
}
++i;
}
SAL_WARN_IF( !bGood && nLen > 1, "sax", "in '" << OString(pStr,std::min<sal_Int32>(nLen,42)) << "'");
}
void FastSaxSerializer::endDocument()
{
assert(mbMarkStackEmpty && maMarkStack.empty());
maCachedOutputStream.flush();
}
void FastSaxSerializer::writeId( ::sal_Int32 nElement )
{
if( HAS_NAMESPACE( nElement ) ) {
auto const Namespace(mxFastTokenHandler->getUTF8Identifier(NAMESPACE(nElement)));
assert(Namespace.hasElements());
writeBytes(Namespace);
writeBytes(sColon.getStr(), sColon.getLength());
auto const Element(mxFastTokenHandler->getUTF8Identifier(TOKEN(nElement)));
assert(Element.hasElements());
writeBytes(Element);
} else {
auto const Element(mxFastTokenHandler->getUTF8Identifier(nElement));
assert(Element.hasElements());
writeBytes(Element);
}
}
#ifdef DBG_UTIL
OString FastSaxSerializer::getId( ::sal_Int32 nElement )
{
if (HAS_NAMESPACE(nElement)) {
Sequence<sal_Int8> const ns(
mxFastTokenHandler->getUTF8Identifier(NAMESPACE(nElement)));
Sequence<sal_Int8> const name(
mxFastTokenHandler->getUTF8Identifier(TOKEN(nElement)));
return std::string_view(
reinterpret_cast<char const*>(ns.getConstArray()), ns.getLength())
+ sColon
+ std::string_view(
reinterpret_cast<char const*>(name.getConstArray()), name.getLength());
} else {
Sequence<sal_Int8> const name(
mxFastTokenHandler->getUTF8Identifier(nElement));
return OString(reinterpret_cast<char const*>(name.getConstArray()), name.getLength());
}
}
#endif
void FastSaxSerializer::startFastElement( ::sal_Int32 Element, FastAttributeList const * pAttrList )
{
if ( !mbMarkStackEmpty )
{
maCachedOutputStream.flush();
maMarkStack.top()->setCurrentElement( Element );
}
#ifdef DBG_UTIL
if (mbMarkStackEmpty)
m_DebugStartedElements.push(Element);
else
maMarkStack.top()->m_DebugStartedElements.push_back(Element);
#endif
writeBytes(sOpeningBracket, N_CHARS(sOpeningBracket));
writeId(Element);
if (pAttrList)
writeFastAttributeList(*pAttrList);
else
writeTokenValueList();
writeBytes(sClosingBracket, N_CHARS(sClosingBracket));
}
void FastSaxSerializer::endFastElement( ::sal_Int32 Element )
{
#ifdef DBG_UTIL
// Well-formedness constraint: Element Type Match
if (mbMarkStackEmpty)
{
assert(!m_DebugStartedElements.empty());
assert(Element == m_DebugStartedElements.top());
m_DebugStartedElements.pop();
}
else
{
if (dynamic_cast<ForSort*>(maMarkStack.top().get()))
{
// Sort is always well-formed fragment
assert(!maMarkStack.top()->m_DebugStartedElements.empty());
}
if (maMarkStack.top()->m_DebugStartedElements.empty())
{
maMarkStack.top()->m_DebugEndedElements.push_back(Element);
}
else
{
assert(Element == maMarkStack.top()->m_DebugStartedElements.back());
maMarkStack.top()->m_DebugStartedElements.pop_back();
}
}
#endif
writeBytes(sOpeningBracketAndSlash, N_CHARS(sOpeningBracketAndSlash));
writeId(Element);
writeBytes(sClosingBracket, N_CHARS(sClosingBracket));
}
void FastSaxSerializer::singleFastElement( ::sal_Int32 Element, FastAttributeList const * pAttrList )
{
if ( !mbMarkStackEmpty )
{
maCachedOutputStream.flush();
maMarkStack.top()->setCurrentElement( Element );
}
writeBytes(sOpeningBracket, N_CHARS(sOpeningBracket));
writeId(Element);
if (pAttrList)
writeFastAttributeList(*pAttrList);
else
writeTokenValueList();
writeBytes(sSlashAndClosingBracket, N_CHARS(sSlashAndClosingBracket));
}
css::uno::Reference< css::io::XOutputStream > const & FastSaxSerializer::getOutputStream() const
{
return maCachedOutputStream.getOutputStream();
}
void FastSaxSerializer::writeTokenValueList()
{
#ifdef DBG_UTIL
::std::set<OString> DebugAttributes;
#endif
for (const TokenValue & rTokenValue : maTokenValues)
{
writeBytes(sSpace, N_CHARS(sSpace));
sal_Int32 nToken = rTokenValue.nToken;
writeId(nToken);
#ifdef DBG_UTIL
// Well-formedness constraint: Unique Att Spec
OString const nameId(getId(nToken));
assert(DebugAttributes.find(nameId) == DebugAttributes.end());
DebugAttributes.insert(nameId);
#endif
writeBytes(sEqualSignAndQuote, N_CHARS(sEqualSignAndQuote));
write(rTokenValue.pValue, -1, true);
writeBytes(sQuote, N_CHARS(sQuote));
}
maTokenValues.clear();
}
void FastSaxSerializer::writeFastAttributeList(FastAttributeList const & rAttrList)
{
#ifdef DBG_UTIL
::std::set<OString> DebugAttributes;
#endif
const std::vector< sal_Int32 >& Tokens = rAttrList.getFastAttributeTokens();
for (size_t j = 0; j < Tokens.size(); j++)
{
writeBytes(sSpace, N_CHARS(sSpace));
sal_Int32 nToken = Tokens[j];
writeId(nToken);
#ifdef DBG_UTIL
// Well-formedness constraint: Unique Att Spec
OString const nameId(getId(nToken));
SAL_WARN_IF(DebugAttributes.find(nameId) != DebugAttributes.end(), "sax", "Duplicate attribute: " << nameId );
assert(DebugAttributes.find(nameId) == DebugAttributes.end());
DebugAttributes.insert(nameId);
#endif
writeBytes(sEqualSignAndQuote, N_CHARS(sEqualSignAndQuote));
const char* pAttributeValue = rAttrList.getFastAttributeValue(j);
// tdf#117274 don't escape the special VML shape type id "#_x0000_t202"
bool bEscape = !(pAttributeValue
&& *pAttributeValue != '\0'
&& (*pAttributeValue == '#'
? strncmp(pAttributeValue, "#_x0000_t", 9) == 0
: strncmp(pAttributeValue, "_x0000_t", 8) == 0));
write(pAttributeValue, rAttrList.AttributeValueLength(j), bEscape);
writeBytes(sQuote, N_CHARS(sQuote));
}
}
void FastSaxSerializer::mark(sal_Int32 const nTag, const Int32Sequence& rOrder)
{
if (rOrder.hasElements())
{
auto pSort = std::make_shared<ForSort>(nTag, rOrder);
maMarkStack.push( pSort );
maCachedOutputStream.setOutput( pSort );
}
else
{
auto pMerge = std::make_shared<ForMerge>(nTag);
maMarkStack.push( pMerge );
maCachedOutputStream.setOutput( pMerge );
}
mbMarkStackEmpty = false;
}
#ifdef DBG_UTIL
static void lcl_DebugMergeAppend(
std::deque<sal_Int32> & rLeftEndedElements,
std::deque<sal_Int32> & rLeftStartedElements,
std::deque<sal_Int32> & rRightEndedElements,
std::deque<sal_Int32> & rRightStartedElements)
{
while (!rRightEndedElements.empty())
{
if (rLeftStartedElements.empty())
{
rLeftEndedElements.push_back(rRightEndedElements.front());
}
else
{
assert(rLeftStartedElements.back() == rRightEndedElements.front());
rLeftStartedElements.pop_back();
}
rRightEndedElements.pop_front();
}
while (!rRightStartedElements.empty())
{
rLeftStartedElements.push_back(rRightStartedElements.front());
rRightStartedElements.pop_front();
}
}
static void lcl_DebugMergePrepend(
std::deque<sal_Int32> & rLeftEndedElements,
std::deque<sal_Int32> & rLeftStartedElements,
std::deque<sal_Int32> & rRightEndedElements,
std::deque<sal_Int32> & rRightStartedElements)
{
while (!rLeftStartedElements.empty())
{
if (rRightEndedElements.empty())
{
rRightStartedElements.push_front(rLeftStartedElements.back());
}
else
{
assert(rRightEndedElements.front() == rLeftStartedElements.back());
rRightEndedElements.pop_front();
}
rLeftStartedElements.pop_back();
}
while (!rLeftEndedElements.empty())
{
rRightEndedElements.push_front(rLeftEndedElements.back());
rLeftEndedElements.pop_back();
}
}
#endif
void FastSaxSerializer::mergeTopMarks(
sal_Int32 const nTag, sax_fastparser::MergeMarks const eMergeType)
{
SAL_WARN_IF(mbMarkStackEmpty, "sax", "Empty mark stack - nothing to merge");
assert(!mbMarkStackEmpty); // should never happen
if ( mbMarkStackEmpty )
return;
assert(maMarkStack.top()->m_Tag == nTag && "mark/merge tag mismatch!");
(void) nTag;
#ifdef DBG_UTIL
if (dynamic_cast<ForSort*>(maMarkStack.top().get()))
{
// Sort is always well-formed fragment
assert(maMarkStack.top()->m_DebugStartedElements.empty());
assert(maMarkStack.top()->m_DebugEndedElements.empty());
}
lcl_DebugMergeAppend(
maMarkStack.top()->m_DebugEndedElements,
maMarkStack.top()->m_DebugStartedElements,
maMarkStack.top()->m_DebugPostponedEndedElements,
maMarkStack.top()->m_DebugPostponedStartedElements);
#endif
// flush, so that we get everything in getData()
maCachedOutputStream.flush();
if (maMarkStack.size() == 1)
{
#ifdef DBG_UTIL
while (!maMarkStack.top()->m_DebugEndedElements.empty())
{
assert(maMarkStack.top()->m_DebugEndedElements.front() == m_DebugStartedElements.top());
maMarkStack.top()->m_DebugEndedElements.pop_front();
m_DebugStartedElements.pop();
}
while (!maMarkStack.top()->m_DebugStartedElements.empty())
{
m_DebugStartedElements.push(maMarkStack.top()->m_DebugStartedElements.front());
maMarkStack.top()->m_DebugStartedElements.pop_front();
}
#endif
Sequence<sal_Int8> aSeq( maMarkStack.top()->getData() );
maMarkStack.pop();
mbMarkStackEmpty = true;
maCachedOutputStream.resetOutputToStream();
maCachedOutputStream.writeBytes( aSeq.getConstArray(), aSeq.getLength() );
return;
}
#ifdef DBG_UTIL
::std::deque<sal_Int32> topDebugStartedElements(maMarkStack.top()->m_DebugStartedElements);
::std::deque<sal_Int32> topDebugEndedElements(maMarkStack.top()->m_DebugEndedElements);
#endif
const Int8Sequence aMerge( maMarkStack.top()->getData() );
maMarkStack.pop();
#ifdef DBG_UTIL
switch (eMergeType)
{
case MergeMarks::APPEND:
lcl_DebugMergeAppend(
maMarkStack.top()->m_DebugEndedElements,
maMarkStack.top()->m_DebugStartedElements,
topDebugEndedElements,
topDebugStartedElements);
break;
case MergeMarks::PREPEND:
if (dynamic_cast<ForSort*>(maMarkStack.top().get())) // argh...
{
lcl_DebugMergeAppend(
maMarkStack.top()->m_DebugEndedElements,
maMarkStack.top()->m_DebugStartedElements,
topDebugEndedElements,
topDebugStartedElements);
}
else
{
lcl_DebugMergePrepend(
topDebugEndedElements,
topDebugStartedElements,
maMarkStack.top()->m_DebugEndedElements,
maMarkStack.top()->m_DebugStartedElements);
}
break;
case MergeMarks::POSTPONE:
lcl_DebugMergeAppend(
maMarkStack.top()->m_DebugPostponedEndedElements,
maMarkStack.top()->m_DebugPostponedStartedElements,
topDebugEndedElements,
topDebugStartedElements);
break;
}
#endif
if (maMarkStack.empty())
{
mbMarkStackEmpty = true;
maCachedOutputStream.resetOutputToStream();
}
else
{
maCachedOutputStream.setOutput( maMarkStack.top() );
}
switch ( eMergeType )
{
case MergeMarks::APPEND: maMarkStack.top()->append( aMerge ); break;
case MergeMarks::PREPEND: maMarkStack.top()->prepend( aMerge ); break;
case MergeMarks::POSTPONE: maMarkStack.top()->postpone( aMerge ); break;
}
}
void FastSaxSerializer::writeBytes( const Sequence< sal_Int8 >& rData )
{
maCachedOutputStream.writeBytes( rData.getConstArray(), rData.getLength() );
}
void FastSaxSerializer::writeBytes( const char* pStr, size_t nLen )
{
#if OSL_DEBUG_LEVEL > 0
{
bool bGood = true;
for (size_t i=0; i < nLen;)
{
if (auto const inv = invalidChar(pStr, nLen, i))
{
bGood = false;
SAL_WARN("sax", "FastSaxSerializer::writeBytes - illegal XML character 0x" <<
std::hex << inv->first);
i += inv->second;
continue;
}
++i;
}
SAL_WARN_IF( !bGood && nLen > 1, "sax", "in '" << OString(pStr,std::min<sal_Int32>(nLen,42)) << "'");
}
#endif
maCachedOutputStream.writeBytes( reinterpret_cast<const sal_Int8*>(pStr), nLen );
}
FastSaxSerializer::Int8Sequence& FastSaxSerializer::ForMerge::getData()
{
merge( maData, maPostponed, true );
maPostponed.realloc( 0 );
return maData;
}
#if OSL_DEBUG_LEVEL > 0
void FastSaxSerializer::ForMerge::print( )
{
std::cerr << "Data: ";
for ( sal_Int32 i=0, len=maData.getLength(); i < len; i++ )
{
std::cerr << maData[i];
}
std::cerr << "\nPostponed: ";
for ( sal_Int32 i=0, len=maPostponed.getLength(); i < len; i++ )
{
std::cerr << maPostponed[i];
}
std::cerr << "\n";
}
#endif
void FastSaxSerializer::ForMerge::prepend( const Int8Sequence &rWhat )
{
merge( maData, rWhat, false );
}
void FastSaxSerializer::ForMerge::append( const css::uno::Sequence<sal_Int8> &rWhat )
{
merge( maData, rWhat, true );
}
void FastSaxSerializer::ForMerge::postpone( const Int8Sequence &rWhat )
{
merge( maPostponed, rWhat, true );
}
void FastSaxSerializer::ForMerge::merge( Int8Sequence &rTop, const Int8Sequence &rMerge, bool bAppend )
{
sal_Int32 nMergeLen = rMerge.getLength();
if ( nMergeLen <= 0 )
return;
sal_Int32 nTopLen = rTop.getLength();
rTop.realloc( nTopLen + nMergeLen );
if ( bAppend )
{
// append the rMerge to the rTop
memcpy( rTop.getArray() + nTopLen, rMerge.getConstArray(), nMergeLen );
}
else
{
// prepend the rMerge to the rTop
memmove( rTop.getArray() + nMergeLen, rTop.getConstArray(), nTopLen );
memcpy( rTop.getArray(), rMerge.getConstArray(), nMergeLen );
}
}
void FastSaxSerializer::ForMerge::resetData( )
{
maData = Int8Sequence();
}
void FastSaxSerializer::ForSort::setCurrentElement( sal_Int32 nElement )
{
const auto & rOrder = maOrder;
if( std::find( rOrder.begin(), rOrder.end(), nElement ) != rOrder.end() )
{
mnCurrentElement = nElement;
if ( maData.find( nElement ) == maData.end() )
maData[ nElement ] = Int8Sequence();
}
}
void FastSaxSerializer::ForSort::prepend( const Int8Sequence &rWhat )
{
append( rWhat );
}
void FastSaxSerializer::ForSort::append( const css::uno::Sequence<sal_Int8> &rWhat )
{
merge( maData[mnCurrentElement], rWhat, true );
}
void FastSaxSerializer::ForSort::sort()
{
// Clear the ForMerge data to avoid duplicate items
resetData();
// Sort it all
std::map< sal_Int32, Int8Sequence >::iterator iter;
for ( const auto nIndex : std::as_const(maOrder) )
{
iter = maData.find( nIndex );
if ( iter != maData.end() )
ForMerge::append( iter->second );
}
}
FastSaxSerializer::Int8Sequence& FastSaxSerializer::ForSort::getData()
{
sort( );
return ForMerge::getData();
}
#if OSL_DEBUG_LEVEL > 0
void FastSaxSerializer::ForSort::print( )
{
for ( const auto& [rElement, rData] : maData )
{
std::cerr << "pair: " << rElement;
for ( sal_Int32 i=0, len=rData.getLength(); i < len; ++i )
std::cerr << rData[i];
std::cerr << "\n";
}
sort( );
ForMerge::print();
}
#endif
} // namespace sax_fastparser
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|