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
|
/* -*- 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 <rtl/ustrbuf.hxx>
#include <sal/log.hxx>
#include <osl/diagnose.h>
#include <tools/diagnose_ex.h>
#include <xmloff/namespacemap.hxx>
#include <fmtcol.hxx>
#include <hints.hxx>
#include <doc.hxx>
#include <docary.hxx>
#include <IDocumentStylePoolAccess.hxx>
#include <unoprnms.hxx>
#include <fmtpdsc.hxx>
#include <pagedesc.hxx>
#include <xmloff/maptype.hxx>
#include <xmloff/xmlnumfi.hxx>
#include <xmloff/xmlprmap.hxx>
#include <xmloff/xmlnamespace.hxx>
#include <xmloff/xmlstyle.hxx>
#include <xmloff/txtstyli.hxx>
#include <xmloff/txtimp.hxx>
#include <xmloff/families.hxx>
#include <xmloff/XMLTextMasterStylesContext.hxx>
#include <xmloff/XMLTextShapeStyleContext.hxx>
#include <xmloff/XMLGraphicsDefaultStyle.hxx>
#include <xmloff/XMLDrawingPageStyleContext.hxx>
#include <xmloff/XMLTextMasterPageContext.hxx>
#include <xmloff/table/XMLTableImport.hxx>
#include <com/sun/star/beans/XPropertySet.hpp>
#include <com/sun/star/frame/XModel.hpp>
#include <com/sun/star/lang/XMultiServiceFactory.hpp>
#include "xmlimp.hxx"
#include <cellatr.hxx>
#include <SwStyleNameMapper.hxx>
#include <xmloff/attrlist.hxx>
#include <ccoll.hxx>
#include <memory>
using namespace ::com::sun::star;
using namespace ::com::sun::star::beans;
using namespace ::com::sun::star::uno;
using namespace ::xmloff::token;
namespace {
class SwXMLConditionParser_Impl
{
OUString sInput;
Master_CollCondition nCondition;
sal_uInt32 nSubCondition;
sal_Int32 nPos;
sal_Int32 nLength;
inline bool SkipWS();
inline bool MatchChar( sal_Unicode c );
inline bool MatchName( OUString& rName );
inline bool MatchNumber( sal_uInt32& rNumber );
public:
explicit SwXMLConditionParser_Impl( const OUString& rInp );
bool IsValid() const { return Master_CollCondition::NONE != nCondition; }
Master_CollCondition GetCondition() const { return nCondition; }
sal_uInt32 GetSubCondition() const { return nSubCondition; }
};
}
inline bool SwXMLConditionParser_Impl::SkipWS()
{
while( nPos < nLength && ' ' == sInput[nPos] )
nPos++;
return true;
}
inline bool SwXMLConditionParser_Impl::MatchChar( sal_Unicode c )
{
bool bRet = false;
if( nPos < nLength && c == sInput[nPos] )
{
nPos++;
bRet = true;
}
return bRet;
}
inline bool SwXMLConditionParser_Impl::MatchName( OUString& rName )
{
OUStringBuffer sBuffer( nLength );
while( nPos < nLength &&
( ('a' <= sInput[nPos] && sInput[nPos] <= 'z') ||
'-' == sInput[nPos] ) )
{
sBuffer.append( sInput[nPos] );
nPos++;
}
rName = sBuffer.makeStringAndClear();
return !rName.isEmpty();
}
inline bool SwXMLConditionParser_Impl::MatchNumber( sal_uInt32& rNumber )
{
OUStringBuffer sBuffer( nLength );
while( nPos < nLength && '0' <= sInput[nPos] && sInput[nPos] <= '9' )
{
sBuffer.append( sInput[nPos] );
nPos++;
}
OUString sNum( sBuffer.makeStringAndClear() );
if( !sNum.isEmpty() )
rNumber = sNum.toInt32();
return !sNum.isEmpty();
}
SwXMLConditionParser_Impl::SwXMLConditionParser_Impl( const OUString& rInp ) :
sInput( rInp ),
nCondition( Master_CollCondition::NONE ),
nSubCondition( 0 ),
nPos( 0 ),
nLength( rInp.getLength() )
{
OUString sFunc;
bool bHasSub = false;
sal_uInt32 nSub = 0;
bool bOK = SkipWS() && MatchName( sFunc ) && SkipWS() &&
MatchChar( '(' ) && SkipWS() && MatchChar( ')' ) && SkipWS();
if( bOK && MatchChar( '=' ) )
{
bOK = SkipWS() && MatchNumber( nSub ) && SkipWS();
bHasSub = true;
}
bOK &= nPos == nLength;
if( !bOK )
return;
if( IsXMLToken( sFunc, XML_ENDNOTE ) && !bHasSub )
nCondition = Master_CollCondition::PARA_IN_ENDNOTE;
else if( IsXMLToken( sFunc, XML_FOOTER ) && !bHasSub )
nCondition = Master_CollCondition::PARA_IN_FOOTER;
else if( IsXMLToken( sFunc, XML_FOOTNOTE ) && !bHasSub )
nCondition = Master_CollCondition::PARA_IN_FOOTNOTE;
else if( IsXMLToken( sFunc, XML_HEADER ) && !bHasSub )
nCondition = Master_CollCondition::PARA_IN_HEADER;
else if( IsXMLToken( sFunc, XML_LIST_LEVEL) &&
nSub >=1 && nSub <= MAXLEVEL )
{
nCondition = Master_CollCondition::PARA_IN_LIST;
nSubCondition = nSub-1;
}
else if( IsXMLToken( sFunc, XML_OUTLINE_LEVEL) &&
nSub >=1 && nSub <= MAXLEVEL )
{
nCondition = Master_CollCondition::PARA_IN_OUTLINE;
nSubCondition = nSub-1;
}
else if( IsXMLToken( sFunc, XML_SECTION ) && !bHasSub )
{
nCondition = Master_CollCondition::PARA_IN_SECTION;
}
else if( IsXMLToken( sFunc, XML_TABLE ) && !bHasSub )
{
nCondition = Master_CollCondition::PARA_IN_TABLEBODY;
}
else if( IsXMLToken( sFunc, XML_TABLE_HEADER ) && !bHasSub )
{
nCondition = Master_CollCondition::PARA_IN_TABLEHEAD;
}
else if( IsXMLToken( sFunc, XML_TEXT_BOX ) && !bHasSub )
{
nCondition = Master_CollCondition::PARA_IN_FRAME;
}
}
namespace {
class SwXMLConditionContext_Impl : public SvXMLImportContext
{
Master_CollCondition nCondition;
sal_uInt32 nSubCondition;
OUString sApplyStyle;
public:
SwXMLConditionContext_Impl(
SvXMLImport& rImport, sal_uInt16 nPrfx,
const OUString& rLName,
const uno::Reference< xml::sax::XAttributeList > & xAttrList );
bool IsValid() const { return Master_CollCondition::NONE != nCondition; }
Master_CollCondition getCondition() const { return nCondition; }
sal_uInt32 getSubCondition() const { return nSubCondition; }
OUString const &getApplyStyle() const { return sApplyStyle; }
};
}
SwXMLConditionContext_Impl::SwXMLConditionContext_Impl(
SvXMLImport& rImport, sal_uInt16 nPrfx,
const OUString& rLName,
const uno::Reference< xml::sax::XAttributeList > & xAttrList ) :
SvXMLImportContext( rImport, nPrfx, rLName ),
nCondition( Master_CollCondition::NONE ),
nSubCondition( 0 )
{
sal_Int16 nAttrCount = xAttrList.is() ? xAttrList->getLength() : 0;
for( sal_Int16 i=0; i < nAttrCount; i++ )
{
const OUString& rAttrName = xAttrList->getNameByIndex( i );
OUString aLocalName;
const sal_uInt16 nPrefix =
GetImport().GetNamespaceMap().GetKeyByAttrName( rAttrName,
&aLocalName );
const OUString& rValue = xAttrList->getValueByIndex( i );
// TODO: use a map here
if( XML_NAMESPACE_STYLE == nPrefix )
{
if( IsXMLToken( aLocalName, XML_CONDITION ) )
{
SwXMLConditionParser_Impl aCondParser( rValue );
if( aCondParser.IsValid() )
{
nCondition = aCondParser.GetCondition();
nSubCondition = aCondParser.GetSubCondition();
}
}
else if( IsXMLToken( aLocalName, XML_APPLY_STYLE_NAME ) )
{
sApplyStyle = rValue;
}
}
}
}
typedef std::vector<rtl::Reference<SwXMLConditionContext_Impl>> SwXMLConditions_Impl;
namespace {
class SwXMLTextStyleContext_Impl : public XMLTextStyleContext
{
std::unique_ptr<SwXMLConditions_Impl> pConditions;
uno::Reference < style::XStyle > xNewStyle;
protected:
virtual uno::Reference < style::XStyle > Create() override;
virtual void Finish( bool bOverwrite ) override;
public:
SwXMLTextStyleContext_Impl( SwXMLImport& rImport, sal_uInt16 nPrfx,
const OUString& rLName,
const uno::Reference< xml::sax::XAttributeList > & xAttrList,
XmlStyleFamily nFamily,
SvXMLStylesContext& rStyles );
virtual SvXMLImportContextRef CreateChildContext(
sal_uInt16 nPrefix,
const OUString& rLocalName,
const uno::Reference< xml::sax::XAttributeList > & xAttrList ) override;
};
}
uno::Reference < style::XStyle > SwXMLTextStyleContext_Impl::Create()
{
if( pConditions && XmlStyleFamily::TEXT_PARAGRAPH == GetFamily() )
{
uno::Reference< lang::XMultiServiceFactory > xFactory( GetImport().GetModel(),
uno::UNO_QUERY );
if( xFactory.is() )
{
uno::Reference < uno::XInterface > xIfc =
xFactory->createInstance( "com.sun.star.style.ConditionalParagraphStyle" );
if( xIfc.is() )
xNewStyle.set( xIfc, uno::UNO_QUERY );
}
}
else
{
xNewStyle = XMLTextStyleContext::Create();
}
return xNewStyle;
}
void
SwXMLTextStyleContext_Impl::Finish( bool bOverwrite )
{
if( pConditions && XmlStyleFamily::TEXT_PARAGRAPH == GetFamily() && xNewStyle.is() )
{
CommandStruct const*const pCommands = SwCondCollItem::GetCmds();
Reference< XPropertySet > xPropSet( xNewStyle, UNO_QUERY );
uno::Sequence< beans::NamedValue > aSeq( pConditions->size() );
for (std::vector<rtl::Reference<SwXMLConditionContext_Impl>>::size_type i = 0;
i < pConditions->size(); ++i)
{
assert((*pConditions)[i]->IsValid()); // checked before inserting
Master_CollCondition nCond = (*pConditions)[i]->getCondition();
sal_uInt32 nSubCond = (*pConditions)[i]->getSubCondition();
for (size_t j = 0; j < COND_COMMAND_COUNT; ++j)
{
if (pCommands[j].nCnd == nCond &&
pCommands[j].nSubCond == nSubCond)
{
aSeq[i].Name = GetCommandContextByIndex( j );
aSeq[i].Value <<= GetImport().GetStyleDisplayName(
GetFamily(), (*pConditions)[i]->getApplyStyle() );
break;
}
}
}
try
{
xPropSet->setPropertyValue(UNO_NAME_PARA_STYLE_CONDITIONS, uno::makeAny(aSeq));
}
catch (uno::Exception const&)
{
TOOLS_WARN_EXCEPTION("sw.xml", "exception when setting ParaStyleConditions");
}
}
XMLTextStyleContext::Finish( bOverwrite );
}
SwXMLTextStyleContext_Impl::SwXMLTextStyleContext_Impl( SwXMLImport& rImport,
sal_uInt16 nPrfx, const OUString& rLName,
const uno::Reference< xml::sax::XAttributeList > & xAttrList,
XmlStyleFamily nFamily,
SvXMLStylesContext& rStyles ) :
XMLTextStyleContext( rImport, nPrfx, rLName, xAttrList, rStyles, nFamily )
{
}
SvXMLImportContextRef SwXMLTextStyleContext_Impl::CreateChildContext(
sal_uInt16 nPrefix,
const OUString& rLocalName,
const uno::Reference< xml::sax::XAttributeList > & xAttrList )
{
SvXMLImportContextRef xContext;
if( XML_NAMESPACE_STYLE == nPrefix && IsXMLToken( rLocalName, XML_MAP ) )
{
rtl::Reference<SwXMLConditionContext_Impl> xCond{
new SwXMLConditionContext_Impl( GetImport(), nPrefix,
rLocalName, xAttrList )};
if( xCond->IsValid() )
{
if( !pConditions )
pConditions = std::make_unique<SwXMLConditions_Impl>();
pConditions->push_back( xCond );
}
xContext = xCond.get();
}
if (!xContext)
xContext = XMLTextStyleContext::CreateChildContext( nPrefix, rLocalName,
xAttrList );
return xContext;
}
namespace {
class SwXMLCellStyleContext : public XMLPropStyleContext
{
OUString m_sDataStyleName;
void AddDataFormat();
public:
using XMLPropStyleContext::XMLPropStyleContext;
virtual void FillPropertySet(const css::uno::Reference<css::beans::XPropertySet>& rPropSet) override;
virtual void SetAttribute(sal_uInt16 nPrefixKey, const OUString& rLocalName, const OUString& rValue) override;
};
class SwXMLItemSetStyleContext_Impl : public SvXMLStyleContext
{
OUString sMasterPageName;
std::unique_ptr<SfxItemSet> pItemSet;
SwXMLTextStyleContext_Impl *pTextStyle;
SvXMLStylesContext &rStyles;
OUString sDataStyleName;
bool bHasMasterPageName : 1;
bool bPageDescConnected : 1;
bool bDataStyleIsResolved;
SvXMLImportContext *CreateItemSetContext(
sal_uInt16 nPrefix,
const OUString& rLName,
const uno::Reference< xml::sax::XAttributeList > & xAttrList);
protected:
virtual void SetAttribute( sal_uInt16 nPrefixKey,
const OUString& rLocalName,
const OUString& rValue ) override;
SwXMLImport& GetSwImport() { return static_cast<SwXMLImport&>(GetImport()); }
public:
SwXMLItemSetStyleContext_Impl(
SwXMLImport& rImport, sal_uInt16 nPrfx,
const OUString& rLName,
const uno::Reference< xml::sax::XAttributeList > & xAttrList,
SvXMLStylesContext& rStylesC,
XmlStyleFamily nFamily);
virtual void CreateAndInsert( bool bOverwrite ) override;
virtual SvXMLImportContextRef CreateChildContext(
sal_uInt16 nPrefix,
const OUString& rLocalName,
const uno::Reference< xml::sax::XAttributeList > & xAttrList ) override;
// The item set may be empty!
SfxItemSet *GetItemSet() { return pItemSet.get(); }
bool HasMasterPageName() const { return bHasMasterPageName; }
bool IsPageDescConnected() const { return bPageDescConnected; }
void ConnectPageDesc();
bool ResolveDataStyleName();
};
}
void SwXMLCellStyleContext::AddDataFormat()
{
if (m_sDataStyleName.isEmpty() || IsDefaultStyle())
return;
const SvXMLNumFormatContext* pStyle = static_cast<const SvXMLNumFormatContext*>(
GetStyles()->FindStyleChildContext(XmlStyleFamily::DATA_STYLE, m_sDataStyleName, true));
if (!pStyle)
{
SAL_WARN("sw.xml", "not possible to get data style " << m_sDataStyleName);
return;
}
sal_Int32 nNumberFormat = const_cast<SvXMLNumFormatContext*>(pStyle)->GetKey();
if (nNumberFormat < 0)
return;
rtl::Reference<SvXMLImportPropertyMapper> xPropertyMapper(GetStyles()->GetImportPropertyMapper(GetFamily()));
if (!xPropertyMapper.is())
{
SAL_WARN("sw.xml", "there is no import prop mapper");
return;
}
const rtl::Reference<XMLPropertySetMapper>& xPropertySetMapper(xPropertyMapper->getPropertySetMapper());
sal_Int32 nIndex = xPropertySetMapper->GetEntryIndex(XML_NAMESPACE_STYLE, GetXMLToken(XML_DATA_STYLE_NAME), 0);
if (nIndex < 0)
{
SAL_WARN("sw.xml", "could not find id for " << GetXMLToken(XML_DATA_STYLE_NAME));
return;
}
auto aIter = std::find_if(GetProperties().begin(), GetProperties().end(),
[&nIndex](const XMLPropertyState& rProp) {
return rProp.mnIndex == nIndex;
});
if (aIter != GetProperties().end())
aIter->maValue <<= nNumberFormat;
else
GetProperties().push_back(XMLPropertyState(nIndex, makeAny(nNumberFormat)));
}
void SwXMLCellStyleContext::FillPropertySet(const css::uno::Reference<css::beans::XPropertySet>& rPropSet)
{
AddDataFormat();
XMLPropStyleContext::FillPropertySet(rPropSet);
}
void SwXMLCellStyleContext::SetAttribute(sal_uInt16 nPrefixKey, const OUString& rLocalName, const OUString& rValue)
{
if (IsXMLToken(rLocalName, XML_DATA_STYLE_NAME))
m_sDataStyleName = rValue;
else
XMLPropStyleContext::SetAttribute(nPrefixKey, rLocalName, rValue);
}
void SwXMLItemSetStyleContext_Impl::SetAttribute( sal_uInt16 nPrefixKey,
const OUString& rLocalName,
const OUString& rValue )
{
if( XML_NAMESPACE_STYLE == nPrefixKey )
{
if ( IsXMLToken( rLocalName, XML_MASTER_PAGE_NAME ) )
{
sMasterPageName = rValue;
bHasMasterPageName = true;
}
else if ( IsXMLToken( rLocalName, XML_DATA_STYLE_NAME ) )
{
// if we have a valid data style name
if (!rValue.isEmpty())
{
sDataStyleName = rValue;
bDataStyleIsResolved = false; // needs to be resolved
}
}
else
{
SvXMLStyleContext::SetAttribute( nPrefixKey, rLocalName, rValue );
}
}
else
{
SvXMLStyleContext::SetAttribute( nPrefixKey, rLocalName, rValue );
}
}
SvXMLImportContext *SwXMLItemSetStyleContext_Impl::CreateItemSetContext(
sal_uInt16 nPrefix, const OUString& rLName,
const uno::Reference< xml::sax::XAttributeList > & xAttrList )
{
OSL_ENSURE( !pItemSet,
"SwXMLItemSetStyleContext_Impl::CreateItemSetContext: item set exists" );
SvXMLImportContext *pContext = nullptr;
SwDoc* pDoc = SwImport::GetDocFromXMLImport( GetSwImport() );
SfxItemPool& rItemPool = pDoc->GetAttrPool();
switch( GetFamily() )
{
case XmlStyleFamily::TABLE_TABLE:
pItemSet.reset( new SfxItemSet( rItemPool, aTableSetRange ) );
break;
case XmlStyleFamily::TABLE_COLUMN:
pItemSet.reset( new SfxItemSet( rItemPool, svl::Items<RES_FRM_SIZE, RES_FRM_SIZE>{} ) );
break;
case XmlStyleFamily::TABLE_ROW:
pItemSet.reset( new SfxItemSet( rItemPool, aTableLineSetRange ) );
break;
case XmlStyleFamily::TABLE_CELL:
pItemSet.reset( new SfxItemSet( rItemPool, aTableBoxSetRange ) );
break;
default:
OSL_ENSURE( false,
"SwXMLItemSetStyleContext_Impl::CreateItemSetContext: unknown family" );
break;
}
if( pItemSet )
pContext = GetSwImport().CreateTableItemImportContext(
nPrefix, rLName, xAttrList, GetFamily(),
*pItemSet );
if( !pContext )
{
pItemSet.reset();
}
return pContext;
}
SwXMLItemSetStyleContext_Impl::SwXMLItemSetStyleContext_Impl( SwXMLImport& rImport,
sal_uInt16 nPrfx, const OUString& rLName,
const uno::Reference< xml::sax::XAttributeList > & xAttrList,
SvXMLStylesContext& rStylesC,
XmlStyleFamily nFamily ) :
SvXMLStyleContext( rImport, nPrfx, rLName, xAttrList, nFamily ),
pTextStyle( nullptr ),
rStyles( rStylesC ),
bHasMasterPageName( false ),
bPageDescConnected( false ),
bDataStyleIsResolved( true )
{
}
void SwXMLItemSetStyleContext_Impl::CreateAndInsert( bool bOverwrite )
{
if( pTextStyle )
pTextStyle->CreateAndInsert( bOverwrite );
}
SvXMLImportContextRef SwXMLItemSetStyleContext_Impl::CreateChildContext(
sal_uInt16 nPrefix,
const OUString& rLocalName,
const uno::Reference< xml::sax::XAttributeList > & xAttrList )
{
SvXMLImportContextRef xContext;
if( XML_NAMESPACE_STYLE == nPrefix )
{
if( IsXMLToken( rLocalName, XML_TABLE_PROPERTIES ) ||
IsXMLToken( rLocalName, XML_TABLE_COLUMN_PROPERTIES ) ||
IsXMLToken( rLocalName, XML_TABLE_ROW_PROPERTIES ) ||
IsXMLToken( rLocalName, XML_TABLE_CELL_PROPERTIES ) )
{
xContext = CreateItemSetContext( nPrefix, rLocalName, xAttrList );
}
else if( IsXMLToken( rLocalName, XML_TEXT_PROPERTIES ) ||
IsXMLToken( rLocalName, XML_PARAGRAPH_PROPERTIES ))
{
if( !pTextStyle )
{
SvXMLAttributeList *pTmp = new SvXMLAttributeList;
const OUString aStr = GetImport().GetNamespaceMap().GetQNameByKey(
nPrefix, GetXMLToken(XML_NAME) );
pTmp->AddAttribute( aStr, GetName() );
uno::Reference <xml::sax::XAttributeList> xTmpAttrList = pTmp;
pTextStyle = new SwXMLTextStyleContext_Impl( GetSwImport(), nPrefix,
rLocalName, xTmpAttrList, XmlStyleFamily::TEXT_PARAGRAPH, rStyles );
pTextStyle->StartElement( xTmpAttrList );
rStyles.AddStyle( *pTextStyle );
}
xContext = pTextStyle->CreateChildContext( nPrefix, rLocalName, xAttrList );
}
}
if (!xContext)
xContext = SvXMLStyleContext::CreateChildContext( nPrefix, rLocalName,
xAttrList );
return xContext;
}
void SwXMLItemSetStyleContext_Impl::ConnectPageDesc()
{
if( bPageDescConnected || !HasMasterPageName() )
return;
bPageDescConnected = true;
SwDoc *pDoc = SwImport::GetDocFromXMLImport( GetSwImport() );
// #i40788# - first determine the display name of the page style,
// then map this name to the corresponding user interface name.
OUString sName = GetImport().GetStyleDisplayName( XmlStyleFamily::MASTER_PAGE,
sMasterPageName );
SwStyleNameMapper::FillUIName( sName,
sName,
SwGetPoolIdFromName::PageDesc);
SwPageDesc *pPageDesc = pDoc->FindPageDesc(sName);
if( !pPageDesc )
{
// If the page style is a pool style, then we maybe have to create it
// first if it hasn't been used by now.
const sal_uInt16 nPoolId = SwStyleNameMapper::GetPoolIdFromUIName( sName, SwGetPoolIdFromName::PageDesc );
if( USHRT_MAX != nPoolId )
pPageDesc = pDoc->getIDocumentStylePoolAccess().GetPageDescFromPool( nPoolId, false );
}
if( !pPageDesc )
return;
if( !pItemSet )
{
SfxItemPool& rItemPool = pDoc->GetAttrPool();
pItemSet.reset( new SfxItemSet( rItemPool, aTableSetRange ) );
}
const SfxPoolItem *pItem;
std::unique_ptr<SwFormatPageDesc> pFormatPageDesc;
if( SfxItemState::SET == pItemSet->GetItemState( RES_PAGEDESC, false,
&pItem ) )
{
if( static_cast<const SwFormatPageDesc *>(pItem)->GetPageDesc() != pPageDesc )
pFormatPageDesc.reset(new SwFormatPageDesc( *static_cast<const SwFormatPageDesc *>(pItem) ));
}
else
pFormatPageDesc.reset(new SwFormatPageDesc());
if( pFormatPageDesc )
{
pFormatPageDesc->RegisterToPageDesc( *pPageDesc );
pItemSet->Put( *pFormatPageDesc );
}
}
bool SwXMLItemSetStyleContext_Impl::ResolveDataStyleName()
{
// resolve, if not already done
if (! bDataStyleIsResolved)
{
// get the format key
sal_Int32 nFormat =
GetImport().GetTextImport()->GetDataStyleKey(sDataStyleName);
// if the key is valid, insert Item into ItemSet
if( -1 != nFormat )
{
if( !pItemSet )
{
SwDoc *pDoc = SwImport::GetDocFromXMLImport( GetSwImport() );
SfxItemPool& rItemPool = pDoc->GetAttrPool();
pItemSet.reset( new SfxItemSet( rItemPool, aTableBoxSetRange ) );
}
SwTableBoxNumFormat aNumFormatItem(nFormat);
pItemSet->Put(aNumFormatItem);
}
// now resolved
bDataStyleIsResolved = true;
return true;
}
else
{
// was already resolved; nothing to do
return false;
}
}
namespace {
class SwXMLStylesContext_Impl : public SvXMLStylesContext
{
SwXMLImport& GetSwImport() { return static_cast<SwXMLImport&>(GetImport()); }
const SwXMLImport& GetSwImport() const
{ return static_cast<const SwXMLImport&>(GetImport()); }
protected:
virtual SvXMLStyleContext *CreateStyleChildContext( sal_uInt16 nPrefix,
const OUString& rLocalName,
const css::uno::Reference< css::xml::sax::XAttributeList > & xAttrList ) override;
virtual SvXMLStyleContext *CreateStyleStyleChildContext( XmlStyleFamily nFamily,
sal_uInt16 nPrefix, const OUString& rLocalName,
const uno::Reference< xml::sax::XAttributeList > & xAttrList ) override;
virtual SvXMLStyleContext *CreateDefaultStyleStyleChildContext(
XmlStyleFamily nFamily, sal_uInt16 nPrefix, const OUString& rLocalName,
const uno::Reference< xml::sax::XAttributeList > & xAttrList ) override;
// HACK
virtual rtl::Reference < SvXMLImportPropertyMapper > GetImportPropertyMapper(
XmlStyleFamily nFamily ) const override;
virtual uno::Reference < container::XNameContainer >
GetStylesContainer( XmlStyleFamily nFamily ) const override;
virtual OUString GetServiceName( XmlStyleFamily nFamily ) const override;
// HACK
public:
SwXMLStylesContext_Impl(
SwXMLImport& rImport,
bool bAuto );
virtual bool InsertStyleFamily( XmlStyleFamily nFamily ) const override;
virtual void EndElement() override;
};
}
SvXMLStyleContext *SwXMLStylesContext_Impl::CreateStyleChildContext( sal_uInt16 nPrefix,
const OUString& rLocalName,
const css::uno::Reference< css::xml::sax::XAttributeList > & xAttrList )
{
SvXMLStyleContext* pContext = nullptr;
if(nPrefix == XML_NAMESPACE_TABLE && IsXMLToken(rLocalName, XML_TABLE_TEMPLATE))
{
rtl::Reference<XMLTableImport> xTableImport = GetImport().GetShapeImport()->GetShapeTableImport();
pContext = xTableImport->CreateTableTemplateContext(nPrefix, rLocalName, xAttrList);
}
if (!pContext)
pContext = SvXMLStylesContext::CreateStyleChildContext(nPrefix, rLocalName, xAttrList);
return pContext;
}
SvXMLStyleContext *SwXMLStylesContext_Impl::CreateStyleStyleChildContext(
XmlStyleFamily nFamily, sal_uInt16 nPrefix, const OUString& rLocalName,
const uno::Reference< xml::sax::XAttributeList > & xAttrList )
{
SvXMLStyleContext *pStyle = nullptr;
switch( nFamily )
{
case XmlStyleFamily::TEXT_PARAGRAPH:
pStyle = new SwXMLTextStyleContext_Impl( GetSwImport(), nPrefix,
rLocalName, xAttrList, nFamily, *this );
break;
case XmlStyleFamily::TABLE_TABLE:
case XmlStyleFamily::TABLE_COLUMN:
case XmlStyleFamily::TABLE_ROW:
case XmlStyleFamily::TABLE_CELL:
// Distinguish real and automatic styles.
if (IsAutomaticStyle())
pStyle = new SwXMLItemSetStyleContext_Impl(GetSwImport(), nPrefix, rLocalName, xAttrList, *this, nFamily);
else if (nFamily == XmlStyleFamily::TABLE_CELL) // Real cell styles are used for table-template import.
pStyle = new SwXMLCellStyleContext(GetSwImport(), nPrefix, rLocalName, xAttrList, *this, nFamily);
else
SAL_WARN("sw.xml", "Context does not exists for non automatic table, column or row style.");
break;
case XmlStyleFamily::SD_GRAPHICS_ID:
// As long as there are no element items, we can use the text
// style class.
pStyle = new XMLTextShapeStyleContext( GetImport(), nPrefix,
rLocalName, xAttrList, *this, nFamily );
break;
case XmlStyleFamily::SD_DRAWINGPAGE_ID:
pStyle = new XMLDrawingPageStyleContext(GetImport(), nPrefix, rLocalName,
xAttrList, *this, g_MasterPageContextIDs, g_MasterPageFamilies);
break;
default:
pStyle = SvXMLStylesContext::CreateStyleStyleChildContext( nFamily,
nPrefix,
rLocalName,
xAttrList );
break;
}
return pStyle;
}
SvXMLStyleContext *SwXMLStylesContext_Impl::CreateDefaultStyleStyleChildContext(
XmlStyleFamily nFamily, sal_uInt16 nPrefix, const OUString& rLocalName,
const uno::Reference< xml::sax::XAttributeList > & xAttrList )
{
SvXMLStyleContext *pStyle = nullptr;
switch( nFamily )
{
case XmlStyleFamily::TEXT_PARAGRAPH:
case XmlStyleFamily::TABLE_TABLE:
case XmlStyleFamily::TABLE_ROW:
pStyle = new XMLTextStyleContext( GetImport(), nPrefix, rLocalName,
xAttrList, *this, nFamily,
true );
break;
case XmlStyleFamily::SD_GRAPHICS_ID:
// There are no writer specific defaults for graphic styles!
pStyle = new XMLGraphicsDefaultStyle( GetImport(), nPrefix,
rLocalName, xAttrList, *this );
break;
default:
pStyle = SvXMLStylesContext::CreateDefaultStyleStyleChildContext( nFamily,
nPrefix,
rLocalName,
xAttrList );
break;
}
return pStyle;
}
SwXMLStylesContext_Impl::SwXMLStylesContext_Impl(
SwXMLImport& rImport,
bool bAuto ) :
SvXMLStylesContext( rImport, bAuto )
{
}
bool SwXMLStylesContext_Impl::InsertStyleFamily( XmlStyleFamily nFamily ) const
{
const SwXMLImport& rSwImport = GetSwImport();
const SfxStyleFamily nStyleFamilyMask = rSwImport.GetStyleFamilyMask();
bool bIns = true;
switch( nFamily )
{
case XmlStyleFamily::TEXT_PARAGRAPH:
bIns = bool(nStyleFamilyMask & SfxStyleFamily::Para);
break;
case XmlStyleFamily::TEXT_TEXT:
bIns = bool(nStyleFamilyMask & SfxStyleFamily::Char);
break;
case XmlStyleFamily::SD_GRAPHICS_ID:
bIns = bool(nStyleFamilyMask & SfxStyleFamily::Frame);
break;
case XmlStyleFamily::TEXT_LIST:
bIns = bool(nStyleFamilyMask & SfxStyleFamily::Pseudo);
break;
case XmlStyleFamily::TEXT_OUTLINE:
case XmlStyleFamily::TEXT_FOOTNOTECONFIG:
case XmlStyleFamily::TEXT_ENDNOTECONFIG:
case XmlStyleFamily::TEXT_LINENUMBERINGCONFIG:
case XmlStyleFamily::TEXT_BIBLIOGRAPHYCONFIG:
bIns = !(rSwImport.IsInsertMode() || rSwImport.IsStylesOnlyMode() ||
rSwImport.IsBlockMode());
break;
default:
bIns = SvXMLStylesContext::InsertStyleFamily( nFamily );
break;
}
return bIns;
}
rtl::Reference < SvXMLImportPropertyMapper > SwXMLStylesContext_Impl::GetImportPropertyMapper(
XmlStyleFamily nFamily ) const
{
rtl::Reference < SvXMLImportPropertyMapper > xMapper;
if( nFamily == XmlStyleFamily::TABLE_TABLE )
xMapper = XMLTextImportHelper::CreateTableDefaultExtPropMapper(
const_cast<SwXMLStylesContext_Impl*>( this )->GetImport() );
else if( nFamily == XmlStyleFamily::TABLE_ROW )
xMapper = XMLTextImportHelper::CreateTableRowDefaultExtPropMapper(
const_cast<SwXMLStylesContext_Impl*>( this )->GetImport() );
else if( nFamily == XmlStyleFamily::TABLE_CELL )
xMapper = XMLTextImportHelper::CreateTableCellExtPropMapper(
const_cast<SwXMLStylesContext_Impl*>( this )->GetImport() );
else if (nFamily == XmlStyleFamily::SD_DRAWINGPAGE_ID)
{
xMapper = XMLTextImportHelper::CreateDrawingPageExtPropMapper(
const_cast<SwXMLStylesContext_Impl*>(this)->GetImport());
}
else
xMapper = SvXMLStylesContext::GetImportPropertyMapper( nFamily );
return xMapper;
}
uno::Reference < container::XNameContainer > SwXMLStylesContext_Impl::GetStylesContainer(
XmlStyleFamily nFamily ) const
{
uno::Reference < container::XNameContainer > xStyles;
if( XmlStyleFamily::SD_GRAPHICS_ID == nFamily )
xStyles = const_cast<SvXMLImport *>(&GetImport())->GetTextImport()->GetFrameStyles();
else if( XmlStyleFamily::TABLE_CELL == nFamily )
xStyles = const_cast<SvXMLImport *>(&GetImport())->GetTextImport()->GetCellStyles();
if (!xStyles.is())
xStyles = SvXMLStylesContext::GetStylesContainer( nFamily );
return xStyles;
}
OUString SwXMLStylesContext_Impl::GetServiceName( XmlStyleFamily nFamily ) const
{
if( XmlStyleFamily::SD_GRAPHICS_ID == nFamily )
return "com.sun.star.style.FrameStyle";
else if( XmlStyleFamily::TABLE_CELL == nFamily )
return "com.sun.star.style.CellStyle";
return SvXMLStylesContext::GetServiceName( nFamily );
}
void SwXMLStylesContext_Impl::EndElement()
{
GetSwImport().InsertStyles( IsAutomaticStyle() );
}
namespace {
class SwXMLMasterStylesContext_Impl : public XMLTextMasterStylesContext
{
protected:
virtual bool InsertStyleFamily( XmlStyleFamily nFamily ) const override;
SwXMLImport& GetSwImport() { return static_cast<SwXMLImport&>(GetImport()); }
const SwXMLImport& GetSwImport() const
{ return static_cast<const SwXMLImport&>(GetImport()); }
public:
SwXMLMasterStylesContext_Impl( SwXMLImport& rImport );
virtual void EndElement() override;
};
}
SwXMLMasterStylesContext_Impl::SwXMLMasterStylesContext_Impl(
SwXMLImport& rImport ) :
XMLTextMasterStylesContext( rImport )
{
}
bool SwXMLMasterStylesContext_Impl::InsertStyleFamily( XmlStyleFamily nFamily ) const
{
bool bIns;
const SwXMLImport& rSwImport = GetSwImport();
const SfxStyleFamily nStyleFamilyMask = rSwImport.GetStyleFamilyMask();
if( XmlStyleFamily::MASTER_PAGE == nFamily )
bIns = bool(nStyleFamilyMask & SfxStyleFamily::Page);
else
bIns = XMLTextMasterStylesContext::InsertStyleFamily( nFamily );
return bIns;
}
void SwXMLMasterStylesContext_Impl::EndElement()
{
FinishStyles( !GetSwImport().IsInsertMode() );
GetSwImport().FinishStyles();
}
SvXMLImportContext *SwXMLImport::CreateStylesContext(
bool bAuto )
{
SvXMLStylesContext *pContext = new SwXMLStylesContext_Impl( *this, bAuto );
if( bAuto )
SetAutoStyles( pContext );
else
SetStyles( pContext );
return pContext;
}
SvXMLImportContext *SwXMLImport::CreateMasterStylesContext()
{
SvXMLStylesContext *pContext =
new SwXMLMasterStylesContext_Impl( *this );
SetMasterStyles( pContext );
return pContext;
}
void SwXMLImport::InsertStyles( bool bAuto )
{
if( bAuto && GetAutoStyles() )
GetAutoStyles()->CopyAutoStylesToDoc();
if( !bAuto && GetStyles() )
GetStyles()->CopyStylesToDoc( !IsInsertMode(), false );
}
void SwXMLImport::FinishStyles()
{
if( GetStyles() )
GetStyles()->FinishStyles( !IsInsertMode() );
}
void SwXMLImport::UpdateTextCollConditions( SwDoc *pDoc )
{
if( !pDoc )
pDoc = SwImport::GetDocFromXMLImport( *this );
const SwTextFormatColls& rColls = *pDoc->GetTextFormatColls();
const size_t nCount = rColls.size();
for( size_t i=0; i < nCount; ++i )
{
SwTextFormatColl *pColl = rColls[i];
if( pColl && RES_CONDTXTFMTCOLL == pColl->Which() )
{
const SwFormatCollConditions& rConditions =
static_cast<const SwConditionTextFormatColl *>(pColl)->GetCondColls();
bool bSendModify = false;
for( size_t j=0; j < rConditions.size() && !bSendModify; ++j )
{
const SwCollCondition& rCond = *rConditions[j];
switch( rCond.GetCondition() )
{
case Master_CollCondition::PARA_IN_TABLEHEAD:
case Master_CollCondition::PARA_IN_TABLEBODY:
case Master_CollCondition::PARA_IN_FOOTER:
case Master_CollCondition::PARA_IN_HEADER:
bSendModify = true;
break;
default: break;
}
}
if( bSendModify )
{
SwCondCollCondChg aMsg( pColl );
pColl->ModifyNotification( &aMsg, &aMsg );
}
}
}
}
bool SwXMLImport::FindAutomaticStyle(
XmlStyleFamily nFamily,
const OUString& rName,
const SfxItemSet **ppItemSet ) const
{
SwXMLItemSetStyleContext_Impl *pStyle = nullptr;
if( GetAutoStyles() )
{
pStyle = const_cast<SwXMLItemSetStyleContext_Impl*>(dynamic_cast< const SwXMLItemSetStyleContext_Impl* >(
GetAutoStyles()->
FindStyleChildContext( nFamily, rName,
true ) ) );
if( pStyle )
{
if( ppItemSet )
{
if( XmlStyleFamily::TABLE_TABLE == pStyle->GetFamily() &&
pStyle->HasMasterPageName() &&
!pStyle->IsPageDescConnected() )
pStyle->ConnectPageDesc();
(*ppItemSet) = pStyle->GetItemSet();
// resolve data style name late
if( XmlStyleFamily::TABLE_CELL == pStyle->GetFamily() &&
pStyle->ResolveDataStyleName() )
{
(*ppItemSet) = pStyle->GetItemSet();
}
}
}
}
return pStyle != nullptr;
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|