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