1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: txtlists.cxx,v $
10 * $Revision: 1.2.24.4 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #include "precompiled_xmloff.hxx"
33 #include <txtlists.hxx>
35 #include <tools/debug.hxx>
36 #include <tools/date.hxx>
37 #include <tools/time.hxx>
39 #include <xmloff/txtimp.hxx>
40 #include <xmloff/xmlimp.hxx>
41 #include <xmloff/xmlnumi.hxx>
43 #include <com/sun/star/style/XStyle.hpp>
44 #include <com/sun/star/beans/XPropertySet.hpp>
45 #include "XMLTextListItemContext.hxx"
46 #include "XMLTextListBlockContext.hxx"
47 #include "txtparai.hxx"
50 using namespace ::com::sun::star
;
53 XMLTextListsHelper::XMLTextListsHelper()
54 : mpProcessedLists( 0 ),
55 msLastProcessedListId(),
56 msListStyleOfLastProcessedList(),
57 // --> OD 2008-08-15 #i92811#
58 mpMapListIdToListStyleDefaultListId( 0 ),
60 mpContinuingLists( 0 ),
65 XMLTextListsHelper::~XMLTextListsHelper()
67 if ( mpProcessedLists
)
69 mpProcessedLists
->clear();
70 delete mpProcessedLists
;
72 // --> OD 2008-08-15 #i92811#
73 if ( mpMapListIdToListStyleDefaultListId
)
75 mpMapListIdToListStyleDefaultListId
->clear();
76 delete mpMapListIdToListStyleDefaultListId
;
79 if ( mpContinuingLists
)
81 mpContinuingLists
->clear();
82 delete mpContinuingLists
;
91 void XMLTextListsHelper::PushListContext(
92 XMLTextListBlockContext
*i_pListBlock
)
94 // fprintf(stderr, "PushListContext\n");
95 mListStack
.push(::boost::make_tuple(i_pListBlock
,
96 static_cast<XMLTextListItemContext
*>(0),
97 static_cast<XMLNumberedParaContext
*>(0)));
100 void XMLTextListsHelper::PushListContext(
101 XMLNumberedParaContext
*i_pNumberedParagraph
)
103 // fprintf(stderr, "PushListContext(NP)\n");
104 mListStack
.push(::boost::make_tuple(
105 static_cast<XMLTextListBlockContext
*>(0),
106 static_cast<XMLTextListItemContext
*>(0), i_pNumberedParagraph
));
109 void XMLTextListsHelper::PopListContext()
111 OSL_ENSURE(mListStack
.size(),
112 "internal error: PopListContext: mListStack empty");
113 // fprintf(stderr, "PopListContext\n");
114 if ( !mListStack
.empty())
118 void XMLTextListsHelper::ListContextTop(
119 XMLTextListBlockContext
*& o_pListBlockContext
,
120 XMLTextListItemContext
*& o_pListItemContext
,
121 XMLNumberedParaContext
*& o_pNumberedParagraphContext
)
123 if ( !mListStack
.empty() ) {
124 o_pListBlockContext
=
125 static_cast<XMLTextListBlockContext
*>(&mListStack
.top().get
<0>());
127 static_cast<XMLTextListItemContext
*>(&mListStack
.top().get
<1>());
128 o_pNumberedParagraphContext
=
129 static_cast<XMLNumberedParaContext
*>(&mListStack
.top().get
<2>());
133 void XMLTextListsHelper::SetListItem( XMLTextListItemContext
*i_pListItem
)
135 // may be cleared by ListBlockContext for upper list...
137 OSL_ENSURE(mListStack
.size(),
138 "internal error: SetListItem: mListStack empty");
139 OSL_ENSURE(mListStack
.top().get
<0>(),
140 "internal error: SetListItem: mListStack has no ListBlock");
141 OSL_ENSURE(!mListStack
.top().get
<1>(),
142 "error: SetListItem: list item already exists");
144 if ( !mListStack
.empty() ) {
145 mListStack
.top().get
<1>() = i_pListItem
;
149 // --> OD 2008-08-15 #i92811# - handling for parameter <sListStyleDefaultListId>
150 void XMLTextListsHelper::KeepListAsProcessed( ::rtl::OUString sListId
,
151 ::rtl::OUString sListStyleName
,
152 ::rtl::OUString sContinueListId
,
153 ::rtl::OUString sListStyleDefaultListId
)
155 if ( IsListProcessed( sListId
) )
158 "<XMLTextListsHelper::KeepListAsProcessed(..)> - list id already added" );
162 if ( mpProcessedLists
== 0 )
164 mpProcessedLists
= new tMapForLists();
167 ::std::pair
< ::rtl::OUString
, ::rtl::OUString
>
168 aListData( sListStyleName
, sContinueListId
);
169 (*mpProcessedLists
)[ sListId
] = aListData
;
171 msLastProcessedListId
= sListId
;
172 msListStyleOfLastProcessedList
= sListStyleName
;
174 // --> OD 2008-08-15 #i92811#
175 if ( sListStyleDefaultListId
.getLength() != 0 )
177 if ( mpMapListIdToListStyleDefaultListId
== 0 )
179 mpMapListIdToListStyleDefaultListId
= new tMapForLists();
182 if ( mpMapListIdToListStyleDefaultListId
->find( sListStyleName
) ==
183 mpMapListIdToListStyleDefaultListId
->end() )
185 ::std::pair
< ::rtl::OUString
, ::rtl::OUString
>
186 aListIdMapData( sListId
, sListStyleDefaultListId
);
187 (*mpMapListIdToListStyleDefaultListId
)[ sListStyleName
] =
194 sal_Bool
XMLTextListsHelper::IsListProcessed( const ::rtl::OUString sListId
) const
196 if ( mpProcessedLists
== 0 )
201 return mpProcessedLists
->find( sListId
) != mpProcessedLists
->end();
204 ::rtl::OUString
XMLTextListsHelper::GetListStyleOfProcessedList(
205 const ::rtl::OUString sListId
) const
207 if ( mpProcessedLists
!= 0 )
209 tMapForLists::const_iterator aIter
= mpProcessedLists
->find( sListId
);
210 if ( aIter
!= mpProcessedLists
->end() )
212 return (*aIter
).second
.first
;
216 return ::rtl::OUString();
219 ::rtl::OUString
XMLTextListsHelper::GetContinueListIdOfProcessedList(
220 const ::rtl::OUString sListId
) const
222 if ( mpProcessedLists
!= 0 )
224 tMapForLists::const_iterator aIter
= mpProcessedLists
->find( sListId
);
225 if ( aIter
!= mpProcessedLists
->end() )
227 return (*aIter
).second
.second
;
231 return ::rtl::OUString();
234 const ::rtl::OUString
& XMLTextListsHelper::GetLastProcessedListId() const
236 return msLastProcessedListId
;
239 const ::rtl::OUString
& XMLTextListsHelper::GetListStyleOfLastProcessedList() const
241 return msListStyleOfLastProcessedList
;
244 ::rtl::OUString
XMLTextListsHelper::GenerateNewListId() const
246 // --> OD 2008-08-06 #i92478#
247 ::rtl::OUString
sTmpStr( ::rtl::OUString::createFromAscii( "list" ) );
249 sal_Int64 n
= Time().GetTime();
250 n
+= Date().GetDate();
252 // --> OD 2008-08-06 #i92478#
253 sTmpStr
+= ::rtl::OUString::valueOf( n
);
257 ::rtl::OUString
sNewListId( sTmpStr
);
258 if ( mpProcessedLists
!= 0 )
260 while ( mpProcessedLists
->find( sNewListId
) != mpProcessedLists
->end() )
263 sNewListId
= sTmpStr
;
264 sNewListId
+= ::rtl::OUString::valueOf( nHitCount
);
271 // --> OD 2008-08-15 #i92811#
272 // provide list id for a certain list block for import
273 ::rtl::OUString
XMLTextListsHelper::GetListIdForListBlock( XMLTextListBlockContext
& rListBlock
)
275 ::rtl::OUString
sListBlockListId( rListBlock
.GetContinueListId() );
276 if ( sListBlockListId
.getLength() == 0 )
278 sListBlockListId
= rListBlock
.GetListId();
281 if ( mpMapListIdToListStyleDefaultListId
!= 0 )
283 if ( sListBlockListId
.getLength() != 0 )
285 const ::rtl::OUString sListStyleName
=
286 GetListStyleOfProcessedList( sListBlockListId
);
288 tMapForLists::const_iterator aIter
=
289 mpMapListIdToListStyleDefaultListId
->find( sListStyleName
);
290 if ( aIter
!= mpMapListIdToListStyleDefaultListId
->end() )
292 if ( (*aIter
).second
.first
== sListBlockListId
)
294 sListBlockListId
= (*aIter
).second
.second
;
300 return sListBlockListId
;
304 void XMLTextListsHelper::StoreLastContinuingList( ::rtl::OUString sListId
,
305 ::rtl::OUString sContinuingListId
)
307 if ( mpContinuingLists
== 0 )
309 mpContinuingLists
= new tMapForContinuingLists();
312 (*mpContinuingLists
)[ sListId
] = sContinuingListId
;
315 ::rtl::OUString
XMLTextListsHelper::GetLastContinuingListId(
316 ::rtl::OUString sListId
) const
318 if ( mpContinuingLists
!= 0)
320 tMapForContinuingLists::const_iterator aIter
=
321 mpContinuingLists
->find( sListId
);
322 if ( aIter
!= mpContinuingLists
->end() )
324 return (*aIter
).second
;
331 void XMLTextListsHelper::PushListOnStack( ::rtl::OUString sListId
,
332 ::rtl::OUString sListStyleName
)
334 if ( mpListStack
== 0 )
336 mpListStack
= new tStackForLists();
338 ::std::pair
< ::rtl::OUString
, ::rtl::OUString
>
339 aListData( sListId
, sListStyleName
);
340 mpListStack
->push_back( aListData
);
342 void XMLTextListsHelper::PopListFromStack()
344 if ( mpListStack
!= 0 &&
345 mpListStack
->size() > 0 )
347 mpListStack
->pop_back();
351 sal_Bool
XMLTextListsHelper::EqualsToTopListStyleOnStack( const ::rtl::OUString sListId
) const
353 return mpListStack
!= 0
354 ? sListId
== mpListStack
->back().second
359 XMLTextListsHelper::GetNumberedParagraphListId(
360 const sal_uInt16 i_Level
,
361 const ::rtl::OUString i_StyleName
)
363 if (!i_StyleName
.getLength()) {
364 OSL_ENSURE(false, "invalid numbered-paragraph: no style-name");
366 if (i_StyleName
.getLength()
367 && (i_Level
< mLastNumberedParagraphs
.size())
368 && (mLastNumberedParagraphs
[i_Level
].first
== i_StyleName
) )
370 OSL_ENSURE(mLastNumberedParagraphs
[i_Level
].second
.getLength(),
371 "internal error: numbered-paragraph style-name but no list-id?");
372 return mLastNumberedParagraphs
[i_Level
].second
;
374 return GenerateNewListId();
379 ClampLevel(uno::Reference
<container::XIndexReplace
> const& i_xNumRules
,
380 sal_Int16
& io_rLevel
)
382 OSL_ENSURE(i_xNumRules
.is(), "internal error: ClampLevel: NumRules null");
383 if (i_xNumRules
.is()) {
384 const sal_Int32
nLevelCount( i_xNumRules
->getCount() );
385 if ( io_rLevel
>= nLevelCount
) {
386 io_rLevel
= sal::static_int_cast
< sal_Int16
>(nLevelCount
-1);
391 uno::Reference
<container::XIndexReplace
>
392 XMLTextListsHelper::EnsureNumberedParagraph(
393 SvXMLImport
& i_rImport
,
394 const ::rtl::OUString i_ListId
,
395 sal_Int16
& io_rLevel
, const ::rtl::OUString i_StyleName
)
397 OSL_ENSURE(i_ListId
.getLength(), "inavlid ListId");
398 OSL_ENSURE(io_rLevel
>= 0, "inavlid Level");
399 NumParaList_t
& rNPList( mNPLists
[i_ListId
] );
400 const ::rtl::OUString none
; // default
401 if ( rNPList
.empty() && (0 != io_rLevel
)) {
402 // create default list style for top level
404 rNPList
.push_back(::std::make_pair(none
,
405 MakeNumRule(i_rImport
, 0, none
, none
, lev
) ));
407 // create num rule first because this might clamp the level...
408 uno::Reference
<container::XIndexReplace
> xNumRules
;
409 if ((0 == io_rLevel
) || rNPList
.empty() || i_StyleName
.getLength()) {
410 // no parent to inherit from, or explicit style given => new numrules!
411 // index of parent: level - 1, but maybe that does not exist
412 const size_t parent( std::min(static_cast<size_t>(io_rLevel
),
413 rNPList
.size()) - 1 );
414 xNumRules
= MakeNumRule(i_rImport
,
415 io_rLevel
> 0 ? rNPList
[parent
].second
: 0,
416 io_rLevel
> 0 ? rNPList
[parent
].first
: none
,
417 i_StyleName
, io_rLevel
);
419 // no style given, but has a parent => reuse parent numrules!
420 ClampLevel(rNPList
.back().second
, io_rLevel
);
422 if (static_cast<sal_uInt16
>(io_rLevel
) + 1U > rNPList
.size()) {
423 // new level: need to enlarge
424 for (size_t i
= rNPList
.size();
425 i
< static_cast<size_t>(io_rLevel
); ++i
) {
426 rNPList
.push_back(rNPList
.back());
428 rNPList
.push_back(xNumRules
.is()
429 ? ::std::make_pair(i_StyleName
, xNumRules
)
432 // old level: no need to enlarge; possibly shrink
433 if (xNumRules
.is()) {
434 rNPList
[io_rLevel
] = std::make_pair(i_StyleName
, xNumRules
);
436 if (static_cast<sal_uInt16
>(io_rLevel
) + 1U < rNPList
.size()) {
437 rNPList
.erase(rNPList
.begin() + io_rLevel
+ 1, rNPList
.end());
440 // remember the list id
441 if (mLastNumberedParagraphs
.size() <= static_cast<size_t>(io_rLevel
)) {
442 mLastNumberedParagraphs
.resize(io_rLevel
+1);
444 mLastNumberedParagraphs
[io_rLevel
] = std::make_pair(i_StyleName
, i_ListId
);
445 return rNPList
.back().second
;
448 /** extracted from the XMLTextListBlockContext constructor */
449 uno::Reference
<container::XIndexReplace
>
450 XMLTextListsHelper::MakeNumRule(
451 SvXMLImport
& i_rImport
,
452 const uno::Reference
<container::XIndexReplace
>& i_rNumRule
,
453 const ::rtl::OUString i_ParentStyleName
,
454 const ::rtl::OUString i_StyleName
,
455 sal_Int16
& io_rLevel
,
456 sal_Bool
* o_pRestartNumbering
,
457 sal_Bool
* io_pSetDefaults
)
459 uno::Reference
<container::XIndexReplace
> xNumRules(i_rNumRule
);
460 if ( i_StyleName
.getLength() &&
461 i_StyleName
!= i_ParentStyleName
)
463 const ::rtl::OUString
sDisplayStyleName(
464 i_rImport
.GetStyleDisplayName( XML_STYLE_FAMILY_TEXT_LIST
,
466 const uno::Reference
< container::XNameContainer
>& rNumStyles(
467 i_rImport
.GetTextImport()->GetNumberingStyles() );
468 if( rNumStyles
.is() && rNumStyles
->hasByName( sDisplayStyleName
) )
470 uno::Reference
< style::XStyle
> xStyle
;
471 uno::Any any
= rNumStyles
->getByName( sDisplayStyleName
);
474 // --> OD 2008-05-07 #refactorlists# - no longer needed
475 // // If the style has not been used, the restart numbering has
476 // // to be set never.
477 // if ( mbRestartNumbering && !xStyle->isInUse() )
479 // mbRestartNumbering = sal_False;
483 uno::Reference
< beans::XPropertySet
> xPropSet( xStyle
,
485 any
= xPropSet
->getPropertyValue(
486 i_rImport
.GetTextImport()->sNumberingRules
);
491 const SvxXMLListStyleContext
*pListStyle(
492 i_rImport
.GetTextImport()->FindAutoListStyle( i_StyleName
) );
495 xNumRules
= pListStyle
->GetNumRules();
496 // --> OD 2008-05-07 #refactorlists# - no longer needed
497 // sal_Bool bUsed = mxNumRules.is();
499 if( !xNumRules
.is() )
501 pListStyle
->CreateAndInsertAuto();
502 xNumRules
= pListStyle
->GetNumRules();
504 // --> OD 2008-05-07 #refactorlists# - no longer needed
505 // if( mbRestartNumbering && !bUsed )
506 // mbRestartNumbering = sal_False;
512 sal_Bool
bSetDefaults(io_pSetDefaults
? *io_pSetDefaults
: sal_False
);
513 if ( !xNumRules
.is() )
515 // If no style name has been specified for this style and for any
516 // parent or if no num rule with the specified name exists,
520 SvxXMLListStyleContext::CreateNumRule( i_rImport
.GetModel() );
521 DBG_ASSERT( xNumRules
.is(), "got no numbering rule" );
522 if ( !xNumRules
.is() )
525 // Because it is a new num rule, numbering must not be restarted.
526 if (o_pRestartNumbering
) *o_pRestartNumbering
= sal_False
;
527 bSetDefaults
= sal_True
;
528 if (io_pSetDefaults
) *io_pSetDefaults
= bSetDefaults
;
531 ClampLevel(xNumRules
, io_rLevel
);
535 // Because there is no list style sheet for this style, a default
536 // format must be set for any level of this num rule.
537 SvxXMLListStyleContext::SetDefaultStyle( xNumRules
, io_rLevel
,