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