LibreOffice Module xmloff (master)  1
txtlists.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 
21 #include <txtlists.hxx>
22 #include <comphelper/random.hxx>
23 
24 #include <o3tl/safeint.hxx>
25 #include <tools/datetime.hxx>
26 #include <tools/long.hxx>
27 #include <sal/log.hxx>
28 
29 #include <xmloff/txtimp.hxx>
30 #include <xmloff/xmlimp.hxx>
31 #include <xmloff/xmlnumi.hxx>
32 
33 #include <com/sun/star/style/XStyle.hpp>
34 #include <com/sun/star/beans/XPropertySet.hpp>
35 #include <com/sun/star/container/XNameContainer.hpp>
38 #include "txtparai.hxx"
39 
40 
41 using namespace ::com::sun::star;
42 
43 
45  // Inconsistent behavior regarding lists (#i92811#)
46 {
47 }
48 
50  XMLTextListBlockContext *i_pListBlock)
51 {
52  mListStack.emplace(i_pListBlock,
53  static_cast<XMLTextListItemContext*>(nullptr),
54  static_cast<XMLNumberedParaContext*>(nullptr));
55 }
56 
58  XMLNumberedParaContext *i_pNumberedParagraph)
59 {
60  mListStack.emplace(
61  static_cast<XMLTextListBlockContext*>(nullptr),
62  static_cast<XMLTextListItemContext*>(nullptr), i_pNumberedParagraph);
63 }
64 
66 {
67  assert(mListStack.size());
68  if ( !mListStack.empty())
69  mListStack.pop();
70 }
71 
73  XMLTextListBlockContext*& o_pListBlockContext,
74  XMLTextListItemContext*& o_pListItemContext,
75  XMLNumberedParaContext*& o_pNumberedParagraphContext )
76 {
77  if ( !mListStack.empty() ) {
78  o_pListBlockContext =
79  static_cast<XMLTextListBlockContext*>(std::get<0>(mListStack.top()).get());
80  o_pListItemContext =
81  static_cast<XMLTextListItemContext *>(std::get<1>(mListStack.top()).get());
82  o_pNumberedParagraphContext =
83  static_cast<XMLNumberedParaContext *>(std::get<2>(mListStack.top()).get());
84  }
85 }
86 
88 {
89  // may be cleared by ListBlockContext for upper list...
90  if (i_pListItem) {
91  assert(mListStack.size());
92  assert(std::get<0>(mListStack.top()).is() &&
93  "internal error: SetListItem: mListStack has no ListBlock");
94  assert(!std::get<1>(mListStack.top()).is() &&
95  "error: SetListItem: list item already exists");
96  }
97  if ( !mListStack.empty() ) {
98  std::get<1>(mListStack.top()) = i_pListItem;
99  }
100 }
101 
102 // Handling for parameter <sListStyleDefaultListId> (#i92811#)
103 void XMLTextListsHelper::KeepListAsProcessed( const OUString& sListId,
104  const OUString& sListStyleName,
105  const OUString& sContinueListId,
106  const OUString& sListStyleDefaultListId )
107 {
108  if ( IsListProcessed( sListId ) )
109  {
110  assert(false &&
111  "<XMLTextListsHelper::KeepListAsProcessed(..)> - list id already added" );
112  return;
113  }
114 
115  if ( !mpProcessedLists )
116  {
117  mpProcessedLists = std::make_unique<tMapForLists>();
118  }
119 
120  ::std::pair< OUString, OUString >
121  aListData( sListStyleName, sContinueListId );
122  (*mpProcessedLists)[ sListId ] = aListData;
123 
124  msLastProcessedListId = sListId;
125  msListStyleOfLastProcessedList = sListStyleName;
126 
127  // Inconsistent behavior regarding lists (#i92811#)
128  if ( sListStyleDefaultListId.isEmpty())
129  return;
130 
132  {
133  mpMapListIdToListStyleDefaultListId = std::make_unique<tMapForLists>();
134  }
135 
136  if ( mpMapListIdToListStyleDefaultListId->find( sListStyleName ) ==
138  {
139  ::std::pair< OUString, OUString >
140  aListIdMapData( sListId, sListStyleDefaultListId );
141  (*mpMapListIdToListStyleDefaultListId)[ sListStyleName ] =
142  aListIdMapData;
143  }
144 }
145 
146 bool XMLTextListsHelper::IsListProcessed( const OUString& sListId ) const
147 {
148  if ( !mpProcessedLists )
149  {
150  return false;
151  }
152 
153  return mpProcessedLists->find( sListId ) != mpProcessedLists->end();
154 }
155 
157  const OUString& sListId ) const
158 {
159  if ( mpProcessedLists )
160  {
161  tMapForLists::const_iterator aIter = mpProcessedLists->find( sListId );
162  if ( aIter != mpProcessedLists->end() )
163  {
164  return (*aIter).second.first;
165  }
166  }
167 
168  return OUString();
169 }
170 
172  const OUString& sListId ) const
173 {
174  if ( mpProcessedLists )
175  {
176  tMapForLists::const_iterator aIter = mpProcessedLists->find( sListId );
177  if ( aIter != mpProcessedLists->end() )
178  {
179  return (*aIter).second.second;
180  }
181  }
182 
183  return OUString();
184 }
185 
186 
188 {
189  static bool bHack = (getenv("LIBO_ONEWAY_STABLE_ODF_EXPORT") != nullptr);
190  OUString sTmpStr( "list" );
191 
192  if (bHack)
193  {
194  static sal_Int64 nIdCounter = SAL_CONST_INT64(5000000000);
195  sTmpStr += OUString::number(nIdCounter++);
196  }
197  else
198  {
199  // Value of xml:id in element <text:list> has to be a valid ID type (#i92478#)
200  DateTime aDateTime( DateTime::SYSTEM );
201  sal_Int64 n = aDateTime.GetTime();
202  n += aDateTime.GetDateUnsigned();
203  n += comphelper::rng::uniform_int_distribution(0, std::numeric_limits<int>::max());
204  // Value of xml:id in element <text:list> has to be a valid ID type (#i92478#)
205  sTmpStr += OUString::number( n );
206  }
207 
208  OUString sNewListId( sTmpStr );
209  if ( mpProcessedLists )
210  {
211  tools::Long nHitCount = 0;
212  while ( mpProcessedLists->find( sNewListId ) != mpProcessedLists->end() )
213  {
214  ++nHitCount;
215  sNewListId = sTmpStr + OUString::number( nHitCount );
216  }
217  }
218 
219  return sNewListId;
220 }
221 
222 // Provide list id for a certain list block for import (#i92811#)
224 {
225  OUString sListBlockListId( rListBlock.GetContinueListId() );
226  if ( sListBlockListId.isEmpty() )
227  {
228  sListBlockListId = rListBlock.GetListId();
229  }
230 
232  {
233  if ( !sListBlockListId.isEmpty() )
234  {
235  const OUString sListStyleName =
236  GetListStyleOfProcessedList( sListBlockListId );
237 
238  tMapForLists::const_iterator aIter =
239  mpMapListIdToListStyleDefaultListId->find( sListStyleName );
240  if ( aIter != mpMapListIdToListStyleDefaultListId->end() )
241  {
242  if ( (*aIter).second.first == sListBlockListId )
243  {
244  sListBlockListId = (*aIter).second.second;
245  }
246  }
247  }
248  }
249 
250  return sListBlockListId;
251 }
252 
253 void XMLTextListsHelper::StoreLastContinuingList( const OUString& sListId,
254  const OUString& sContinuingListId )
255 {
256  if ( !mpContinuingLists )
257  {
258  mpContinuingLists = std::make_unique<tMapForContinuingLists>();
259  }
260 
261  (*mpContinuingLists)[ sListId ] = sContinuingListId;
262 }
263 
265  const OUString& sListId ) const
266 {
267  if ( mpContinuingLists )
268  {
269  tMapForContinuingLists::const_iterator aIter =
270  mpContinuingLists->find( sListId );
271  if ( aIter != mpContinuingLists->end() )
272  {
273  return (*aIter).second;
274  }
275  }
276 
277  return sListId;
278 }
279 
280 void XMLTextListsHelper::PushListOnStack( const OUString& sListId,
281  const OUString& sListStyleName )
282 {
283  if ( !mpListStack )
284  {
285  mpListStack = std::make_unique<tStackForLists>();
286  }
287  ::std::pair< OUString, OUString >
288  aListData( sListId, sListStyleName );
289  mpListStack->push_back( aListData );
290 }
292 {
293  if ( mpListStack &&
294  !mpListStack->empty() )
295  {
296  mpListStack->pop_back();
297  }
298 }
299 
300 bool XMLTextListsHelper::EqualsToTopListStyleOnStack( std::u16string_view sListId ) const
301 {
302  return mpListStack && sListId == mpListStack->back().second;
303 }
304 
305 OUString
307  const sal_uInt16 i_Level,
308  std::u16string_view i_StyleName)
309 {
310  if (i_StyleName.empty()) {
311  SAL_INFO("xmloff.text", "invalid numbered-paragraph: no style-name");
312  }
313  if (!i_StyleName.empty()
314  && (i_Level < mLastNumberedParagraphs.size())
315  && (mLastNumberedParagraphs[i_Level].first == i_StyleName) )
316  {
317  assert(!mLastNumberedParagraphs[i_Level].second.isEmpty() &&
318  "internal error: numbered-paragraph style-name but no list-id?");
319  return mLastNumberedParagraphs[i_Level].second;
320  } else {
321  return GenerateNewListId();
322  }
323 }
324 
325 static void
326 ClampLevel(uno::Reference<container::XIndexReplace> const& i_xNumRules,
327  sal_Int16 & io_rLevel)
328 {
329  assert(i_xNumRules.is());
330  if (i_xNumRules.is()) {
331  const sal_Int32 nLevelCount( i_xNumRules->getCount() );
332  if ( io_rLevel >= nLevelCount ) {
333  io_rLevel = sal::static_int_cast< sal_Int16 >(nLevelCount-1);
334  }
335  }
336 }
337 
338 uno::Reference<container::XIndexReplace>
340  SvXMLImport & i_rImport,
341  const OUString& i_ListId,
342  sal_Int16 & io_rLevel, const OUString& i_StyleName)
343 {
344  assert(!i_ListId.isEmpty());
345  assert(io_rLevel >= 0);
346  NumParaList_t & rNPList( mNPLists[i_ListId] );
347  const OUString none; // default
348  if ( rNPList.empty() ) {
349  // create default list style for top level
350  sal_Int16 lev(0);
351  rNPList.emplace_back(none,
352  MakeNumRule(i_rImport, nullptr, none, none, lev) );
353  }
354  // create num rule first because this might clamp the level...
355  uno::Reference<container::XIndexReplace> xNumRules;
356  if ((0 == io_rLevel) || rNPList.empty() || !i_StyleName.isEmpty()) {
357  // no parent to inherit from, or explicit style given => new numrules!
358  // index of parent: level - 1, but maybe that does not exist
359  const size_t parent( std::min(static_cast<size_t>(io_rLevel),
360  rNPList.size()) - 1 );
361  xNumRules = MakeNumRule(i_rImport,
362  io_rLevel > 0 ? rNPList[parent].second : nullptr,
363  io_rLevel > 0 ? rNPList[parent].first : none,
364  i_StyleName, io_rLevel);
365  } else {
366  // no style given, but has a parent => reuse parent numrules!
367  ClampLevel(rNPList.back().second, io_rLevel);
368  }
369  if (static_cast<sal_uInt16>(io_rLevel) + 1U > rNPList.size()) {
370  // new level: need to enlarge
371  for (size_t i = rNPList.size();
372  i < o3tl::make_unsigned(io_rLevel); ++i)
373  {
374  NumParaList_t::value_type const rule(rNPList.back());
375  rNPList.push_back(rule);
376  }
377  NumParaList_t::value_type const rule(rNPList.back());
378  rNPList.push_back(xNumRules.is()
379  ? ::std::make_pair(i_StyleName, xNumRules)
380  : rule);
381  } else {
382  // old level: no need to enlarge; possibly shrink
383  if (xNumRules.is()) {
384  rNPList[io_rLevel] = std::make_pair(i_StyleName, xNumRules);
385  }
386  if (static_cast<sal_uInt16>(io_rLevel) + 1U < rNPList.size()) {
387  rNPList.erase(rNPList.begin() + io_rLevel + 1, rNPList.end());
388  }
389  }
390  // remember the list id
391  if (mLastNumberedParagraphs.size() <= o3tl::make_unsigned(io_rLevel)) {
392  mLastNumberedParagraphs.resize(io_rLevel+1);
393  }
394  mLastNumberedParagraphs[io_rLevel] = std::make_pair(i_StyleName, i_ListId);
395  return rNPList.back().second;
396 }
397 
399 uno::Reference<container::XIndexReplace>
401  SvXMLImport & i_rImport,
402  const uno::Reference<container::XIndexReplace>& i_rNumRule,
403  std::u16string_view i_ParentStyleName,
404  const OUString& i_StyleName,
405  sal_Int16 & io_rLevel,
406  bool* o_pRestartNumbering,
407  bool* io_pSetDefaults)
408 {
409  uno::Reference<container::XIndexReplace> xNumRules(i_rNumRule);
410  if ( !i_StyleName.isEmpty() && i_StyleName != i_ParentStyleName )
411  {
412  const OUString sDisplayStyleName(
414  i_StyleName) );
415  const uno::Reference < container::XNameContainer >& rNumStyles(
416  i_rImport.GetTextImport()->GetNumberingStyles() );
417  if( rNumStyles.is() && rNumStyles->hasByName( sDisplayStyleName ) )
418  {
419  uno::Reference < style::XStyle > xStyle;
420  uno::Any any = rNumStyles->getByName( sDisplayStyleName );
421  any >>= xStyle;
422 
423  uno::Reference< beans::XPropertySet > xPropSet( xStyle,
424  uno::UNO_QUERY );
425  any = xPropSet->getPropertyValue("NumberingRules");
426  any >>= xNumRules;
427  }
428  else
429  {
430  const SvxXMLListStyleContext *pListStyle(
431  i_rImport.GetTextImport()->FindAutoListStyle( i_StyleName ) );
432  if( pListStyle )
433  {
434  xNumRules = pListStyle->GetNumRules();
435  if( !xNumRules.is() )
436  {
437  pListStyle->CreateAndInsertAuto();
438  xNumRules = pListStyle->GetNumRules();
439  }
440  }
441  }
442  }
443 
444  bool bSetDefaults(io_pSetDefaults && *io_pSetDefaults);
445  if ( !xNumRules.is() )
446  {
447  // If no style name has been specified for this style and for any
448  // parent or if no num rule with the specified name exists,
449  // create a new one.
450 
451  xNumRules =
453  SAL_INFO_IF(xNumRules.is(), "xmloff.core", "cannot create numrules");
454  if ( !xNumRules.is() )
455  return xNumRules;
456 
457  // Because it is a new num rule, numbering must not be restarted.
458  if (o_pRestartNumbering) *o_pRestartNumbering = false;
459  bSetDefaults = true;
460  if (io_pSetDefaults) *io_pSetDefaults = bSetDefaults;
461  }
462 
463  ClampLevel(xNumRules, io_rLevel);
464 
465  if ( bSetDefaults )
466  {
467  // Because there is no list style sheet for this style, a default
468  // format must be set for any level of this num rule.
469  SvxXMLListStyleContext::SetDefaultStyle( xNumRules, io_rLevel,
470  false );
471  }
472 
473  return xNumRules;
474 }
475 
476 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
std::unique_ptr< tMapForLists > mpMapListIdToListStyleDefaultListId
Definition: txtlists.hxx:147
std::stack< ListStackFrame_t > mListStack
Definition: txtlists.hxx:132
const OUString & GetContinueListId() const
LastNumberedParagraphs_t mLastNumberedParagraphs
Definition: txtlists.hxx:166
#define SAL_INFO_IF(condition, area, stream)
long Long
bool EqualsToTopListStyleOnStack(std::u16string_view sListId) const
Definition: txtlists.cxx:300
void StoreLastContinuingList(const OUString &sListId, const OUString &sContinuingListId)
Definition: txtlists.cxx:253
sal_Int64 n
OUString GetListStyleOfProcessedList(const OUString &sListId) const
Definition: txtlists.cxx:156
std::unique_ptr< tStackForLists > mpListStack
Definition: txtlists.hxx:159
OUString msListStyleOfLastProcessedList
Definition: txtlists.hxx:141
rtl::Reference< XMLTextImportHelper > const & GetTextImport()
Definition: xmlimp.hxx:600
const OUString & GetListId() const
OUString GetNumberedParagraphListId(const sal_uInt16 i_Level, std::u16string_view i_StyleName)
get ID of the last numbered-paragraph iff it has given style-name
Definition: txtlists.cxx:306
OUString GenerateNewListId() const
Definition: txtlists.cxx:187
OUString GetContinueListIdOfProcessedList(const OUString &sListId) const
Definition: txtlists.cxx:171
void ListContextTop(XMLTextListBlockContext *&o_pListBlockContext, XMLTextListItemContext *&o_pListItemContext, XMLNumberedParaContext *&o_pNumberedParagraphContext)
peek at the top of the list context stack
Definition: txtlists.cxx:72
void KeepListAsProcessed(const OUString &sListId, const OUString &sListStyleName, const OUString &sContinueListId, const OUString &sListStyleDefaultListId=OUString())
Definition: txtlists.cxx:103
static css::uno::Reference< css::container::XIndexReplace > CreateNumRule(const css::uno::Reference< css::frame::XModel > &rModel)
Definition: xmlnumi.cxx:1023
none
void PopListFromStack()
Definition: txtlists.cxx:291
void PushListOnStack(const OUString &sListId, const OUString &sListStyleName)
Definition: txtlists.cxx:280
int i
OUString GetLastContinuingListId(const OUString &sListId) const
Definition: txtlists.cxx:264
const Any & any
constexpr std::enable_if_t< std::is_signed_v< T >, std::make_unsigned_t< T > > make_unsigned(T value)
css::uno::Reference< css::container::XIndexReplace > EnsureNumberedParagraph(SvXMLImport &i_rImport, const OUString &i_ListId, sal_Int16 &io_rLevel, const OUString &i_StyleName)
for importing numbered-paragraph note that the ID namespace for numbered-paragraph and regular list i...
Definition: txtlists.cxx:339
std::unique_ptr< tMapForContinuingLists > mpContinuingLists
Definition: txtlists.hxx:153
void PushListContext(XMLTextListBlockContext *i_pListBlock)
list stack for importing:
Definition: txtlists.cxx:49
::std::map< OUString, NumParaList_t > mNPLists
Definition: txtlists.hxx:173
OUString msLastProcessedListId
Definition: txtlists.hxx:140
OUString GetStyleDisplayName(XmlStyleFamily nFamily, const OUString &rName) const
Definition: xmlimp.cxx:1406
void SetListItem(XMLTextListItemContext *pListItem)
set list item on top of the list context stack
Definition: txtlists.cxx:87
#define SAL_INFO(area, stream)
void PopListContext()
pop the list context stack
Definition: txtlists.cxx:65
int uniform_int_distribution(int a, int b)
const css::uno::Reference< css::container::XIndexReplace > & GetNumRules() const
Definition: xmlnumi.hxx:63
sal_Int64 GetTime() const
const css::uno::Reference< css::frame::XModel > & GetModel() const
Definition: xmlimp.hxx:404
static void ClampLevel(uno::Reference< container::XIndexReplace > const &i_xNumRules, sal_Int16 &io_rLevel)
Definition: txtlists.cxx:326
::std::vector< ::std::pair< OUString, css::uno::Reference< css::container::XIndexReplace > > > NumParaList_t
numbered-paragraphs
Definition: txtlists.hxx:172
static void SetDefaultStyle(const css::uno::Reference< css::container::XIndexReplace > &rNumRule, sal_Int16 nLevel, bool bOrdered)
Definition: xmlnumi.cxx:1043
std::unique_ptr< tMapForLists > mpProcessedLists
Definition: txtlists.hxx:139
constexpr OUStringLiteral first
sal_uInt32 GetDateUnsigned() const
OUString GetListIdForListBlock(XMLTextListBlockContext const &rListBlock)
Definition: txtlists.cxx:223
bool IsListProcessed(const OUString &sListId) const
Definition: txtlists.cxx:146
static css::uno::Reference< css::container::XIndexReplace > MakeNumRule(SvXMLImport &i_rImport, const css::uno::Reference< css::container::XIndexReplace > &i_xNumRule, std::u16string_view i_ParentStyleName, const OUString &i_StyleName, sal_Int16 &io_rLevel, bool *o_pRestartNumbering=nullptr, bool *io_pSetDefaults=nullptr)
Creates a NumRule from given style-name.
Definition: txtlists.cxx:400