summaryrefslogtreecommitdiffstats
path: root/xmloff/source/core/xmlexp.cxx
blob: 55e9b1956560697ff4d1093ba212dd5066925830 (plain)
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
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
/* -*- 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 <config_wasm_strip.h>

#include <memory>
#include <sal/config.h>
#include <sal/log.hxx>

#include <mutex>
#include <stack>
#include <optional>
#include <utility>

#include <xmloff/unointerfacetouniqueidentifiermapper.hxx>
#include <tools/urlobj.hxx>
#include <vcl/graph.hxx>
#include <comphelper/genericpropertyset.hxx>
#include <com/sun/star/container/XNameAccess.hpp>
#include <com/sun/star/io/XInputStream.hpp>
#include <com/sun/star/document/XBinaryStreamResolver.hpp>
#include <com/sun/star/document/XEmbeddedObjectResolver.hpp>
#include <com/sun/star/text/XTextContent.hpp>
#include <com/sun/star/xml/sax/SAXInvalidCharacterException.hpp>
#include <com/sun/star/uri/XUriReferenceFactory.hpp>
#include <com/sun/star/uri/UriReferenceFactory.hpp>
#include <com/sun/star/util/XNumberFormatsSupplier.hpp>
#include <com/sun/star/util/MeasureUnit.hpp>
#include <i18nlangtag/languagetag.hxx>
#include <comphelper/processfactory.hxx>
#include <comphelper/propertysetinfo.hxx>
#include <comphelper/propertyvalue.hxx>
#include <xmloff/attrlist.hxx>
#include <xmloff/namespacemap.hxx>
#include <xmloff/xmluconv.hxx>
#include <xmloff/xmlnamespace.hxx>
#include <xmloff/xmltoken.hxx>
#include <xmloff/xmlexp.hxx>
#include <xmloff/xmlnumfe.hxx>
#include <xmloff/xmlmetae.hxx>
#include <xmloff/XMLSettingsExportContext.hxx>
#include <xmloff/XMLEventExport.hxx>
#include <xmloff/ProgressBarHelper.hxx>
#include <XMLStarBasicExportHandler.hxx>
#include <XMLScriptExportHandler.hxx>
#include <xmloff/SettingsExportHelper.hxx>
#include <com/sun/star/document/XEventsSupplier.hpp>
#include <com/sun/star/document/XViewDataSupplier.hpp>
#include <com/sun/star/frame/XModel.hpp>
#include <com/sun/star/frame/XModule.hpp>
#include <xmloff/GradientStyle.hxx>
#include <xmloff/HatchStyle.hxx>
#include <xmloff/ImageStyle.hxx>
#include <TransGradientStyle.hxx>
#include <xmloff/MarkerStyle.hxx>
#include <xmloff/DashStyle.hxx>
#include <xmloff/XMLFontAutoStylePool.hxx>
#include <XMLImageMapExport.hxx>
#include <XMLBase64Export.hxx>
#include <xmloff/xmlerror.hxx>
#include <com/sun/star/style/XStyle.hpp>
#include <com/sun/star/style/XStyleFamiliesSupplier.hpp>
#include <com/sun/star/lang/ServiceNotRegisteredException.hpp>
#include <com/sun/star/lang/XMultiServiceFactory.hpp>
#include <com/sun/star/beans/PropertyAttribute.hpp>
#include <xmloff/XMLFilterServiceNames.h>
#include <XMLEmbeddedObjectExportFilter.hxx>
#include <XMLBasicExportFilter.hxx>
#include <cppuhelper/exc_hlp.hxx>
#include <cppuhelper/implbase.hxx>
#include <cppuhelper/supportsservice.hxx>
#include <comphelper/extract.hxx>
#include <comphelper/SetFlagContextHelper.hxx>
#include <PropertySetMerger.hxx>

#include <unotools/docinfohelper.hxx>
#include <com/sun/star/document/XDocumentProperties.hpp>
#include <com/sun/star/document/XDocumentPropertiesSupplier.hpp>
#include <com/sun/star/document/XMLOasisBasicExporter.hpp>
#include <com/sun/star/embed/XEncryptionProtectedSource2.hpp>
#include <com/sun/star/document/XGraphicStorageHandler.hpp>
#include <com/sun/star/rdf/XMetadatable.hpp>
#include <RDFaExportHelper.hxx>

#include <comphelper/xmltools.hxx>
#include <comphelper/graphicmimetype.hxx>

using namespace ::com::sun::star;
using namespace ::com::sun::star::uno;
using namespace ::com::sun::star::frame;
using namespace ::com::sun::star::container;
using namespace ::com::sun::star::lang;
using namespace ::com::sun::star::document;
using namespace ::com::sun::star::beans;
using namespace ::com::sun::star::xml::sax;
using namespace ::com::sun::star::io;
using namespace ::xmloff::token;

constexpr OUStringLiteral XML_MODEL_SERVICE_WRITER = u"com.sun.star.text.TextDocument";
constexpr OUStringLiteral XML_MODEL_SERVICE_CALC = u"com.sun.star.sheet.SpreadsheetDocument";
constexpr OUStringLiteral XML_MODEL_SERVICE_DRAW = u"com.sun.star.drawing.DrawingDocument";
constexpr OUStringLiteral XML_MODEL_SERVICE_IMPRESS = u"com.sun.star.presentation.PresentationDocument";
constexpr OUStringLiteral XML_MODEL_SERVICE_MATH = u"com.sun.star.formula.FormulaProperties";
constexpr OUStringLiteral XML_MODEL_SERVICE_CHART = u"com.sun.star.chart.ChartDocument";

constexpr OUStringLiteral XML_USEPRETTYPRINTING = u"UsePrettyPrinting";

constexpr OUStringLiteral XML_EMBEDDEDOBJECTGRAPHIC_URL_BASE = u"vnd.sun.star.GraphicObject:";
constexpr OUStringLiteral XML_EMBEDDEDOBJECT_URL_BASE = u"vnd.sun.star.EmbeddedObject:";

const std::pair<OUString, OUString> aServiceMap[] = {
    { XML_MODEL_SERVICE_WRITER, XML_EXPORT_FILTER_WRITER },
    { XML_MODEL_SERVICE_CALC, XML_EXPORT_FILTER_CALC },
    { XML_MODEL_SERVICE_IMPRESS, XML_EXPORT_FILTER_IMPRESS }, // Impress supports DrawingDocument,
    { XML_MODEL_SERVICE_DRAW, XML_EXPORT_FILTER_DRAW }, // too, so it must appear before Draw
    { XML_MODEL_SERVICE_MATH, XML_EXPORT_FILTER_MATH },
    { XML_MODEL_SERVICE_CHART, XML_EXPORT_FILTER_CHART },
};

namespace {

class SettingsExportFacade : public ::xmloff::XMLSettingsExportContext
{
public:
    explicit SettingsExportFacade( SvXMLExport& i_rExport )
        :m_rExport( i_rExport )
    {
    }

    virtual ~SettingsExportFacade()
    {
    }

    virtual void    AddAttribute( enum ::xmloff::token::XMLTokenEnum i_eName,
                                  const OUString& i_rValue ) override;
    virtual void    AddAttribute( enum ::xmloff::token::XMLTokenEnum i_eName,
                                  enum ::xmloff::token::XMLTokenEnum i_eValue ) override;

    virtual void    StartElement( enum ::xmloff::token::XMLTokenEnum i_eName ) override;
    virtual void    EndElement(   const bool i_bIgnoreWhitespace ) override;

    virtual void    Characters( const OUString& i_rCharacters ) override;

    virtual css::uno::Reference< css::uno::XComponentContext >
                    GetComponentContext() const override;
private:
    SvXMLExport&                    m_rExport;
    ::std::stack< OUString > m_aElements;
};

}

void SettingsExportFacade::AddAttribute( enum ::xmloff::token::XMLTokenEnum i_eName, const OUString& i_rValue )
{
    m_rExport.AddAttribute( XML_NAMESPACE_CONFIG, i_eName, i_rValue );
}

void SettingsExportFacade::AddAttribute( enum ::xmloff::token::XMLTokenEnum i_eName, enum ::xmloff::token::XMLTokenEnum i_eValue )
{
    m_rExport.AddAttribute( XML_NAMESPACE_CONFIG, i_eName, i_eValue );
}

void SettingsExportFacade::StartElement( enum ::xmloff::token::XMLTokenEnum i_eName )
{
    const OUString sElementName( m_rExport.GetNamespaceMap().GetQNameByKey( XML_NAMESPACE_CONFIG, GetXMLToken( i_eName ) ) );
    m_rExport.StartElement( sElementName, true/*i_bIgnoreWhitespace*/ );
    m_aElements.push( sElementName );
}

void SettingsExportFacade::EndElement( const bool i_bIgnoreWhitespace )
{
    const OUString sElementName( m_aElements.top() );
    m_rExport.EndElement( sElementName, i_bIgnoreWhitespace );
    m_aElements.pop();
}

void SettingsExportFacade::Characters( const OUString& i_rCharacters )
{
    m_rExport.GetDocHandler()->characters( i_rCharacters );
}

Reference< XComponentContext > SettingsExportFacade::GetComponentContext() const
{
    return m_rExport.getComponentContext();
}

namespace {

class SvXMLExportEventListener : public cppu::WeakImplHelper<
                            css::lang::XEventListener >
{
private:
    SvXMLExport*    pExport;

public:
    explicit SvXMLExportEventListener(SvXMLExport* pExport);

                            // XEventListener
    virtual void SAL_CALL disposing(const lang::EventObject& rEventObject) override;
};

}

SvXMLExportEventListener::SvXMLExportEventListener(SvXMLExport* pTempExport)
    : pExport(pTempExport)
{
}

// XEventListener
void SAL_CALL SvXMLExportEventListener::disposing( const lang::EventObject& )
{
    if (pExport)
    {
        pExport->DisposingModel();
        pExport = nullptr;
    }
}

class SvXMLExport_Impl
{
public:
    SvXMLExport_Impl();

    ::comphelper::UnoInterfaceToUniqueIdentifierMapper  maInterfaceToIdentifierMapper;
    uno::Reference< uri::XUriReferenceFactory >         mxUriReferenceFactory;
    OUString                                            msPackageURI;
    OUString                                            msPackageURIScheme;
    // Written OpenDocument file format doesn't fit to the created text document (#i69627#)
    bool                                                mbOutlineStyleAsNormalListStyle;

    uno::Reference< embed::XStorage >                   mxTargetStorage;

    std::optional<SvtSaveOptions::ODFSaneDefaultVersion> m_oOverrideODFVersion;

    /// name of stream in package, e.g., "content.xml"
    OUString mStreamName;

    OUString maSrcShellID;
    OUString maDestShellID;

    /// stack of backed up namespace maps
    /// long: depth at which namespace map has been backed up into the stack
    ::std::stack< ::std::pair< std::unique_ptr<SvXMLNamespaceMap>, tools::Long > > mNamespaceMaps;
    /// counts depth (number of open elements/start tags)
    tools::Long mDepth;

    ::std::unique_ptr< ::xmloff::RDFaExportHelper> mpRDFaHelper;

    bool                                                mbExportTextNumberElement;
    bool                                                mbNullDateInitialized;

    void SetSchemeOf( std::u16string_view rOrigFileName )
    {
        size_t nSep = rOrigFileName.find(':');
        if( nSep != std::u16string_view::npos )
            msPackageURIScheme = rOrigFileName.substr( 0, nSep );
    }
};

SvXMLExport_Impl::SvXMLExport_Impl()
:    mxUriReferenceFactory( uri::UriReferenceFactory::create(comphelper::getProcessComponentContext()) ),
    // Written OpenDocument file format doesn't fit to the created text document (#i69627#)
    mbOutlineStyleAsNormalListStyle( false ),
    mDepth( 0 ),
    mbExportTextNumberElement( false ),
    mbNullDateInitialized( false )
{
}

void SvXMLExport::SetDocHandler( const uno::Reference< xml::sax::XDocumentHandler > &rHandler )
{
    mxHandler = rHandler;
    mxExtHandler.set( mxHandler, UNO_QUERY );
}

void SvXMLExport::InitCtor_()
{
    // note: it is not necessary to add XML_NP_XML (it is declared implicitly)
    if( getExportFlags() & ~SvXMLExportFlags::OASIS )
    {
        mpNamespaceMap->Add( GetXMLToken(XML_NP_OFFICE), GetXMLToken(XML_N_OFFICE), XML_NAMESPACE_OFFICE );
        mpNamespaceMap->Add( GetXMLToken(XML_NP_OOO), GetXMLToken(XML_N_OOO), XML_NAMESPACE_OOO );
    }
    if( getExportFlags() & (SvXMLExportFlags::STYLES|SvXMLExportFlags::MASTERSTYLES|SvXMLExportFlags::AUTOSTYLES|SvXMLExportFlags::FONTDECLS) )
    {
        mpNamespaceMap->Add( GetXMLToken(XML_NP_FO), GetXMLToken(XML_N_FO_COMPAT), XML_NAMESPACE_FO );
    }
    if( getExportFlags() & (SvXMLExportFlags::META|SvXMLExportFlags::STYLES|SvXMLExportFlags::MASTERSTYLES|SvXMLExportFlags::AUTOSTYLES|SvXMLExportFlags::CONTENT|SvXMLExportFlags::SCRIPTS|SvXMLExportFlags::SETTINGS) )
    {
        mpNamespaceMap->Add( GetXMLToken(XML_NP_XLINK), GetXMLToken(XML_N_XLINK), XML_NAMESPACE_XLINK );
    }
    if( getExportFlags() & SvXMLExportFlags::SETTINGS )
    {
        mpNamespaceMap->Add( GetXMLToken(XML_NP_CONFIG), GetXMLToken(XML_N_CONFIG), XML_NAMESPACE_CONFIG );
    }

    if( getExportFlags() & (SvXMLExportFlags::META|SvXMLExportFlags::MASTERSTYLES|SvXMLExportFlags::CONTENT) )
    {
        mpNamespaceMap->Add( GetXMLToken(XML_NP_DC), GetXMLToken(XML_N_DC), XML_NAMESPACE_DC );
        mpNamespaceMap->Add( GetXMLToken(XML_NP_META), GetXMLToken(XML_N_META), XML_NAMESPACE_META );
    }
    if( getExportFlags() & (SvXMLExportFlags::STYLES|SvXMLExportFlags::MASTERSTYLES|SvXMLExportFlags::AUTOSTYLES|SvXMLExportFlags::CONTENT|SvXMLExportFlags::FONTDECLS) )
    {
        mpNamespaceMap->Add( GetXMLToken(XML_NP_STYLE), GetXMLToken(XML_N_STYLE), XML_NAMESPACE_STYLE );
    }

    // namespaces for documents
    if( getExportFlags() & (SvXMLExportFlags::STYLES|SvXMLExportFlags::AUTOSTYLES|SvXMLExportFlags::MASTERSTYLES|SvXMLExportFlags::CONTENT) )
    {
        mpNamespaceMap->Add( GetXMLToken(XML_NP_DC),    GetXMLToken(XML_N_DC),      XML_NAMESPACE_DC );
        mpNamespaceMap->Add( GetXMLToken(XML_NP_TEXT),  GetXMLToken(XML_N_TEXT),    XML_NAMESPACE_TEXT );
        mpNamespaceMap->Add( GetXMLToken(XML_NP_DRAW),  GetXMLToken(XML_N_DRAW),    XML_NAMESPACE_DRAW );
        mpNamespaceMap->Add( GetXMLToken(XML_NP_DR3D),  GetXMLToken(XML_N_DR3D),    XML_NAMESPACE_DR3D );
        mpNamespaceMap->Add( GetXMLToken(XML_NP_SVG),   GetXMLToken(XML_N_SVG_COMPAT),  XML_NAMESPACE_SVG );
        mpNamespaceMap->Add( GetXMLToken(XML_NP_CHART), GetXMLToken(XML_N_CHART),   XML_NAMESPACE_CHART );
        mpNamespaceMap->Add( GetXMLToken(XML_NP_RPT),   GetXMLToken(XML_N_RPT),     XML_NAMESPACE_REPORT );
        mpNamespaceMap->Add( GetXMLToken(XML_NP_TABLE), GetXMLToken(XML_N_TABLE),   XML_NAMESPACE_TABLE );
        mpNamespaceMap->Add( GetXMLToken(XML_NP_NUMBER),GetXMLToken(XML_N_NUMBER),  XML_NAMESPACE_NUMBER );
        mpNamespaceMap->Add( GetXMLToken(XML_NP_OOOW),  GetXMLToken(XML_N_OOOW),    XML_NAMESPACE_OOOW );
        mpNamespaceMap->Add( GetXMLToken(XML_NP_OOOC),  GetXMLToken(XML_N_OOOC),    XML_NAMESPACE_OOOC );
        mpNamespaceMap->Add( GetXMLToken(XML_NP_OF),    GetXMLToken(XML_N_OF),      XML_NAMESPACE_OF );

        if (getSaneDefaultVersion() & SvtSaveOptions::ODFSVER_EXTENDED)
        {
            mpNamespaceMap->Add(
                GetXMLToken(XML_NP_TABLE_EXT), GetXMLToken(XML_N_TABLE_EXT), XML_NAMESPACE_TABLE_EXT);
            mpNamespaceMap->Add(
                GetXMLToken(XML_NP_CALC_EXT), GetXMLToken(XML_N_CALC_EXT), XML_NAMESPACE_CALC_EXT);
            mpNamespaceMap->Add(
                GetXMLToken(XML_NP_DRAW_EXT), GetXMLToken(XML_N_DRAW_EXT), XML_NAMESPACE_DRAW_EXT);
            mpNamespaceMap->Add(
                GetXMLToken(XML_NP_LO_EXT), GetXMLToken(XML_N_LO_EXT),
                XML_NAMESPACE_LO_EXT);
            mpNamespaceMap->Add( GetXMLToken(XML_NP_FIELD), GetXMLToken(XML_N_FIELD), XML_NAMESPACE_FIELD );
        }
    }
    if( getExportFlags() & (SvXMLExportFlags::MASTERSTYLES|SvXMLExportFlags::CONTENT) )
    {
        mpNamespaceMap->Add( GetXMLToken(XML_NP_MATH), GetXMLToken(XML_N_MATH), XML_NAMESPACE_MATH );
        mpNamespaceMap->Add( GetXMLToken(XML_NP_FORM), GetXMLToken(XML_N_FORM), XML_NAMESPACE_FORM );
    }
    if( getExportFlags() & (SvXMLExportFlags::STYLES|SvXMLExportFlags::AUTOSTYLES|SvXMLExportFlags::MASTERSTYLES|SvXMLExportFlags::CONTENT|SvXMLExportFlags::SCRIPTS) )
    {
        mpNamespaceMap->Add( GetXMLToken(XML_NP_SCRIPT), GetXMLToken(XML_N_SCRIPT), XML_NAMESPACE_SCRIPT );
        mpNamespaceMap->Add( GetXMLToken(XML_NP_DOM), GetXMLToken(XML_N_DOM), XML_NAMESPACE_DOM );
    }
    if( getExportFlags() & SvXMLExportFlags::CONTENT )
    {
        mpNamespaceMap->Add( GetXMLToken(XML_NP_XFORMS_1_0), GetXMLToken(XML_N_XFORMS_1_0), XML_NAMESPACE_XFORMS );
        mpNamespaceMap->Add( GetXMLToken(XML_NP_XSD), GetXMLToken(XML_N_XSD), XML_NAMESPACE_XSD );
        mpNamespaceMap->Add( GetXMLToken(XML_NP_XSI), GetXMLToken(XML_N_XSI), XML_NAMESPACE_XSI );
        mpNamespaceMap->Add( GetXMLToken(XML_NP_FORMX), GetXMLToken(XML_N_FORMX), XML_NAMESPACE_FORMX );
    }

    // RDFa: needed for content and header/footer styles
    if( getExportFlags() & (SvXMLExportFlags::STYLES|SvXMLExportFlags::AUTOSTYLES|SvXMLExportFlags::MASTERSTYLES|SvXMLExportFlags::CONTENT) )
    {
        mpNamespaceMap->Add( GetXMLToken(XML_NP_XHTML),
            GetXMLToken(XML_N_XHTML), XML_NAMESPACE_XHTML );
    }
    // GRDDL: to convert RDFa and meta.xml to RDF
    if( getExportFlags() & (SvXMLExportFlags::META|SvXMLExportFlags::STYLES|SvXMLExportFlags::AUTOSTYLES|SvXMLExportFlags::MASTERSTYLES|SvXMLExportFlags::CONTENT) )
    {
        mpNamespaceMap->Add( GetXMLToken(XML_NP_GRDDL),
            GetXMLToken(XML_N_GRDDL), XML_NAMESPACE_GRDDL );
    }
    // CSS Text Level 3 for distributed text justification.
    if ( getExportFlags() & (SvXMLExportFlags::STYLES|SvXMLExportFlags::AUTOSTYLES|SvXMLExportFlags::MASTERSTYLES) )
    {
        mpNamespaceMap->Add(
            GetXMLToken(XML_NP_CSS3TEXT), GetXMLToken(XML_N_CSS3TEXT), XML_NAMESPACE_CSS3TEXT );
    }

    if (mxModel.is() && !mxEventListener.is())
    {
        mxEventListener.set( new SvXMLExportEventListener(this));
        mxModel->addEventListener(mxEventListener);
    }

    // Determine model type (#i51726#)
    DetermineModelType_();
}

// Shapes in Writer cannot be named via context menu (#i51726#)
void SvXMLExport::DetermineModelType_()
{
    meModelType = SvtModuleOptions::EFactory::UNKNOWN_FACTORY;

    if ( !mxModel.is() )
        return;

    meModelType = SvtModuleOptions::ClassifyFactoryByModel( mxModel );

    // note: MATH documents will throw NotInitializedException; maybe unit test problem
    if (meModelType == SvtModuleOptions::EFactory::WRITER)
    {
        uno::Reference<frame::XModule> const xModule(mxModel, uno::UNO_QUERY);
        bool const isBaseForm(xModule.is() &&
            xModule->getIdentifier() == "com.sun.star.sdb.FormDesign");
        if (isBaseForm)
        {
            switch (GetODFSaneDefaultVersion())
            {
                case SvtSaveOptions::ODFSVER_013_EXTENDED:
                    SAL_INFO("xmloff.core", "tdf#138209 force form export to ODF 1.2");
                    mpImpl->m_oOverrideODFVersion = SvtSaveOptions::ODFSVER_012_EXTENDED;
                    maUnitConv.overrideSaneDefaultVersion(SvtSaveOptions::ODFSVER_012_EXTENDED);
                    break;
                case SvtSaveOptions::ODFSVER_013:
                    SAL_INFO("xmloff.core", "tdf#138209 force form export to ODF 1.2");
                    mpImpl->m_oOverrideODFVersion = SvtSaveOptions::ODFSVER_012;
                    maUnitConv.overrideSaneDefaultVersion(SvtSaveOptions::ODFSVER_012);
                    break;
                default:
                    break;
            }
        }
    }
}

SvXMLExport::SvXMLExport(
    const uno::Reference< uno::XComponentContext >& xContext,
    OUString const & implementationName,
    sal_Int16 const eDefaultMeasureUnit /*css::util::MeasureUnit*/,
    const enum XMLTokenEnum eClass, SvXMLExportFlags nExportFlags )
:   mpImpl( new SvXMLExport_Impl ),
    m_xContext(xContext), m_implementationName(implementationName),
    mxAttrList( new SvXMLAttributeList ),
    mpNamespaceMap( new SvXMLNamespaceMap ),
    mpAuthorIDs( new SvtSecurityMapPersonalInfo ),
    maUnitConv(xContext, util::MeasureUnit::MM_100TH, eDefaultMeasureUnit, getSaneDefaultVersion()),
    meClass( eClass ),
    mnExportFlags( nExportFlags ),
    mnErrorFlags( SvXMLErrorFlags::NO ),
    msWS( GetXMLToken(XML_WS) ),
    mbSaveLinkedSections(true),
    mbAutoStylesCollected(false)
{
    SAL_WARN_IF( !xContext.is(), "xmloff.core", "got no service manager" );
    InitCtor_();
}

SvXMLExport::SvXMLExport(
    const css::uno::Reference< css::uno::XComponentContext >& xContext,
    OUString const & implementationName,
    const OUString &rFileName,
    sal_Int16 const eDefaultMeasureUnit /*css::util::MeasureUnit*/,
    const uno::Reference< xml::sax::XDocumentHandler > & rHandler)
:   mpImpl( new SvXMLExport_Impl ),
    m_xContext(xContext), m_implementationName(implementationName),
    mxHandler( rHandler ),
    mxExtHandler( rHandler, uno::UNO_QUERY ),
    mxAttrList( new SvXMLAttributeList ),
    msOrigFileName( rFileName ),
    mpNamespaceMap( new SvXMLNamespaceMap ),
    mpAuthorIDs( new SvtSecurityMapPersonalInfo ),
    maUnitConv(xContext, util::MeasureUnit::MM_100TH, eDefaultMeasureUnit, getSaneDefaultVersion()),
    meClass( XML_TOKEN_INVALID ),
    mnExportFlags( SvXMLExportFlags::NONE ),
    mnErrorFlags( SvXMLErrorFlags::NO ),
    msWS( GetXMLToken(XML_WS) ),
    mbSaveLinkedSections(true),
    mbAutoStylesCollected(false)
{
    SAL_WARN_IF( !xContext.is(), "xmloff.core", "got no service manager" );
    mpImpl->SetSchemeOf( msOrigFileName );
    InitCtor_();

    if (mxNumberFormatsSupplier.is())
        mpNumExport.reset( new SvXMLNumFmtExport(*this, mxNumberFormatsSupplier) );
}

SvXMLExport::SvXMLExport(
    const css::uno::Reference< css::uno::XComponentContext >& xContext,
    OUString const & implementationName,
    const OUString &rFileName,
    const uno::Reference< xml::sax::XDocumentHandler > & rHandler,
    const Reference< XModel >& rModel,
    FieldUnit const eDefaultFieldUnit,
    SvXMLExportFlags nExportFlag)
:   mpImpl( new SvXMLExport_Impl ),
    m_xContext(xContext), m_implementationName(implementationName),
    mxModel( rModel ),
    mxHandler( rHandler ),
    mxExtHandler( rHandler, uno::UNO_QUERY ),
    mxNumberFormatsSupplier (rModel, uno::UNO_QUERY),
    mxAttrList( new SvXMLAttributeList ),
    msOrigFileName( rFileName ),
    mpNamespaceMap( new SvXMLNamespaceMap ),
    mpAuthorIDs( new SvtSecurityMapPersonalInfo ),
    maUnitConv( xContext,
                util::MeasureUnit::MM_100TH,
                SvXMLUnitConverter::GetMeasureUnit(eDefaultFieldUnit),
                getSaneDefaultVersion()),
    meClass( XML_TOKEN_INVALID ),
    mnExportFlags( nExportFlag ),
    mnErrorFlags( SvXMLErrorFlags::NO ),
    msWS( GetXMLToken(XML_WS) ),
    mbSaveLinkedSections(true),
    mbAutoStylesCollected(false)
{
    SAL_WARN_IF(!xContext.is(), "xmloff.core", "got no service manager" );
    mpImpl->SetSchemeOf( msOrigFileName );
    InitCtor_();

    if (mxNumberFormatsSupplier.is())
        mpNumExport.reset( new SvXMLNumFmtExport(*this, mxNumberFormatsSupplier) );
}

SvXMLExport::~SvXMLExport()
{
    mpXMLErrors.reset();
    mpImageMapExport.reset();
    mpEventExport.reset();
    mpNamespaceMap.reset();
    if (mpProgressBarHelper || mpNumExport)
    {
        if (mxExportInfo.is())
        {
            uno::Reference< beans::XPropertySetInfo > xPropertySetInfo = mxExportInfo->getPropertySetInfo();
            if (xPropertySetInfo.is())
            {
                if (mpProgressBarHelper)
                {
                    static constexpr OUStringLiteral sProgressMax(XML_PROGRESSMAX);
                    static constexpr OUStringLiteral sProgressCurrent(XML_PROGRESSCURRENT);
                    static constexpr OUStringLiteral sRepeat(XML_PROGRESSREPEAT);
                    if (xPropertySetInfo->hasPropertyByName(sProgressMax) &&
                        xPropertySetInfo->hasPropertyByName(sProgressCurrent))
                    {
                        sal_Int32 nProgressMax(mpProgressBarHelper->GetReference());
                        sal_Int32 nProgressCurrent(mpProgressBarHelper->GetValue());
                        mxExportInfo->setPropertyValue(sProgressMax, uno::Any(nProgressMax));
                        mxExportInfo->setPropertyValue(sProgressCurrent, uno::Any(nProgressCurrent));
                    }
                    if (xPropertySetInfo->hasPropertyByName(sRepeat))
                        mxExportInfo->setPropertyValue(sRepeat, css::uno::Any(mpProgressBarHelper->GetRepeat()));
                }
                if (mpNumExport && (mnExportFlags & (SvXMLExportFlags::AUTOSTYLES | SvXMLExportFlags::STYLES)))
                {
                    static constexpr OUStringLiteral sWrittenNumberFormats(XML_WRITTENNUMBERSTYLES);
                    if (xPropertySetInfo->hasPropertyByName(sWrittenNumberFormats))
                    {
                        mxExportInfo->setPropertyValue(sWrittenNumberFormats, Any(mpNumExport->GetWasUsed()));
                    }
                }
            }
        }
        mpProgressBarHelper.reset();
        mpNumExport.reset();
    }

    if (mxEventListener.is() && mxModel.is())
        mxModel->removeEventListener(mxEventListener);
}

// XExporter
void SAL_CALL SvXMLExport::setSourceDocument( const uno::Reference< lang::XComponent >& xDoc )
{
    mxModel.set( xDoc, UNO_QUERY );
    if( !mxModel.is() )
        throw lang::IllegalArgumentException();
    if (mxModel.is() && ! mxEventListener.is())
    {
        mxEventListener.set( new SvXMLExportEventListener(this));
        mxModel->addEventListener(mxEventListener);
    }

    if(!mxNumberFormatsSupplier.is() )
    {
        mxNumberFormatsSupplier.set(mxModel, css::uno::UNO_QUERY);
        if(mxNumberFormatsSupplier.is() && mxHandler.is())
            mpNumExport.reset( new SvXMLNumFmtExport(*this, mxNumberFormatsSupplier) );
    }
    if (mxExportInfo.is())
    {
        uno::Reference< beans::XPropertySetInfo > xPropertySetInfo = mxExportInfo->getPropertySetInfo();
        if (xPropertySetInfo.is())
        {
            OUString sUsePrettyPrinting(XML_USEPRETTYPRINTING);
            if (xPropertySetInfo->hasPropertyByName(sUsePrettyPrinting))
            {
                uno::Any aAny = mxExportInfo->getPropertyValue(sUsePrettyPrinting);
                if (::cppu::any2bool(aAny))
                    mnExportFlags |= SvXMLExportFlags::PRETTY;
                else
                    mnExportFlags &= ~SvXMLExportFlags::PRETTY;
            }

            if (mpNumExport && (mnExportFlags & (SvXMLExportFlags::AUTOSTYLES | SvXMLExportFlags::STYLES)))
            {
                OUString sWrittenNumberFormats(XML_WRITTENNUMBERSTYLES);
                if (xPropertySetInfo->hasPropertyByName(sWrittenNumberFormats))
                {
                    uno::Any aAny = mxExportInfo->getPropertyValue(sWrittenNumberFormats);
                    uno::Sequence<sal_Int32> aWasUsed;
                    if(aAny >>= aWasUsed)
                        mpNumExport->SetWasUsed(aWasUsed);
                }
            }
        }
    }

    // namespaces for user defined attributes
    Reference< XMultiServiceFactory > xFactory( mxModel,    UNO_QUERY );
    if( xFactory.is() )
    {
        try
        {
            Reference < XInterface > xIfc =
                xFactory->createInstance("com.sun.star.xml.NamespaceMap");
            if( xIfc.is() )
            {
                Reference< XNameAccess > xNamespaceMap( xIfc, UNO_QUERY );
                if( xNamespaceMap.is() )
                {
                    const Sequence< OUString > aPrefixes( xNamespaceMap->getElementNames() );
                    for( OUString const & prefix : aPrefixes )
                    {
                        OUString aURL;
                        if( xNamespaceMap->getByName( prefix ) >>= aURL )
                            GetNamespaceMap_().Add( prefix, aURL );
                    }
                }
            }
        }
        catch(const css::uno::Exception&)
        {
        }
    }

    // Determine model type (#i51726#)
    DetermineModelType_();
}

// XInitialize
void SAL_CALL SvXMLExport::initialize( const uno::Sequence< uno::Any >& aArguments )
{
    // #93186# we need to queryInterface every single Any with any expected outcome. This variable hold the queryInterface results.

    for( const auto& rAny : aArguments )
    {
        Reference<XInterface> xValue;
        rAny >>= xValue;

        // status indicator
        uno::Reference<task::XStatusIndicator> xTmpStatus( xValue, UNO_QUERY );
        if ( xTmpStatus.is() )
            mxStatusIndicator = xTmpStatus;

        // graphic storage handler
        uno::Reference<document::XGraphicStorageHandler> xGraphicStorageHandler(xValue, UNO_QUERY);
        if (xGraphicStorageHandler.is())
            mxGraphicStorageHandler = xGraphicStorageHandler;

        // object resolver
        uno::Reference<document::XEmbeddedObjectResolver> xTmpObjectResolver(
            xValue, UNO_QUERY );
        if ( xTmpObjectResolver.is() )
            mxEmbeddedResolver = xTmpObjectResolver;

        // document handler
        uno::Reference<xml::sax::XDocumentHandler> xTmpDocHandler(
            xValue, UNO_QUERY );
        if( xTmpDocHandler.is() )
        {
            mxHandler = xTmpDocHandler;
            rAny >>= mxExtHandler;

            if (mxNumberFormatsSupplier.is() && mpNumExport == nullptr)
                mpNumExport.reset( new SvXMLNumFmtExport(*this, mxNumberFormatsSupplier) );
        }

        // property set to transport data across
        uno::Reference<beans::XPropertySet> xTmpPropertySet(
            xValue, UNO_QUERY );
        if( xTmpPropertySet.is() )
            mxExportInfo = xTmpPropertySet;
    }

    if( !mxExportInfo.is() )
        return;

    uno::Reference< beans::XPropertySetInfo > xPropertySetInfo =
        mxExportInfo->getPropertySetInfo();
    static constexpr OUStringLiteral sBaseURI = u"BaseURI";
    if( xPropertySetInfo->hasPropertyByName(sBaseURI) )
    {
        uno::Any aAny = mxExportInfo->getPropertyValue(sBaseURI);
        aAny >>= msOrigFileName;
        mpImpl->msPackageURI = msOrigFileName;
        mpImpl->SetSchemeOf( msOrigFileName );
    }
    OUString sRelPath;
    static constexpr OUStringLiteral sStreamRelPath = u"StreamRelPath";
    if( xPropertySetInfo->hasPropertyByName(sStreamRelPath) )
    {
        uno::Any aAny = mxExportInfo->getPropertyValue(sStreamRelPath);
        aAny >>= sRelPath;
    }
    OUString sName;
    static constexpr OUStringLiteral sStreamName = u"StreamName";
    if( xPropertySetInfo->hasPropertyByName(sStreamName) )
    {
        uno::Any aAny = mxExportInfo->getPropertyValue(sStreamName);
        aAny >>= sName;
    }
    if( !msOrigFileName.isEmpty() && !sName.isEmpty() )
    {
        INetURLObject aBaseURL( msOrigFileName );
        if( !sRelPath.isEmpty() )
            aBaseURL.insertName( sRelPath );
        aBaseURL.insertName( sName );
        msOrigFileName = aBaseURL.GetMainURL(INetURLObject::DecodeMechanism::ToIUri);
    }
    mpImpl->mStreamName = sName; // Note: may be empty (XSLT)

    // Written OpenDocument file format doesn't fit to the created text document (#i69627#)
    static const OUStringLiteral sOutlineStyleAsNormalListStyle(
            u"OutlineStyleAsNormalListStyle" );
    if( xPropertySetInfo->hasPropertyByName( sOutlineStyleAsNormalListStyle ) )
    {
        uno::Any aAny = mxExportInfo->getPropertyValue( sOutlineStyleAsNormalListStyle );
        aAny >>= mpImpl->mbOutlineStyleAsNormalListStyle;
    }

    OUString sTargetStorage( "TargetStorage" );
    if( xPropertySetInfo->hasPropertyByName( sTargetStorage ) )
        mxExportInfo->getPropertyValue( sTargetStorage ) >>= mpImpl->mxTargetStorage;

    static const OUStringLiteral sExportTextNumberElement(
            u"ExportTextNumberElement" );
    if( xPropertySetInfo->hasPropertyByName( sExportTextNumberElement ) )
    {
        uno::Any aAny = mxExportInfo->getPropertyValue( sExportTextNumberElement );
        aAny >>= mpImpl->mbExportTextNumberElement;
    }
}

// XFilter
sal_Bool SAL_CALL SvXMLExport::filter( const uno::Sequence< beans::PropertyValue >& aDescriptor )
{
    // check for xHandler first... should have been supplied in initialize
    if( !mxHandler.is() )
        return false;

    try
    {
        const SvXMLExportFlags nTest =
            SvXMLExportFlags::META|SvXMLExportFlags::STYLES|SvXMLExportFlags::CONTENT|SvXMLExportFlags::SETTINGS;
        if( (mnExportFlags & nTest) == nTest && msOrigFileName.isEmpty() )
        {
            // evaluate descriptor only for flat files and if a base URI
            // has not been provided already

            for( const auto& rProp : aDescriptor )
            {
                const OUString& rPropName = rProp.Name;
                const Any& rValue = rProp.Value;

                if ( rPropName == "FileName" )
                {
                    if( !(rValue >>= msOrigFileName ) )
                        return false;
                }
                else if ( rPropName == "FilterName" )
                {
                    if( !(rValue >>= msFilterName ) )
                        return false;
                }
            }
        }

        for( const auto& rProp : aDescriptor )
        {
            const OUString& rPropName = rProp.Name;
            const Any& rValue = rProp.Value;

            if (rPropName == "SourceShellID")
            {
                if (!(rValue >>= mpImpl->maSrcShellID))
                    return false;
            }
            else if (rPropName == "DestinationShellID")
            {
                if (!(rValue >>= mpImpl->maDestShellID))
                    return false;
            }
            else if( rPropName == "ImageFilter")
            {
                if (!(rValue >>= msImgFilterName))
                    return false;
            }
        }


        exportDoc( meClass );
    }
    catch(const uno::Exception& e)
    {
        // We must catch exceptions, because according to the
        // API definition export must not throw one!
        css::uno::Any ex(cppu::getCaughtException());
        OUString sMessage( ex.getValueTypeName() + ": \"" + e.Message + "\"");
        if (e.Context.is())
        {
            const char* pContext = typeid(*e.Context).name();
            sMessage += " (context: " + OUString::createFromAscii(pContext) + " )";
        }
        SetError( XMLERROR_FLAG_ERROR | XMLERROR_FLAG_SEVERE | XMLERROR_API,
                  Sequence<OUString>(), sMessage, nullptr );
    }

    // return true only if no error occurred
    return (mnErrorFlags & (SvXMLErrorFlags::DO_NOTHING|SvXMLErrorFlags::ERROR_OCCURRED)) == SvXMLErrorFlags::NO;
}

void SAL_CALL SvXMLExport::cancel()
{
    // stop export
    Sequence<OUString> aEmptySeq;
    SetError(XMLERROR_CANCEL|XMLERROR_FLAG_SEVERE, aEmptySeq);
}

OUString SAL_CALL SvXMLExport::getName(  )
{
    return msFilterName;
}

void SAL_CALL SvXMLExport::setName( const OUString& )
{
    // do nothing, because it is not possible to set the FilterName
}

// XServiceInfo
OUString SAL_CALL SvXMLExport::getImplementationName(  )
{
    return m_implementationName;
}

sal_Bool SAL_CALL SvXMLExport::supportsService( const OUString& rServiceName )
{
    return cppu::supportsService(this, rServiceName);
}

uno::Sequence< OUString > SAL_CALL SvXMLExport::getSupportedServiceNames(  )
{
    return { "com.sun.star.document.ExportFilter", "com.sun.star.xml.XMLExportFilter" };
}

OUString
SvXMLExport::EnsureNamespace(OUString const & i_rNamespace)
{
    OUString const aPreferredPrefix("gen");
    OUString sPrefix;
    sal_uInt16 nKey( GetNamespaceMap_().GetKeyByName( i_rNamespace ) );
    if( XML_NAMESPACE_UNKNOWN == nKey )
    {
        // There is no prefix for the namespace, so
        // we have to generate one and have to add it.
        sPrefix = aPreferredPrefix;
        nKey = GetNamespaceMap_().GetKeyByPrefix( sPrefix );
        sal_Int32 n( 0 );
        OUStringBuffer buf;
        while( nKey != USHRT_MAX )
        {
            buf.append( aPreferredPrefix );
            buf.append( ++n );
            sPrefix = buf.makeStringAndClear();
            nKey = GetNamespaceMap_().GetKeyByPrefix( sPrefix );
        }

        if (mpImpl->mNamespaceMaps.empty()
            || (mpImpl->mNamespaceMaps.top().second != mpImpl->mDepth))
        {
            // top was created for lower depth... need a new namespace map!
            auto pNew = new SvXMLNamespaceMap( *mpNamespaceMap );
            mpImpl->mNamespaceMaps.push(
                ::std::make_pair(std::move(mpNamespaceMap), mpImpl->mDepth) );
            mpNamespaceMap.reset( pNew );
        }

        // add the namespace to the map and as attribute
        mpNamespaceMap->Add( sPrefix, i_rNamespace );
        AddAttribute( GetXMLToken(XML_XMLNS) + ":" + sPrefix, i_rNamespace );
    }
    else
    {
        // If there is a prefix for the namespace, reuse that.
        sPrefix = GetNamespaceMap_().GetPrefixByKey( nKey );
    }
    return sPrefix;
}

void SvXMLExport::AddAttribute( sal_uInt16 nPrefixKey, const OUString& rName,
                              const OUString& rValue )
{
    AddAttribute(GetNamespaceMap_().GetQNameByKey(nPrefixKey, rName), rValue);
}

void SvXMLExport::AddAttribute( sal_uInt16 nPrefixKey,
                                enum XMLTokenEnum eName,
                                const OUString& rValue )
{
    AddAttribute(nPrefixKey, GetXMLToken(eName), rValue);
}

void SvXMLExport::AddAttribute( sal_uInt16 nPrefixKey,
                                enum XMLTokenEnum eName,
                                enum XMLTokenEnum eValue)
{
    AddAttribute(nPrefixKey, eName, GetXMLToken(eValue));
}

void SvXMLExport::AddAttribute( const OUString& rQName,
                                const OUString& rValue )
{
      mxAttrList->AddAttribute(
        rQName,
        rValue );
}

void SvXMLExport::AddAttribute( const OUString& rQName,
                                enum ::xmloff::token::XMLTokenEnum eValue )
{
    AddAttribute(rQName, GetXMLToken(eValue));
}

void SvXMLExport::AddLanguageTagAttributes( sal_uInt16 nPrefix, sal_uInt16 nPrefixRfc,
        const css::lang::Locale& rLocale, bool bWriteEmpty )
{
    if (rLocale.Variant.isEmpty())
    {
        // Per convention The BCP 47 string is always stored in Variant, if
        // that is empty we have a plain language-country combination, no need
        // to convert to LanguageTag first. Also catches the case of empty
        // locale denoting system locale.
        xmloff::token::XMLTokenEnum eLanguage, eCountry;
        eLanguage = XML_LANGUAGE;
        eCountry  = XML_COUNTRY;
        if (bWriteEmpty || !rLocale.Language.isEmpty())
            AddAttribute( nPrefix, eLanguage, rLocale.Language);
        if (bWriteEmpty || !rLocale.Country.isEmpty())
            AddAttribute( nPrefix, eCountry, rLocale.Country);
    }
    else
    {
        LanguageTag aLanguageTag( rLocale);
        AddLanguageTagAttributes( nPrefix, nPrefixRfc, aLanguageTag, bWriteEmpty);
    }
}

void SvXMLExport::AddLanguageTagAttributes( sal_uInt16 nPrefix, sal_uInt16 nPrefixRfc,
        const LanguageTag& rLanguageTag, bool bWriteEmpty )
{
    if (rLanguageTag.isIsoODF())
    {
        if (bWriteEmpty || !rLanguageTag.isSystemLocale())
        {
            AddAttribute( nPrefix, XML_LANGUAGE, rLanguageTag.getLanguage());
            if (rLanguageTag.hasScript() && getSaneDefaultVersion() >= SvtSaveOptions::ODFSVER_012)
                AddAttribute( nPrefix, XML_SCRIPT, rLanguageTag.getScript());
            if (bWriteEmpty || !rLanguageTag.getCountry().isEmpty())
                AddAttribute( nPrefix, XML_COUNTRY, rLanguageTag.getCountry());
        }
    }
    else
    {
        if (getSaneDefaultVersion() >= SvtSaveOptions::ODFSVER_012)
            AddAttribute( nPrefixRfc, XML_RFC_LANGUAGE_TAG, rLanguageTag.getBcp47());
        // Also in case of non-pure-ISO tag store best matching fo: attributes
        // for consumers not handling *:rfc-language-tag, ensuring that only
        // valid ISO codes are stored. Here the bWriteEmpty parameter has no
        // meaning.
        OUString aLanguage, aScript, aCountry;
        rLanguageTag.getIsoLanguageScriptCountry( aLanguage, aScript, aCountry);
        if (!aLanguage.isEmpty())
        {
            AddAttribute( nPrefix, XML_LANGUAGE, aLanguage);
            if (!aScript.isEmpty() && getSaneDefaultVersion() >= SvtSaveOptions::ODFSVER_012)
                AddAttribute( nPrefix, XML_SCRIPT, aScript);
            if (!aCountry.isEmpty())
                AddAttribute( nPrefix, XML_COUNTRY, aCountry);
        }
    }
}

void SvXMLExport::AddAttributeList( const uno::Reference< xml::sax::XAttributeList >& xAttrList )
{
    if( xAttrList.is())
        mxAttrList->AppendAttributeList( xAttrList );
}

void SvXMLExport::ClearAttrList()
{
    mxAttrList->Clear();
}

#ifdef DBG_UTIL
void SvXMLExport::CheckAttrList()
{
    SAL_WARN_IF( mxAttrList->getLength(), "xmloff.core", "XMLExport::CheckAttrList: list is not empty" );
}
#endif

void SvXMLExport::ImplExportMeta()
{
    CheckAttrList();

    ExportMeta_();
}

void SvXMLExport::ImplExportSettings()
{
    CheckAttrList();

    ::std::vector< SettingsGroup > aSettings;
    sal_Int32 nSettingsCount = 0;

    // view settings
    uno::Sequence< beans::PropertyValue > aViewSettings;
    GetViewSettingsAndViews( aViewSettings );
    aSettings.emplace_back( XML_VIEW_SETTINGS, aViewSettings );
    nSettingsCount += aViewSettings.getLength();

    // configuration settings
    uno::Sequence<beans::PropertyValue> aConfigSettings;
    GetConfigurationSettings( aConfigSettings );
    aSettings.emplace_back( XML_CONFIGURATION_SETTINGS, aConfigSettings );
    nSettingsCount += aConfigSettings.getLength();

    // any document specific settings
    nSettingsCount += GetDocumentSpecificSettings( aSettings );

    {
        SvXMLElementExport aElem( *this,
                                nSettingsCount != 0,
                                XML_NAMESPACE_OFFICE, XML_SETTINGS,
                                true, true );

        SettingsExportFacade aSettingsExportContext( *this );
        XMLSettingsExportHelper aSettingsExportHelper( aSettingsExportContext );

        for (auto const& settings : aSettings)
        {
            if ( !settings.aSettings.hasElements() )
                continue;

            const OUString& sSettingsName( GetXMLToken( settings.eGroupName ) );
            OUString sQName = GetNamespaceMap().GetQNameByKey( XML_NAMESPACE_OOO, sSettingsName );
            aSettingsExportHelper.exportAllSettings( settings.aSettings, sQName );
        }
    }
}

void SvXMLExport::ImplExportStyles()
{
    CheckAttrList();

    {
        // <style:styles>
        SvXMLElementExport aElem( *this, XML_NAMESPACE_OFFICE, XML_STYLES,
                                true, true );

        ExportStyles_( false );
    }

    // transfer style names (+ families) TO other components (if appropriate)
    if( ( mnExportFlags & SvXMLExportFlags::CONTENT ) || !mxExportInfo.is() )
        return;

    static constexpr OUStringLiteral sStyleNames( u"StyleNames" );
    static constexpr OUStringLiteral sStyleFamilies( u"StyleFamilies" );
    uno::Reference< beans::XPropertySetInfo > xPropertySetInfo = mxExportInfo->getPropertySetInfo();
    if ( xPropertySetInfo->hasPropertyByName( sStyleNames ) && xPropertySetInfo->hasPropertyByName( sStyleFamilies ) )
    {
        Sequence<sal_Int32> aStyleFamilies;
        Sequence<OUString> aStyleNames;
        mxAutoStylePool->GetRegisteredNames( aStyleFamilies, aStyleNames );
        mxExportInfo->setPropertyValue( sStyleNames, Any( aStyleNames ) );
        mxExportInfo->setPropertyValue( sStyleFamilies,
                                       Any( aStyleFamilies ) );
    }
}

void SvXMLExport::ImplExportAutoStyles()
{
    // transfer style names (+ families) FROM other components (if appropriate)
    OUString sStyleNames( "StyleNames" );
    OUString sStyleFamilies( "StyleFamilies" );
    if( ( !( mnExportFlags & SvXMLExportFlags::STYLES ) )
        && mxExportInfo.is()
        && mxExportInfo->getPropertySetInfo()->hasPropertyByName( sStyleNames )
        && mxExportInfo->getPropertySetInfo()->hasPropertyByName( sStyleFamilies ) )
    {
        Sequence<sal_Int32> aStyleFamilies;
        mxExportInfo->getPropertyValue( sStyleFamilies ) >>= aStyleFamilies;
        Sequence<OUString> aStyleNames;
        mxExportInfo->getPropertyValue( sStyleNames ) >>= aStyleNames;
        mxAutoStylePool->RegisterNames( aStyleFamilies, aStyleNames );
    }

    {
        // <style:automatic-styles>
        SvXMLElementExport aElem( *this, XML_NAMESPACE_OFFICE,
                                  XML_AUTOMATIC_STYLES, true, true );

        ExportAutoStyles_();
    }
}

void SvXMLExport::ImplExportMasterStyles()
{
    // <style:master-styles>
    SvXMLElementExport aElem( *this, XML_NAMESPACE_OFFICE, XML_MASTER_STYLES,
                            true, true );

    ExportMasterStyles_();
}

void SvXMLExport::ImplExportContent()
{
    CheckAttrList();

    {
        SvXMLElementExport aElement( *this, XML_NAMESPACE_OFFICE, XML_BODY,
                                  true, true );
        {
            XMLTokenEnum eClass = meClass;
            if( XML_TEXT_GLOBAL == eClass )
            {
                AddAttribute( XML_NAMESPACE_TEXT, XML_GLOBAL,
                      GetXMLToken( XML_TRUE ) );
                eClass = XML_TEXT;
            }
            if ( XML_GRAPHICS == eClass )
                eClass = XML_DRAWING;
            // <office:body ...>
            SetBodyAttributes();
            SvXMLElementExport aElem( *this, meClass != XML_TOKEN_INVALID,
                                      XML_NAMESPACE_OFFICE, eClass,
                                        true, true );

            ExportContent_();
        }
    }
}

void SvXMLExport::SetBodyAttributes()
{
}

static void
lcl_AddGrddl(SvXMLExport const & rExport, const SvXMLExportFlags /*nExportMode*/)
{
    // check version >= 1.2
    switch (rExport.getSaneDefaultVersion()) {
        case SvtSaveOptions::ODFSVER_011: // fall through
        case SvtSaveOptions::ODFSVER_010: return;
        default: break;
    }

    // #i115030#: disabled, the XSLT is not finished, and not available via HTTP
#if 0
    if (SvXMLExportFlags::SETTINGS != nExportMode) // meta, content, styles
    {
        rExport.AddAttribute( XML_NAMESPACE_GRDDL, XML_TRANSFORMATION,
            OUString("http://FIXME") );
    }
#endif
}

void SvXMLExport::addChaffWhenEncryptedStorage()
{
    uno::Reference< embed::XEncryptionProtectedSource2 > xEncr(mpImpl->mxTargetStorage, uno::UNO_QUERY);

    if (xEncr.is() && xEncr->hasEncryptionData() && mxExtHandler.is())
    {
        mxExtHandler->comment(OStringToOUString(comphelper::xml::makeXMLChaff(), RTL_TEXTENCODING_ASCII_US));
    }
}

auto SvXMLExport::GetODFVersionAttributeValue() const -> char const*
{
    char const* pVersion(nullptr);
    switch (getSaneDefaultVersion())
    {
    case SvtSaveOptions::ODFSVER_013_EXTENDED: [[fallthrough]];
    case SvtSaveOptions::ODFSVER_013: pVersion = "1.3"; break;
    case SvtSaveOptions::ODFSVER_012_EXTENDED: [[fallthrough]];
    case SvtSaveOptions::ODFSVER_012_EXT_COMPAT: [[fallthrough]];
    case SvtSaveOptions::ODFSVER_012: pVersion = "1.2"; break;
    case SvtSaveOptions::ODFSVER_011: pVersion = "1.1"; break;
    case SvtSaveOptions::ODFSVER_010: break;

    default:
        assert(!"xmloff::SvXMLExport::exportDoc(), unexpected odf default version!");
    }
    return pVersion;
}

ErrCode SvXMLExport::exportDoc( enum ::xmloff::token::XMLTokenEnum eClass )
{
    bool bOwnGraphicResolver = false;
    bool bOwnEmbeddedResolver = false;

    if (!mxGraphicStorageHandler.is() || !mxEmbeddedResolver.is())
    {
        Reference< XMultiServiceFactory > xFactory( mxModel,    UNO_QUERY );
        if( xFactory.is() )
        {
            try
            {
                if (!mxGraphicStorageHandler.is())
                {
                    mxGraphicStorageHandler.set(xFactory->createInstance( "com.sun.star.document.ExportGraphicStorageHandler"), UNO_QUERY);
                    bOwnGraphicResolver = mxGraphicStorageHandler.is();
                }

                if( !mxEmbeddedResolver.is() )
                {
                    mxEmbeddedResolver.set(
                        xFactory->createInstance( "com.sun.star.document.ExportEmbeddedObjectResolver" ), UNO_QUERY);
                    bOwnEmbeddedResolver = mxEmbeddedResolver.is();
                }
            }
            catch(const css::uno::Exception&)
            {
            }
        }
    }
    if( (getExportFlags() & SvXMLExportFlags::OASIS) == SvXMLExportFlags::NONE )
    {
        try
        {
            static ::comphelper::PropertyMapEntry const aInfoMap[] =
            {
                { OUString("Class"), 0,
                    ::cppu::UnoType<OUString>::get(),
                      PropertyAttribute::MAYBEVOID, 0},
            };
            Reference< XPropertySet > xConvPropSet(
                ::comphelper::GenericPropertySet_CreateInstance(
                        new ::comphelper::PropertySetInfo( aInfoMap ) ) );

            xConvPropSet->setPropertyValue( "Class", Any(GetXMLToken( eClass )) );

            Reference< XPropertySet > xPropSet =
                mxExportInfo.is()
                ?  PropertySetMerger_CreateInstance( mxExportInfo,
                                                     xConvPropSet )
                : xConvPropSet;

            Sequence<Any> aArgs{ Any(mxHandler), Any(xPropSet), Any(mxModel) };
            // get filter component
            Reference< xml::sax::XDocumentHandler > xTmpDocHandler(
                m_xContext->getServiceManager()->createInstanceWithArgumentsAndContext("com.sun.star.comp.Oasis2OOoTransformer", aArgs, m_xContext),
                UNO_QUERY);
            SAL_WARN_IF(!xTmpDocHandler.is(), "xmloff.core", "can't instantiate OASIS transformer component" );
            if( xTmpDocHandler.is() )
            {
                mxHandler = xTmpDocHandler;
                mxExtHandler.set( mxHandler, UNO_QUERY );
            }
        }
        catch(const css::uno::Exception&)
        {
        }
    }

    mxHandler->startDocument();

    addChaffWhenEncryptedStorage();

    // <office:document ...>
    CheckAttrList();

    // namespace attributes
    // ( The namespace decls should be first attributes in the element;
    //   some faulty XML parsers (JAXP1.1) have a problem with this,
    //   also it's more elegant )
    sal_uInt16 nPos = mpNamespaceMap->GetFirstKey();
    while( USHRT_MAX != nPos )
    {
        mxAttrList->AddAttribute( mpNamespaceMap->GetAttrNameByKey( nPos ),
                                  mpNamespaceMap->GetNameByKey( nPos ) );
        nPos = mpNamespaceMap->GetNextKey( nPos );
    }

    // office:version = ...
    const char*const pVersion = GetODFVersionAttributeValue();

    if (pVersion)
    {
        AddAttribute( XML_NAMESPACE_OFFICE, XML_VERSION,
                          OUString::createFromAscii(pVersion) );
    }

    {
        enum XMLTokenEnum eRootService = XML_TOKEN_INVALID;
        const SvXMLExportFlags nExportMode = mnExportFlags & (SvXMLExportFlags::META|SvXMLExportFlags::STYLES|SvXMLExportFlags::CONTENT|SvXMLExportFlags::SETTINGS);

        lcl_AddGrddl(*this, nExportMode);

        if( SvXMLExportFlags::META == nExportMode )
        {
            // export only meta
            eRootService = XML_DOCUMENT_META;
        }
        else if ( SvXMLExportFlags::SETTINGS == nExportMode )
        {
            // export only settings
            eRootService = XML_DOCUMENT_SETTINGS;
        }
        else if( SvXMLExportFlags::STYLES == nExportMode )
        {
            // export only styles
            eRootService = XML_DOCUMENT_STYLES;
        }
        else if( SvXMLExportFlags::CONTENT == nExportMode )
        {
            // export only content
            eRootService = XML_DOCUMENT_CONTENT;
        }
        else
        {
            // the god'ol one4all element
            eRootService = XML_DOCUMENT;
            // office:mimetype = ... (only for stream containing the content)
            if( eClass != XML_TOKEN_INVALID )
            {
                OUString aTmp = "application/vnd.oasis.opendocument." + GetXMLToken( eClass );
                AddAttribute( XML_NAMESPACE_OFFICE, XML_MIMETYPE, aTmp );
            }
        }

        SvXMLElementExport aElem( *this, XML_NAMESPACE_OFFICE, eRootService, true, true );

        // meta information
        if( mnExportFlags & SvXMLExportFlags::META )
            ImplExportMeta();

        // settings
        if( mnExportFlags & SvXMLExportFlags::SETTINGS )
            ImplExportSettings();

        // scripts
        if( mnExportFlags & SvXMLExportFlags::SCRIPTS )
            ExportScripts_();

        // font declarations
        if( mnExportFlags & SvXMLExportFlags::FONTDECLS )
            ExportFontDecls_();

        // styles
        if( mnExportFlags & SvXMLExportFlags::STYLES )
            ImplExportStyles();

        // autostyles
        if( mnExportFlags & SvXMLExportFlags::AUTOSTYLES )
            ImplExportAutoStyles();

        // masterstyles
        if( mnExportFlags & SvXMLExportFlags::MASTERSTYLES )
            ImplExportMasterStyles();

        // content
        if( mnExportFlags & SvXMLExportFlags::CONTENT )
            ImplExportContent();
    }

    mxHandler->endDocument();

    if( bOwnGraphicResolver )
    {
        uno::Reference<XComponent> xComp(mxGraphicStorageHandler, UNO_QUERY);
        xComp->dispose();
    }

    if( bOwnEmbeddedResolver )
    {
        Reference< XComponent > xComp( mxEmbeddedResolver, UNO_QUERY );
        xComp->dispose();
    }

    return ERRCODE_NONE;
}

void SvXMLExport::ResetNamespaceMap()
{
    mpNamespaceMap->Clear();
}

OUString const & SvXMLExport::GetSourceShellID() const
{
    return mpImpl->maSrcShellID;
}

OUString const & SvXMLExport::GetDestinationShellID() const
{
    return mpImpl->maDestShellID;
}

void SvXMLExport::ExportMeta_()
{
    OUString generator( ::utl::DocInfoHelper::GetGeneratorString() );
    Reference< XDocumentPropertiesSupplier > xDocPropsSupplier(mxModel,
        UNO_QUERY);
    if (xDocPropsSupplier.is()) {
        Reference<XDocumentProperties> xDocProps(
            xDocPropsSupplier->getDocumentProperties());
        if (!xDocProps.is()) throw;
        // update generator here
        xDocProps->setGenerator(generator);
        rtl::Reference<SvXMLMetaExport> pMeta = new SvXMLMetaExport(*this, xDocProps);
        pMeta->Export();
    } else {
        // office:meta
        SvXMLElementExport aElem( *this, XML_NAMESPACE_OFFICE, XML_META,
                                true, true );
        {
    // BM: #i60323# export generator even if xInfoProp is empty (which is the
    // case for charts). The generator does not depend on xInfoProp
            SvXMLElementExport anElem( *this, XML_NAMESPACE_META, XML_GENERATOR,
                                      true, true );
            Characters(generator);
        }
    }
}

void SvXMLExport::ExportScripts_()
{
    SvXMLElementExport aElement( *this, XML_NAMESPACE_OFFICE, XML_SCRIPTS, true, true );

    // export Basic macros (only for FlatXML)
    if ( mnExportFlags & SvXMLExportFlags::EMBEDDED )
    {
        OUString aValue( GetNamespaceMap().GetPrefixByKey( XML_NAMESPACE_OOO ) + ":Basic" );
        AddAttribute( XML_NAMESPACE_SCRIPT, XML_LANGUAGE, aValue );

        SvXMLElementExport aElem( *this, XML_NAMESPACE_OFFICE, XML_SCRIPT, true, true );

        // initialize Basic
        if ( mxModel.is() )
        {
            Reference< beans::XPropertySet > xPSet( mxModel, UNO_QUERY );
            if ( xPSet.is() )
                xPSet->getPropertyValue("BasicLibraries");
        }

        Reference < XDocumentHandler > xHdl( new XMLBasicExportFilter( mxHandler ) );
        Reference< document::XXMLBasicExporter > xExporter = document::XMLOasisBasicExporter::createWithHandler( m_xContext, xHdl );

        xExporter->setSourceDocument( mxModel );
        Sequence< PropertyValue > aMediaDesc( 0 );
        xExporter->filter( aMediaDesc );
    }

    // export document events
    Reference< document::XEventsSupplier > xEvents( GetModel(), UNO_QUERY );
    GetEventExport().Export( xEvents );
}

void SvXMLExport::ExportFontDecls_()
{
    if( mxFontAutoStylePool.is() )
        mxFontAutoStylePool->exportXML();
}

void SvXMLExport::ExportStyles_( bool )
{
    uno::Reference< lang::XMultiServiceFactory > xFact( GetModel(), uno::UNO_QUERY );
    if( !xFact.is())
        return;

    // export (fill-)gradient-styles
    try
    {
        uno::Reference< container::XNameAccess > xGradient( xFact->createInstance("com.sun.star.drawing.GradientTable"), uno::UNO_QUERY );
        if( xGradient.is() )
        {
            XMLGradientStyleExport aGradientStyle( *this );

            if( xGradient->hasElements() )
            {
                const uno::Sequence< OUString > aNamesSeq ( xGradient->getElementNames() );
                for( const OUString& rStrName : aNamesSeq )
                {
                    try
                    {
                        uno::Any aValue = xGradient->getByName( rStrName );

                        aGradientStyle.exportXML( rStrName, aValue );
                    }
                    catch(const container::NoSuchElementException&)
                    {
                    }
                }
            }
        }
    }
    catch(const lang::ServiceNotRegisteredException&)
    {
    }

    // export (fill-)hatch-styles
    try
    {
        uno::Reference< container::XNameAccess > xHatch( xFact->createInstance("com.sun.star.drawing.HatchTable"), uno::UNO_QUERY );
        if( xHatch.is() )
        {
            XMLHatchStyleExport aHatchStyle( *this );

            if( xHatch->hasElements() )
            {
                const uno::Sequence< OUString > aNamesSeq ( xHatch->getElementNames() );
                for( const OUString& rStrName : aNamesSeq )
                {
                    try
                    {
                        uno::Any aValue = xHatch->getByName( rStrName );

                        aHatchStyle.exportXML( rStrName, aValue );
                    }
                    catch(const container::NoSuchElementException&)
                    {}
                }
            }
        }
    }
    catch(const lang::ServiceNotRegisteredException&)
    {
    }

    // export (fill-)bitmap-styles
    try
    {
        uno::Reference< container::XNameAccess > xBitmap( xFact->createInstance("com.sun.star.drawing.BitmapTable"), uno::UNO_QUERY );
        if( xBitmap.is() )
        {
            if( xBitmap->hasElements() )
            {
                const uno::Sequence< OUString > aNamesSeq ( xBitmap->getElementNames() );
                for( const OUString& rStrName : aNamesSeq )
                {
                    try
                    {
                        uno::Any aValue = xBitmap->getByName( rStrName );

                        XMLImageStyle::exportXML( rStrName, aValue, *this );
                    }
                    catch(const container::NoSuchElementException&)
                    {
                    }
                }
            }
        }
    }
    catch(const lang::ServiceNotRegisteredException&)
    {
    }

    // export transparency-gradient -styles
    try
    {
        uno::Reference< container::XNameAccess > xTransGradient( xFact->createInstance("com.sun.star.drawing.TransparencyGradientTable"), uno::UNO_QUERY );
        if( xTransGradient.is() )
        {
            XMLTransGradientStyleExport aTransGradientstyle( *this );

            if( xTransGradient->hasElements() )
            {
                const uno::Sequence< OUString > aNamesSeq ( xTransGradient->getElementNames() );
                for( const OUString& rStrName : aNamesSeq )
                {
                    try
                    {
                        uno::Any aValue = xTransGradient->getByName( rStrName );

                        aTransGradientstyle.exportXML( rStrName, aValue );
                    }
                    catch(const container::NoSuchElementException&)
                    {
                    }
                }
            }
        }
    }
    catch(const lang::ServiceNotRegisteredException&)
    {
    }

    // export marker-styles
    try
    {
        uno::Reference< container::XNameAccess > xMarker( xFact->createInstance("com.sun.star.drawing.MarkerTable"), uno::UNO_QUERY );
        if( xMarker.is() )
        {
            XMLMarkerStyleExport aMarkerStyle( *this );

            if( xMarker->hasElements() )
            {
                const uno::Sequence< OUString > aNamesSeq ( xMarker->getElementNames() );
                for( const OUString& rStrName : aNamesSeq )
                {
                    try
                    {
                        uno::Any aValue = xMarker->getByName( rStrName );

                        aMarkerStyle.exportXML( rStrName, aValue );
                    }
                    catch(const container::NoSuchElementException&)
                    {
                    }
                }
            }
        }
    }
    catch(const lang::ServiceNotRegisteredException&)
    {
    }

    // export dash-styles
    try
    {
        uno::Reference< container::XNameAccess > xDashes( xFact->createInstance("com.sun.star.drawing.DashTable"), uno::UNO_QUERY );
        if( xDashes.is() )
        {
            XMLDashStyleExport aDashStyle( *this );

            if( xDashes->hasElements() )
            {
                const uno::Sequence< OUString > aNamesSeq ( xDashes->getElementNames() );
                for( const OUString& rStrName : aNamesSeq )
                {
                    try
                    {
                        uno::Any aValue = xDashes->getByName( rStrName );

                        aDashStyle.exportXML( rStrName, aValue );
                    }
                    catch(const container::NoSuchElementException&)
                    {
                    }
                }
            }
        }
    }
    catch(const lang::ServiceNotRegisteredException&)
    {
    }
}

XMLTextParagraphExport* SvXMLExport::CreateTextParagraphExport()
{
    return new XMLTextParagraphExport( *this, *GetAutoStylePool() );
}

XMLShapeExport* SvXMLExport::CreateShapeExport()
{
    return new XMLShapeExport(*this);
}

SvXMLAutoStylePoolP* SvXMLExport::CreateAutoStylePool()
{
    return new SvXMLAutoStylePoolP(*this);
}

void SvXMLExport::collectAutoStyles()
{
}

XMLPageExport* SvXMLExport::CreatePageExport()
{
    return new XMLPageExport( *this );
}

SchXMLExportHelper* SvXMLExport::CreateChartExport()
{
// WASM_CHART change
// TODO: With Chart extracted this cannot really happen since
// no Chart could've been added at all
#if !ENABLE_WASM_STRIP_CHART
    return new SchXMLExportHelper(*this, *GetAutoStylePool());
#else
    return nullptr;
#endif
}

XMLFontAutoStylePool* SvXMLExport::CreateFontAutoStylePool()
{
    return new XMLFontAutoStylePool( *this );
}

xmloff::OFormLayerXMLExport* SvXMLExport::CreateFormExport()
{
    return new xmloff::OFormLayerXMLExport(*this);
}

void SvXMLExport::GetViewSettingsAndViews(uno::Sequence<beans::PropertyValue>& rProps)
{
    GetViewSettings(rProps);
    uno::Reference<document::XViewDataSupplier> xViewDataSupplier(GetModel(), uno::UNO_QUERY);
    if(!xViewDataSupplier.is())
        return;

    uno::Reference<container::XIndexAccess> xIndexAccess;
    xViewDataSupplier->setViewData( xIndexAccess ); // make sure we get a newly created sequence
    {
        // tdf#130559: don't export preview view data if active
        static constexpr OUStringLiteral sNoPreviewData = u"NoPreviewData";
        css::uno::ContextLayer layer(comphelper::NewFlagContext(sNoPreviewData));
        xIndexAccess = xViewDataSupplier->getViewData();
    }
    bool bAdd = false;
    uno::Any aAny;
    if(xIndexAccess.is() && xIndexAccess->hasElements() )
    {
        sal_Int32 nCount = xIndexAccess->getCount();
        for (sal_Int32 i = 0; i < nCount; i++)
        {
            aAny = xIndexAccess->getByIndex(i);
            uno::Sequence<beans::PropertyValue> aProps;
            if( aAny >>= aProps )
            {
                if( aProps.hasElements() )
                {
                    bAdd = true;
                    break;
                }
            }
        }
    }

    if( bAdd )
    {
        sal_Int32 nOldLength(rProps.getLength());
        rProps.realloc(nOldLength + 1);
        rProps.getArray()[nOldLength] = comphelper::makePropertyValue("Views", xIndexAccess);
    }
}

void SvXMLExport::GetViewSettings(uno::Sequence<beans::PropertyValue>&)
{
}

void SvXMLExport::GetConfigurationSettings(uno::Sequence<beans::PropertyValue>&)
{
}

sal_Int32 SvXMLExport::GetDocumentSpecificSettings( ::std::vector< SettingsGroup >& )
{
    return 0;
}

void SvXMLExport::collectDataStyles(bool bFromUsedStyles)
{
    Reference<style::XStyleFamiliesSupplier> xStyleFamiliesSupplier(GetModel(), uno::UNO_QUERY);
    if (!xStyleFamiliesSupplier.is())
        return;

    Reference<container::XNameAccess> xStylesFamilies(xStyleFamiliesSupplier->getStyleFamilies());
    if (!xStylesFamilies.is())
        return;

    Reference<container::XIndexAccess> xCellStyles(xStylesFamilies->getByName("CellStyles"), uno::UNO_QUERY);
    if (!xCellStyles.is())
        return;

    sal_Int32 nCount(xCellStyles->getCount());
    for (sal_Int32 i = 0; i < nCount; ++i)
    {
        Reference<style::XStyle> xStyle(xCellStyles->getByIndex(i), uno::UNO_QUERY);
        if (bFromUsedStyles && !xStyle->isInUse())
            continue;

        Reference<beans::XPropertySet> xCellProperties(xStyle, uno::UNO_QUERY);
        if (xCellProperties.is())
        {
            sal_Int32 nNumberFormat = 0;
            if (xCellProperties->getPropertyValue("NumberFormat") >>= nNumberFormat)
                addDataStyle(nNumberFormat);
        }
    }
}

void SvXMLExport::addDataStyle(const sal_Int32 nNumberFormat, bool /*bTimeFormat*/ )
{
    if(mpNumExport)
        mpNumExport->SetUsed(nNumberFormat);
}

void SvXMLExport::exportDataStyles()
{
    if(mpNumExport)
        mpNumExport->Export(false);
}

void SvXMLExport::exportAutoDataStyles()
{
    if(mpNumExport)
        mpNumExport->Export(true);

    if (mxFormExport.is())
        mxFormExport->exportAutoControlNumberStyles();
}

OUString SvXMLExport::getDataStyleName(const sal_Int32 nNumberFormat, bool /*bTimeFormat*/ ) const
{
    OUString sTemp;
    if(mpNumExport)
        sTemp = mpNumExport->GetStyleName(nNumberFormat);
    return sTemp;
}

void SvXMLExport::exportAnnotationMeta(const uno::Reference<drawing::XShape>&)
{
}

sal_Int32 SvXMLExport::dataStyleForceSystemLanguage(sal_Int32 nFormat) const
{
    return ( mpNumExport != nullptr )
                 ? mpNumExport->ForceSystemLanguage( nFormat ) : nFormat;
}

OUString SvXMLExport::AddEmbeddedXGraphic(uno::Reference<graphic::XGraphic> const & rxGraphic, OUString & rOutMimeType, OUString const & rRequestedName)
{
    OUString sURL;

    Graphic aGraphic(rxGraphic);
    OUString aOriginURL = aGraphic.getOriginURL();

    if (!aOriginURL.isEmpty())
    {
        sURL = GetRelativeReference(aOriginURL);
    }
    else
    {
        if (mxGraphicStorageHandler.is())
        {
            if (!(getExportFlags() & SvXMLExportFlags::EMBEDDED))
                sURL = mxGraphicStorageHandler->saveGraphicByName(rxGraphic, rOutMimeType, rRequestedName);
        }
    }
    return sURL;
}

bool SvXMLExport::GetGraphicMimeTypeFromStream(uno::Reference<graphic::XGraphic> const & rxGraphic, OUString & rOutMimeType)
{
    if (mxGraphicStorageHandler.is())
    {
        Reference<XInputStream> xInputStream(mxGraphicStorageHandler->createInputStream(rxGraphic));
        if (xInputStream.is())
        {
            rOutMimeType = comphelper::GraphicMimeTypeHelper::GetMimeTypeForImageStream(xInputStream);
            return true;
        }
    }

    return false;
}

bool SvXMLExport::AddEmbeddedXGraphicAsBase64(uno::Reference<graphic::XGraphic> const & rxGraphic)
{
    if ((getExportFlags() & SvXMLExportFlags::EMBEDDED) &&
        mxGraphicStorageHandler.is())
    {
        Reference<XInputStream> xInputStream(mxGraphicStorageHandler->createInputStream(rxGraphic));
        if (xInputStream.is())
        {
            Graphic aGraphic(rxGraphic);
            if (aGraphic.getOriginURL().isEmpty()) // don't add the base64 if the origin URL is set (image is from an external URL)
            {
                XMLBase64Export aBase64Exp(*this);
                return aBase64Exp.exportOfficeBinaryDataElement(xInputStream);
            }
        }
    }

    return false;
}

OUString SvXMLExport::AddEmbeddedObject( const OUString& rEmbeddedObjectURL )
{
    OUString sRet;
    bool bSupportedURL = rEmbeddedObjectURL.startsWith(XML_EMBEDDEDOBJECT_URL_BASE) ||
                         rEmbeddedObjectURL.startsWith(XML_EMBEDDEDOBJECTGRAPHIC_URL_BASE);
    if (bSupportedURL && mxEmbeddedResolver.is())
    {
        sRet = mxEmbeddedResolver->resolveEmbeddedObjectURL(rEmbeddedObjectURL);
    }
    else
        sRet = GetRelativeReference( rEmbeddedObjectURL );

    return sRet;
}

bool SvXMLExport::AddEmbeddedObjectAsBase64( const OUString& rEmbeddedObjectURL )
{
    bool bRet = false;
    bool bSupportedURL = rEmbeddedObjectURL.startsWith(XML_EMBEDDEDOBJECT_URL_BASE) ||
                         rEmbeddedObjectURL.startsWith(XML_EMBEDDEDOBJECTGRAPHIC_URL_BASE);
    if (bSupportedURL && mxEmbeddedResolver.is())
    {
        Reference < XNameAccess > xNA( mxEmbeddedResolver, UNO_QUERY );
        if( xNA.is() )
        {
            Any aAny = xNA->getByName( rEmbeddedObjectURL );
            Reference < XInputStream > xIn;
            aAny >>= xIn;
            if( xIn.is() )
            {
                XMLBase64Export aBase64Exp( *this );
                bRet = aBase64Exp.exportOfficeBinaryDataElement( xIn );
            }
        }
    }

    return bRet;
}

OUString SvXMLExport::EncodeStyleName(
        const OUString& rName,
        bool *pEncoded ) const
{
    return GetMM100UnitConverter().encodeStyleName( rName, pEncoded );
}

ProgressBarHelper*  SvXMLExport::GetProgressBarHelper()
{
    if (!mpProgressBarHelper)
    {
        mpProgressBarHelper.reset( new ProgressBarHelper(mxStatusIndicator, true) );

        if (mxExportInfo.is())
        {
            uno::Reference< beans::XPropertySetInfo > xPropertySetInfo = mxExportInfo->getPropertySetInfo();
            if (xPropertySetInfo.is())
            {
                OUString sProgressRange(XML_PROGRESSRANGE);
                OUString sProgressMax(XML_PROGRESSMAX);
                OUString sProgressCurrent(XML_PROGRESSCURRENT);
                OUString sRepeat(XML_PROGRESSREPEAT);
                if (xPropertySetInfo->hasPropertyByName(sProgressMax) &&
                    xPropertySetInfo->hasPropertyByName(sProgressCurrent) &&
                    xPropertySetInfo->hasPropertyByName(sProgressRange))
                {
                    uno::Any aAny;
                    sal_Int32 nProgressMax(0);
                    sal_Int32 nProgressCurrent(0);
                    sal_Int32 nProgressRange(0);
                    aAny = mxExportInfo->getPropertyValue(sProgressRange);
                    if (aAny >>= nProgressRange)
                        mpProgressBarHelper->SetRange(nProgressRange);
                    aAny = mxExportInfo->getPropertyValue(sProgressMax);
                    if (aAny >>= nProgressMax)
                        mpProgressBarHelper->SetReference(nProgressMax);
                    aAny = mxExportInfo->getPropertyValue(sProgressCurrent);
                    if (aAny >>= nProgressCurrent)
                        mpProgressBarHelper->SetValue(nProgressCurrent);
                }
                if (xPropertySetInfo->hasPropertyByName(sRepeat))
                {
                    uno::Any aAny = mxExportInfo->getPropertyValue(sRepeat);
                    if (aAny.getValueType() == cppu::UnoType<bool>::get())
                        mpProgressBarHelper->SetRepeat(::cppu::any2bool(aAny));
                    else {
                        SAL_WARN("xmloff.core", "why is it no boolean?" );
                    }
                }
            }
        }
    }
    return mpProgressBarHelper.get();
}

XMLEventExport& SvXMLExport::GetEventExport()
{
    if( nullptr == mpEventExport)
    {
        // create EventExport on demand
        mpEventExport.reset( new XMLEventExport(*this) );

        // and register standard handlers + names
        mpEventExport->AddHandler("StarBasic", std::make_unique<XMLStarBasicExportHandler>());
        mpEventExport->AddHandler("Script", std::make_unique<XMLScriptExportHandler>());
        mpEventExport->AddTranslationTable(aStandardEventTable);
    }

    return *mpEventExport;
}

XMLImageMapExport& SvXMLExport::GetImageMapExport()
{
    // image map export, create on-demand
    if( nullptr == mpImageMapExport )
    {
        mpImageMapExport.reset( new XMLImageMapExport(*this) );
    }

    return *mpImageMapExport;
}

// XUnoTunnel & co
UNO3_GETIMPLEMENTATION_IMPL(SvXMLExport);

void SvXMLExport::ExportEmbeddedOwnObject( Reference< XComponent > const & rComp )
{
    OUString sFilterService;

    Reference < lang::XServiceInfo > xServiceInfo( rComp, UNO_QUERY );
    if( xServiceInfo.is() )
    {
        for (const auto& [sModelService, sMatchingFilterService] : aServiceMap)
        {
            if( xServiceInfo->supportsService( sModelService ) )
            {
                sFilterService = sMatchingFilterService;
                break;
            }
        }
    }

    SAL_WARN_IF( !sFilterService.getLength(), "xmloff.core", "no export filter for own object" );

    if( sFilterService.isEmpty() )
        return;

    Reference < XDocumentHandler > xHdl =
        new XMLEmbeddedObjectExportFilter( mxHandler );

    Sequence < Any > aArgs{ Any(xHdl) };
    Reference< document::XExporter > xExporter(
        m_xContext->getServiceManager()->createInstanceWithArgumentsAndContext(sFilterService, aArgs, m_xContext),
        UNO_QUERY);
    SAL_WARN_IF( !xExporter.is(), "xmloff.core", "can't instantiate export filter component for own object" );
    if( !xExporter.is() )
        return;

    xExporter->setSourceDocument( rComp );

    Reference<XFilter> xFilter( xExporter, UNO_QUERY );

    Sequence < PropertyValue > aMediaDesc( 0 );
    xFilter->filter( aMediaDesc );
}

OUString SvXMLExport::GetRelativeReference(const OUString& rValue)
{
    OUString sValue( rValue );
    // #i65474# handling of fragment URLs ("#...") is undefined
    // they are stored 'as is'
    uno::Reference< uri::XUriReference > xUriRef;
    if(!sValue.isEmpty() && sValue[0] != '#')
    {
        try
        {
            xUriRef = mpImpl->mxUriReferenceFactory->parse( rValue );
            if( xUriRef.is() && !xUriRef->isAbsolute() )
            {
                //#i61943# relative URLs need special handling
                INetURLObject aTemp( mpImpl->msPackageURI );
                bool bWasAbsolute = false;
                sValue = aTemp.smartRel2Abs(sValue, bWasAbsolute ).GetMainURL(INetURLObject::DecodeMechanism::ToIUri);
            }
        }
        catch(const uno::Exception&)
        {
        }
    }
    if( xUriRef.is() )//no conversion for empty values or for fragments
    {
        //conversion for matching schemes only
        if( xUriRef->getScheme() == mpImpl->msPackageURIScheme )
        {
            sValue = INetURLObject::GetRelURL( msOrigFileName, sValue );
        }
    }
    return sValue;
}

void SvXMLExport::StartElement(sal_uInt16 nPrefix,
                        enum ::xmloff::token::XMLTokenEnum eName,
                        bool bIgnWSOutside )
{
    StartElement(GetNamespaceMap_().GetQNameByKey( nPrefix,
        GetXMLToken(eName) ), bIgnWSOutside);
}

void SvXMLExport::StartElement(const OUString& rName,
                        bool bIgnWSOutside )
{
    if ((mnErrorFlags & SvXMLErrorFlags::DO_NOTHING) != SvXMLErrorFlags::DO_NOTHING)
    {
        try
        {
            if( bIgnWSOutside && ((mnExportFlags & SvXMLExportFlags::PRETTY) == SvXMLExportFlags::PRETTY))
                mxHandler->ignorableWhitespace( msWS );
            mxHandler->startElement( rName, GetXAttrList() );
        }
        catch (const SAXInvalidCharacterException& e)
        {
            Sequence<OUString> aPars { rName };
            SetError( XMLERROR_SAX|XMLERROR_FLAG_WARNING, aPars, e.Message, nullptr );
        }
        catch (const SAXException& e)
        {
            Sequence<OUString> aPars { rName };
            SetError( XMLERROR_SAX|XMLERROR_FLAG_ERROR|XMLERROR_FLAG_SEVERE,
                      aPars, e.Message, nullptr );
        }
    }
    ClearAttrList();
    ++mpImpl->mDepth; // increment nesting depth counter
}

void SvXMLExport::Characters(const OUString& rChars)
{
    if ((mnErrorFlags & SvXMLErrorFlags::DO_NOTHING) == SvXMLErrorFlags::DO_NOTHING)
        return;

    try
    {
        mxHandler->characters(rChars);
    }
    catch (const SAXInvalidCharacterException& e)
    {
        Sequence<OUString> aPars { rChars };
        SetError( XMLERROR_SAX|XMLERROR_FLAG_WARNING, aPars, e.Message, nullptr );
    }
    catch (const SAXException& e)
    {
        Sequence<OUString> aPars { rChars };
        SetError( XMLERROR_SAX|XMLERROR_FLAG_ERROR|XMLERROR_FLAG_SEVERE,
                  aPars, e.Message, nullptr );
    }
}

void SvXMLExport::EndElement(sal_uInt16 nPrefix,
                        enum ::xmloff::token::XMLTokenEnum eName,
                        bool bIgnWSInside )
{
    EndElement(GetNamespaceMap_().GetQNameByKey( nPrefix, GetXMLToken(eName) ),
        bIgnWSInside);
}

void SvXMLExport::EndElement(const OUString& rName,
                        bool bIgnWSInside )
{
    // decrement nesting depth counter & (maybe) restore namespace map
    --mpImpl->mDepth;
    if (!mpImpl->mNamespaceMaps.empty() &&
        (mpImpl->mNamespaceMaps.top().second == mpImpl->mDepth))
    {
        mpNamespaceMap = std::move(mpImpl->mNamespaceMaps.top().first);
        mpImpl->mNamespaceMaps.pop();
    }
    SAL_WARN_IF(!mpImpl->mNamespaceMaps.empty() &&
        (mpImpl->mNamespaceMaps.top().second >= mpImpl->mDepth), "xmloff.core", "SvXMLExport: NamespaceMaps corrupted");

    if ((mnErrorFlags & SvXMLErrorFlags::DO_NOTHING) == SvXMLErrorFlags::DO_NOTHING)
        return;

    try
    {
        if( bIgnWSInside && ((mnExportFlags & SvXMLExportFlags::PRETTY) == SvXMLExportFlags::PRETTY))
            mxHandler->ignorableWhitespace( msWS );
        mxHandler->endElement( rName );
    }
    catch (const SAXException& e)
    {
        Sequence<OUString> aPars { rName };
        SetError( XMLERROR_SAX|XMLERROR_FLAG_ERROR|XMLERROR_FLAG_SEVERE,
                  aPars, e.Message, nullptr );
    }
}

void SvXMLExport::IgnorableWhitespace()
{
    if ((mnExportFlags & SvXMLExportFlags::PRETTY) != SvXMLExportFlags::PRETTY)
        return;

    if ((mnErrorFlags & SvXMLErrorFlags::DO_NOTHING) == SvXMLErrorFlags::DO_NOTHING)
        return;

    try
    {
        mxHandler->ignorableWhitespace( msWS );
    }
    catch (const SAXException& e)
    {
        SetError( XMLERROR_SAX|XMLERROR_FLAG_ERROR|XMLERROR_FLAG_SEVERE,
                  {}, e.Message, nullptr );
    }
}

void SvXMLExport::SetError(
    sal_Int32 nId,
    const Sequence<OUString>& rMsgParams,
    const OUString& rExceptionMessage,
    const Reference<XLocator>& rLocator )
{
    // allow multi-threaded access to the cancel() method
    static std::mutex aMutex;
    std::scoped_lock aGuard(aMutex);

    // maintain error flags
    if ( ( nId & XMLERROR_FLAG_ERROR ) != 0 )
        mnErrorFlags |= SvXMLErrorFlags::ERROR_OCCURRED;
    if ( ( nId & XMLERROR_FLAG_WARNING ) != 0 )
        mnErrorFlags |= SvXMLErrorFlags::WARNING_OCCURRED;
    if ( ( nId & XMLERROR_FLAG_SEVERE ) != 0 )
        mnErrorFlags |= SvXMLErrorFlags::DO_NOTHING;

    // create error list on demand
    if ( mpXMLErrors == nullptr )
        mpXMLErrors.reset( new XMLErrors() );

    // save error information
    mpXMLErrors->AddRecord( nId, rMsgParams, rExceptionMessage, rLocator );
}

void SvXMLExport::SetError(
    sal_Int32 nId,
    const Sequence<OUString>& rMsgParams)
{
    SetError( nId, rMsgParams, "", nullptr );
}

void SvXMLExport::DisposingModel()
{
    mxModel.clear();
    // Shapes in Writer cannot be named via context menu (#i51726#)
    meModelType = SvtModuleOptions::EFactory::UNKNOWN_FACTORY;
    mxEventListener.clear();
}


::comphelper::UnoInterfaceToUniqueIdentifierMapper& SvXMLExport::getInterfaceToIdentifierMapper()
{
    return mpImpl->maInterfaceToIdentifierMapper;
}

// Written OpenDocument file format doesn't fit to the created text document (#i69627#)
bool SvXMLExport::writeOutlineStyleAsNormalListStyle() const
{
    return mpImpl->mbOutlineStyleAsNormalListStyle;
}

uno::Reference< embed::XStorage > const & SvXMLExport::GetTargetStorage() const
{
    return mpImpl->mxTargetStorage;
}

SvtSaveOptions::ODFSaneDefaultVersion SvXMLExport::getSaneDefaultVersion() const
{
    if (mpImpl->m_oOverrideODFVersion)
    {
        return *mpImpl->m_oOverrideODFVersion;
    }
    return GetODFSaneDefaultVersion();
}

void
SvXMLExport::AddAttributeIdLegacy(
        sal_uInt16 const nLegacyPrefix, OUString const& rValue)
{
    switch (getSaneDefaultVersion()) {
        case SvtSaveOptions::ODFSVER_011: // fall through
        case SvtSaveOptions::ODFSVER_010: break;
        default: // ODF 1.2: xml:id
            AddAttribute(XML_NAMESPACE_XML, XML_ID, rValue);
    }
    // in ODF 1.1 this was form:id, anim:id, draw:id, or text:id
    // backward compatibility: in ODF 1.2 write _both_ id attrs
    AddAttribute(nLegacyPrefix, XML_ID, rValue);
    // FIXME: this function simply assumes that rValue is unique
}

void
SvXMLExport::AddAttributeXmlId(uno::Reference<uno::XInterface> const & i_xIfc)
{
    // check version >= 1.2
    switch (getSaneDefaultVersion()) {
        case SvtSaveOptions::ODFSVER_011: // fall through
        case SvtSaveOptions::ODFSVER_010: return;
        default: break;
    }
    const uno::Reference<rdf::XMetadatable> xMeta(i_xIfc,
        uno::UNO_QUERY);
//FIXME not yet...
    if ( !xMeta.is() )
        return;

    const beans::StringPair mdref( xMeta->getMetadataReference() );
    if ( mdref.Second.isEmpty() )
        return;

    const OUString streamName = mpImpl->mStreamName;
    if ( !streamName.isEmpty() )
    {
        if ( streamName == mdref.First )
        {
            AddAttribute( XML_NAMESPACE_XML, XML_ID, mdref.Second );
        }
        else
        {
            SAL_WARN("xmloff.core","SvXMLExport::AddAttributeXmlId: invalid stream name");
        }
    }
    else
    {
        // FIXME: this is ugly
        // there is no stream name (e.g. XSLT, flat-xml format)!
        // but how do we ensure uniqueness in this case?
        // a) just omit styles.xml ids -- they are unlikely anyway...
        // b) somehow find out whether we are currently exporting styles
        //    or content, and prefix "s" or "c" => unique
        if ( mdref.First == "content.xml" )
        {
            AddAttribute( XML_NAMESPACE_XML, XML_ID, mdref.Second );
        }
        else
        {
            SAL_INFO("xmloff.core", "SvXMLExport::AddAttributeXmlId: no stream name given: dropping styles.xml xml:id");
        }
    }
}

void
SvXMLExport::AddAttributesRDFa(
    uno::Reference<text::XTextContent> const & i_xTextContent)
{
    // check version >= 1.2
    switch (getSaneDefaultVersion()) {
        case SvtSaveOptions::ODFSVER_011: // fall through
        case SvtSaveOptions::ODFSVER_010: return;
        default: break;
    }

    const uno::Reference<rdf::XMetadatable> xMeta(
        i_xTextContent, uno::UNO_QUERY);
    if (!xMeta.is() || xMeta->getMetadataReference().Second.isEmpty())
    {
        return; // no xml:id => no RDFa
    }

    if (!mpImpl->mpRDFaHelper)
    {
        mpImpl->mpRDFaHelper.reset( new ::xmloff::RDFaExportHelper(*this) );
    }
    mpImpl->mpRDFaHelper->AddRDFa(xMeta);
}

bool SvXMLExport::exportTextNumberElement() const
{
    return mpImpl->mbExportTextNumberElement;
}

bool SvXMLExport::SetNullDateOnUnitConverter()
{
    // if the null date has already been set, don't set it again (performance)
    if (!mpImpl->mbNullDateInitialized)
        mpImpl->mbNullDateInitialized = GetMM100UnitConverter().setNullDate(GetModel());

    return mpImpl->mbNullDateInitialized;
}

OUString const & SvXMLExport::GetImageFilterName() const
{
    return msImgFilterName;
}

void SvXMLElementExport::StartElement(
    const sal_uInt16 nPrefixKey,
    const OUString& rLName,
    const bool bIgnoreWhitespaceOutside )
{
    maElementName = mrExport.GetNamespaceMap().GetQNameByKey(nPrefixKey, rLName);
    mrExport.StartElement(maElementName, bIgnoreWhitespaceOutside);
}

SvXMLElementExport::SvXMLElementExport(
    SvXMLExport& rExp,
    sal_uInt16 nPrefixKey,
    const char *pLName,
    bool bIWSOutside,
    bool bIWSInside )
    : mrExport( rExp )
    , mbIgnoreWhitespaceInside( bIWSInside )
    , mbDoSomething( true )
{
    const OUString sLName( OUString::createFromAscii( pLName ) );
    StartElement( nPrefixKey, sLName, bIWSOutside );
}

SvXMLElementExport::SvXMLElementExport(
    SvXMLExport& rExp,
    sal_uInt16 nPrefixKey,
    const OUString& rLName,
    bool bIWSOutside,
    bool bIWSInside )
    : mrExport( rExp )
    , mbIgnoreWhitespaceInside( bIWSInside )
    , mbDoSomething( true )
{
    StartElement( nPrefixKey, rLName, bIWSOutside );
}

SvXMLElementExport::SvXMLElementExport(
    SvXMLExport& rExp,
    sal_uInt16 nPrefixKey,
    enum XMLTokenEnum eLName,
    bool bIWSOutside,
    bool bIWSInside )
    : mrExport( rExp )
    , mbIgnoreWhitespaceInside( bIWSInside )
    , mbDoSomething( true )
{
    StartElement( nPrefixKey, GetXMLToken(eLName), bIWSOutside );
}

SvXMLElementExport::SvXMLElementExport(
    SvXMLExport& rExp,
    bool bDoSth,
    sal_uInt16 nPrefixKey,
    enum XMLTokenEnum eLName,
    bool bIWSOutside,
    bool bIWSInside )
    : mrExport( rExp )
    , mbIgnoreWhitespaceInside( bIWSInside )
    , mbDoSomething( bDoSth )
{
    if ( mbDoSomething )
        StartElement( nPrefixKey, GetXMLToken( eLName ), bIWSOutside );
}

SvXMLElementExport::SvXMLElementExport(
    SvXMLExport& rExp,
    const OUString& rQName,
    bool bIWSOutside,
    bool bIWSInside )
    : mrExport( rExp )
    , mbIgnoreWhitespaceInside( bIWSInside )
    , mbDoSomething( true )
{
    maElementName = rQName;
    rExp.StartElement( rQName, bIWSOutside );
}

SvXMLElementExport::~SvXMLElementExport()
{
    if ( mbDoSomething )
    {
        mrExport.EndElement( maElementName, mbIgnoreWhitespaceInside );
    }
}

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