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