LibreOffice Module sw (master)  1
xmlfmt.cxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
5  * This Source Code Form is subject to the terms of the Mozilla Public
6  * License, v. 2.0. If a copy of the MPL was not distributed with this
7  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
11  * Licensed to the Apache Software Foundation (ASF) under one or more
12  * contributor license agreements. See the NOTICE file distributed
13  * with this work for additional information regarding copyright
14  * ownership. The ASF licenses this file to you under the Apache
15  * License, Version 2.0 (the "License"); you may not use this file
16  * except in compliance with the License. You may obtain a copy of
17  * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18  */
19 
20 #include <rtl/ustrbuf.hxx>
21 #include <sal/log.hxx>
22 #include <osl/diagnose.h>
23 #include <tools/diagnose_ex.h>
24 #include <xmloff/namespacemap.hxx>
25 #include <fmtcol.hxx>
26 #include <hints.hxx>
27 #include <doc.hxx>
28 #include <docary.hxx>
30 #include <unoprnms.hxx>
31 #include <fmtpdsc.hxx>
32 #include <pagedesc.hxx>
33 #include <xmloff/maptype.hxx>
34 #include <xmloff/xmlnumfi.hxx>
35 #include <xmloff/xmlprmap.hxx>
36 #include <xmloff/xmlnamespace.hxx>
37 #include <xmloff/xmlstyle.hxx>
38 #include <xmloff/txtstyli.hxx>
39 #include <xmloff/txtimp.hxx>
40 #include <xmloff/families.hxx>
47 #include <com/sun/star/beans/XPropertySet.hpp>
48 #include <com/sun/star/frame/XModel.hpp>
49 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
50 #include "xmlimp.hxx"
51 #include <cellatr.hxx>
52 #include <SwStyleNameMapper.hxx>
53 #include <xmloff/attrlist.hxx>
54 #include <ccoll.hxx>
55 
56 #include <memory>
57 
58 using namespace ::com::sun::star;
59 using namespace ::com::sun::star::beans;
60 using namespace ::com::sun::star::uno;
61 using namespace ::xmloff::token;
62 
63 namespace {
64 
65 class SwXMLConditionParser_Impl
66 {
67  OUString sInput;
68 
69  Master_CollCondition nCondition;
70  sal_uInt32 nSubCondition;
71 
72  sal_Int32 nPos;
73  sal_Int32 nLength;
74 
75  inline bool SkipWS();
76  inline bool MatchChar( sal_Unicode c );
77  inline bool MatchName( OUString& rName );
78  inline bool MatchNumber( sal_uInt32& rNumber );
79 
80 public:
81 
82  explicit SwXMLConditionParser_Impl( const OUString& rInp );
83 
84  bool IsValid() const { return Master_CollCondition::NONE != nCondition; }
85 
86  Master_CollCondition GetCondition() const { return nCondition; }
87  sal_uInt32 GetSubCondition() const { return nSubCondition; }
88 };
89 
90 }
91 
92 inline bool SwXMLConditionParser_Impl::SkipWS()
93 {
94  while( nPos < nLength && ' ' == sInput[nPos] )
95  nPos++;
96  return true;
97 }
98 
99 inline bool SwXMLConditionParser_Impl::MatchChar( sal_Unicode c )
100 {
101  bool bRet = false;
102  if( nPos < nLength && c == sInput[nPos] )
103  {
104  nPos++;
105  bRet = true;
106  }
107  return bRet;
108 }
109 
110 inline bool SwXMLConditionParser_Impl::MatchName( OUString& rName )
111 {
112  OUStringBuffer sBuffer( nLength );
113  while( nPos < nLength &&
114  ( ('a' <= sInput[nPos] && sInput[nPos] <= 'z') ||
115  '-' == sInput[nPos] ) )
116  {
117  sBuffer.append( sInput[nPos] );
118  nPos++;
119  }
120  rName = sBuffer.makeStringAndClear();
121  return !rName.isEmpty();
122 }
123 
124 inline bool SwXMLConditionParser_Impl::MatchNumber( sal_uInt32& rNumber )
125 {
126  OUStringBuffer sBuffer( nLength );
127  while( nPos < nLength && '0' <= sInput[nPos] && sInput[nPos] <= '9' )
128  {
129  sBuffer.append( sInput[nPos] );
130  nPos++;
131  }
132 
133  OUString sNum( sBuffer.makeStringAndClear() );
134  if( !sNum.isEmpty() )
135  rNumber = sNum.toInt32();
136  return !sNum.isEmpty();
137 }
138 
139 SwXMLConditionParser_Impl::SwXMLConditionParser_Impl( const OUString& rInp ) :
140  sInput( rInp ),
141  nCondition( Master_CollCondition::NONE ),
142  nSubCondition( 0 ),
143  nPos( 0 ),
144  nLength( rInp.getLength() )
145 {
146  OUString sFunc;
147  bool bHasSub = false;
148  sal_uInt32 nSub = 0;
149  bool bOK = SkipWS() && MatchName( sFunc ) && SkipWS() &&
150  MatchChar( '(' ) && SkipWS() && MatchChar( ')' ) && SkipWS();
151  if( bOK && MatchChar( '=' ) )
152  {
153  bOK = SkipWS() && MatchNumber( nSub ) && SkipWS();
154  bHasSub = true;
155  }
156 
157  bOK &= nPos == nLength;
158 
159  if( !bOK )
160  return;
161 
162  if( IsXMLToken( sFunc, XML_ENDNOTE ) && !bHasSub )
164  else if( IsXMLToken( sFunc, XML_FOOTER ) && !bHasSub )
166  else if( IsXMLToken( sFunc, XML_FOOTNOTE ) && !bHasSub )
168  else if( IsXMLToken( sFunc, XML_HEADER ) && !bHasSub )
170  else if( IsXMLToken( sFunc, XML_LIST_LEVEL) &&
171  nSub >=1 && nSub <= MAXLEVEL )
172  {
174  nSubCondition = nSub-1;
175  }
176  else if( IsXMLToken( sFunc, XML_OUTLINE_LEVEL) &&
177  nSub >=1 && nSub <= MAXLEVEL )
178  {
180  nSubCondition = nSub-1;
181  }
182  else if( IsXMLToken( sFunc, XML_SECTION ) && !bHasSub )
183  {
185  }
186  else if( IsXMLToken( sFunc, XML_TABLE ) && !bHasSub )
187  {
189  }
190  else if( IsXMLToken( sFunc, XML_TABLE_HEADER ) && !bHasSub )
191  {
193  }
194  else if( IsXMLToken( sFunc, XML_TEXT_BOX ) && !bHasSub )
195  {
197  }
198 }
199 
200 namespace {
201 
202 class SwXMLConditionContext_Impl : public SvXMLImportContext
203 {
204  Master_CollCondition nCondition;
205  sal_uInt32 nSubCondition;
206 
207  OUString sApplyStyle;
208 
209 public:
210 
211  SwXMLConditionContext_Impl(
212  SvXMLImport& rImport, sal_Int32 nElement,
213  const uno::Reference< xml::sax::XFastAttributeList > & xAttrList );
214 
215  bool IsValid() const { return Master_CollCondition::NONE != nCondition; }
216 
217  Master_CollCondition getCondition() const { return nCondition; }
218  sal_uInt32 getSubCondition() const { return nSubCondition; }
219  OUString const &getApplyStyle() const { return sApplyStyle; }
220 };
221 
222 }
223 
224 SwXMLConditionContext_Impl::SwXMLConditionContext_Impl(
225  SvXMLImport& rImport, sal_Int32 /*nElement*/,
226  const uno::Reference< xml::sax::XFastAttributeList > & xAttrList ) :
227  SvXMLImportContext( rImport ),
228  nCondition( Master_CollCondition::NONE ),
229  nSubCondition( 0 )
230 {
231  for (auto &aIter : sax_fastparser::castToFastAttributeList( xAttrList ))
232  {
233  OUString sValue = aIter.toString();
234  switch (aIter.getToken())
235  {
236  case XML_ELEMENT(STYLE, XML_CONDITION):
237  {
238  SwXMLConditionParser_Impl aCondParser( sValue );
239  if( aCondParser.IsValid() )
240  {
241  nCondition = aCondParser.GetCondition();
242  nSubCondition = aCondParser.GetSubCondition();
243  }
244  break;
245  }
246  case XML_ELEMENT(STYLE, XML_APPLY_STYLE_NAME):
247  sApplyStyle = sValue;
248  break;
249  default:
250  SAL_WARN("sw", "unknown attribute " << SvXMLImport::getPrefixAndNameFromToken(aIter.getToken()) << "=" << sValue);
251  }
252  }
253 }
254 
255 typedef std::vector<rtl::Reference<SwXMLConditionContext_Impl>> SwXMLConditions_Impl;
256 
257 namespace {
258 
259 class SwXMLTextStyleContext_Impl : public XMLTextStyleContext
260 {
261  std::unique_ptr<SwXMLConditions_Impl> pConditions;
262  uno::Reference < style::XStyle > xNewStyle;
263 
264 protected:
265 
266  virtual uno::Reference < style::XStyle > Create() override;
267  virtual void Finish( bool bOverwrite ) override;
268 
269 public:
270 
271 
272  SwXMLTextStyleContext_Impl( SwXMLImport& rImport,
273  XmlStyleFamily nFamily,
274  SvXMLStylesContext& rStyles );
275 
276  virtual css::uno::Reference< css::xml::sax::XFastContextHandler > SAL_CALL createFastChildContext(
277  sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList >& AttrList ) override;
278 };
279 
280 }
281 
282 uno::Reference < style::XStyle > SwXMLTextStyleContext_Impl::Create()
283 {
284 
285  if( pConditions && XmlStyleFamily::TEXT_PARAGRAPH == GetFamily() )
286  {
287  uno::Reference< lang::XMultiServiceFactory > xFactory( GetImport().GetModel(),
288  uno::UNO_QUERY );
289  if( xFactory.is() )
290  {
291  uno::Reference < uno::XInterface > xIfc =
292  xFactory->createInstance( "com.sun.star.style.ConditionalParagraphStyle" );
293  if( xIfc.is() )
294  xNewStyle.set( xIfc, uno::UNO_QUERY );
295  }
296  }
297  else
298  {
299  xNewStyle = XMLTextStyleContext::Create();
300  }
301 
302  return xNewStyle;
303 }
304 
305 void
306 SwXMLTextStyleContext_Impl::Finish( bool bOverwrite )
307 {
308 
309  if( pConditions && XmlStyleFamily::TEXT_PARAGRAPH == GetFamily() && xNewStyle.is() )
310  {
311  CommandStruct const*const pCommands = SwCondCollItem::GetCmds();
312 
313  Reference< XPropertySet > xPropSet( xNewStyle, UNO_QUERY );
314 
315  uno::Sequence< beans::NamedValue > aSeq( pConditions->size() );
316 
317  for (std::vector<rtl::Reference<SwXMLConditionContext_Impl>>::size_type i = 0;
318  i < pConditions->size(); ++i)
319  {
320  assert((*pConditions)[i]->IsValid()); // checked before inserting
321  Master_CollCondition nCond = (*pConditions)[i]->getCondition();
322  sal_uInt32 nSubCond = (*pConditions)[i]->getSubCondition();
323 
324  for (size_t j = 0; j < COND_COMMAND_COUNT; ++j)
325  {
326  if (pCommands[j].nCnd == nCond &&
327  pCommands[j].nSubCond == nSubCond)
328  {
329  aSeq[i].Name = GetCommandContextByIndex( j );
330  aSeq[i].Value <<= GetImport().GetStyleDisplayName(
331  GetFamily(), (*pConditions)[i]->getApplyStyle() );
332  break;
333  }
334  }
335  }
336 
337  try
338  {
339  xPropSet->setPropertyValue(UNO_NAME_PARA_STYLE_CONDITIONS, uno::makeAny(aSeq));
340  }
341  catch (uno::Exception const&)
342  {
343  TOOLS_WARN_EXCEPTION("sw.xml", "exception when setting ParaStyleConditions");
344  }
345  }
346  XMLTextStyleContext::Finish( bOverwrite );
347 }
348 
349 SwXMLTextStyleContext_Impl::SwXMLTextStyleContext_Impl( SwXMLImport& rImport,
350  XmlStyleFamily nFamily,
351  SvXMLStylesContext& rStyles ) :
352  XMLTextStyleContext( rImport, rStyles, nFamily )
353 {
354 }
355 
356 css::uno::Reference< css::xml::sax::XFastContextHandler > SwXMLTextStyleContext_Impl::createFastChildContext(
357  sal_Int32 nElement,
358  const css::uno::Reference< css::xml::sax::XFastAttributeList >& xAttrList )
359 {
360  if( nElement == XML_ELEMENT(STYLE, XML_MAP) )
361  {
363  new SwXMLConditionContext_Impl( GetImport(), nElement, xAttrList )};
364  if( xCond->IsValid() )
365  {
366  if( !pConditions )
367  pConditions = std::make_unique<SwXMLConditions_Impl>();
368  pConditions->push_back( xCond );
369  }
370  return xCond.get();
371  }
372 
373  return XMLTextStyleContext::createFastChildContext( nElement, xAttrList );
374 }
375 
376 namespace {
377 
378 class SwXMLCellStyleContext : public XMLPropStyleContext
379 {
380  OUString m_sDataStyleName;
381  void AddDataFormat();
382 public:
384  virtual void FillPropertySet(const css::uno::Reference<css::beans::XPropertySet>& rPropSet) override;
385  virtual void SetAttribute(sal_uInt16 nPrefixKey, const OUString& rLocalName, const OUString& rValue) override;
386 };
387 
388 class SwXMLItemSetStyleContext_Impl : public SvXMLStyleContext
389 {
390  OUString sMasterPageName;
391  std::unique_ptr<SfxItemSet> pItemSet;
392  SwXMLTextStyleContext_Impl *pTextStyle;
393  SvXMLStylesContext &rStyles;
394 
395  OUString sDataStyleName;
396 
397  bool bHasMasterPageName : 1;
398  bool bPageDescConnected : 1;
399  bool bDataStyleIsResolved;
400 
401  SvXMLImportContext *CreateItemSetContext(
402  sal_Int32 nElement,
403  const uno::Reference< xml::sax::XFastAttributeList > & xAttrList);
404 
405 protected:
406 
407  virtual void SetAttribute( sal_uInt16 nPrefixKey,
408  const OUString& rLocalName,
409  const OUString& rValue ) override;
410 
411  SwXMLImport& GetSwImport() { return static_cast<SwXMLImport&>(GetImport()); }
412 
413 public:
414 
415 
416  SwXMLItemSetStyleContext_Impl(
417  SwXMLImport& rImport,
418  SvXMLStylesContext& rStylesC,
419  XmlStyleFamily nFamily);
420 
421  virtual void CreateAndInsert( bool bOverwrite ) override;
422 
423  virtual css::uno::Reference< css::xml::sax::XFastContextHandler > SAL_CALL createFastChildContext(
424  sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList >& AttrList ) override;
425 
426  // The item set may be empty!
427  SfxItemSet *GetItemSet() { return pItemSet.get(); }
428 
429  bool HasMasterPageName() const { return bHasMasterPageName; }
430 
431  bool IsPageDescConnected() const { return bPageDescConnected; }
432  void ConnectPageDesc();
433 
434  bool ResolveDataStyleName();
435 };
436 
437 }
438 
439 void SwXMLCellStyleContext::AddDataFormat()
440 {
441  if (m_sDataStyleName.isEmpty() || IsDefaultStyle())
442  return;
443 
444  const SvXMLNumFormatContext* pStyle = static_cast<const SvXMLNumFormatContext*>(
445  GetStyles()->FindStyleChildContext(XmlStyleFamily::DATA_STYLE, m_sDataStyleName, true));
446 
447  if (!pStyle)
448  {
449  SAL_WARN("sw.xml", "not possible to get data style " << m_sDataStyleName);
450  return;
451  }
452 
453  sal_Int32 nNumberFormat = const_cast<SvXMLNumFormatContext*>(pStyle)->GetKey();
454  if (nNumberFormat < 0)
455  return;
456 
457  rtl::Reference<SvXMLImportPropertyMapper> xPropertyMapper(GetStyles()->GetImportPropertyMapper(GetFamily()));
458  if (!xPropertyMapper.is())
459  {
460  SAL_WARN("sw.xml", "there is no import prop mapper");
461  return;
462  }
463 
464  const rtl::Reference<XMLPropertySetMapper>& xPropertySetMapper(xPropertyMapper->getPropertySetMapper());
465  sal_Int32 nIndex = xPropertySetMapper->GetEntryIndex(XML_NAMESPACE_STYLE, GetXMLToken(XML_DATA_STYLE_NAME), 0);
466  if (nIndex < 0)
467  {
468  SAL_WARN("sw.xml", "could not find id for " << GetXMLToken(XML_DATA_STYLE_NAME));
469  return;
470  }
471 
472  auto aIter = std::find_if(GetProperties().begin(), GetProperties().end(),
473  [&nIndex](const XMLPropertyState& rProp) {
474  return rProp.mnIndex == nIndex;
475  });
476 
477  if (aIter != GetProperties().end())
478  aIter->maValue <<= nNumberFormat;
479  else
480  GetProperties().push_back(XMLPropertyState(nIndex, makeAny(nNumberFormat)));
481 }
482 
483 void SwXMLCellStyleContext::FillPropertySet(const css::uno::Reference<css::beans::XPropertySet>& rPropSet)
484 {
485  AddDataFormat();
487 }
488 
489 void SwXMLCellStyleContext::SetAttribute(sal_uInt16 nPrefixKey, const OUString& rLocalName, const OUString& rValue)
490 {
491  if (IsXMLToken(rLocalName, XML_DATA_STYLE_NAME))
492  m_sDataStyleName = rValue;
493  else
494  XMLPropStyleContext::SetAttribute(nPrefixKey, rLocalName, rValue);
495 }
496 
497 void SwXMLItemSetStyleContext_Impl::SetAttribute( sal_uInt16 nPrefixKey,
498  const OUString& rLocalName,
499  const OUString& rValue )
500 {
501  if( XML_NAMESPACE_STYLE == nPrefixKey )
502  {
503  if ( IsXMLToken( rLocalName, XML_MASTER_PAGE_NAME ) )
504  {
505  sMasterPageName = rValue;
506  bHasMasterPageName = true;
507  }
508  else if ( IsXMLToken( rLocalName, XML_DATA_STYLE_NAME ) )
509  {
510  // if we have a valid data style name
511  if (!rValue.isEmpty())
512  {
513  sDataStyleName = rValue;
514  bDataStyleIsResolved = false; // needs to be resolved
515  }
516  }
517  else
518  {
519  SvXMLStyleContext::SetAttribute( nPrefixKey, rLocalName, rValue );
520  }
521  }
522  else
523  {
524  SvXMLStyleContext::SetAttribute( nPrefixKey, rLocalName, rValue );
525  }
526 }
527 
528 SvXMLImportContext *SwXMLItemSetStyleContext_Impl::CreateItemSetContext(
529  sal_Int32 nElement,
530  const uno::Reference< xml::sax::XFastAttributeList > & xAttrList )
531 {
532  OSL_ENSURE( !pItemSet,
533  "SwXMLItemSetStyleContext_Impl::CreateItemSetContext: item set exists" );
534 
535  SvXMLImportContext *pContext = nullptr;
536 
537  SwDoc* pDoc = SwImport::GetDocFromXMLImport( GetSwImport() );
538 
539  SfxItemPool& rItemPool = pDoc->GetAttrPool();
540  switch( GetFamily() )
541  {
542  case XmlStyleFamily::TABLE_TABLE:
543  pItemSet.reset( new SfxItemSet( rItemPool, aTableSetRange ) );
544  break;
545  case XmlStyleFamily::TABLE_COLUMN:
546  pItemSet.reset( new SfxItemSet( rItemPool, svl::Items<RES_FRM_SIZE, RES_FRM_SIZE>{} ) );
547  break;
548  case XmlStyleFamily::TABLE_ROW:
549  pItemSet.reset( new SfxItemSet( rItemPool, aTableLineSetRange ) );
550  break;
551  case XmlStyleFamily::TABLE_CELL:
552  pItemSet.reset( new SfxItemSet( rItemPool, aTableBoxSetRange ) );
553  break;
554  default:
555  OSL_ENSURE( false,
556  "SwXMLItemSetStyleContext_Impl::CreateItemSetContext: unknown family" );
557  break;
558  }
559  if( pItemSet )
560  pContext = GetSwImport().CreateTableItemImportContext(
561  nElement, xAttrList, GetFamily(),
562  *pItemSet );
563  if( !pContext )
564  {
565  pItemSet.reset();
566  }
567 
568  return pContext;
569 }
570 
571 
572 SwXMLItemSetStyleContext_Impl::SwXMLItemSetStyleContext_Impl( SwXMLImport& rImport,
573  SvXMLStylesContext& rStylesC,
574  XmlStyleFamily nFamily ) :
575  SvXMLStyleContext( rImport, nFamily ),
576  pTextStyle( nullptr ),
577  rStyles( rStylesC ),
578  bHasMasterPageName( false ),
579  bPageDescConnected( false ),
580  bDataStyleIsResolved( true )
581 {
582 }
583 
584 void SwXMLItemSetStyleContext_Impl::CreateAndInsert( bool bOverwrite )
585 {
586  if( pTextStyle )
587  pTextStyle->CreateAndInsert( bOverwrite );
588 }
589 
590 css::uno::Reference< css::xml::sax::XFastContextHandler > SwXMLItemSetStyleContext_Impl::createFastChildContext(
591  sal_Int32 nElement,
592  const uno::Reference< xml::sax::XFastAttributeList > & xAttrList )
593 {
594  switch (nElement)
595  {
596  case XML_ELEMENT(STYLE, XML_TABLE_PROPERTIES):
600  return CreateItemSetContext( nElement, xAttrList );
601  break;
602  case XML_ELEMENT(STYLE, XML_TEXT_PROPERTIES):
604  {
605  if( !pTextStyle )
606  {
607  pTextStyle = new SwXMLTextStyleContext_Impl( GetSwImport(), XmlStyleFamily::TEXT_PARAGRAPH, rStyles );
609  xTmpAttrList->add(XML_ELEMENT(STYLE, XML_NAME), GetName().toUtf8() );
610  pTextStyle->startFastElement( nElement, xTmpAttrList.get() );
611  rStyles.AddStyle( *pTextStyle );
612  }
613  return pTextStyle->createFastChildContext( nElement, xAttrList );
614  }
615  default:
616  SAL_WARN("sw", "unknown element " << SvXMLImport::getPrefixAndNameFromToken(nElement));
617  }
618 
619  return nullptr;
620 }
621 
622 void SwXMLItemSetStyleContext_Impl::ConnectPageDesc()
623 {
624  if( bPageDescConnected || !HasMasterPageName() )
625  return;
626  bPageDescConnected = true;
627 
628  SwDoc *pDoc = SwImport::GetDocFromXMLImport( GetSwImport() );
629 
630  // #i40788# - first determine the display name of the page style,
631  // then map this name to the corresponding user interface name.
632  OUString sName = GetImport().GetStyleDisplayName( XmlStyleFamily::MASTER_PAGE,
633  sMasterPageName );
635  sName,
637  SwPageDesc *pPageDesc = pDoc->FindPageDesc(sName);
638  if( !pPageDesc )
639  {
640  // If the page style is a pool style, then we maybe have to create it
641  // first if it hasn't been used by now.
642  const sal_uInt16 nPoolId = SwStyleNameMapper::GetPoolIdFromUIName( sName, SwGetPoolIdFromName::PageDesc );
643  if( USHRT_MAX != nPoolId )
644  pPageDesc = pDoc->getIDocumentStylePoolAccess().GetPageDescFromPool( nPoolId, false );
645  }
646 
647  if( !pPageDesc )
648  return;
649 
650  if( !pItemSet )
651  {
652  SfxItemPool& rItemPool = pDoc->GetAttrPool();
653  pItemSet.reset( new SfxItemSet( rItemPool, aTableSetRange ) );
654  }
655 
656  const SfxPoolItem *pItem;
657  std::unique_ptr<SwFormatPageDesc> pFormatPageDesc;
658  if( SfxItemState::SET == pItemSet->GetItemState( RES_PAGEDESC, false,
659  &pItem ) )
660  {
661  if( static_cast<const SwFormatPageDesc *>(pItem)->GetPageDesc() != pPageDesc )
662  pFormatPageDesc.reset(new SwFormatPageDesc( *static_cast<const SwFormatPageDesc *>(pItem) ));
663  }
664  else
665  pFormatPageDesc.reset(new SwFormatPageDesc());
666 
667  if( pFormatPageDesc )
668  {
669  pFormatPageDesc->RegisterToPageDesc( *pPageDesc );
670  pItemSet->Put( *pFormatPageDesc );
671  }
672 }
673 
674 bool SwXMLItemSetStyleContext_Impl::ResolveDataStyleName()
675 {
676  // resolve, if not already done
677  if (! bDataStyleIsResolved)
678  {
679  // get the format key
680  sal_Int32 nFormat =
681  GetImport().GetTextImport()->GetDataStyleKey(sDataStyleName);
682 
683  // if the key is valid, insert Item into ItemSet
684  if( -1 != nFormat )
685  {
686  if( !pItemSet )
687  {
688  SwDoc *pDoc = SwImport::GetDocFromXMLImport( GetSwImport() );
689 
690  SfxItemPool& rItemPool = pDoc->GetAttrPool();
691  pItemSet.reset( new SfxItemSet( rItemPool, aTableBoxSetRange ) );
692  }
693  SwTableBoxNumFormat aNumFormatItem(nFormat);
694  pItemSet->Put(aNumFormatItem);
695  }
696 
697  // now resolved
698  bDataStyleIsResolved = true;
699  return true;
700  }
701  else
702  {
703  // was already resolved; nothing to do
704  return false;
705  }
706 }
707 
708 namespace {
709 
710 class SwXMLStylesContext_Impl : public SvXMLStylesContext
711 {
712  SwXMLImport& GetSwImport() { return static_cast<SwXMLImport&>(GetImport()); }
713  const SwXMLImport& GetSwImport() const
714  { return static_cast<const SwXMLImport&>(GetImport()); }
715 
716 protected:
717 
719  virtual SvXMLStyleContext *CreateStyleChildContext( sal_Int32 nElement,
720  const css::uno::Reference< css::xml::sax::XFastAttributeList > & xAttrList ) override;
721 
724  sal_Int32 nElement,
725  const uno::Reference< xml::sax::XFastAttributeList > & xAttrList ) override;
728  XmlStyleFamily nFamily, sal_Int32 nElement,
729  const uno::Reference< xml::sax::XFastAttributeList > & xAttrList ) override;
730  // HACK
732  XmlStyleFamily nFamily ) const override;
733 
734  virtual uno::Reference < container::XNameContainer >
735  GetStylesContainer( XmlStyleFamily nFamily ) const override;
736  virtual OUString GetServiceName( XmlStyleFamily nFamily ) const override;
737  // HACK
738 
739 public:
740 
741  SwXMLStylesContext_Impl(
742  SwXMLImport& rImport,
743  bool bAuto );
744 
745  virtual bool InsertStyleFamily( XmlStyleFamily nFamily ) const override;
746 
747  virtual void SAL_CALL endFastElement(sal_Int32 nElement) override;
748 };
749 
750 }
751 
752 SvXMLStyleContext *SwXMLStylesContext_Impl::CreateStyleChildContext(
753  sal_Int32 nElement,
754  const css::uno::Reference< css::xml::sax::XFastAttributeList > & xAttrList )
755 {
756  SvXMLStyleContext* pContext = nullptr;
757 
758  if(nElement == XML_ELEMENT(TABLE, XML_TABLE_TEMPLATE))
759  {
760  rtl::Reference<XMLTableImport> xTableImport = GetImport().GetShapeImport()->GetShapeTableImport();
761  pContext = xTableImport->CreateTableTemplateContext(nElement, xAttrList);
762  }
763  if (!pContext)
764  pContext = SvXMLStylesContext::CreateStyleChildContext(nElement, xAttrList);
765 
766  return pContext;
767 }
768 
769 SvXMLStyleContext *SwXMLStylesContext_Impl::CreateStyleStyleChildContext(
770  XmlStyleFamily nFamily, sal_Int32 nElement,
771  const uno::Reference< xml::sax::XFastAttributeList > & xAttrList )
772 {
773  SvXMLStyleContext *pStyle = nullptr;
774 
775  switch( nFamily )
776  {
777  case XmlStyleFamily::TEXT_PARAGRAPH:
778  pStyle = new SwXMLTextStyleContext_Impl( GetSwImport(), nFamily, *this );
779  break;
780  case XmlStyleFamily::TABLE_TABLE:
781  case XmlStyleFamily::TABLE_COLUMN:
782  case XmlStyleFamily::TABLE_ROW:
783  case XmlStyleFamily::TABLE_CELL:
784  // Distinguish real and automatic styles.
785  if (IsAutomaticStyle())
786  pStyle = new SwXMLItemSetStyleContext_Impl(GetSwImport(), *this, nFamily);
787  else if (nFamily == XmlStyleFamily::TABLE_CELL) // Real cell styles are used for table-template import.
788  pStyle = new SwXMLCellStyleContext(GetSwImport(), *this, nFamily);
789  else
790  SAL_WARN("sw.xml", "Context does not exists for non automatic table, column or row style.");
791  break;
792  case XmlStyleFamily::SD_GRAPHICS_ID:
793  // As long as there are no element items, we can use the text
794  // style class.
795  pStyle = new XMLTextShapeStyleContext( GetImport(), *this, nFamily );
796  break;
797  case XmlStyleFamily::SD_DRAWINGPAGE_ID:
798  pStyle = new XMLDrawingPageStyleContext(GetImport(),
799  *this, g_MasterPageContextIDs, g_MasterPageFamilies);
800  break;
801  default:
803  nElement,
804  xAttrList );
805  break;
806  }
807 
808  return pStyle;
809 }
810 
811 SvXMLStyleContext *SwXMLStylesContext_Impl::CreateDefaultStyleStyleChildContext(
812  XmlStyleFamily nFamily, sal_Int32 nElement,
813  const uno::Reference< xml::sax::XFastAttributeList > & xAttrList )
814 {
815  SvXMLStyleContext *pStyle = nullptr;
816 
817  switch( nFamily )
818  {
819  case XmlStyleFamily::TEXT_PARAGRAPH:
820  case XmlStyleFamily::TABLE_TABLE:
821  case XmlStyleFamily::TABLE_ROW:
822  pStyle = new XMLTextStyleContext( GetImport(),
823  *this, nFamily,
824  true );
825  break;
826  case XmlStyleFamily::SD_GRAPHICS_ID:
827  // There are no writer specific defaults for graphic styles!
828  pStyle = new XMLGraphicsDefaultStyle( GetImport(), *this );
829  break;
830  default:
832  nElement,
833  xAttrList );
834  break;
835  }
836 
837  return pStyle;
838 }
839 
840 SwXMLStylesContext_Impl::SwXMLStylesContext_Impl(
841  SwXMLImport& rImport,
842  bool bAuto ) :
843  SvXMLStylesContext( rImport, bAuto )
844 {
845 }
846 
847 bool SwXMLStylesContext_Impl::InsertStyleFamily( XmlStyleFamily nFamily ) const
848 {
849  const SwXMLImport& rSwImport = GetSwImport();
850  const SfxStyleFamily nStyleFamilyMask = rSwImport.GetStyleFamilyMask();
851 
852  bool bIns = true;
853  switch( nFamily )
854  {
855  case XmlStyleFamily::TEXT_PARAGRAPH:
856  bIns = bool(nStyleFamilyMask & SfxStyleFamily::Para);
857  break;
858  case XmlStyleFamily::TEXT_TEXT:
859  bIns = bool(nStyleFamilyMask & SfxStyleFamily::Char);
860  break;
861  case XmlStyleFamily::SD_GRAPHICS_ID:
862  bIns = bool(nStyleFamilyMask & SfxStyleFamily::Frame);
863  break;
864  case XmlStyleFamily::TEXT_LIST:
865  bIns = bool(nStyleFamilyMask & SfxStyleFamily::Pseudo);
866  break;
867  case XmlStyleFamily::TEXT_OUTLINE:
868  case XmlStyleFamily::TEXT_FOOTNOTECONFIG:
869  case XmlStyleFamily::TEXT_ENDNOTECONFIG:
870  case XmlStyleFamily::TEXT_LINENUMBERINGCONFIG:
871  case XmlStyleFamily::TEXT_BIBLIOGRAPHYCONFIG:
872  bIns = !(rSwImport.IsInsertMode() || rSwImport.IsStylesOnlyMode() ||
873  rSwImport.IsBlockMode());
874  break;
875  default:
876  bIns = SvXMLStylesContext::InsertStyleFamily( nFamily );
877  break;
878  }
879 
880  return bIns;
881 }
882 
883 rtl::Reference < SvXMLImportPropertyMapper > SwXMLStylesContext_Impl::GetImportPropertyMapper(
884  XmlStyleFamily nFamily ) const
885 {
887  if( nFamily == XmlStyleFamily::TABLE_TABLE )
889  const_cast<SwXMLStylesContext_Impl*>( this )->GetImport() );
890  else if( nFamily == XmlStyleFamily::TABLE_ROW )
892  const_cast<SwXMLStylesContext_Impl*>( this )->GetImport() );
893  else if( nFamily == XmlStyleFamily::TABLE_CELL )
895  const_cast<SwXMLStylesContext_Impl*>( this )->GetImport() );
896  else if (nFamily == XmlStyleFamily::SD_DRAWINGPAGE_ID)
897  {
899  const_cast<SwXMLStylesContext_Impl*>(this)->GetImport());
900  }
901  else
902  xMapper = SvXMLStylesContext::GetImportPropertyMapper( nFamily );
903  return xMapper;
904 }
905 
906 uno::Reference < container::XNameContainer > SwXMLStylesContext_Impl::GetStylesContainer(
907  XmlStyleFamily nFamily ) const
908 {
909  uno::Reference < container::XNameContainer > xStyles;
910  if( XmlStyleFamily::SD_GRAPHICS_ID == nFamily )
911  xStyles = const_cast<SvXMLImport *>(&GetImport())->GetTextImport()->GetFrameStyles();
912  else if( XmlStyleFamily::TABLE_CELL == nFamily )
913  xStyles = const_cast<SvXMLImport *>(&GetImport())->GetTextImport()->GetCellStyles();
914 
915  if (!xStyles.is())
916  xStyles = SvXMLStylesContext::GetStylesContainer( nFamily );
917 
918  return xStyles;
919 }
920 
921 OUString SwXMLStylesContext_Impl::GetServiceName( XmlStyleFamily nFamily ) const
922 {
923  if( XmlStyleFamily::SD_GRAPHICS_ID == nFamily )
924  return "com.sun.star.style.FrameStyle";
925  else if( XmlStyleFamily::TABLE_CELL == nFamily )
926  return "com.sun.star.style.CellStyle";
927 
928  return SvXMLStylesContext::GetServiceName( nFamily );
929 }
930 
931 void SwXMLStylesContext_Impl::endFastElement(sal_Int32 )
932 {
933  GetSwImport().InsertStyles( IsAutomaticStyle() );
934 }
935 
936 namespace {
937 
938 class SwXMLMasterStylesContext_Impl : public XMLTextMasterStylesContext
939 {
940 protected:
941  virtual bool InsertStyleFamily( XmlStyleFamily nFamily ) const override;
942 
943  SwXMLImport& GetSwImport() { return static_cast<SwXMLImport&>(GetImport()); }
944  const SwXMLImport& GetSwImport() const
945  { return static_cast<const SwXMLImport&>(GetImport()); }
946 
947 public:
948 
949 
950  SwXMLMasterStylesContext_Impl( SwXMLImport& rImport );
951 
952  virtual void SAL_CALL endFastElement(sal_Int32 nElement) override;
953 };
954 
955 }
956 
957 SwXMLMasterStylesContext_Impl::SwXMLMasterStylesContext_Impl(
958  SwXMLImport& rImport ) :
959  XMLTextMasterStylesContext( rImport )
960 {
961 }
962 
963 bool SwXMLMasterStylesContext_Impl::InsertStyleFamily( XmlStyleFamily nFamily ) const
964 {
965  bool bIns;
966 
967  const SwXMLImport& rSwImport = GetSwImport();
968  const SfxStyleFamily nStyleFamilyMask = rSwImport.GetStyleFamilyMask();
969  if( XmlStyleFamily::MASTER_PAGE == nFamily )
970  bIns = bool(nStyleFamilyMask & SfxStyleFamily::Page);
971  else
973 
974  return bIns;
975 }
976 
977 void SwXMLMasterStylesContext_Impl::endFastElement(sal_Int32 )
978 {
979  FinishStyles( !GetSwImport().IsInsertMode() );
980  GetSwImport().FinishStyles();
981 }
982 
984  bool bAuto )
985 {
986  SvXMLStylesContext *pContext = new SwXMLStylesContext_Impl( *this, bAuto );
987  if( bAuto )
988  SetAutoStyles( pContext );
989  else
990  SetStyles( pContext );
991 
992  return pContext;
993 }
994 
996 {
997  SvXMLStylesContext *pContext =
998  new SwXMLMasterStylesContext_Impl( *this );
999  SetMasterStyles( pContext );
1000 
1001  return pContext;
1002 }
1003 
1004 void SwXMLImport::InsertStyles( bool bAuto )
1005 {
1006  if( bAuto && GetAutoStyles() )
1008  if( !bAuto && GetStyles() )
1009  GetStyles()->CopyStylesToDoc( !IsInsertMode(), false );
1010 }
1011 
1013 {
1014  if( GetStyles() )
1016 }
1017 
1019 {
1020  if( !pDoc )
1021  pDoc = SwImport::GetDocFromXMLImport( *this );
1022 
1023  const SwTextFormatColls& rColls = *pDoc->GetTextFormatColls();
1024  const size_t nCount = rColls.size();
1025  for( size_t i=0; i < nCount; ++i )
1026  {
1027  SwTextFormatColl *pColl = rColls[i];
1028  if( pColl && RES_CONDTXTFMTCOLL == pColl->Which() )
1029  {
1030  const SwFormatCollConditions& rConditions =
1031  static_cast<const SwConditionTextFormatColl *>(pColl)->GetCondColls();
1032  bool bSendModify = false;
1033  for( size_t j=0; j < rConditions.size() && !bSendModify; ++j )
1034  {
1035  const SwCollCondition& rCond = *rConditions[j];
1036  switch( rCond.GetCondition() )
1037  {
1042  bSendModify = true;
1043  break;
1044  default: break;
1045  }
1046  }
1047  if( bSendModify )
1048  {
1049  SwCondCollCondChg aMsg( pColl );
1050  pColl->ModifyNotification( &aMsg, &aMsg );
1051  }
1052  }
1053  }
1054 }
1055 
1057  XmlStyleFamily nFamily,
1058  const OUString& rName,
1059  const SfxItemSet **ppItemSet ) const
1060 {
1061  SwXMLItemSetStyleContext_Impl *pStyle = nullptr;
1062  if( GetAutoStyles() )
1063  {
1064  pStyle = const_cast<SwXMLItemSetStyleContext_Impl*>(dynamic_cast< const SwXMLItemSetStyleContext_Impl* >(
1065  GetAutoStyles()->
1066  FindStyleChildContext( nFamily, rName,
1067  true ) ) );
1068  if( pStyle )
1069  {
1070  if( ppItemSet )
1071  {
1072  if( XmlStyleFamily::TABLE_TABLE == pStyle->GetFamily() &&
1073  pStyle->HasMasterPageName() &&
1074  !pStyle->IsPageDescConnected() )
1075  pStyle->ConnectPageDesc();
1076  (*ppItemSet) = pStyle->GetItemSet();
1077 
1078  // resolve data style name late
1079  if( XmlStyleFamily::TABLE_CELL == pStyle->GetFamily() &&
1080  pStyle->ResolveDataStyleName() )
1081  {
1082  (*ppItemSet) = pStyle->GetItemSet();
1083  }
1084 
1085  }
1086  }
1087  }
1088 
1089  return pStyle != nullptr;
1090 }
1091 
1092 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
#define UNO_NAME_PARA_STYLE_CONDITIONS
Definition: unoprnms.hxx:775
virtual SvXMLStyleContext * CreateStyleChildContext(sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList > &xAttrList)
bool IsXMLToken(const OUString &rString, enum XMLTokenEnum eToken)
constexpr TypedWhichId< SwFormatPageDesc > RES_PAGEDESC(93)
sal_Int32 nIndex
Represents the style of a paragraph.
Definition: fmtcol.hxx:55
void SetStyles(SvXMLStylesContext *pStyles)
Pagedescriptor Client of SwPageDesc that is "described" by the attribute.
Definition: fmtpdsc.hxx:35
bool FindAutomaticStyle(XmlStyleFamily nFamily, const OUString &rName, const SfxItemSet **ppItemSet) const
Definition: xmlfmt.cxx:1056
void CopyAutoStylesToDoc()
void FinishStyles()
Definition: xmlfmt.cxx:1012
static void FillUIName(const OUString &rName, OUString &rFillName, SwGetPoolIdFromName)
void Create(SwFormatVertOrient &rItem, SvStream &rStrm, sal_uInt16 nVersionAbusedAsSize)
Definition: legacyitem.cxx:32
SvXMLImportContext * CreateMasterStylesContext()
Definition: xmlfmt.cxx:995
SvXMLImport & GetImport()
bool IsBlockMode() const
Definition: xmlimp.hxx:145
Definition: doc.hxx:184
Master_CollCondition GetCondition() const
Definition: fmtcol.hxx:192
Master_CollCondition
Conditional styles.
Definition: fmtcol.hxx:157
virtual void SetAttribute(sal_uInt16 nPrefixKey, const OUString &rLocalName, const OUString &rValue) override
sal_Int32 mnIndex
FastAttributeList & castToFastAttributeList(const css::uno::Reference< css::xml::sax::XFastAttributeList > &xAttrList)
XML_TABLE_COLUMN_PROPERTIES
static const CommandStruct * GetCmds()
Definition: ccoll.hxx:58
XmlStyleFamily
sal_uInt16 sal_Unicode
bool IsStylesOnlyMode() const
Definition: xmlimp.hxx:144
XML_TABLE_CELL_PROPERTIES
enumrange< T >::Iterator begin(enumrange< T >)
const BorderLinePrimitive2D *pCandidateB assert(pCandidateA)
IDocumentStylePoolAccess const & getIDocumentStylePoolAccess() const
Definition: doc.cxx:426
int nCount
virtual css::uno::Reference< css::style::XStyle > Create()
const sal_uInt8 MAXLEVEL
Definition: swtypes.hxx:95
static SW_DLLPUBLIC sal_uInt16 GetPoolIdFromUIName(const OUString &rName, SwGetPoolIdFromName)
virtual bool InsertStyleFamily(XmlStyleFamily nFamily) const override
SfxStyleFamily
static OUString getPrefixAndNameFromToken(sal_Int32 nToken)
std::vector< rtl::Reference< SwXMLConditionContext_Impl > > SwXMLConditions_Impl
Definition: xmlfmt.cxx:255
const char * sName
static SvXMLImportPropertyMapper * CreateTableDefaultExtPropMapper(SvXMLImport &)
SvXMLStylesContext * GetStyles()
virtual void ModifyNotification(const SfxPoolItem *pOldValue, const SfxPoolItem *pNewValue)
Definition: calbck.hxx:156
virtual SwPageDesc * GetPageDescFromPool(sal_uInt16 nId, bool bRegardLanguage=true)=0
Return required automatic page style.
SwPageDesc * FindPageDesc(const OUString &rName, size_t *pPos=nullptr) const
Definition: docdesc.cxx:825
sal_uInt16 const aTableSetRange[]
Definition: init.cxx:191
virtual css::uno::Reference< css::xml::sax::XFastContextHandler > SAL_CALL createFastChildContext(sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList > &AttrList) override
virtual rtl::Reference< SvXMLImportPropertyMapper > GetImportPropertyMapper(XmlStyleFamily nFamily) const
#define TOOLS_WARN_EXCEPTION(area, stream)
int i
virtual void FillPropertySet(const css::uno::Reference< css::beans::XPropertySet > &rPropSet)
OUString GetCommandContextByIndex(sal_Int16 nIndex)
Definition: ccoll.cxx:73
virtual void Finish(bool bOverwrite) override
void CopyStylesToDoc(bool bOverwrite, bool bFinish=true)
virtual SvXMLStyleContext * CreateDefaultStyleStyleChildContext(XmlStyleFamily nFamily, sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList > &xAttrList)
void SetMasterStyles(SvXMLStylesContext *pMasterStyles)
void UpdateTextCollConditions(SwDoc *pDoc)
Definition: xmlfmt.cxx:1018
const SfxItemSet * GetItemSet(const SfxPoolItem &rAttr)
Returns the item set associated with a character/inet/auto style.
Definition: atrstck.cxx:133
size_t size() const
Definition: docary.hxx:84
XML_PARAGRAPH_PROPERTIES
#define COND_COMMAND_COUNT
Definition: ccoll.hxx:29
sal_uInt16 const aTableBoxSetRange[]
Definition: init.cxx:219
XML_TABLE_ROW_PROPERTIES
void InsertStyles(bool bAuto)
Definition: xmlfmt.cxx:1004
virtual bool InsertStyleFamily(XmlStyleFamily nFamily) const
std::vector< std::unique_ptr< SwCollCondition >> SwFormatCollConditions
Definition: fmtcol.hxx:200
enumrange< T >::Iterator end(enumrange< T >)
sal_uInt16 const aTableLineSetRange[]
Definition: init.cxx:207
virtual OUString GetServiceName(XmlStyleFamily nFamily) const
static SvXMLImportPropertyMapper * CreateTableCellExtPropMapper(SvXMLImport &)
SfxStyleFamily GetStyleFamilyMask() const
Definition: xmlimp.hxx:142
sal_uInt16 Which() const
for Querying of Writer-functions.
Definition: format.hxx:78
virtual void CreateAndInsert(bool bOverwrite)
virtual void SAL_CALL endFastElement(sal_Int32 Element) override
const SwTextFormatColls * GetTextFormatColls() const
Definition: doc.hxx:775
const OUString & GetXMLToken(enum XMLTokenEnum eToken)
const o3tl::enumarray< SvxAdjust, unsigned short > aSvxToUnoAdjust USHRT_MAX
Definition: unosett.cxx:254
virtual css::uno::Reference< XFastContextHandler > SAL_CALL createFastChildContext(sal_Int32 Element, const css::uno::Reference< css::xml::sax::XFastAttributeList > &Attribs) override
#define XML_ELEMENT(prefix, name)
virtual SvXMLStyleContext * CreateStyleStyleChildContext(XmlStyleFamily nFamily, sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList > &xAttrList)
constexpr TypedWhichId< SwConditionTextFormatColl > RES_CONDTXTFMTCOLL(158)
Sequence< sal_Int8 > aSeq
double getLength(const B2DPolygon &rCandidate)
static SvXMLImportPropertyMapper * CreateDrawingPageExtPropMapper(SvXMLImport &)
void SetAutoStyles(SvXMLStylesContext *pAutoStyles)
virtual void SetAttribute(sal_uInt16 nPrefixKey, const OUString &rLocalName, const OUString &rValue)
SvXMLImportContext * CreateStylesContext(bool bAuto)
Definition: xmlfmt.cxx:983
#define SAL_WARN(area, stream)
Reference< XSingleServiceFactory > xFactory
XMLPropStyleContext(SvXMLImport &rImport, SvXMLStylesContext &rStyles, XmlStyleFamily nFamily, bool bDefaultStyle=false)
sal_Int32 nLength
virtual css::uno::Reference< css::container::XNameContainer > GetStylesContainer(XmlStyleFamily nFamily) const
SwDoc * GetDocFromXMLImport(SvXMLImport const &)
Definition: xmlimp.cxx:1721
static SvXMLImportPropertyMapper * CreateTableRowDefaultExtPropMapper(SvXMLImport &)
SvXMLStylesContext * GetAutoStyles()
void FinishStyles(bool bOverwrite)
sal_uInt16 nPos
const SwAttrPool & GetAttrPool() const
Definition: doc.hxx:1315
bool IsInsertMode() const
Definition: xmlimp.hxx:143
css::uno::Any SAL_CALL makeAny(const SharedUNOComponent< INTERFACE, COMPONENT > &value)