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
/* -*- 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/.
 */

#include "docxtablestyleexport.hxx"
#include "docxhelper.hxx"
#include <doc.hxx>
#include <docsh.hxx>
#include <oox/token/tokens.hxx>
#include <comphelper/sequenceashashmap.hxx>
#include <sax/fastattribs.hxx>

#include <optional>

#include <com/sun/star/beans/XPropertySet.hpp>
#include <com/sun/star/beans/PropertyValue.hpp>
#include <com/sun/star/frame/XModel.hpp>

using namespace com::sun::star;
using namespace oox;

/// Methods in this class handle values in a table style.
struct DocxTableStyleExport::Impl
{
private:
    SwDoc* m_pDoc;
    sax_fastparser::FSHelperPtr m_pSerializer;

public:
    Impl(SwDoc* pDoc)
        : m_pDoc(pDoc)
    {
    }

    void TableStyle(const uno::Sequence<beans::PropertyValue>& rStyle);

    void setSerializer(sax_fastparser::FSHelperPtr pSerializer)
    {
        m_pSerializer = std::move(pSerializer);
    }

    const sax_fastparser::FSHelperPtr& getSerializer() const { return m_pSerializer; }

    SwDoc* getDoc() const { return m_pDoc; }

    /// Handles a boolean value.
    void handleBoolean(const OUString& aValue, sal_Int32 nToken);

    /// Export of w:pPr.
    void tableStylePPr(const uno::Sequence<beans::PropertyValue>& rPPr);
    /// Export of w:tblStylePr.
    void tableStyleTableStylePr(const uno::Sequence<beans::PropertyValue>& rTableStylePr);
    /// Export of w:rPr.
    void tableStyleRPr(const uno::Sequence<beans::PropertyValue>& rRPr);
    /// Export of w:rFonts.
    void tableStyleRRFonts(const uno::Sequence<beans::PropertyValue>& rRFonts);
    /// Export of w:lang.
    void tableStyleRLang(const uno::Sequence<beans::PropertyValue>& rLang);
    /// Export of w:ind in a pPr.
    void tableStylePInd(const uno::Sequence<beans::PropertyValue>& rInd);
    /// Export of w:spacing.
    void tableStylePSpacing(const uno::Sequence<beans::PropertyValue>& rSpacing);
    /// Export of w:tblPr.
    void tableStyleTablePr(const uno::Sequence<beans::PropertyValue>& rTablePr);
    /// Export of w:trPr.
    void tableStyleTrPr(const uno::Sequence<beans::PropertyValue>& rTrPr);
    /// Export of w:tcPr.
    void tableStyleTcPr(const uno::Sequence<beans::PropertyValue>& rTcPr);
    /// Export of w:tcBorders (and w:tblBorders).
    void tableStyleTcBorders(const uno::Sequence<beans::PropertyValue>& rTcBorders,
                             sal_Int32 nToken = XML_tcBorders);
    /// Export of w:tblInd.
    void tableStyleTableInd(const uno::Sequence<beans::PropertyValue>& rTableInd);
    /// Export of w:tblCellMar (and w:tcMar).
    void tableStyleTableCellMar(const uno::Sequence<beans::PropertyValue>& rTableCellMar,
                                sal_Int32 nType = XML_tblCellMar);
    /// Export of a given table cell border type.
    void tableStyleTcBorder(sal_Int32 nToken, const uno::Sequence<beans::PropertyValue>& rTcBorder);
    /// Export of w:shd.
    void tableStyleShd(const uno::Sequence<beans::PropertyValue>& rShd);
    /// Export of w:color.
    void tableStyleRColor(const uno::Sequence<beans::PropertyValue>& rColor);
};

void DocxTableStyleExport::CnfStyle(const uno::Sequence<beans::PropertyValue>& rAttributeList)
{
    sax_fastparser::FastAttributeList* pAttributeList
        = sax_fastparser::FastSerializerHelper::createAttrList();

    for (const auto& rAttribute : rAttributeList)
    {
        if (rAttribute.Name == "val")
            pAttributeList->add(FSNS(XML_w, XML_val), rAttribute.Value.get<OUString>().toUtf8());
        else
        {
            static DocxStringTokenMap const aTokens[]
                = { { "firstRow", XML_firstRow },
                    { "lastRow", XML_lastRow },
                    { "firstColumn", XML_firstColumn },
                    { "lastColumn", XML_lastColumn },
                    { "oddVBand", XML_oddVBand },
                    { "evenVBand", XML_evenVBand },
                    { "oddHBand", XML_oddHBand },
                    { "evenHBand", XML_evenHBand },
                    { "firstRowFirstColumn", XML_firstRowFirstColumn },
                    { "firstRowLastColumn", XML_firstRowLastColumn },
                    { "lastRowFirstColumn", XML_lastRowFirstColumn },
                    { "lastRowLastColumn", XML_lastRowLastColumn },
                    { nullptr, 0 } };

            if (sal_Int32 nToken = DocxStringGetToken(aTokens, rAttribute.Name))
                pAttributeList->add(FSNS(XML_w, nToken), rAttribute.Value.get<OUString>().toUtf8());
        }
    }

    sax_fastparser::XFastAttributeListRef xAttributeList(pAttributeList);
    m_pImpl->getSerializer()->singleElementNS(XML_w, XML_cnfStyle, xAttributeList);
}

void DocxTableStyleExport::TableStyles(sal_Int32 nCountStylesToWrite)
{
    // Do we have table styles from InteropGrabBag available?
    uno::Reference<beans::XPropertySet> xPropertySet(
        m_pImpl->getDoc()->GetDocShell()->GetBaseModel(), uno::UNO_QUERY_THROW);
    uno::Sequence<beans::PropertyValue> aInteropGrabBag;
    xPropertySet->getPropertyValue("InteropGrabBag") >>= aInteropGrabBag;
    uno::Sequence<beans::PropertyValue> aTableStyles;
    auto pProp = std::find_if(
        aInteropGrabBag.begin(), aInteropGrabBag.end(),
        [](const beans::PropertyValue& rProp) { return rProp.Name == "tableStyles"; });
    if (pProp != aInteropGrabBag.end())
        pProp->Value >>= aTableStyles;
    if (!aTableStyles.hasElements())
        return;

    if (nCountStylesToWrite > aTableStyles.getLength())
        nCountStylesToWrite = aTableStyles.getLength();

    for (sal_Int32 i = 0; i < nCountStylesToWrite; ++i)
    {
        uno::Sequence<beans::PropertyValue> aTableStyle;
        aTableStyles[i].Value >>= aTableStyle;
        m_pImpl->TableStyle(aTableStyle);
    }
}

void DocxTableStyleExport::Impl::tableStyleTableCellMar(
    const uno::Sequence<beans::PropertyValue>& rTableCellMar, sal_Int32 nType)
{
    static DocxStringTokenMap const aTableCellMarTokens[]
        = { { "left", XML_left }, { "right", XML_right }, { "start", XML_start },
            { "end", XML_end },   { "top", XML_top },     { "bottom", XML_bottom },
            { nullptr, 0 } };

    if (!rTableCellMar.hasElements())
        return;

    m_pSerializer->startElementNS(XML_w, nType);
    for (const auto& rProp : rTableCellMar)
    {
        if (sal_Int32 nToken = DocxStringGetToken(aTableCellMarTokens, rProp.Name))
        {
            comphelper::SequenceAsHashMap aMap(
                rProp.Value.get<uno::Sequence<beans::PropertyValue>>());
            m_pSerializer->singleElementNS(
                XML_w, nToken, FSNS(XML_w, XML_w), OString::number(aMap["w"].get<sal_Int32>()),
                FSNS(XML_w, XML_type), aMap["type"].get<OUString>().toUtf8());
        }
    }
    m_pSerializer->endElementNS(XML_w, nType);
}

void DocxTableStyleExport::Impl::tableStyleTcBorder(
    sal_Int32 nToken, const uno::Sequence<beans::PropertyValue>& rTcBorder)
{
    static DocxStringTokenMap const aTcBorderTokens[] = { { "val", XML_val },
                                                          { "sz", XML_sz },
                                                          { "color", XML_color },
                                                          { "space", XML_space },
                                                          { "themeColor", XML_themeColor },
                                                          { "themeTint", XML_themeTint },
                                                          { nullptr, 0 } };

    if (!rTcBorder.hasElements())
        return;

    sax_fastparser::FastAttributeList* pAttributeList
        = sax_fastparser::FastSerializerHelper::createAttrList();
    for (const auto& rProp : rTcBorder)
        if (sal_Int32 nAttrToken = DocxStringGetToken(aTcBorderTokens, rProp.Name))
            pAttributeList->add(FSNS(XML_w, nAttrToken), rProp.Value.get<OUString>().toUtf8());

    sax_fastparser::XFastAttributeListRef xAttributeList(pAttributeList);
    m_pSerializer->singleElementNS(XML_w, nToken, xAttributeList);
}

void DocxTableStyleExport::Impl::tableStyleTcBorders(
    const uno::Sequence<beans::PropertyValue>& rTcBorders, sal_Int32 nToken)
{
    static DocxStringTokenMap const aTcBordersTokens[] = { { "left", XML_left },
                                                           { "right", XML_right },
                                                           { "start", XML_start },
                                                           { "end", XML_end },
                                                           { "top", XML_top },
                                                           { "bottom", XML_bottom },
                                                           { "insideH", XML_insideH },
                                                           { "insideV", XML_insideV },
                                                           { "tl2br", XML_tl2br },
                                                           { "tr2bl", XML_tr2bl },
                                                           { nullptr, 0 } };

    if (!rTcBorders.hasElements())
        return;

    m_pSerializer->startElementNS(XML_w, nToken);
    for (const auto& rTcBorder : rTcBorders)
        if (sal_Int32 nSubToken = DocxStringGetToken(aTcBordersTokens, rTcBorder.Name))
            tableStyleTcBorder(nSubToken,
                               rTcBorder.Value.get<uno::Sequence<beans::PropertyValue>>());
    m_pSerializer->endElementNS(XML_w, nToken);
}

void DocxTableStyleExport::Impl::tableStyleShd(const uno::Sequence<beans::PropertyValue>& rShd)
{
    if (!rShd.hasElements())
        return;

    sax_fastparser::FastAttributeList* pAttributeList
        = sax_fastparser::FastSerializerHelper::createAttrList();
    for (const auto& rProp : rShd)
    {
        if (rProp.Name == "val")
            pAttributeList->add(FSNS(XML_w, XML_val), rProp.Value.get<OUString>().toUtf8());
        else if (rProp.Name == "color")
            pAttributeList->add(FSNS(XML_w, XML_color), rProp.Value.get<OUString>().toUtf8());
        else if (rProp.Name == "fill")
            pAttributeList->add(FSNS(XML_w, XML_fill), rProp.Value.get<OUString>().toUtf8());
        else if (rProp.Name == "themeFill")
            pAttributeList->add(FSNS(XML_w, XML_themeFill), rProp.Value.get<OUString>().toUtf8());
        else if (rProp.Name == "themeFillShade")
            pAttributeList->add(FSNS(XML_w, XML_themeFillShade),
                                rProp.Value.get<OUString>().toUtf8());
        else if (rProp.Name == "themeFillTint")
            pAttributeList->add(FSNS(XML_w, XML_themeFillTint),
                                rProp.Value.get<OUString>().toUtf8());
    }
    sax_fastparser::XFastAttributeListRef xAttributeList(pAttributeList);
    m_pSerializer->singleElementNS(XML_w, XML_shd, xAttributeList);
}

void DocxTableStyleExport::Impl::tableStyleRColor(const uno::Sequence<beans::PropertyValue>& rColor)
{
    if (!rColor.hasElements())
        return;

    sax_fastparser::FastAttributeList* pAttributeList
        = sax_fastparser::FastSerializerHelper::createAttrList();
    for (const auto& rProp : rColor)
    {
        if (rProp.Name == "val")
            pAttributeList->add(FSNS(XML_w, XML_val), rProp.Value.get<OUString>().toUtf8());
        else if (rProp.Name == "themeColor")
            pAttributeList->add(FSNS(XML_w, XML_themeColor), rProp.Value.get<OUString>().toUtf8());
        else if (rProp.Name == "themeTint")
            pAttributeList->add(FSNS(XML_w, XML_themeTint), rProp.Value.get<OUString>().toUtf8());
        else if (rProp.Name == "themeShade")
            pAttributeList->add(FSNS(XML_w, XML_themeShade), rProp.Value.get<OUString>().toUtf8());
    }
    sax_fastparser::XFastAttributeListRef xAttributeList(pAttributeList);
    m_pSerializer->singleElementNS(XML_w, XML_color, xAttributeList);
}

void DocxTableStyleExport::Impl::tableStyleRLang(const uno::Sequence<beans::PropertyValue>& rLang)
{
    if (!rLang.hasElements())
        return;

    sax_fastparser::FastAttributeList* pAttributeList
        = sax_fastparser::FastSerializerHelper::createAttrList();
    for (const auto& rProp : rLang)
    {
        if (rProp.Name == "eastAsia")
            pAttributeList->add(FSNS(XML_w, XML_eastAsia), rProp.Value.get<OUString>().toUtf8());
        else if (rProp.Name == "val")
            pAttributeList->add(FSNS(XML_w, XML_val), rProp.Value.get<OUString>().toUtf8());
        else if (rProp.Name == "bidi")
            pAttributeList->add(FSNS(XML_w, XML_bidi), rProp.Value.get<OUString>().toUtf8());
    }
    sax_fastparser::XFastAttributeListRef xAttributeList(pAttributeList);
    m_pSerializer->singleElementNS(XML_w, XML_lang, xAttributeList);
}

void DocxTableStyleExport::Impl::tableStyleRRFonts(
    const uno::Sequence<beans::PropertyValue>& rRFonts)
{
    if (!rRFonts.hasElements())
        return;

    sax_fastparser::FastAttributeList* pAttributeList
        = sax_fastparser::FastSerializerHelper::createAttrList();
    for (const auto& rRFont : rRFonts)
    {
        if (rRFont.Name == "eastAsiaTheme")
            pAttributeList->add(FSNS(XML_w, XML_eastAsiaTheme),
                                rRFont.Value.get<OUString>().toUtf8());
        else if (rRFont.Name == "asciiTheme")
            pAttributeList->add(FSNS(XML_w, XML_asciiTheme), rRFont.Value.get<OUString>().toUtf8());
        else if (rRFont.Name == "cstheme")
            pAttributeList->add(FSNS(XML_w, XML_cstheme), rRFont.Value.get<OUString>().toUtf8());
        else if (rRFont.Name == "hAnsiTheme")
            pAttributeList->add(FSNS(XML_w, XML_hAnsiTheme), rRFont.Value.get<OUString>().toUtf8());
    }
    sax_fastparser::XFastAttributeListRef xAttributeList(pAttributeList);
    m_pSerializer->singleElementNS(XML_w, XML_rFonts, xAttributeList);
}

void DocxTableStyleExport::Impl::tableStylePSpacing(
    const uno::Sequence<beans::PropertyValue>& rSpacing)
{
    if (!rSpacing.hasElements())
        return;

    sax_fastparser::FastAttributeList* pAttributeList
        = sax_fastparser::FastSerializerHelper::createAttrList();
    for (const auto& rProp : rSpacing)
    {
        if (rProp.Name == "after")
            pAttributeList->add(FSNS(XML_w, XML_after), rProp.Value.get<OUString>().toUtf8());
        else if (rProp.Name == "before")
            pAttributeList->add(FSNS(XML_w, XML_before), rProp.Value.get<OUString>().toUtf8());
        else if (rProp.Name == "line")
            pAttributeList->add(FSNS(XML_w, XML_line), rProp.Value.get<OUString>().toUtf8());
        else if (rProp.Name == "lineRule")
            pAttributeList->add(FSNS(XML_w, XML_lineRule), rProp.Value.get<OUString>().toUtf8());
        else if (rProp.Name == "beforeLines")
            pAttributeList->add(FSNS(XML_w, XML_beforeLines), rProp.Value.get<OUString>().toUtf8());
        else if (rProp.Name == "ParaTopMarginBeforeAutoSpacing")
            // Auto spacing will be available in grab bag only if it was set to true
            pAttributeList->add(FSNS(XML_w, XML_beforeAutospacing), "1");
        else if (rProp.Name == "afterLines")
            pAttributeList->add(FSNS(XML_w, XML_afterLines), rProp.Value.get<OUString>().toUtf8());
        else if (rProp.Name == "ParaBottomMarginAfterAutoSpacing")
            // Auto spacing will be available in grab bag only if it was set to true
            pAttributeList->add(FSNS(XML_w, XML_afterAutospacing), "1");
    }
    sax_fastparser::XFastAttributeListRef xAttributeList(pAttributeList);
    m_pSerializer->singleElementNS(XML_w, XML_spacing, xAttributeList);
}

void DocxTableStyleExport::Impl::tableStylePInd(const uno::Sequence<beans::PropertyValue>& rInd)
{
    if (!rInd.hasElements())
        return;

    sax_fastparser::FastAttributeList* pAttributeList
        = sax_fastparser::FastSerializerHelper::createAttrList();
    for (const auto& rProp : rInd)
    {
        if (rProp.Name == "rightChars")
            pAttributeList->add(FSNS(XML_w, XML_rightChars), rProp.Value.get<OUString>().toUtf8());
        else if (rProp.Name == "right")
            pAttributeList->add(FSNS(XML_w, XML_right), rProp.Value.get<OUString>().toUtf8());
    }
    sax_fastparser::XFastAttributeListRef xAttributeList(pAttributeList);
    m_pSerializer->singleElementNS(XML_w, XML_ind, xAttributeList);
}

void DocxTableStyleExport::Impl::tableStyleTableInd(
    const uno::Sequence<beans::PropertyValue>& rTableInd)
{
    if (!rTableInd.hasElements())
        return;

    sax_fastparser::FastAttributeList* pAttributeList
        = sax_fastparser::FastSerializerHelper::createAttrList();
    for (const auto& rProp : rTableInd)
    {
        if (rProp.Name == "w")
            pAttributeList->add(FSNS(XML_w, XML_w), OString::number(rProp.Value.get<sal_Int32>()));
        else if (rProp.Name == "type")
            pAttributeList->add(FSNS(XML_w, XML_type), rProp.Value.get<OUString>().toUtf8());
    }
    sax_fastparser::XFastAttributeListRef xAttributeList(pAttributeList);
    m_pSerializer->singleElementNS(XML_w, XML_tblInd, xAttributeList);
}

void DocxTableStyleExport::Impl::handleBoolean(const OUString& aValue, sal_Int32 nToken)
{
    if (aValue.isEmpty())
        return;
    sax_fastparser::FastAttributeList* pAttributeList
        = sax_fastparser::FastSerializerHelper::createAttrList();
    if (aValue != "1")
        pAttributeList->add(FSNS(XML_w, XML_val), aValue.toUtf8());
    sax_fastparser::XFastAttributeListRef xAttributeList(pAttributeList);
    m_pSerializer->singleElementNS(XML_w, nToken, xAttributeList);
}

void DocxTableStyleExport::Impl::tableStyleRPr(const uno::Sequence<beans::PropertyValue>& rRPr)
{
    if (!rRPr.hasElements())
        return;

    m_pSerializer->startElementNS(XML_w, XML_rPr);

    uno::Sequence<beans::PropertyValue> aRFonts;
    uno::Sequence<beans::PropertyValue> aLang;
    uno::Sequence<beans::PropertyValue> aColor;
    uno::Sequence<beans::PropertyValue> aSpacingSequence;
    bool bSequenceFlag = false;
    OUString aB;
    OUString aBCs;
    OUString aI;
    OUString aSz;
    OUString aSzCs;
    OUString aCaps;
    OUString aSmallCaps;
    OUString aSpacing;
    for (const auto& rProp : rRPr)
    {
        if (rProp.Name == "rFonts")
            aRFonts = rProp.Value.get<uno::Sequence<beans::PropertyValue>>();
        else if (rProp.Name == "lang")
            aLang = rProp.Value.get<uno::Sequence<beans::PropertyValue>>();
        else if (rProp.Name == "b")
            aB = rProp.Value.get<OUString>();
        else if (rProp.Name == "bCs")
            aBCs = rProp.Value.get<OUString>();
        else if (rProp.Name == "i")
            aI = rProp.Value.get<OUString>();
        else if (rProp.Name == "color")
            aColor = rProp.Value.get<uno::Sequence<beans::PropertyValue>>();
        else if (rProp.Name == "sz")
            aSz = rProp.Value.get<OUString>();
        else if (rProp.Name == "szCs")
            aSzCs = rProp.Value.get<OUString>();
        else if (rProp.Name == "caps")
            aCaps = rProp.Value.get<OUString>();
        else if (rProp.Name == "smallCaps")
            aSmallCaps = rProp.Value.get<OUString>();
        else if (rProp.Name == "spacing")
        {
            if (rProp.Value.has<OUString>())
            {
                aSpacing = rProp.Value.get<OUString>();
            }
            else
            {
                aSpacingSequence = rProp.Value.get<uno::Sequence<beans::PropertyValue>>();
                bSequenceFlag = true; // set the uno::Sequence flag.
            }
        }
    }
    tableStyleRRFonts(aRFonts);
    tableStyleRLang(aLang);
    handleBoolean(aB, XML_b);
    handleBoolean(aBCs, XML_bCs);
    handleBoolean(aI, XML_i);
    handleBoolean(aCaps, XML_caps);
    handleBoolean(aSmallCaps, XML_smallCaps);
    tableStyleRColor(aColor);
    if (bSequenceFlag)
    {
        m_pSerializer->singleElementNS(XML_w, XML_spacing, FSNS(XML_w, XML_val),
                                       aSpacingSequence[0].Value.get<OUString>().toUtf8());
    }
    if (!aSpacing.isEmpty())
        m_pSerializer->singleElementNS(XML_w, XML_spacing, FSNS(XML_w, XML_val), aSpacing.toUtf8());
    if (!aSz.isEmpty())
        m_pSerializer->singleElementNS(XML_w, XML_sz, FSNS(XML_w, XML_val), aSz.toUtf8());
    if (!aSzCs.isEmpty())
        m_pSerializer->singleElementNS(XML_w, XML_szCs, FSNS(XML_w, XML_val), aSzCs.toUtf8());

    m_pSerializer->endElementNS(XML_w, XML_rPr);
}

void DocxTableStyleExport::Impl::tableStylePPr(const uno::Sequence<beans::PropertyValue>& rPPr)
{
    if (!rPPr.hasElements())
        return;

    m_pSerializer->startElementNS(XML_w, XML_pPr);

    uno::Sequence<beans::PropertyValue> aSpacing;
    uno::Sequence<beans::PropertyValue> aInd;
    bool bWordWrap = false;
    OUString aJc;
    OUString aSnapToGrid;
    for (const auto& rProp : rPPr)
    {
        if (rProp.Name == "spacing")
            aSpacing = rProp.Value.get<uno::Sequence<beans::PropertyValue>>();
        else if (rProp.Name == "ind")
            aInd = rProp.Value.get<uno::Sequence<beans::PropertyValue>>();
        else if (rProp.Name == "wordWrap")
            bWordWrap = true;
        else if (rProp.Name == "jc")
            aJc = rProp.Value.get<OUString>();
        else if (rProp.Name == "snapToGrid")
            aSnapToGrid = rProp.Value.get<OUString>();
    }
    if (bWordWrap)
        m_pSerializer->singleElementNS(XML_w, XML_wordWrap);
    tableStylePInd(aInd);
    handleBoolean(aSnapToGrid, XML_snapToGrid);
    tableStylePSpacing(aSpacing);
    if (!aJc.isEmpty())
        m_pSerializer->singleElementNS(XML_w, XML_jc, FSNS(XML_w, XML_val), aJc.toUtf8());

    m_pSerializer->endElementNS(XML_w, XML_pPr);
}

void DocxTableStyleExport::Impl::tableStyleTablePr(
    const uno::Sequence<beans::PropertyValue>& rTablePr)
{
    if (!rTablePr.hasElements())
        return;

    m_pSerializer->startElementNS(XML_w, XML_tblPr);

    uno::Sequence<beans::PropertyValue> aTableInd;
    uno::Sequence<beans::PropertyValue> aTableBorders;
    uno::Sequence<beans::PropertyValue> aTableCellMar;
    std::optional<sal_Int32> oTableStyleRowBandSize;
    std::optional<sal_Int32> oTableStyleColBandSize;
    for (const auto& rProp : rTablePr)
    {
        if (rProp.Name == "tblStyleRowBandSize")
            oTableStyleRowBandSize = rProp.Value.get<sal_Int32>();
        else if (rProp.Name == "tblStyleColBandSize")
            oTableStyleColBandSize = rProp.Value.get<sal_Int32>();
        else if (rProp.Name == "tblInd")
            aTableInd = rProp.Value.get<uno::Sequence<beans::PropertyValue>>();
        else if (rProp.Name == "tblBorders")
            aTableBorders = rProp.Value.get<uno::Sequence<beans::PropertyValue>>();
        else if (rProp.Name == "tblCellMar")
            aTableCellMar = rProp.Value.get<uno::Sequence<beans::PropertyValue>>();
    }
    if (oTableStyleRowBandSize)
        m_pSerializer->singleElementNS(XML_w, XML_tblStyleRowBandSize, FSNS(XML_w, XML_val),
                                       OString::number(*oTableStyleRowBandSize));
    if (oTableStyleColBandSize)
        m_pSerializer->singleElementNS(XML_w, XML_tblStyleColBandSize, FSNS(XML_w, XML_val),
                                       OString::number(*oTableStyleColBandSize));
    tableStyleTableInd(aTableInd);
    tableStyleTcBorders(aTableBorders, XML_tblBorders);
    tableStyleTableCellMar(aTableCellMar);

    m_pSerializer->endElementNS(XML_w, XML_tblPr);
}

void DocxTableStyleExport::Impl::tableStyleTrPr(const uno::Sequence<beans::PropertyValue>& rTrPr)
{
    if (!rTrPr.hasElements())
        return;

    m_pSerializer->startElementNS(XML_w, XML_trPr);

    for (const auto& rProp : rTrPr)
    {
        if (rProp.Name == "tblHeader")
            m_pSerializer->singleElementNS(XML_w, XML_tblHeader);
    }

    m_pSerializer->endElementNS(XML_w, XML_trPr);
}

void DocxTableStyleExport::Impl::tableStyleTcPr(const uno::Sequence<beans::PropertyValue>& rTcPr)
{
    if (!rTcPr.hasElements())
        return;

    m_pSerializer->startElementNS(XML_w, XML_tcPr);

    uno::Sequence<beans::PropertyValue> aShd;
    uno::Sequence<beans::PropertyValue> aTcBorders;
    uno::Sequence<beans::PropertyValue> aTcMar;
    OUString aVAlign;
    for (const auto& rProp : rTcPr)
    {
        if (rProp.Name == "shd")
            aShd = rProp.Value.get<uno::Sequence<beans::PropertyValue>>();
        else if (rProp.Name == "tcBorders")
            aTcBorders = rProp.Value.get<uno::Sequence<beans::PropertyValue>>();
        else if (rProp.Name == "tcMar")
            aTcMar = rProp.Value.get<uno::Sequence<beans::PropertyValue>>();
        else if (rProp.Name == "vAlign")
            aVAlign = rProp.Value.get<OUString>();
    }
    tableStyleTcBorders(aTcBorders);
    tableStyleTableCellMar(aTcMar, XML_tcMar);
    tableStyleShd(aShd);
    if (!aVAlign.isEmpty())
        m_pSerializer->singleElementNS(XML_w, XML_vAlign, FSNS(XML_w, XML_val), aVAlign.toUtf8());

    m_pSerializer->endElementNS(XML_w, XML_tcPr);
}

void DocxTableStyleExport::Impl::tableStyleTableStylePr(
    const uno::Sequence<beans::PropertyValue>& rTableStylePr)
{
    if (!rTableStylePr.hasElements())
        return;

    OUString aType;
    uno::Sequence<beans::PropertyValue> aPPr;
    uno::Sequence<beans::PropertyValue> aRPr;
    uno::Sequence<beans::PropertyValue> aTablePr;
    uno::Sequence<beans::PropertyValue> aTrPr;
    uno::Sequence<beans::PropertyValue> aTcPr;
    for (const auto& rProp : rTableStylePr)
    {
        if (rProp.Name == "type")
            aType = rProp.Value.get<OUString>();
        else if (rProp.Name == "pPr")
            aPPr = rProp.Value.get<uno::Sequence<beans::PropertyValue>>();
        else if (rProp.Name == "rPr")
            aRPr = rProp.Value.get<uno::Sequence<beans::PropertyValue>>();
        else if (rProp.Name == "tblPr")
            aTablePr = rProp.Value.get<uno::Sequence<beans::PropertyValue>>();
        else if (rProp.Name == "trPr")
            aTrPr = rProp.Value.get<uno::Sequence<beans::PropertyValue>>();
        else if (rProp.Name == "tcPr")
            aTcPr = rProp.Value.get<uno::Sequence<beans::PropertyValue>>();
    }

    m_pSerializer->startElementNS(XML_w, XML_tblStylePr, FSNS(XML_w, XML_type), aType.toUtf8());

    tableStylePPr(aPPr);
    tableStyleRPr(aRPr);
    if (aTablePr.hasElements())
        tableStyleTablePr(aTablePr);
    else
    {
        // Even if we have an empty container, write it out, as Word does.
        m_pSerializer->singleElementNS(XML_w, XML_tblPr);
    }
    tableStyleTrPr(aTrPr);
    tableStyleTcPr(aTcPr);

    m_pSerializer->endElementNS(XML_w, XML_tblStylePr);
}

void DocxTableStyleExport::Impl::TableStyle(const uno::Sequence<beans::PropertyValue>& rStyle)
{
    bool bDefault = false;
    bool bCustomStyle = false;
    bool bQFormat = false;
    bool bSemiHidden = false;
    bool bUnhideWhenUsed = false;
    OUString aStyleId;
    OUString aName;
    OUString aBasedOn;
    OUString aRsid;
    OUString aUiPriority;
    uno::Sequence<beans::PropertyValue> aPPr;
    uno::Sequence<beans::PropertyValue> aRPr;
    uno::Sequence<beans::PropertyValue> aTablePr;
    uno::Sequence<beans::PropertyValue> aTcPr;
    std::vector<uno::Sequence<beans::PropertyValue>> aTableStylePrs;
    for (const auto& rProp : rStyle)
    {
        if (rProp.Name == "default")
            bDefault = rProp.Value.get<bool>();
        else if (rProp.Name == "customStyle")
            bCustomStyle = rProp.Value.get<bool>();
        else if (rProp.Name == "styleId")
            aStyleId = rProp.Value.get<OUString>();
        else if (rProp.Name == "name")
            aName = rProp.Value.get<OUString>();
        else if (rProp.Name == "basedOn")
            aBasedOn = rProp.Value.get<OUString>();
        else if (rProp.Name == "uiPriority")
            aUiPriority = rProp.Value.get<OUString>();
        else if (rProp.Name == "qFormat")
            bQFormat = true;
        else if (rProp.Name == "semiHidden")
            bSemiHidden = true;
        else if (rProp.Name == "unhideWhenUsed")
            bUnhideWhenUsed = true;
        else if (rProp.Name == "rsid")
            aRsid = rProp.Value.get<OUString>();
        else if (rProp.Name == "pPr")
            aPPr = rProp.Value.get<uno::Sequence<beans::PropertyValue>>();
        else if (rProp.Name == "rPr")
            aRPr = rProp.Value.get<uno::Sequence<beans::PropertyValue>>();
        else if (rProp.Name == "tblPr")
            aTablePr = rProp.Value.get<uno::Sequence<beans::PropertyValue>>();
        else if (rProp.Name == "tcPr")
            aTcPr = rProp.Value.get<uno::Sequence<beans::PropertyValue>>();
        else if (rProp.Name == "tblStylePr")
            aTableStylePrs.push_back(rProp.Value.get<uno::Sequence<beans::PropertyValue>>());
    }

    sax_fastparser::FastAttributeList* pAttributeList
        = sax_fastparser::FastSerializerHelper::createAttrList();
    pAttributeList->add(FSNS(XML_w, XML_type), "table");
    if (bDefault)
        pAttributeList->add(FSNS(XML_w, XML_default), "1");
    if (bCustomStyle)
        pAttributeList->add(FSNS(XML_w, XML_customStyle), "1");
    if (!aStyleId.isEmpty())
        pAttributeList->add(FSNS(XML_w, XML_styleId), aStyleId.toUtf8());
    sax_fastparser::XFastAttributeListRef xAttributeList(pAttributeList);
    m_pSerializer->startElementNS(XML_w, XML_style, xAttributeList);

    m_pSerializer->singleElementNS(XML_w, XML_name, FSNS(XML_w, XML_val), aName.toUtf8());
    if (!aBasedOn.isEmpty())
        m_pSerializer->singleElementNS(XML_w, XML_basedOn, FSNS(XML_w, XML_val), aBasedOn.toUtf8());
    if (!aUiPriority.isEmpty())
        m_pSerializer->singleElementNS(XML_w, XML_uiPriority, FSNS(XML_w, XML_val),
                                       aUiPriority.toUtf8());
    if (bSemiHidden)
        m_pSerializer->singleElementNS(XML_w, XML_semiHidden);
    if (bUnhideWhenUsed)
        m_pSerializer->singleElementNS(XML_w, XML_unhideWhenUsed);
    if (bQFormat)
        m_pSerializer->singleElementNS(XML_w, XML_qFormat);
    if (!aRsid.isEmpty())
        m_pSerializer->singleElementNS(XML_w, XML_rsid, FSNS(XML_w, XML_val), aRsid.toUtf8());

    tableStylePPr(aPPr);
    tableStyleRPr(aRPr);
    tableStyleTablePr(aTablePr);
    tableStyleTcPr(aTcPr);
    for (const uno::Sequence<beans::PropertyValue>& i : std::as_const(aTableStylePrs))
        tableStyleTableStylePr(i);

    m_pSerializer->endElementNS(XML_w, XML_style);
}

void DocxTableStyleExport::SetSerializer(const sax_fastparser::FSHelperPtr& pSerializer)
{
    m_pImpl->setSerializer(pSerializer);
}

DocxTableStyleExport::DocxTableStyleExport(SwDoc* pDoc,
                                           const sax_fastparser::FSHelperPtr& pSerializer)
    : m_pImpl(std::make_unique<Impl>(pDoc))
{
    m_pImpl->setSerializer(pSerializer);
}

DocxTableStyleExport::~DocxTableStyleExport() = default;

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */