summaryrefslogtreecommitdiffstats
path: root/xmloff/source/forms/elementimport.cxx
blob: 19f247aae12278bb11e0f2955b0832b7c6775b4f (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
/* -*- 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 "elementimport.hxx"
#include <xmloff/xmlimp.hxx>
#include <xmloff/namespacemap.hxx>
#include "strings.hxx"
#include "callbacks.hxx"
#include <xmloff/xmlnamespace.hxx>
#include "eventimport.hxx"
#include <xmloff/txtstyli.hxx>
#include "formenums.hxx"
#include <xmloff/xmltoken.hxx>
#include "gridcolumnproptranslator.hxx"
#include "property_description.hxx"
#include "property_meta_data.hxx"

#include <com/sun/star/uno/XComponentContext.hpp>
#include <com/sun/star/text/XText.hpp>
#include <com/sun/star/util/XCloneable.hpp>
#include <com/sun/star/util/Duration.hpp>
#include <com/sun/star/form/FormComponentType.hpp>
#include <com/sun/star/awt/ImagePosition.hpp>
#include <com/sun/star/beans/XMultiPropertySet.hpp>
#include <com/sun/star/beans/XPropertyContainer.hpp>
#include <com/sun/star/beans/PropertyAttribute.hpp>

#include <sax/tools/converter.hxx>
#include <tools/urlobj.hxx>
#include <tools/diagnose_ex.h>
#include <rtl/strbuf.hxx>
#include <sal/log.hxx>
#include <comphelper/extract.hxx>
#include <comphelper/types.hxx>
#include <comphelper/sequence.hxx>
#include <o3tl/string_view.hxx>

#include <algorithm>

namespace xmloff
{

    using namespace ::xmloff::token;
    using namespace ::com::sun::star;
    using namespace ::com::sun::star::uno;
    using namespace ::com::sun::star::awt;
    using namespace ::com::sun::star::container;
    using namespace ::com::sun::star::beans;
    using namespace ::com::sun::star::script;
    using namespace ::com::sun::star::lang;
    using namespace ::com::sun::star::form;
    using namespace ::com::sun::star::xml;
    using namespace ::com::sun::star::xml::sax;
    using namespace ::com::sun::star::util;
    using namespace ::com::sun::star::text;
    using namespace ::comphelper;
    using ::com::sun::star::xml::sax::XAttributeList;

#define PROPID_VALUE            1
#define PROPID_CURRENT_VALUE    2
#define PROPID_MIN_VALUE        3
#define PROPID_MAX_VALUE        4

    namespace {

    struct PropertyValueLess
    {
        bool operator()(const PropertyValue& _rLeft, const PropertyValue& _rRight)
        {
            return _rLeft.Name < _rRight.Name;
        }
    };

    }

    //= OElementNameMap
    std::map<sal_Int32, OControlElement::ElementType>  OElementNameMap::s_sElementTranslations2;

    const OControlElement::ElementType& operator ++(OControlElement::ElementType& _e)
    {
        OControlElement::ElementType e = _e;
        sal_Int32 nAsInt = static_cast<sal_Int32>(e);
        _e = static_cast<OControlElement::ElementType>( ++nAsInt );
        return _e;
    }

    OControlElement::ElementType OElementNameMap::getElementType(sal_Int32 nElement)
    {
        if ( s_sElementTranslations2.empty() )
        {   // initialize
            for (ElementType eType=ElementType(0); eType<UNKNOWN; ++eType)
                s_sElementTranslations2[getElementToken(eType)] = eType;
        }
        auto aPos = s_sElementTranslations2.find(nElement & TOKEN_MASK);
        if (s_sElementTranslations2.end() != aPos)
            return aPos->second;

        return UNKNOWN;
    }

    //= OElementImport
    OElementImport::OElementImport(OFormLayerXMLImport_Impl& _rImport, IEventAttacherManager& _rEventManager,
            const Reference< XNameContainer >& _rxParentContainer)
        :OPropertyImport(_rImport)
        ,m_rFormImport(_rImport)
        ,m_rEventManager(_rEventManager)
        ,m_pStyleElement( nullptr )
        ,m_xParentContainer(_rxParentContainer)
        ,m_bImplicitGenericAttributeHandling( true )
    {
        OSL_ENSURE(m_xParentContainer.is(), "OElementImport::OElementImport: invalid parent container!");
    }

    OElementImport::~OElementImport()
    {
    }

    OUString OElementImport::determineDefaultServiceName() const
    {
        return OUString();
    }

    void OElementImport::startFastElement(sal_Int32 nElement, const Reference< css::xml::sax::XFastAttributeList >& _rxAttrList)
    {
        ENTER_LOG_CONTEXT( "xmloff::OElementImport - importing one element" );

        const OUString sControlImplementation = _rxAttrList->getOptionalValue( XML_ELEMENT(FORM, XML_CONTROL_IMPLEMENTATION) );

        // retrieve the service name
        if ( !sControlImplementation.isEmpty() )
        {
            OUString sOOoImplementationName;
            const sal_uInt16 nImplPrefix = GetImport().GetNamespaceMap().GetKeyByAttrValueQName( sControlImplementation, &sOOoImplementationName );
            m_sServiceName = ( nImplPrefix == XML_NAMESPACE_OOO ) ? sOOoImplementationName : sControlImplementation;
        }

        if ( m_sServiceName.isEmpty() )
            m_sServiceName = determineDefaultServiceName();

        // create the object *now*. This allows setting properties in the various handleAttribute methods.
        // (Though currently not all code is migrated to this pattern, most attributes are still handled
        // by remembering the value (via implPushBackPropertyValue), and setting the correct property value
        // later (in OControlImport::StartElement).)
        m_xElement = createElement();
        if ( m_xElement.is() )
            m_xInfo = m_xElement->getPropertySetInfo();

        // call the base class
        OPropertyImport::startFastElement( nElement, _rxAttrList );
    }

    css::uno::Reference< css::xml::sax::XFastContextHandler > OElementImport::createFastChildContext(
        sal_Int32 nElement,
        const css::uno::Reference< css::xml::sax::XFastAttributeList >& _rxAttrList )
    {
        if( nElement == XML_ELEMENT(OFFICE, XML_EVENT_LISTENERS) )
            return new OFormEventsImportContext(m_rFormImport.getGlobalContext(), *this);

        return OPropertyImport::createFastChildContext(nElement, _rxAttrList);
    }

    void OElementImport::endFastElement(sal_Int32 )
    {
        OSL_ENSURE(m_xElement.is(), "OElementImport::EndElement: invalid element created!");
        if (!m_xElement.is())
            return;

        // apply the non-generic properties
        implApplySpecificProperties();

        // set the generic properties
        implApplyGenericProperties();

        // set the style properties
        if ( m_pStyleElement && m_xElement.is() )
        {
            Reference< XPropertySet > xPropTranslation =
                new OGridColumnPropertyTranslator( Reference< XMultiPropertySet >( m_xElement, UNO_QUERY ) );
            const_cast< XMLTextStyleContext* >( m_pStyleElement )->FillPropertySet( xPropTranslation );

            const OUString sNumberStyleName = m_pStyleElement->GetDataStyleName( );
            if ( !sNumberStyleName.isEmpty() )
                // the style also has a number (sub) style
                m_rContext.applyControlNumberStyle( m_xElement, sNumberStyleName );
        }

        // insert the element into the parent container
        if (m_sName.isEmpty())
        {
            OSL_FAIL("OElementImport::EndElement: did not find a name attribute!");
            m_sName = implGetDefaultName();
        }

        if (m_xParentContainer.is())
            m_xParentContainer->insertByName(m_sName, Any(m_xElement));

        LEAVE_LOG_CONTEXT( );
    }

    void OElementImport::implApplySpecificProperties()
    {
        if ( m_aValues.empty() )
            return;

        // set all the properties we collected
#if OSL_DEBUG_LEVEL > 0
        // check if the object has all the properties
        // (We do this in the non-pro version only. Doing it all the time would be too much expensive)
        if ( m_xInfo.is() )
        {
            for ( const auto& rCheck : m_aValues )
            {
                OSL_ENSURE(m_xInfo->hasPropertyByName(rCheck.Name),
                        OStringBuffer("OElementImport::implApplySpecificProperties: read a property (" +
                            OUStringToOString(rCheck.Name, RTL_TEXTENCODING_ASCII_US) +
                            ") which does not exist on the element!").getStr());
            }
        }
#endif

        // set the properties
        const Reference< XMultiPropertySet > xMultiProps(m_xElement, UNO_QUERY);
        bool bSuccess = false;
        if (xMultiProps.is())
        {
            // translate our properties so that the XMultiPropertySet can handle them

            // sort our property value array so that we can use it in a setPropertyValues
            ::std::sort( m_aValues.begin(), m_aValues.end(), PropertyValueLess());

            // the names
            Sequence< OUString > aNames(m_aValues.size());
            OUString* pNames = aNames.getArray();
            // the values
            Sequence< Any > aValues(m_aValues.size());
            Any* pValues = aValues.getArray();
            // copy

            for ( const auto& rPropValues : m_aValues )
            {
                *pNames = rPropValues.Name;
                *pValues = rPropValues.Value;
                ++pNames;
                ++pValues;
            }

            try
            {
                xMultiProps->setPropertyValues(aNames, aValues);
                bSuccess = true;
            }
            catch(const Exception&)
            {
                DBG_UNHANDLED_EXCEPTION("xmloff.forms");
                OSL_FAIL("OElementImport::implApplySpecificProperties: could not set the properties (using the XMultiPropertySet)!");
            }
        }

        if (bSuccess)
            return;

        // no XMultiPropertySet or setting all properties at once failed
        for ( const auto& rPropValues : m_aValues )
        {
            // this try/catch here is expensive, but because this is just a fallback which should normally not be
            // used it's acceptable this way ...
            try
            {
                m_xElement->setPropertyValue(rPropValues.Name, rPropValues.Value);
            }
            catch(const Exception&)
            {
                DBG_UNHANDLED_EXCEPTION("xmloff.forms");
                OSL_FAIL(OStringBuffer("OElementImport::implApplySpecificProperties: could not set the property \"" +
                        OUStringToOString(rPropValues.Name, RTL_TEXTENCODING_ASCII_US) +
                        "\"!").getStr());
            }
        }
    }

    void OElementImport::implApplyGenericProperties()
    {
        if ( m_aGenericValues.empty() )
            return;

        Reference< XPropertyContainer > xDynamicProperties( m_xElement, UNO_QUERY );

        // PropertyValueArray::iterator aEnd = m_aGenericValues.end();
        for ( auto& rPropValues : m_aGenericValues )
        {
            // check property type for numeric types before setting
            // the property
            try
            {
                // if such a property does not yet exist at the element, create it if necessary
                const bool bExistentProperty = m_xInfo->hasPropertyByName( rPropValues.Name );
                if ( !bExistentProperty )
                {
                    if ( !xDynamicProperties.is() )
                    {
                        SAL_WARN( "xmloff", "OElementImport::implApplyGenericProperties: encountered an unknown property ("
                                    << rPropValues.Name << "), but component is no PropertyBag!");
                        continue;
                    }

                    xDynamicProperties->addProperty(
                        rPropValues.Name,
                        PropertyAttribute::BOUND | PropertyAttribute::REMOVABLE,
                        rPropValues.Value
                    );

                    // re-fetch the PropertySetInfo
                    m_xInfo = m_xElement->getPropertySetInfo();
                }

                // determine the type of the value (source for the following conversion)
                TypeClass eValueTypeClass = rPropValues.Value.getValueTypeClass();
                const bool bValueIsSequence = TypeClass_SEQUENCE == eValueTypeClass;
                if ( bValueIsSequence )
                {
                    uno::Type aSimpleType( getSequenceElementType( rPropValues.Value.getValueType() ) );
                    eValueTypeClass = aSimpleType.getTypeClass();
                }

                // determine the type of the property (target for the following conversion)
                const Property aProperty( m_xInfo->getPropertyByName( rPropValues.Name ) );
                TypeClass ePropTypeClass = aProperty.Type.getTypeClass();
                const bool bPropIsSequence = TypeClass_SEQUENCE == ePropTypeClass;
                if( bPropIsSequence )
                {
                    uno::Type aSimpleType( ::comphelper::getSequenceElementType( aProperty.Type ) );
                    ePropTypeClass = aSimpleType.getTypeClass();
                }

                if ( bPropIsSequence != bValueIsSequence )
                {
                    OSL_FAIL( "OElementImport::implImportGenericProperties: either both value and property should be a sequence, or none of them!" );
                    continue;
                }

                if ( bValueIsSequence )
                {
                    Sequence< Any > aXMLValueList;
                    rPropValues.Value >>= aXMLValueList;
                    // just skip this part if empty sequence
                    if (!aXMLValueList.getLength())
                        continue;

                    Sequence< sal_Int16 > aPropertyValueList( aXMLValueList.getLength() );

                    SAL_WARN_IF( eValueTypeClass != TypeClass_ANY, "xmloff",
                        "OElementImport::implApplyGenericProperties: only ANYs should have been imported as generic list property!" );
                        // (OPropertyImport should produce only Sequencer< Any >, since it cannot know the real type

                    SAL_WARN_IF( ePropTypeClass != TypeClass_SHORT, "xmloff",
                        "OElementImport::implApplyGenericProperties: conversion to sequences other than 'sequence< short >' not implemented, yet!" );


                    std::transform(std::cbegin(aXMLValueList), std::cend(aXMLValueList), aPropertyValueList.getArray(),
                        [](const Any& rXMLValue) -> sal_Int16 {
                            // only value sequences of numeric types implemented so far.
                            double nVal( 0 );
                            OSL_VERIFY( rXMLValue >>= nVal );
                            return static_cast< sal_Int16 >( nVal );
                        });

                    rPropValues.Value <<= aPropertyValueList;
                }
                else if ( ePropTypeClass != eValueTypeClass )
                {
                    switch ( eValueTypeClass )
                    {
                    case TypeClass_DOUBLE:
                    {
                        double nVal = 0;
                        rPropValues.Value >>= nVal;
                        switch( ePropTypeClass )
                        {
                        case TypeClass_BYTE:
                            rPropValues.Value <<= static_cast< sal_Int8 >( nVal );
                            break;
                        case TypeClass_SHORT:
                            rPropValues.Value <<= static_cast< sal_Int16 >( nVal );
                            break;
                        case TypeClass_UNSIGNED_SHORT:
                            rPropValues.Value <<= static_cast< sal_uInt16 >( nVal );
                            break;
                        case TypeClass_LONG:
                        case TypeClass_ENUM:
                            rPropValues.Value <<= static_cast< sal_Int32 >( nVal );
                            break;
                        case TypeClass_UNSIGNED_LONG:
                            rPropValues.Value <<= static_cast< sal_uInt32 >( nVal );
                            break;
                        case TypeClass_UNSIGNED_HYPER:
                            rPropValues.Value <<= static_cast< sal_uInt64 >( nVal );
                            break;
                        case TypeClass_HYPER:
                            rPropValues.Value <<= static_cast< sal_Int64 >( nVal );
                            break;
                        default:
                            OSL_FAIL( "OElementImport::implImportGenericProperties: unsupported value type!" );
                            break;
                        }
                    }
                    break;
                    default:
                        OSL_FAIL( "OElementImport::implImportGenericProperties: non-double values not supported!" );
                        break;
                    }
                }

                m_xElement->setPropertyValue( rPropValues.Name, rPropValues.Value );
            }
            catch(const Exception&)
            {
                DBG_UNHANDLED_EXCEPTION("xmloff.forms");
                OSL_FAIL(OStringBuffer("OElementImport::EndElement: could not set the property \"" +
                            OUStringToOString(rPropValues.Name, RTL_TEXTENCODING_ASCII_US) +
                            "\"!").getStr());
            }
        }
    }

    OUString OElementImport::implGetDefaultName() const
    {
        // no optimization here. If this method gets called, the XML stream did not contain a name for the
        // element, which is a heavy error. So in this case we don't care for performance
        static constexpr OUStringLiteral sUnnamedName = u"unnamed";
        OSL_ENSURE(m_xParentContainer.is(), "OElementImport::implGetDefaultName: no parent container!");
        if (!m_xParentContainer.is())
            return sUnnamedName;
        Sequence< OUString > aNames = m_xParentContainer->getElementNames();

        for (sal_Int32 i=0; i<32768; ++i)   // the limit is nearly arbitrary...
        {
            // assemble the new name (suggestion)
            OUString sReturn = sUnnamedName + OUString::number(i);
            // check the existence (this is the bad performance part...)
            if (comphelper::findValue(aNames, sReturn) == -1)
                // not found the name
                return sReturn;
        }
        OSL_FAIL("OElementImport::implGetDefaultName: did not find a free name!");
        return sUnnamedName;
    }

    PropertyGroups::const_iterator OElementImport::impl_matchPropertyGroup( const PropertyGroups& i_propertyGroups ) const
    {
        ENSURE_OR_RETURN( m_xInfo.is(), "OElementImport::impl_matchPropertyGroup: no property set info!", i_propertyGroups.end() );

        return std::find_if(i_propertyGroups.cbegin(), i_propertyGroups.cend(), [&](const PropertyDescriptionList& rGroup) {
            return std::all_of(rGroup.cbegin(), rGroup.cend(), [&](const PropertyDescription* prop) {
                return m_xInfo->hasPropertyByName( prop->propertyName );
            });
        });
    }

    bool OElementImport::tryGenericAttribute( sal_Int32 nElement, const OUString& _rValue )
    {
        // the generic approach (which I hope all props will be migrated to, on the medium term): property handlers
        const AttributeDescription attribute( metadata::getAttributeDescription( nElement ) );
        if ( attribute.attributeToken != XML_TOKEN_INVALID )
        {
            PropertyGroups propertyGroups;
            metadata::getPropertyGroupList( attribute, propertyGroups );
            const PropertyGroups::const_iterator pos = impl_matchPropertyGroup( propertyGroups );
            if ( pos == propertyGroups.end() )
                return false;

            do
            {
                const PropertyDescriptionList& rProperties( *pos );
                const PropertyDescription* first = *rProperties.begin();
                if ( !first )
                {
                    SAL_WARN( "xmloff.forms", "OElementImport::handleAttribute: invalid property description!" );
                    break;
                }

                const PPropertyHandler handler = (*first->factory)( first->propertyId );
                if ( !handler )
                {
                    SAL_WARN( "xmloff.forms", "OElementImport::handleAttribute: invalid property handler!" );
                    break;
                }

                PropertyValues aValues;
                for ( const auto& propDesc : rProperties )
                {
                    aValues[ propDesc->propertyId ] = Any();
                }
                if ( handler->getPropertyValues( _rValue, aValues ) )
                {
                    for ( const auto& propDesc : rProperties )
                    {
                        implPushBackPropertyValue( propDesc->propertyName, aValues[ propDesc->propertyId ] );
                    }
                }
            }
            while ( false );

            // handled
            return true;
        }
        return false;
    }

    bool OElementImport::handleAttribute(sal_Int32 nElement, const OUString& _rValue)
    {
        auto nLocal = nElement & TOKEN_MASK;
        if ( nLocal == XML_CONTROL_IMPLEMENTATION )
            // ignore this, it has already been handled in OElementImport::StartElement
            return true;

        if ( nLocal == XML_NAME )
        {
            if ( m_sName.isEmpty() )
                // remember the name for later use in EndElement
                m_sName = _rValue;
            return true;
        }

        // maybe it's the style attribute?
        if ( nLocal == XML_TEXT_STYLE_NAME )
        {
            const SvXMLStyleContext* pStyleContext = m_rContext.getStyleElement( _rValue );
            OSL_ENSURE( pStyleContext, "OElementImport::handleAttribute: do not know the style!" );
            // remember the element for later usage.
            m_pStyleElement = dynamic_cast<const XMLTextStyleContext*>( pStyleContext  );
            return true;
        }

        if ( m_bImplicitGenericAttributeHandling )
            if ( tryGenericAttribute( nElement, _rValue ) )
                return true;

        // let the base class handle it
        return OPropertyImport::handleAttribute( nElement, _rValue);
    }

    Reference< XPropertySet > OElementImport::createElement()
    {
        Reference< XPropertySet > xReturn;
        if (!m_sServiceName.isEmpty())
        {
            Reference< XComponentContext > xContext = m_rFormImport.getGlobalContext().GetComponentContext();
            Reference< XInterface > xPure = xContext->getServiceManager()->createInstanceWithContext(m_sServiceName, xContext);
            OSL_ENSURE(xPure.is(),
                        OStringBuffer("OElementImport::createElement: service factory gave me no object (service name: " +
                            OUStringToOString(m_sServiceName, RTL_TEXTENCODING_ASCII_US) +
                            ")!").getStr());
            xReturn.set(xPure, UNO_QUERY);
        }
        else
            OSL_FAIL("OElementImport::createElement: no service name to create an element!");

        return xReturn;
    }

    void OElementImport::registerEvents(const Sequence< ScriptEventDescriptor >& _rEvents)
    {
        OSL_ENSURE(m_xElement.is(), "OElementImport::registerEvents: no element to register events for!");
        m_rEventManager.registerEvents(m_xElement, _rEvents);
    }

    void OElementImport::simulateDefaultedAttribute(sal_Int32 nElement, const OUString& _rPropertyName, const char* _pAttributeDefault)
    {
        OSL_ENSURE( m_xInfo.is(), "OPropertyImport::simulateDefaultedAttribute: the component should be more gossipy about it's properties!" );

        if ( !m_xInfo.is() || m_xInfo->hasPropertyByName( _rPropertyName ) )
        {
            if ( !encounteredAttribute( nElement ) )
                OSL_VERIFY( handleAttribute( XML_ELEMENT(FORM, (nElement & TOKEN_MASK)), OUString::createFromAscii( _pAttributeDefault ) ) );
        }
    }

    //= OControlImport
    OControlImport::OControlImport(OFormLayerXMLImport_Impl& _rImport, IEventAttacherManager& _rEventManager,
            const Reference< XNameContainer >& _rxParentContainer)
        :OElementImport(_rImport, _rEventManager, _rxParentContainer)
        ,m_eElementType(OControlElement::UNKNOWN)
    {
        disableImplicitGenericAttributeHandling();
    }

    OControlImport::OControlImport(OFormLayerXMLImport_Impl& _rImport, IEventAttacherManager& _rEventManager,
            const Reference< XNameContainer >& _rxParentContainer, OControlElement::ElementType _eType)
        :OElementImport(_rImport, _rEventManager, _rxParentContainer)
        ,m_eElementType(_eType)
    {
        disableImplicitGenericAttributeHandling();
    }

    OUString OControlImport::determineDefaultServiceName() const
    {
        const char* pServiceName = nullptr;
        switch ( m_eElementType )
        {
        case OControlElement::TEXT:
        case OControlElement::TEXT_AREA:
        case OControlElement::PASSWORD:          pServiceName = "com.sun.star.form.component.TextField"; break;
        case OControlElement::FILE:              pServiceName = "com.sun.star.form.component.FileControl"; break;
        case OControlElement::FORMATTED_TEXT:    pServiceName = "com.sun.star.form.component.FormattedField"; break;
        case OControlElement::FIXED_TEXT:        pServiceName = "com.sun.star.form.component.FixedText"; break;
        case OControlElement::COMBOBOX:          pServiceName = "com.sun.star.form.component.ComboBox"; break;
        case OControlElement::LISTBOX:           pServiceName = "com.sun.star.form.component.ListBox"; break;
        case OControlElement::BUTTON:            pServiceName = "com.sun.star.form.component.CommandButton"; break;
        case OControlElement::IMAGE:             pServiceName = "com.sun.star.form.component.ImageButton"; break;
        case OControlElement::CHECKBOX:          pServiceName = "com.sun.star.form.component.CheckBox"; break;
        case OControlElement::RADIO:             pServiceName = "com.sun.star.form.component.RadioButton"; break;
        case OControlElement::FRAME:             pServiceName = "com.sun.star.form.component.GroupBox"; break;
        case OControlElement::IMAGE_FRAME:       pServiceName = "com.sun.star.form.component.DatabaseImageControl"; break;
        case OControlElement::HIDDEN:            pServiceName = "com.sun.star.form.component.HiddenControl"; break;
        case OControlElement::GRID:              pServiceName = "com.sun.star.form.component.GridControl"; break;
        case OControlElement::VALUERANGE:        pServiceName = "com.sun.star.form.component.ScrollBar"; break;
        case OControlElement::TIME:              pServiceName = "com.sun.star.form.component.TimeField"; break;
        case OControlElement::DATE:              pServiceName = "com.sun.star.form.component.DateField"; break;
        default:                                 break;
        }
        if ( pServiceName != nullptr )
            return OUString::createFromAscii( pServiceName );
        return OUString();
    }

    void OControlImport::addOuterAttributes(const Reference< XFastAttributeList >& _rxOuterAttribs)
    {
        OSL_ENSURE(!m_xOuterAttributes.is(), "OControlImport::addOuterAttributes: already have these attributes!");
        m_xOuterAttributes = _rxOuterAttribs;
    }

    bool OControlImport::handleAttribute(sal_Int32 nElement, const OUString& _rValue)
    {
        static sal_Int32 nLinkedCellAttributeName = OAttributeMetaData::getBindingAttributeToken(BAFlags::LinkedCell);

        if ((nElement & TOKEN_MASK) == XML_ID)
        {   // it's the control id
            if (IsTokenInNamespace(nElement, XML_NAMESPACE_XML))
            {
                m_sControlId = _rValue;
            }
            else if (IsTokenInNamespace(nElement, XML_NAMESPACE_FORM))
            {
                if (m_sControlId.isEmpty())
                {
                    m_sControlId = _rValue;
                }
            }
            return true;
        }

        if ( (nElement & TOKEN_MASK) == nLinkedCellAttributeName )
        {   // it's the address of a spreadsheet cell
            m_sBoundCellAddress = _rValue;
            return true;
        }

        if ( nElement == XML_ELEMENT(XFORMS, XML_BIND ) )
        {
            m_sBindingID = _rValue;
            return true;
        }

        if ( nElement == XML_ELEMENT(FORM, XML_XFORMS_LIST_SOURCE)  )
        {
            m_sListBindingID = _rValue;
            return true;
        }

        if  (   nElement == XML_ELEMENT(FORM, XML_XFORMS_SUBMISSION)
            ||  nElement == XML_ELEMENT(XFORMS, XML_SUBMISSION) )
        {
            m_sSubmissionID = _rValue;
            return true;
        }

        if ( OElementImport::tryGenericAttribute( nElement, _rValue ) )
            return true;

        static const sal_Int32 nValueAttributeName = OAttributeMetaData::getCommonControlAttributeToken(CCAFlags::Value);
        static const sal_Int32 nCurrentValueAttributeName = OAttributeMetaData::getCommonControlAttributeToken(CCAFlags::CurrentValue);
        static const sal_Int32 nMinValueAttributeName = OAttributeMetaData::getSpecialAttributeToken(SCAFlags::MinValue);
        static const sal_Int32 nMaxValueAttributeName = OAttributeMetaData::getSpecialAttributeToken(SCAFlags::MaxValue);
        static const sal_Int32 nRepeatDelayAttributeName = OAttributeMetaData::getSpecialAttributeToken( SCAFlags::RepeatDelay );

        sal_Int32 nHandle = -1;
        if ( (nElement & TOKEN_MASK) == nValueAttributeName )
            nHandle = PROPID_VALUE;
        else if ( (nElement & TOKEN_MASK) == nCurrentValueAttributeName )
            nHandle = PROPID_CURRENT_VALUE;
        else if ( (nElement & TOKEN_MASK) == nMinValueAttributeName )
            nHandle = PROPID_MIN_VALUE;
        else if ( (nElement & TOKEN_MASK) == nMaxValueAttributeName )
            nHandle = PROPID_MAX_VALUE;
        if ( nHandle != -1 )
        {
            // for the moment, simply remember the name and the value
            PropertyValue aProp;
            aProp.Name = SvXMLImport::getNameFromToken(nElement);
            aProp.Handle = nHandle;
            aProp.Value <<= _rValue;
            m_aValueProperties.push_back(aProp);
            return true;
        }

        if ( (nElement & TOKEN_MASK) == nRepeatDelayAttributeName )
        {
            util::Duration aDuration;
            if (::sax::Converter::convertDuration(aDuration, _rValue))
            {
                PropertyValue aProp;
                aProp.Name = PROPERTY_REPEAT_DELAY;
                sal_Int32 const nMS =
                    ((aDuration.Hours * 60 + aDuration.Minutes) * 60
                     + aDuration.Seconds) * 1000 + aDuration.NanoSeconds/1000000;
                aProp.Value <<= nMS;

                implPushBackPropertyValue(aProp);
            }
            return true;
        }

        return OElementImport::handleAttribute( nElement, _rValue );
    }

    void OControlImport::startFastElement(sal_Int32 nElement, const Reference< css::xml::sax::XFastAttributeList >& _rxAttrList)
    {
        css::uno::Reference< css::xml::sax::XFastAttributeList > xMergedAttributes;
        if( m_xOuterAttributes.is() )
        {
            // merge the attribute lists, our own one
            rtl::Reference<sax_fastparser::FastAttributeList> xMerger(new sax_fastparser::FastAttributeList(_rxAttrList));
            // and the ones of our enclosing element
            xMerger->add(m_xOuterAttributes);
            xMergedAttributes = xMerger.get();
        }
        else
        {
            xMergedAttributes = _rxAttrList;
        }

        // let the base class handle all the attributes
        OElementImport::startFastElement(nElement, xMergedAttributes);

        if ( m_aValueProperties.empty() || !m_xElement.is())
            return;

        // get the property set info
        if (!m_xInfo.is())
        {
            OSL_FAIL("OControlImport::StartElement: no PropertySetInfo!");
            return;
        }

        const char* pValueProperty = nullptr;
        const char* pCurrentValueProperty = nullptr;
        const char* pMinValueProperty = nullptr;
        const char* pMaxValueProperty = nullptr;

        bool bRetrievedValues = false;
        bool bRetrievedValueLimits = false;

        // get the class id of our element
        sal_Int16 nClassId = FormComponentType::CONTROL;
        m_xElement->getPropertyValue(PROPERTY_CLASSID) >>= nClassId;

        // translate the value properties we collected in handleAttributes
        for ( auto& rValueProps : m_aValueProperties )
        {
            bool bSuccess = false;
            switch (rValueProps.Handle)
            {
                case PROPID_VALUE:
                case PROPID_CURRENT_VALUE:
                {
                    // get the property names
                    if (!bRetrievedValues)
                    {
                        getValuePropertyNames(m_eElementType, nClassId, pCurrentValueProperty, pValueProperty);
                        if ( !pCurrentValueProperty && !pValueProperty )
                        {
                            SAL_WARN( "xmloff.forms", "OControlImport::StartElement: illegal value property names!" );
                            break;
                        }

                        bRetrievedValues = true;
                    }
                    if ( PROPID_VALUE == rValueProps.Handle && !pValueProperty )
                    {
                        SAL_WARN( "xmloff.forms", "OControlImport::StartElement: the control does not have a value property!");
                        break;
                    }

                    if ( PROPID_CURRENT_VALUE == rValueProps.Handle && !pCurrentValueProperty )
                    {
                        SAL_WARN( "xmloff.forms", "OControlImport::StartElement: the control does not have a current-value property!");
                        break;
                    }

                    // transfer the name
                    if (PROPID_VALUE == rValueProps.Handle)
                        rValueProps.Name = OUString::createFromAscii(pValueProperty);
                    else
                        rValueProps.Name = OUString::createFromAscii(pCurrentValueProperty);
                    bSuccess = true;
                }
                break;
                case PROPID_MIN_VALUE:
                case PROPID_MAX_VALUE:
                {
                    // get the property names
                    if (!bRetrievedValueLimits)
                    {
                        getValueLimitPropertyNames(nClassId, pMinValueProperty, pMaxValueProperty);
                        if ( !pMinValueProperty || !pMaxValueProperty )
                        {
                            SAL_WARN( "xmloff.forms", "OControlImport::StartElement: illegal value limit property names!" );
                            break;
                        }

                        bRetrievedValueLimits = true;
                    }
                    OSL_ENSURE((PROPID_MIN_VALUE != rValueProps.Handle) || pMinValueProperty,
                        "OControlImport::StartElement: the control does not have a value property!");
                    OSL_ENSURE((PROPID_MAX_VALUE != rValueProps.Handle) || pMaxValueProperty,
                        "OControlImport::StartElement: the control does not have a current-value property!");

                    // transfer the name
                    if (PROPID_MIN_VALUE == rValueProps.Handle)
                        rValueProps.Name = OUString::createFromAscii(pMinValueProperty);
                    else
                        rValueProps.Name = OUString::createFromAscii(pMaxValueProperty);
                    bSuccess = true;
                }
                break;
            }

            if ( !bSuccess )
                continue;

            // translate the value
            implTranslateValueProperty(m_xInfo, rValueProps);
            // add the property to the base class' array
            implPushBackPropertyValue(rValueProps);
        }

    }

    void OControlImport::implTranslateValueProperty(const Reference< XPropertySetInfo >& _rxPropInfo,
        PropertyValue& _rPropValue)
    {
        OSL_ENSURE(_rxPropInfo->hasPropertyByName(_rPropValue.Name),
            "OControlImport::implTranslateValueProperty: invalid property name!");

        // retrieve the type of the property
        Property aProp = _rxPropInfo->getPropertyByName(_rPropValue.Name);
        // the untranslated string value as read in handleAttribute
        OUString sValue;
        bool bSuccess = _rPropValue.Value >>= sValue;
        OSL_ENSURE(bSuccess, "OControlImport::implTranslateValueProperty: supposed to be called with non-translated string values!");

        if (TypeClass_ANY == aProp.Type.getTypeClass())
        {
            // we have exactly 2 properties where this type class is allowed:
            SAL_WARN_IF(
                    _rPropValue.Name != PROPERTY_EFFECTIVE_VALUE
                &&  _rPropValue.Name != PROPERTY_EFFECTIVE_DEFAULT, "xmloff",
                "OControlImport::implTranslateValueProperty: invalid property type/name combination, Any and " << _rPropValue.Name);

            // Both properties are allowed to have a double or a string value,
            // so first try to convert the string into a number
            double nValue;
            if (::sax::Converter::convertDouble(nValue, sValue))
                _rPropValue.Value <<= nValue;
            else
                _rPropValue.Value <<= sValue;
        }
        else
            _rPropValue.Value = PropertyConversion::convertString(aProp.Type, sValue);
    }

    void OControlImport::endFastElement(sal_Int32 nElement)
    {
        OSL_ENSURE(m_xElement.is(), "OControlImport::EndElement: invalid control!");
        if ( !m_xElement.is() )
            return;

        // register our control with its id
        if (!m_sControlId.isEmpty())
            m_rFormImport.registerControlId(m_xElement, m_sControlId);
        // it's allowed to have no control id. In this case we're importing a column

        // one more pre-work to do:
        // when we set default values, then by definition the respective value is set
        // to this default value, too. This means if the sequence contains for example
        // a DefaultText value, then the Text will be affected by this, too.
        // In case the Text is not part of the property sequence (or occurs _before_
        // the DefaultText, which can happen for other value/default-value property names),
        // this means that the Text (the value property) is incorrectly imported.

        bool bRestoreValuePropertyValue = false;
        Any aValuePropertyValue;

        sal_Int16 nClassId = FormComponentType::CONTROL;
        try
        {
            // get the class id of our element
            m_xElement->getPropertyValue(PROPERTY_CLASSID) >>= nClassId;
        }
        catch( const Exception& )
        {
            TOOLS_WARN_EXCEPTION("xmloff.forms",
                                 "caught an exception while retrieving the class id!");
        }

        const char* pValueProperty = nullptr;
        const char* pDefaultValueProperty = nullptr;
        getRuntimeValuePropertyNames(m_eElementType, nClassId, pValueProperty, pDefaultValueProperty);
        if ( pDefaultValueProperty && pValueProperty )
        {
            bool bNonDefaultValuePropertyValue = false;
                // is the "value property" part of the sequence?

            // look up this property in our sequence
            for ( const auto& rCheck : m_aValues )
            {
                if ( rCheck.Name.equalsAscii( pDefaultValueProperty ) )
                    bRestoreValuePropertyValue = true;
                else if ( rCheck.Name.equalsAscii( pValueProperty ) )
                {
                    bNonDefaultValuePropertyValue = true;
                    // we need to restore the value property we found here, nothing else
                    aValuePropertyValue = rCheck.Value;
                }
            }

            if ( bRestoreValuePropertyValue && !bNonDefaultValuePropertyValue )
            {
                // found it -> need to remember (and restore) the "value property value", which is not set explicitly
                try
                {
                    aValuePropertyValue = m_xElement->getPropertyValue( OUString::createFromAscii( pValueProperty ) );
                }
                catch( const Exception& )
                {
                    TOOLS_WARN_EXCEPTION(
                        "xmloff.forms",
                        "caught an exception while retrieving the current value property!");
                }
            }
        }

        // let the base class set all the values
        OElementImport::endFastElement(nElement);

        // restore the "value property value", if necessary
        if ( bRestoreValuePropertyValue && pValueProperty )
        {
            try
            {
                m_xElement->setPropertyValue( OUString::createFromAscii( pValueProperty ), aValuePropertyValue );
            }
            catch( const Exception& )
            {
                TOOLS_WARN_EXCEPTION("xmloff.forms",
                                     "caught an exception while restoring the value property!");
            }
        }

        // the external cell binding, if applicable
        if ( m_xElement.is() && !m_sBoundCellAddress.isEmpty() )
            doRegisterCellValueBinding( m_sBoundCellAddress );

        // XForms binding, if applicable
        if ( m_xElement.is() && !m_sBindingID.isEmpty() )
            doRegisterXFormsValueBinding( m_sBindingID );

        // XForms list binding, if applicable
        if ( m_xElement.is() && !m_sListBindingID.isEmpty() )
            doRegisterXFormsListBinding( m_sListBindingID );

        // XForms submission, if applicable
        if ( m_xElement.is() && !m_sSubmissionID.isEmpty() )
            doRegisterXFormsSubmission( m_sSubmissionID );
    }

    void OControlImport::doRegisterCellValueBinding( const OUString& _rBoundCellAddress )
    {
        OSL_PRECOND( m_xElement.is(), "OControlImport::doRegisterCellValueBinding: invalid element!" );
        OSL_PRECOND( !_rBoundCellAddress.isEmpty(),
            "OControlImport::doRegisterCellValueBinding: invalid address!" );

        m_rContext.registerCellValueBinding( m_xElement, _rBoundCellAddress );
    }

    void OControlImport::doRegisterXFormsValueBinding( const OUString& _rBindingID )
    {
        OSL_PRECOND( m_xElement.is(), "need element" );
        OSL_PRECOND( !_rBindingID.isEmpty(), "binding ID is not valid" );

        m_rContext.registerXFormsValueBinding( m_xElement, _rBindingID );
    }

    void OControlImport::doRegisterXFormsListBinding( const OUString& _rBindingID )
    {
        OSL_PRECOND( m_xElement.is(), "need element" );
        OSL_PRECOND( !_rBindingID.isEmpty(), "binding ID is not valid" );

        m_rContext.registerXFormsListBinding( m_xElement, _rBindingID );
    }

    void OControlImport::doRegisterXFormsSubmission( const OUString& _rSubmissionID )
    {
        OSL_PRECOND( m_xElement.is(), "need element" );
        OSL_PRECOND( !_rSubmissionID.isEmpty(), "binding ID is not valid" );

        m_rContext.registerXFormsSubmission( m_xElement, _rSubmissionID );
    }

    Reference< XPropertySet > OControlImport::createElement()
    {
        const Reference<XPropertySet> xPropSet = OElementImport::createElement();
        if ( xPropSet.is() )
        {
            m_xInfo = xPropSet->getPropertySetInfo();
            if ( m_xInfo.is() && m_xInfo->hasPropertyByName(PROPERTY_ALIGN) )
            {
                Any aValue;
                xPropSet->setPropertyValue(PROPERTY_ALIGN,aValue);
            }
        }
        return xPropSet;
    }

    //= OImagePositionImport
    OImagePositionImport::OImagePositionImport( OFormLayerXMLImport_Impl& _rImport, IEventAttacherManager& _rEventManager,
        const Reference< XNameContainer >& _rxParentContainer,
        OControlElement::ElementType _eType )
        :OControlImport( _rImport, _rEventManager, _rxParentContainer, _eType )
        ,m_nImagePosition( -1 )
        ,m_nImageAlign( 0 )
        ,m_bHaveImagePosition( false )
    {
    }

    bool OImagePositionImport::handleAttribute( sal_Int32 nElement,
        const OUString& _rValue )
    {
        static const sal_Int32 s_nImageDataAttributeName = OAttributeMetaData::getCommonControlAttributeToken(CCAFlags::ImageData);

        if ( (nElement & TOKEN_MASK) == s_nImageDataAttributeName)
        {
            m_xGraphic = m_rContext.getGlobalContext().loadGraphicByURL(_rValue);
            return true;
        }
        else if ( (nElement & TOKEN_MASK) == XML_IMAGE_POSITION )
        {
            OSL_VERIFY( PropertyConversion::convertString(
                cppu::UnoType<decltype(m_nImagePosition)>::get(),
                _rValue, aImagePositionMap
            ) >>= m_nImagePosition );
            m_bHaveImagePosition = true;
            return true;
        }
        else if ( (nElement & TOKEN_MASK) == XML_IMAGE_ALIGN )
        {
            OSL_VERIFY( PropertyConversion::convertString(
                cppu::UnoType<decltype(m_nImageAlign)>::get(),
                _rValue, aImageAlignMap
            ) >>= m_nImageAlign );
            return true;
        }

        return OControlImport::handleAttribute( nElement, _rValue );
    }

    void OImagePositionImport::startFastElement(sal_Int32 nElement, const Reference< XFastAttributeList >& _rxAttrList)
    {
        OControlImport::startFastElement( nElement, _rxAttrList );

        if (m_xGraphic.is())
        {
            PropertyValue aGraphicProperty;
            aGraphicProperty.Name = PROPERTY_GRAPHIC;
            aGraphicProperty.Value <<= m_xGraphic;
            implPushBackPropertyValue(aGraphicProperty);
        }
        if ( !m_bHaveImagePosition )
            return;

        sal_Int16 nUnoImagePosition = ImagePosition::Centered;
        if ( m_nImagePosition >= 0 )
        {
            OSL_ENSURE( ( m_nImagePosition <= 3 ) && ( m_nImageAlign >= 0 ) && ( m_nImageAlign < 3 ),
                "OImagePositionImport::StartElement: unknown image align and/or position!" );
            nUnoImagePosition = m_nImagePosition * 3 + m_nImageAlign;
        }

        PropertyValue aImagePosition;
        aImagePosition.Name = PROPERTY_IMAGE_POSITION;
        aImagePosition.Value <<= nUnoImagePosition;
        implPushBackPropertyValue( aImagePosition );
    }

    //= OReferredControlImport
    OReferredControlImport::OReferredControlImport(
            OFormLayerXMLImport_Impl& _rImport, IEventAttacherManager& _rEventManager,
            const Reference< XNameContainer >& _rxParentContainer )
        :OControlImport(_rImport, _rEventManager, _rxParentContainer)
    {
    }

    void OReferredControlImport::startFastElement(sal_Int32 nElement, const Reference< XFastAttributeList >& _rxAttrList)
    {
        OControlImport::startFastElement(nElement, _rxAttrList);

        // the base class should have created the control, so we can register it
        if ( !m_sReferringControls.isEmpty() )
            m_rFormImport.registerControlReferences(m_xElement, m_sReferringControls);
    }

    bool OReferredControlImport::handleAttribute(sal_Int32 nElement,
        const OUString& _rValue)
    {
        static const sal_Int32 s_nReferenceAttributeName = OAttributeMetaData::getCommonControlAttributeToken(CCAFlags::For);
        if ((nElement & TOKEN_MASK) == s_nReferenceAttributeName)
        {
            m_sReferringControls = _rValue;
            return true;
        }
        return OControlImport::handleAttribute(nElement, _rValue);
    }

    //= OPasswordImport
    OPasswordImport::OPasswordImport(OFormLayerXMLImport_Impl& _rImport, IEventAttacherManager& _rEventManager,
            const Reference< XNameContainer >& _rxParentContainer, OControlElement::ElementType _eType)
        :OControlImport(_rImport, _rEventManager, _rxParentContainer, _eType)
    {
    }

    bool OPasswordImport::handleAttribute(sal_Int32 nElement, const OUString& _rValue)
    {
        static const sal_Int32 s_nEchoCharAttributeName = OAttributeMetaData::getSpecialAttributeToken(SCAFlags::EchoChar);
        if ((nElement & TOKEN_MASK) == s_nEchoCharAttributeName)
        {
            // need a special handling for the EchoChar property
            PropertyValue aEchoChar;
            aEchoChar.Name = PROPERTY_ECHOCHAR;
            OSL_ENSURE(_rValue.getLength() == 1, "OPasswordImport::handleAttribute: invalid echo char attribute!");
                // we ourself should not have written values other than of length 1
            if (_rValue.getLength() >= 1)
                aEchoChar.Value <<= static_cast<sal_Int16>(_rValue[0]);
            else
                aEchoChar.Value <<= sal_Int16(0);
            implPushBackPropertyValue(aEchoChar);
            return true;
        }
        return OControlImport::handleAttribute(nElement, _rValue);
    }

    //= ORadioImport
    ORadioImport::ORadioImport(OFormLayerXMLImport_Impl& _rImport, IEventAttacherManager& _rEventManager,
            const Reference< XNameContainer >& _rxParentContainer, OControlElement::ElementType _eType)
        :OImagePositionImport( _rImport, _rEventManager, _rxParentContainer, _eType )
    {
    }

    bool ORadioImport::handleAttribute(sal_Int32 nElement, const OUString& _rValue)
    {
        // need special handling for the State & CurrentState properties:
        // they're stored as booleans, but expected to be int16 properties
        static const sal_Int32 nCurrentSelectedAttributeName = OAttributeMetaData::getCommonControlAttributeToken(CCAFlags::CurrentSelected);
        static const sal_Int32 nSelectedAttributeName = OAttributeMetaData::getCommonControlAttributeToken(CCAFlags::Selected);
        if  (  (nElement & TOKEN_MASK) == nCurrentSelectedAttributeName
            || (nElement & TOKEN_MASK) == nSelectedAttributeName
            )
        {
            const OAttribute2Property::AttributeAssignment* pProperty = m_rContext.getAttributeMap().getAttributeTranslation(nElement & TOKEN_MASK);
            assert(pProperty && "ORadioImport::handleAttribute: invalid property map!");
            if (pProperty)
            {
                const Any aBooleanValue( PropertyConversion::convertString(pProperty->aPropertyType, _rValue, pProperty->pEnumMap) );

                // create and store a new PropertyValue
                PropertyValue aNewValue;
                aNewValue.Name = pProperty->sPropertyName;
                aNewValue.Value <<= static_cast<sal_Int16>(::cppu::any2bool(aBooleanValue));

                implPushBackPropertyValue(aNewValue);
            }
            return true;
        }
        return OImagePositionImport::handleAttribute( nElement, _rValue );
    }

    //= OURLReferenceImport
    OURLReferenceImport::OURLReferenceImport(OFormLayerXMLImport_Impl& _rImport, IEventAttacherManager& _rEventManager,
            const Reference< XNameContainer >& _rxParentContainer,
            OControlElement::ElementType _eType)
        :OImagePositionImport(_rImport, _rEventManager, _rxParentContainer, _eType)
    {
    }

    bool OURLReferenceImport::handleAttribute(sal_Int32 nElement, const OUString& _rValue)
    {
        static const sal_Int32 s_nTargetLocationAttributeName   = OAttributeMetaData::getCommonControlAttributeToken( CCAFlags::TargetLocation );
        static const sal_Int32 s_nImageDataAttributeName        = OAttributeMetaData::getCommonControlAttributeToken( CCAFlags::ImageData );

        // need to make the URL absolute if
        // * it's the image-data attribute
        // * it's the target-location attribute, and we're dealing with an object which has the respective property
        bool bMakeAbsolute =
                (nElement & TOKEN_MASK) == s_nImageDataAttributeName
            ||  (   (nElement & TOKEN_MASK) == s_nTargetLocationAttributeName
                &&  (   ( OControlElement::BUTTON == m_eElementType )
                    ||  ( OControlElement::IMAGE == m_eElementType )
                    )
                );

        if (bMakeAbsolute && !_rValue.isEmpty())
        {
            OUString sAdjustedValue = _rValue;
            if ((nElement & TOKEN_MASK) != s_nImageDataAttributeName)
                sAdjustedValue = m_rContext.getGlobalContext().GetAbsoluteReference( _rValue );
            return OImagePositionImport::handleAttribute( nElement, sAdjustedValue );
        }

        return OImagePositionImport::handleAttribute( nElement, _rValue );
    }

    //= OButtonImport
    OButtonImport::OButtonImport(OFormLayerXMLImport_Impl& _rImport, IEventAttacherManager& _rEventManager,
            const Reference< XNameContainer >& _rxParentContainer,
            OControlElement::ElementType _eType)
        :OURLReferenceImport(_rImport, _rEventManager, _rxParentContainer, _eType)
    {
        enableTrackAttributes();
    }

    void OButtonImport::startFastElement(sal_Int32 nElement, const Reference< XFastAttributeList >& _rxAttrList)
    {
        OURLReferenceImport::startFastElement(nElement, _rxAttrList);

        // handle the target-frame attribute
        simulateDefaultedAttribute(OAttributeMetaData::getCommonControlAttributeToken(CCAFlags::TargetFrame), PROPERTY_TARGETFRAME, "_blank");
    }

    //= OValueRangeImport
    OValueRangeImport::OValueRangeImport( OFormLayerXMLImport_Impl& _rImport, IEventAttacherManager& _rEventManager,
            const Reference< XNameContainer >& _rxParentContainer, OControlElement::ElementType _eType )
        :OControlImport( _rImport, _rEventManager, _rxParentContainer, _eType )
        ,m_nStepSizeValue( 1 )
    {

    }

    bool OValueRangeImport::handleAttribute( sal_Int32 nElement, const OUString& _rValue )
    {
        if ( (nElement & TOKEN_MASK) == OAttributeMetaData::getSpecialAttributeToken( SCAFlags::StepSize ) )
        {
            ::sax::Converter::convertNumber( m_nStepSizeValue, _rValue );
            return true;
        }
        return OControlImport::handleAttribute( nElement, _rValue );
    }

    void OValueRangeImport::startFastElement( sal_Int32 nElement, const Reference< XFastAttributeList >& _rxAttrList )
    {
        OControlImport::startFastElement( nElement, _rxAttrList );

        if ( m_xInfo.is() )
        {
            if ( m_xInfo->hasPropertyByName( PROPERTY_SPIN_INCREMENT ) )
                m_xElement->setPropertyValue( PROPERTY_SPIN_INCREMENT, Any( m_nStepSizeValue ) );
            else if ( m_xInfo->hasPropertyByName( PROPERTY_LINE_INCREMENT ) )
                m_xElement->setPropertyValue( PROPERTY_LINE_INCREMENT, Any( m_nStepSizeValue ) );
        }
    }

    //= OTextLikeImport
    OTextLikeImport::OTextLikeImport(OFormLayerXMLImport_Impl& _rImport, IEventAttacherManager& _rEventManager,
            const Reference< XNameContainer >& _rxParentContainer,
            OControlElement::ElementType _eType)
        :OControlImport(_rImport, _rEventManager, _rxParentContainer, _eType)
        ,m_bEncounteredTextPara( false )
    {
        enableTrackAttributes();
    }

    css::uno::Reference< css::xml::sax::XFastContextHandler > OTextLikeImport::createFastChildContext(
        sal_Int32 nElement,
        const css::uno::Reference< css::xml::sax::XFastAttributeList >& xAttrList )
    {
        if ( nElement == XML_ELEMENT(TEXT, XML_P) )
        {
            OSL_ENSURE( m_eElementType == OControlElement::TEXT_AREA,
                "OTextLikeImport::CreateChildContext: text paragraphs in a non-text-area?" );

            if ( m_eElementType == OControlElement::TEXT_AREA )
            {
                Reference< XText > xTextElement( m_xElement, UNO_QUERY );
                if ( xTextElement.is() )
                {
                    rtl::Reference < XMLTextImportHelper > xTextImportHelper( m_rContext.getGlobalContext().GetTextImport() );

                    if ( !m_xCursor.is() )
                    {
                        m_xOldCursor = xTextImportHelper->GetCursor();
                        m_xCursor = xTextElement->createTextCursor();

                        if ( m_xCursor.is() )
                            xTextImportHelper->SetCursor( m_xCursor );
                    }
                    if ( m_xCursor.is() )
                    {
                        m_bEncounteredTextPara = true;
                        return xTextImportHelper->CreateTextChildContext( m_rContext.getGlobalContext(), nElement, xAttrList );
                    }
                }
                else
                {
                    // in theory, we could accumulate all the text portions (without formatting),
                    // and set it as Text property at the model ...
                }
            }
        }

        return OControlImport::createFastChildContext( nElement, xAttrList );
    }

    void OTextLikeImport::startFastElement(sal_Int32 nElement, const Reference< css::xml::sax::XFastAttributeList >& _rxAttrList)
    {
        OControlImport::startFastElement(nElement, _rxAttrList);

        // handle the convert-empty-to-null attribute, whose default is different from the property default
        // unfortunately, different classes are imported by this class ('cause they're represented by the
        // same XML element), though not all of them know this property.
        // So we have to do a check ...
        if (m_xElement.is() && m_xInfo.is() && m_xInfo->hasPropertyByName(PROPERTY_EMPTY_IS_NULL) )
            simulateDefaultedAttribute(OAttributeMetaData::getDatabaseAttributeToken(DAFlags::ConvertEmpty), PROPERTY_EMPTY_IS_NULL, "false");
    }

    namespace {

    struct EqualHandle
    {
        const sal_Int32 m_nHandle;
        explicit EqualHandle( sal_Int32 _nHandle ) : m_nHandle( _nHandle ) { }

        bool operator()( const PropertyValue& _rProp )
        {
            return _rProp.Handle == m_nHandle;
        }
    };

    }

    void OTextLikeImport::removeRedundantCurrentValue()
    {
        if ( !m_bEncounteredTextPara )
            return;

        // In case the text is written in the text:p elements, we need to ignore what we read as
        // current-value attribute, since it's redundant.
        // fortunately, OElementImport tagged the value property with the PROPID_CURRENT_VALUE
        // handle, so we do not need to determine the name of our value property here
        // (normally, it should be "Text", since no other controls than the edit field should
        // have the text:p elements)
        PropertyValueArray::iterator aValuePropertyPos = ::std::find_if(
            m_aValues.begin(),
            m_aValues.end(),
            EqualHandle( PROPID_CURRENT_VALUE )
        );
        if ( aValuePropertyPos != m_aValues.end() )
        {
            OSL_ENSURE( aValuePropertyPos->Name == PROPERTY_TEXT, "OTextLikeImport::EndElement: text:p was present, but our value property is *not* 'Text'!" );
            if ( aValuePropertyPos->Name == PROPERTY_TEXT )
            {
                m_aValues.erase(aValuePropertyPos);
            }
        }

        // additionally, we need to set the "RichText" property of our element to sal_True
        // (the presence of the text:p is used as indicator for the value of the RichText property)
        bool bHasRichTextProperty = false;
        if ( m_xInfo.is() )
            bHasRichTextProperty = m_xInfo->hasPropertyByName( PROPERTY_RICH_TEXT );
        OSL_ENSURE( bHasRichTextProperty, "OTextLikeImport::EndElement: text:p, but no rich text control?" );
        if ( bHasRichTextProperty )
            m_xElement->setPropertyValue( PROPERTY_RICH_TEXT, Any( true ) );
        // Note that we do *not* set the RichText property (in case our element has one) to sal_False here
        // since this is the default of this property, anyway.
    }

    namespace {

    struct EqualName
    {
        const OUString & m_sName;
        explicit EqualName( const OUString& _rName ) : m_sName( _rName ) { }

        bool operator()( const PropertyValue& _rProp )
        {
            return _rProp.Name == m_sName;
        }
    };

    }

    void OTextLikeImport::adjustDefaultControlProperty()
    {
        // In OpenOffice.org 2.0, we changed the implementation of the css.form.component.TextField (the model of a text field control),
        // so that it now uses another default control. So if we encounter a text field where the *old* default
        // control property is writing, we are not allowed to use it
        PropertyValueArray::iterator aDefaultControlPropertyPos = ::std::find_if(
            m_aValues.begin(),
            m_aValues.end(),
            EqualName( "DefaultControl" )
        );
        if ( aDefaultControlPropertyPos != m_aValues.end() )
        {
            OUString sDefaultControl;
            OSL_VERIFY( aDefaultControlPropertyPos->Value >>= sDefaultControl );
            if ( sDefaultControl == "stardiv.one.form.control.Edit" )
            {
                // complete remove this property value from the array. Today's "default value" of the "DefaultControl"
                // property is sufficient
                m_aValues.erase(aDefaultControlPropertyPos);
            }
        }
    }

    void OTextLikeImport::endFastElement(sal_Int32 nElement)
    {
        removeRedundantCurrentValue();
        adjustDefaultControlProperty();

        // let the base class do the stuff
        OControlImport::endFastElement(nElement);

        // some cleanups
        rtl::Reference < XMLTextImportHelper > xTextImportHelper( m_rContext.getGlobalContext().GetTextImport() );
        if ( m_xCursor.is() )
        {
            // delete the newline which has been imported erroneously
            // TODO (fs): stole this code somewhere - why don't we fix the text import??
            m_xCursor->gotoEnd( false );
            m_xCursor->goLeft( 1, true );
            m_xCursor->setString( OUString() );

            // reset cursor
            xTextImportHelper->ResetCursor();
        }

        if ( m_xOldCursor.is() )
            xTextImportHelper->SetCursor( m_xOldCursor );

    }

    //= OListAndComboImport
    OListAndComboImport::OListAndComboImport(OFormLayerXMLImport_Impl& _rImport, IEventAttacherManager& _rEventManager,
            const Reference< XNameContainer >& _rxParentContainer,
            OControlElement::ElementType _eType)
        :OControlImport(_rImport, _rEventManager, _rxParentContainer, _eType)
        ,m_nEmptyListItems( 0 )
        ,m_nEmptyValueItems( 0 )
        ,m_bEncounteredLSAttrib( false )
        ,m_bLinkWithIndexes( false )
    {
        if (OControlElement::COMBOBOX == m_eElementType)
            enableTrackAttributes();
    }

    css::uno::Reference< css::xml::sax::XFastContextHandler > OListAndComboImport::createFastChildContext(
        sal_Int32 nElement,
        const css::uno::Reference< css::xml::sax::XFastAttributeList >& _rxAttrList )
    {
        // is it the "option" sub tag of a listbox ?
        if ((nElement & TOKEN_MASK) == XML_OPTION)
            return new OListOptionImport(GetImport(), this);

        // is it the "item" sub tag of a combobox ?
        if ((nElement & TOKEN_MASK) == XML_ITEM)
            return new OComboItemImport(GetImport(), this);

        // everything else
        return OControlImport::createFastChildContext(nElement, _rxAttrList);
    }

    void OListAndComboImport::startFastElement(sal_Int32 nElement, const Reference< XFastAttributeList >& _rxAttrList)
    {
        m_bLinkWithIndexes = false;

        OControlImport::startFastElement(nElement, _rxAttrList);

        if (OControlElement::COMBOBOX == m_eElementType)
        {
            // for the auto-completion
            // the attribute default does not equal the property default, so in case we did not read this attribute,
            // we have to simulate it
            simulateDefaultedAttribute( OAttributeMetaData::getSpecialAttributeToken( SCAFlags::AutoCompletion ), PROPERTY_AUTOCOMPLETE, "false");

            // same for the convert-empty-to-null attribute, which's default is different from the property default
            simulateDefaultedAttribute( OAttributeMetaData::getDatabaseAttributeToken( DAFlags::ConvertEmpty ), PROPERTY_EMPTY_IS_NULL, "false");
        }
    }

    void OListAndComboImport::endFastElement(sal_Int32 nElement)
    {
        // append the list source property the properties sequence of our importer
        // the string item list
        PropertyValue aItemList;
        aItemList.Name = PROPERTY_STRING_ITEM_LIST;
        aItemList.Value <<= comphelper::containerToSequence(m_aListSource);
        implPushBackPropertyValue(aItemList);

        if (OControlElement::LISTBOX == m_eElementType)
        {
            OSL_ENSURE((m_aListSource.size() + m_nEmptyListItems) == (m_aValueList.size() + m_nEmptyValueItems),
                "OListAndComboImport::EndElement: inconsistence between labels and values!");

            if ( !m_bEncounteredLSAttrib )
            {
                // the value sequence
                PropertyValue aValueList;
                aValueList.Name = PROPERTY_LISTSOURCE;
                aValueList.Value <<= comphelper::containerToSequence(m_aValueList);
                implPushBackPropertyValue(aValueList);
            }

            // the select sequence
            PropertyValue aSelected;
            aSelected.Name = PROPERTY_SELECT_SEQ;
            aSelected.Value <<= comphelper::containerToSequence(m_aSelectedSeq);
            implPushBackPropertyValue(aSelected);

            // the default select sequence
            PropertyValue aDefaultSelected;
            aDefaultSelected.Name = PROPERTY_DEFAULT_SELECT_SEQ;
            aDefaultSelected.Value <<= comphelper::containerToSequence(m_aDefaultSelectedSeq);
            implPushBackPropertyValue(aDefaultSelected);
        }

        OControlImport::endFastElement(nElement);

        // the external list source, if applicable
        if ( m_xElement.is() && !m_sCellListSource.isEmpty() )
            m_rContext.registerCellRangeListSource( m_xElement, m_sCellListSource );
    }

    void OListAndComboImport::doRegisterCellValueBinding( const OUString& _rBoundCellAddress )
    {
        OUString sBoundCellAddress( _rBoundCellAddress );
        if ( m_bLinkWithIndexes )
        {
            // This is a HACK. We register a string which is no valid address, but allows
            // (somewhere else) to determine that a non-standard binding should be created.
            // This hack is acceptable for OOo 1.1.1, since the file format for value
            // bindings of form controls is to be changed afterwards, anyway.
            sBoundCellAddress += ":index";
        }

        OControlImport::doRegisterCellValueBinding( sBoundCellAddress );
    }

    bool OListAndComboImport::handleAttribute(sal_Int32 nElement, const OUString& _rValue)
    {
        static const sal_Int32 nListSourceAttributeName = OAttributeMetaData::getDatabaseAttributeToken(DAFlags::ListSource);
        if ( (nElement & TOKEN_MASK) == nListSourceAttributeName )
        {
            PropertyValue aListSource;
            aListSource.Name = PROPERTY_LISTSOURCE;

            // it's the ListSource attribute
            m_bEncounteredLSAttrib = true;
            if ( OControlElement::COMBOBOX == m_eElementType )
            {
                aListSource.Value <<= _rValue;
            }
            else
            {
                // a listbox which has a list-source attribute must have a list-source-type of something
                // not equal to ValueList.
                // In this case, the list-source value is simply the one and only element of the ListSource property.
                Sequence<OUString> aListSourcePropValue { _rValue };
                aListSource.Value <<= aListSourcePropValue;
            }

            implPushBackPropertyValue( aListSource );
            return true;
        }

        if ( (nElement & TOKEN_MASK) == OAttributeMetaData::getBindingAttributeToken( BAFlags::ListCellRange ) )
        {
            m_sCellListSource = _rValue;
            return true;
        }

        if ( (nElement & TOKEN_MASK) == OAttributeMetaData::getBindingAttributeToken( BAFlags::ListLinkingType ) )
        {
            sal_Int16 nLinkageType = 0;
            PropertyConversion::convertString(
                ::cppu::UnoType<sal_Int16>::get(),
                _rValue,
                aListLinkageMap
            ) >>= nLinkageType;

            m_bLinkWithIndexes = ( nLinkageType != 0 );
            return true;
        }

        return OControlImport::handleAttribute(nElement, _rValue);
    }

    void OListAndComboImport::implPushBackLabel(const OUString& _rLabel)
    {
        OSL_ENSURE(!m_nEmptyListItems, "OListAndComboImport::implPushBackValue: label list is already done!");
        if (!m_nEmptyListItems)
            m_aListSource.push_back(_rLabel);
    }

    void OListAndComboImport::implPushBackValue(const OUString& _rValue)
    {
        OSL_ENSURE(!m_nEmptyValueItems, "OListAndComboImport::implPushBackValue: value list is already done!");
        if (!m_nEmptyValueItems)
        {
            OSL_ENSURE( !m_bEncounteredLSAttrib, "OListAndComboImport::implPushBackValue: invalid structure! Did you save this document with a version prior SRC641 m?" );
                // We already had the list-source attribute, which means that the ListSourceType is
                // not ValueList, which means that the ListSource should contain only one string in
                // the first element of the sequence
                // All other values in the file are invalid

            m_aValueList.push_back( _rValue );
        }
    }

    void OListAndComboImport::implEmptyLabelFound()
    {
        ++m_nEmptyListItems;
    }

    void OListAndComboImport::implEmptyValueFound()
    {
        ++m_nEmptyValueItems;
    }

    void OListAndComboImport::implSelectCurrentItem()
    {
        OSL_ENSURE((m_aListSource.size() + m_nEmptyListItems) == (m_aValueList.size() + m_nEmptyValueItems),
            "OListAndComboImport::implSelectCurrentItem: inconsistence between labels and values!");

        sal_Int16 nItemNumber = static_cast<sal_Int16>(m_aListSource.size() - 1 + m_nEmptyListItems);
        m_aSelectedSeq.push_back(nItemNumber);
    }

    void OListAndComboImport::implDefaultSelectCurrentItem()
    {
        OSL_ENSURE((m_aListSource.size() + m_nEmptyListItems) == (m_aValueList.size() + m_nEmptyValueItems),
            "OListAndComboImport::implDefaultSelectCurrentItem: inconsistence between labels and values!");

        sal_Int16 nItemNumber = static_cast<sal_Int16>(m_aListSource.size() - 1 + m_nEmptyListItems);
        m_aDefaultSelectedSeq.push_back(nItemNumber);
    }

    //= OListOptionImport
    OListOptionImport::OListOptionImport(SvXMLImport& _rImport,
            const OListAndComboImportRef& _rListBox)
        :SvXMLImportContext(_rImport)
        ,m_xListBoxImport(_rListBox)
    {
    }

    void OListOptionImport::startFastElement(sal_Int32 nElement, const Reference< XFastAttributeList >& _rxAttrList)
    {
        // the label and the value
        const sal_Int32 nLabelAttribute = (nElement & ~TOKEN_MASK) | XML_LABEL;
        const sal_Int32 nValueAttribute = (nElement & ~TOKEN_MASK) | XML_VALUE;

        // the label attribute
        OUString sValue = _rxAttrList->getOptionalValue(nLabelAttribute);
        bool bNonexistentAttribute = !_rxAttrList->hasAttribute(nLabelAttribute);

        if (bNonexistentAttribute)
            m_xListBoxImport->implEmptyLabelFound();
        else
            m_xListBoxImport->implPushBackLabel( sValue );

        // the value attribute
        sValue = _rxAttrList->getOptionalValue(nValueAttribute);
        bNonexistentAttribute = !_rxAttrList->hasAttribute(nValueAttribute);

        if (bNonexistentAttribute)
            m_xListBoxImport->implEmptyValueFound();
        else
            m_xListBoxImport->implPushBackValue( sValue );

        // the current-selected and selected
        const sal_Int32 nSelectedAttribute = (nElement & ~TOKEN_MASK) | OAttributeMetaData::getCommonControlAttributeToken(CCAFlags::CurrentSelected);
        const sal_Int32 nDefaultSelectedAttribute = (nElement & ~TOKEN_MASK) | OAttributeMetaData::getCommonControlAttributeToken(CCAFlags::Selected);

        // propagate the selected flag
        bool bSelected(false);
        (void)::sax::Converter::convertBool(bSelected,
            _rxAttrList->getOptionalValue(nSelectedAttribute));
        if (bSelected)
            m_xListBoxImport->implSelectCurrentItem();

        // same for the default selected
        bool bDefaultSelected(false);
        (void)::sax::Converter::convertBool(bDefaultSelected,
            _rxAttrList->getOptionalValue(nDefaultSelectedAttribute));
        if (bDefaultSelected)
            m_xListBoxImport->implDefaultSelectCurrentItem();
    }

    //= OComboItemImport
    OComboItemImport::OComboItemImport(SvXMLImport& _rImport,
            const OListAndComboImportRef& _rListBox)
        :SvXMLImportContext(_rImport)
        ,m_xListBoxImport(_rListBox)
    {
    }

    void OComboItemImport::startFastElement(sal_Int32 nElement, const Reference< XFastAttributeList >& _rxAttrList)
    {
        const sal_Int32 nLabelAttributeName = (nElement & ~TOKEN_MASK) |
            OAttributeMetaData::getCommonControlAttributeToken(CCAFlags::Label);
        m_xListBoxImport->implPushBackLabel(_rxAttrList->getOptionalValue(nLabelAttributeName));
    }

    //= OColumnWrapperImport
    OColumnWrapperImport::OColumnWrapperImport(OFormLayerXMLImport_Impl& _rImport,
            IEventAttacherManager& _rEventManager, sal_Int32 /*nElement*/,
            const Reference< XNameContainer >& _rxParentContainer)
        :SvXMLImportContext(_rImport.getGlobalContext())
        ,m_xParentContainer(_rxParentContainer)
        ,m_rFormImport(_rImport)
        ,m_rEventManager(_rEventManager)
    {
    }
    css::uno::Reference< css::xml::sax::XFastContextHandler > OColumnWrapperImport::createFastChildContext(
        sal_Int32 nElement,
        const css::uno::Reference< css::xml::sax::XFastAttributeList >&  )
    {
        OControlImport* pReturn = implCreateChildContext(nElement, OElementNameMap::getElementType(nElement & TOKEN_MASK));
        if (pReturn)
        {
            OSL_ENSURE(m_xOwnAttributes.is(), "OColumnWrapperImport::CreateChildContext: had no form:column element!");
            pReturn->addOuterAttributes(m_xOwnAttributes);
        }
        return pReturn;
    }
    void OColumnWrapperImport::startFastElement(sal_Int32 /*nElement*/, const Reference< XFastAttributeList >& _rxAttrList)
    {
        OSL_ENSURE(!m_xOwnAttributes.is(), "OColumnWrapperImport::StartElement: already have the cloned list!");

        // clone the attributes
        Reference< XCloneable > xCloneList(_rxAttrList, UNO_QUERY_THROW);
        m_xOwnAttributes.set(xCloneList->createClone(), UNO_QUERY_THROW);
    }

    OControlImport* OColumnWrapperImport::implCreateChildContext(
            sal_Int32 /*nElement*/,
            OControlElement::ElementType _eType)
    {
        OSL_ENSURE( (OControlElement::TEXT == _eType)
                ||  (OControlElement::TEXT_AREA == _eType)
                ||  (OControlElement::FORMATTED_TEXT == _eType)
                ||  (OControlElement::CHECKBOX == _eType)
                ||  (OControlElement::LISTBOX == _eType)
                ||  (OControlElement::COMBOBOX == _eType)
                ||  (OControlElement::TIME == _eType)
                ||  (OControlElement::DATE == _eType),
                "OColumnWrapperImport::implCreateChildContext: invalid or unrecognized sub element!");

        switch (_eType)
        {
            case OControlElement::COMBOBOX:
            case OControlElement::LISTBOX:
                return new OColumnImport<OListAndComboImport>(m_rFormImport, m_rEventManager, m_xParentContainer, _eType );

            case OControlElement::PASSWORD:
                return new OColumnImport<OPasswordImport>(m_rFormImport, m_rEventManager, m_xParentContainer, _eType );

            case OControlElement::TEXT:
            case OControlElement::TEXT_AREA:
            case OControlElement::FORMATTED_TEXT:
                return new OColumnImport< OTextLikeImport >( m_rFormImport, m_rEventManager, m_xParentContainer, _eType );

            default:
                return new OColumnImport<OControlImport>(m_rFormImport, m_rEventManager, m_xParentContainer, _eType );
        }
    }

    //= OGridImport
    OGridImport::OGridImport(OFormLayerXMLImport_Impl& _rImport, IEventAttacherManager& _rEventManager,
            const Reference< XNameContainer >& _rxParentContainer,
            OControlElement::ElementType _eType)
        :OControlImport(_rImport, _rEventManager, _rxParentContainer)
    {
        setElementType(_eType);
    }

    css::uno::Reference< css::xml::sax::XFastContextHandler > OGridImport::createFastChildContext(
        sal_Int32 nElement,
        const css::uno::Reference< css::xml::sax::XFastAttributeList >& xAttrList )
    {
        // maybe it's a sub control
        if ((nElement & TOKEN_MASK) == XML_COLUMN)
        {
            if (m_xMeAsContainer.is())
                return new OColumnWrapperImport(m_rFormImport, *this, nElement, m_xMeAsContainer);
            else
            {
                OSL_FAIL("OGridImport::CreateChildContext: don't have an element!");
                return nullptr;
            }
        }

        return OControlImport::createFastChildContext(nElement, xAttrList);
    }

    void OGridImport::endFastElement(sal_Int32 nElement)
    {
        OControlImport::endFastElement(nElement);

        // now that we have all children, attach the events
        css::uno::Reference< css::container::XIndexAccess > xIndexContainer(m_xMeAsContainer, css::uno::UNO_QUERY);
        if (xIndexContainer.is())
            ODefaultEventAttacherManager::setEvents(xIndexContainer);
    }

    css::uno::Reference< css::beans::XPropertySet > OGridImport::createElement()
    {
        // let the base class create the object
        css::uno::Reference< css::beans::XPropertySet > xReturn = OControlImport::createElement();
        if (!xReturn.is())
            return xReturn;

        // ensure that the object is a XNameContainer (we strongly need this for inserting child elements)
        m_xMeAsContainer.set(xReturn, css::uno::UNO_QUERY);
        if (!m_xMeAsContainer.is())
        {
            OSL_FAIL("OContainerImport::createElement: invalid element (no XNameContainer) created!");
            xReturn.clear();
        }

        return xReturn;
    }

    //= OFormImport
    OFormImport::OFormImport(OFormLayerXMLImport_Impl& _rImport, IEventAttacherManager& _rEventManager,
            const Reference< XNameContainer >& _rxParentContainer)
        :OElementImport(_rImport, _rEventManager, _rxParentContainer)
    {
        enableTrackAttributes();
    }

    css::uno::Reference< css::xml::sax::XFastContextHandler > OFormImport::createFastChildContext(
        sal_Int32 nElement,
        const uno::Reference< xml::sax::XFastAttributeList>& _rxAttrList )
    {
        auto nToken = (nElement & TOKEN_MASK);
        if( nToken == XML_FORM )
            return new OFormImport( m_rFormImport, *this, m_xMeAsContainer);
        else if ( nToken == XML_CONNECTION_RESOURCE )
            return new OXMLDataSourceImport(GetImport(), _rxAttrList, m_xElement);
        else if( nElement == XML_ELEMENT(OFFICE, XML_EVENT_LISTENERS) ||
                 nToken == XML_PROPERTIES )
            return OElementImport::createFastChildContext( nElement, _rxAttrList );
        else
        {
            OControlElement::ElementType eType = OElementNameMap::getElementType(nToken);
            switch (eType)
            {
                case OControlElement::TEXT:
                case OControlElement::TEXT_AREA:
                case OControlElement::FORMATTED_TEXT:
                    return new OTextLikeImport(m_rFormImport, *this, m_xMeAsContainer, eType);
                case OControlElement::GRID:
                    return new OGridImport(m_rFormImport, *this, m_xMeAsContainer, eType);
                case OControlElement::COMBOBOX:
                case OControlElement::LISTBOX:
                    return new OListAndComboImport(m_rFormImport, *this, m_xMeAsContainer, eType);
                case OControlElement::PASSWORD:
                    return new OPasswordImport(m_rFormImport, *this, m_xMeAsContainer, eType);
                case OControlElement::BUTTON:
                case OControlElement::IMAGE:
                case OControlElement::IMAGE_FRAME:
                    return new OButtonImport( m_rFormImport, *this, m_xMeAsContainer, eType );
                case OControlElement::RADIO:
                    return new ORadioImport(m_rFormImport, *this, m_xMeAsContainer, eType);
                case OControlElement::CHECKBOX:
                    return new OImagePositionImport(m_rFormImport, *this, m_xMeAsContainer, eType);
                case OControlElement::FRAME:
                case OControlElement::FIXED_TEXT:
                    return new OReferredControlImport(m_rFormImport, *this, m_xMeAsContainer);
                case OControlElement::VALUERANGE:
                    return new OValueRangeImport( m_rFormImport, *this, m_xMeAsContainer, eType );
                default:
                    return new OControlImport(m_rFormImport, *this, m_xMeAsContainer, eType);
            }
        }
    }

    void OFormImport::startFastElement(sal_Int32 nElement, const Reference< XFastAttributeList >& _rxAttrList)
    {
        m_rFormImport.enterEventContext();
        OElementImport::startFastElement(nElement, _rxAttrList);

        // handle the target-frame attribute
        simulateDefaultedAttribute(OAttributeMetaData::getCommonControlAttributeToken(CCAFlags::TargetFrame), PROPERTY_TARGETFRAME, "_blank");
    }

    void OFormImport::endFastElement(sal_Int32 nElement)
    {
        OElementImport::endFastElement(nElement);

        // now that we have all children, attach the events
        css::uno::Reference< css::container::XIndexAccess > xIndexContainer(m_xMeAsContainer, css::uno::UNO_QUERY);
        if (xIndexContainer.is())
            ODefaultEventAttacherManager::setEvents(xIndexContainer);

        m_rFormImport.leaveEventContext();
    }

    css::uno::Reference< css::beans::XPropertySet > OFormImport::createElement()
    {
        // let the base class create the object
        css::uno::Reference< css::beans::XPropertySet > xReturn = OElementImport::createElement();
        if (!xReturn.is())
            return xReturn;

        // ensure that the object is a XNameContainer (we strongly need this for inserting child elements)
        m_xMeAsContainer.set(xReturn, css::uno::UNO_QUERY);
        if (!m_xMeAsContainer.is())
        {
            OSL_FAIL("OContainerImport::createElement: invalid element (no XNameContainer) created!");
            xReturn.clear();
        }

        return xReturn;
    }

    bool OFormImport::handleAttribute(sal_Int32 nElement, const OUString& _rValue)
    {
        // handle the master/details field attributes (they're way too special to let the OPropertyImport handle them)
        static const sal_Int32 s_nMasterFieldsAttributeName = OAttributeMetaData::getFormAttributeToken(faMasterFields);
        static const sal_Int32 s_nDetailFieldsAttributeName = OAttributeMetaData::getFormAttributeToken(faDetailFields);

        if ( (nElement & TOKEN_MASK) == s_nMasterFieldsAttributeName)
        {
            implTranslateStringListProperty(PROPERTY_MASTERFIELDS, _rValue);
            return true;
        }

        if ( (nElement & TOKEN_MASK) == s_nDetailFieldsAttributeName)
        {
            implTranslateStringListProperty(PROPERTY_DETAILFIELDS, _rValue);
            return true;
        }

        return OElementImport::handleAttribute(nElement, _rValue);
    }

    void OFormImport::implTranslateStringListProperty(const OUString& _rPropertyName, const OUString& _rValue)
    {
        PropertyValue aProp;
        aProp.Name = _rPropertyName;

        Sequence< OUString > aList;

        // split up the value string
        if (!_rValue.isEmpty())
        {
            // For the moment, we build a vector instead of a Sequence. It's easier to handle because of its
            // push_back method
            ::std::vector< OUString > aElements;
            // estimate the number of tokens
            sal_Int32 nEstimate = 0, nLength = _rValue.getLength();
            const sal_Unicode* pChars = _rValue.getStr();
            for (sal_Int32 i=0; i<nLength; ++i, ++pChars)
                if (*pChars == ',')
                    ++nEstimate;
            aElements.reserve(nEstimate + 1);
                // that's the worst case. If the string contains the separator character _quoted_, we reserved too much...

            sal_Int32 nElementStart = 0;
            sal_Int32 nNextSep = 0;
            do
            {
                // extract the current element
                nNextSep = ::sax::Converter::indexOfComma(
                    _rValue, nElementStart);
                if (-1 == nNextSep)
                    nNextSep = nLength;
                std::u16string_view sElement = _rValue.subView(nElementStart, nNextSep - nElementStart);

                size_t nElementLength = sElement.size();
                // when writing the sequence, we quoted the single elements with " characters
                OSL_ENSURE( o3tl::starts_with(sElement, u"\"") && o3tl::ends_with(sElement, u"\""),
                        "OFormImport::implTranslateStringListProperty: invalid quoted element name.");
                sElement = sElement.substr(1, nElementLength - 2);

                aElements.push_back(OUString(sElement));

                // switch to the next element
                nElementStart = 1 + nNextSep;
            }
            while (nElementStart < nLength);

            aList = Sequence< OUString >(aElements.data(), aElements.size());
        }
        else
        {
            OSL_FAIL("OFormImport::implTranslateStringListProperty: invalid value (empty)!");
        }

        aProp.Value <<= aList;

        // add the property to the base class' array
        implPushBackPropertyValue(aProp);
    }
    //= OXMLDataSourceImport
    OXMLDataSourceImport::OXMLDataSourceImport(
                    SvXMLImport& _rImport
                    ,const Reference< css::xml::sax::XFastAttributeList > & _xAttrList
                    ,const css::uno::Reference< css::beans::XPropertySet >& _xElement) :
        SvXMLImportContext( _rImport)
    {
        for( auto& aIter : sax_fastparser::castToFastAttributeList(_xAttrList) )
        {
            if  (  aIter.getToken() ==
                    XML_ELEMENT(XLINK, OAttributeMetaData::getCommonControlAttributeToken( CCAFlags::TargetLocation ) ) )
            {
                OUString sValue = aIter.toString();
                sValue = _rImport.GetAbsoluteReference(sValue);
                INetURLObject aURL(sValue);
                if ( aURL.GetProtocol() == INetProtocol::File )
                    _xElement->setPropertyValue(PROPERTY_DATASOURCENAME,Any(sValue));
                else
                    _xElement->setPropertyValue(PROPERTY_URL,Any(sValue)); // the url is the "sdbc:" string
                break;
            }
            else
                SAL_WARN("xmloff", "unknown attribute " << SvXMLImport::getPrefixAndNameFromToken(aIter.getToken()) << "=" << aIter.toString());
        }
    }

    OUString OFormImport::determineDefaultServiceName() const
    {
        return "com.sun.star.form.component.Form";
    }

}   // namespace xmloff

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