1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <tools/debug.hxx>
21 #include <com/sun/star/container/XNameContainer.hpp>
22 #include <com/sun/star/container/XIndexReplace.hpp>
23 #include <com/sun/star/style/XStyle.hpp>
24 #include <com/sun/star/beans/XPropertySet.hpp>
25 #include <xmloff/xmlimp.hxx>
26 #include <xmloff/xmlnumi.hxx>
27 #include <xmloff/nmspmap.hxx>
28 #include <xmloff/xmlnmspe.hxx>
29 #include <xmloff/xmltoken.hxx>
30 #include "XMLTextListItemContext.hxx"
31 #include "XMLTextListBlockContext.hxx"
32 #include "txtlists.hxx"
35 using namespace ::com::sun::star
;
36 using namespace ::com::sun::star::uno
;
37 using namespace ::com::sun::star::container
;
38 using namespace ::com::sun::star::style
;
39 using namespace ::com::sun::star::beans
;
40 using namespace ::xmloff::token
;
42 TYPEINIT1( XMLTextListBlockContext
, SvXMLImportContext
);
44 // OD 2008-05-07 #refactorlists#
45 // add optional parameter <bRestartNumberingAtSubList> and its handling
46 XMLTextListBlockContext::XMLTextListBlockContext(
48 XMLTextImportHelper
& rTxtImp
,
50 const OUString
& rLName
,
51 const Reference
< xml::sax::XAttributeList
> & xAttrList
,
52 const bool bRestartNumberingAtSubList
)
53 : SvXMLImportContext( rImport
, nPrfx
, rLName
)
54 , mrTxtImport( rTxtImp
)
56 , mxParentListBlock( )
58 , mbRestartNumbering( false )
59 , mbSetDefaults( false )
63 static const char s_PropNameDefaultListId
[] = "DefaultListId";
65 // get the parent list block context (if any); this is a bit ugly...
66 XMLTextListBlockContext
* pLB(0);
67 XMLTextListItemContext
* pLI(0);
68 XMLNumberedParaContext
* pNP(0);
69 rTxtImp
.GetTextListHelper().ListContextTop(pLB
, pLI
, pNP
);
70 mxParentListBlock
= pLB
;
72 // Inherit style name from parent list, as well as the flags whether
73 // numbering must be restarted and formats have to be created.
74 OUString sParentListStyleName
;
75 if( mxParentListBlock
.Is() )
77 XMLTextListBlockContext
*pParent
=
78 static_cast<XMLTextListBlockContext
*>(&mxParentListBlock
);
79 msListStyleName
= pParent
->GetListStyleName();
80 sParentListStyleName
= msListStyleName
;
81 mxNumRules
= pParent
->GetNumRules();
82 mnLevel
= pParent
->GetLevel() + 1;
83 mbRestartNumbering
= pParent
->IsRestartNumbering() ||
84 bRestartNumberingAtSubList
;
85 mbSetDefaults
= pParent
->mbSetDefaults
;
86 msListId
= pParent
->GetListId();
87 msContinueListId
= pParent
->GetContinueListId();
90 const SvXMLTokenMap
& rTokenMap
= mrTxtImport
.GetTextListBlockAttrTokenMap();
92 bool bIsContinueNumberingAttributePresent( false );
93 sal_Int16 nAttrCount
= xAttrList
.is() ? xAttrList
->getLength() : 0;
94 for( sal_Int16 i
=0; i
< nAttrCount
; i
++ )
96 const OUString
& rAttrName
= xAttrList
->getNameByIndex( i
);
97 const OUString
& rValue
= xAttrList
->getValueByIndex( i
);
101 GetImport().GetNamespaceMap().GetKeyByAttrName( rAttrName
,
103 switch( rTokenMap
.Get( nPrefix
, aLocalName
) )
105 case XML_TOK_TEXT_LIST_BLOCK_XMLID
:
107 //FIXME: there is no UNO API for lists
108 // xml:id is also the list ID (#i92221#)
109 if ( mnLevel
== 0 ) // root <list> element
114 case XML_TOK_TEXT_LIST_BLOCK_CONTINUE_NUMBERING
:
115 mbRestartNumbering
= !IsXMLToken(rValue
, XML_TRUE
);
116 bIsContinueNumberingAttributePresent
= true;
118 case XML_TOK_TEXT_LIST_BLOCK_STYLE_NAME
:
119 msListStyleName
= rValue
;
121 case XML_TOK_TEXT_LIST_BLOCK_CONTINUE_LIST
:
122 if ( mnLevel
== 0 ) // root <list> element
124 msContinueListId
= rValue
;
130 mxNumRules
= XMLTextListsHelper::MakeNumRule(GetImport(), mxNumRules
,
131 sParentListStyleName
, msListStyleName
,
132 mnLevel
, &mbRestartNumbering
, &mbSetDefaults
);
133 if( !mxNumRules
.is() )
136 if ( mnLevel
== 0 ) // root <list> element
138 XMLTextListsHelper
& rTextListsHelper( mrTxtImport
.GetTextListHelper() );
139 // Inconsistent behavior regarding lists (#i92811#)
140 OUString sListStyleDefaultListId
;
142 uno::Reference
< beans::XPropertySet
> xNumRuleProps( mxNumRules
, UNO_QUERY
);
143 if ( xNumRuleProps
.is() )
145 uno::Reference
< beans::XPropertySetInfo
> xNumRulePropSetInfo(
146 xNumRuleProps
->getPropertySetInfo());
147 if (xNumRulePropSetInfo
.is() &&
148 xNumRulePropSetInfo
->hasPropertyByName(
149 s_PropNameDefaultListId
))
151 xNumRuleProps
->getPropertyValue(s_PropNameDefaultListId
)
152 >>= sListStyleDefaultListId
;
153 DBG_ASSERT( !sListStyleDefaultListId
.isEmpty(),
154 "no default list id found at numbering rules instance. Serious defect -> please inform OD." );
158 if ( msListId
.isEmpty() ) // no text:id property found
161 sal_Int32
nBuild( 0 );
162 const bool bBuildIdFound
= GetImport().getBuildIds( nUPD
, nBuild
);
163 if ( rImport
.IsTextDocInOOoFileFormat() ||
164 ( bBuildIdFound
&& nUPD
== 680 ) )
166 /* handling former documents written by OpenOffice.org:
167 use default list id of numbering rules instance, if existing
170 if ( !sListStyleDefaultListId
.isEmpty() )
172 msListId
= sListStyleDefaultListId
;
173 if ( !bIsContinueNumberingAttributePresent
&&
174 !mbRestartNumbering
&&
175 rTextListsHelper
.IsListProcessed( msListId
) )
177 mbRestartNumbering
= true;
181 if ( msListId
.isEmpty() )
183 // generate a new list id for the list
184 msListId
= rTextListsHelper
.GenerateNewListId();
188 if ( bIsContinueNumberingAttributePresent
&& !mbRestartNumbering
&&
189 msContinueListId
.isEmpty() )
191 OUString
Last( rTextListsHelper
.GetLastProcessedListId() );
192 if ( rTextListsHelper
.GetListStyleOfLastProcessedList() == msListStyleName
193 && Last
!= msListId
)
195 msContinueListId
= Last
;
199 if ( !msContinueListId
.isEmpty() )
201 if ( !rTextListsHelper
.IsListProcessed( msContinueListId
) )
203 msContinueListId
.clear();
207 // search continue list chain for master list and
208 // continue the master list.
210 rTextListsHelper
.GetContinueListIdOfProcessedList( msContinueListId
);
211 while ( !sTmpStr
.isEmpty() )
213 msContinueListId
= sTmpStr
;
216 rTextListsHelper
.GetContinueListIdOfProcessedList( msContinueListId
);
221 if ( !rTextListsHelper
.IsListProcessed( msListId
) )
223 // Inconsistent behavior regarding lists (#i92811#)
224 rTextListsHelper
.KeepListAsProcessed(
225 msListId
, msListStyleName
, msContinueListId
,
226 sListStyleDefaultListId
);
230 // Remember this list block.
231 mrTxtImport
.GetTextListHelper().PushListContext( this );
234 XMLTextListBlockContext::~XMLTextListBlockContext()
238 void XMLTextListBlockContext::EndElement()
240 // Numbering has not to be restarted if it has been restarted within
242 XMLTextListBlockContext
*pParent
=
243 static_cast<XMLTextListBlockContext
*>(&mxParentListBlock
);
246 pParent
->mbRestartNumbering
= mbRestartNumbering
;
249 // Restore current list block.
250 mrTxtImport
.GetTextListHelper().PopListContext();
252 // Any paragraph following the list within the same list item must not
254 mrTxtImport
.GetTextListHelper().SetListItem( 0 );
257 SvXMLImportContext
*XMLTextListBlockContext::CreateChildContext(
259 const OUString
& rLocalName
,
260 const Reference
< xml::sax::XAttributeList
> & xAttrList
)
262 SvXMLImportContext
*pContext
= 0;
264 const SvXMLTokenMap
& rTokenMap
=
265 mrTxtImport
.GetTextListBlockElemTokenMap();
266 bool bHeader
= false;
267 switch( rTokenMap
.Get( nPrefix
, rLocalName
) )
269 case XML_TOK_TEXT_LIST_HEADER
:
272 case XML_TOK_TEXT_LIST_ITEM
:
273 pContext
= new XMLTextListItemContext( GetImport(), mrTxtImport
,
275 xAttrList
, bHeader
);
280 pContext
= new SvXMLImportContext( GetImport(), nPrefix
, rLocalName
);
287 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */