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