1*63bba73cSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*63bba73cSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*63bba73cSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*63bba73cSAndrew Rist  * distributed with this work for additional information
6*63bba73cSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*63bba73cSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*63bba73cSAndrew Rist  * "License"); you may not use this file except in compliance
9*63bba73cSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*63bba73cSAndrew Rist  *
11*63bba73cSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*63bba73cSAndrew Rist  *
13*63bba73cSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*63bba73cSAndrew Rist  * software distributed under the License is distributed on an
15*63bba73cSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*63bba73cSAndrew Rist  * KIND, either express or implied.  See the License for the
17*63bba73cSAndrew Rist  * specific language governing permissions and limitations
18*63bba73cSAndrew Rist  * under the License.
19*63bba73cSAndrew Rist  *
20*63bba73cSAndrew Rist  *************************************************************/
21*63bba73cSAndrew Rist 
22*63bba73cSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_xmloff.hxx"
26cdf0e10cSrcweir #include <tools/debug.hxx>
27cdf0e10cSrcweir #include <com/sun/star/container/XNameContainer.hpp>
28cdf0e10cSrcweir #include <com/sun/star/container/XIndexReplace.hpp>
29cdf0e10cSrcweir #include <com/sun/star/style/XStyle.hpp>
30cdf0e10cSrcweir #include <com/sun/star/beans/XPropertySet.hpp>
31cdf0e10cSrcweir #include <xmloff/xmlimp.hxx>
32cdf0e10cSrcweir #include <xmloff/xmlnumi.hxx>
33cdf0e10cSrcweir #include <xmloff/nmspmap.hxx>
34cdf0e10cSrcweir #include "xmloff/xmlnmspe.hxx"
35cdf0e10cSrcweir #include <xmloff/xmltoken.hxx>
36cdf0e10cSrcweir #include "XMLTextListItemContext.hxx"
37cdf0e10cSrcweir #include "XMLTextListBlockContext.hxx"
38cdf0e10cSrcweir #include "txtlists.hxx"
39cdf0e10cSrcweir 
40cdf0e10cSrcweir using ::rtl::OUString;
41cdf0e10cSrcweir using ::rtl::OUStringBuffer;
42cdf0e10cSrcweir 
43cdf0e10cSrcweir using namespace ::com::sun::star;
44cdf0e10cSrcweir using namespace ::com::sun::star::uno;
45cdf0e10cSrcweir using namespace ::com::sun::star::container;
46cdf0e10cSrcweir using namespace ::com::sun::star::style;
47cdf0e10cSrcweir using namespace ::com::sun::star::beans;
48cdf0e10cSrcweir using namespace ::xmloff::token;
49cdf0e10cSrcweir 
50cdf0e10cSrcweir TYPEINIT1( XMLTextListBlockContext, SvXMLImportContext );
51cdf0e10cSrcweir 
52cdf0e10cSrcweir // OD 2008-05-07 #refactorlists#
53cdf0e10cSrcweir // add optional parameter <bRestartNumberingAtSubList> and its handling
XMLTextListBlockContext(SvXMLImport & rImport,XMLTextImportHelper & rTxtImp,sal_uInt16 nPrfx,const OUString & rLName,const Reference<xml::sax::XAttributeList> & xAttrList,const sal_Bool bRestartNumberingAtSubList)54cdf0e10cSrcweir XMLTextListBlockContext::XMLTextListBlockContext(
55cdf0e10cSrcweir 		SvXMLImport& rImport,
56cdf0e10cSrcweir 		XMLTextImportHelper& rTxtImp,
57cdf0e10cSrcweir         sal_uInt16 nPrfx,
58cdf0e10cSrcweir         const OUString& rLName,
59cdf0e10cSrcweir         const Reference< xml::sax::XAttributeList > & xAttrList,
60cdf0e10cSrcweir         const sal_Bool bRestartNumberingAtSubList )
61cdf0e10cSrcweir :	SvXMLImportContext( rImport, nPrfx, rLName )
62cdf0e10cSrcweir ,   mrTxtImport( rTxtImp )
63cdf0e10cSrcweir // --> OD 2008-04-22 #refactorlists#
64cdf0e10cSrcweir ,   msListStyleName()
65cdf0e10cSrcweir // <--
66cdf0e10cSrcweir ,   mxParentListBlock( )
67cdf0e10cSrcweir ,   mnLevel( 0 )
68cdf0e10cSrcweir // --> OD 2008-05-07 #refactorlists#
69cdf0e10cSrcweir //,   mbRestartNumbering( sal_True )
70cdf0e10cSrcweir ,   mbRestartNumbering( sal_False )
71cdf0e10cSrcweir // <--
72cdf0e10cSrcweir ,   mbSetDefaults( sal_False )
73cdf0e10cSrcweir // --> OD 2008-04-22 #refactorlists#
74cdf0e10cSrcweir ,   msListId()
75cdf0e10cSrcweir ,   msContinueListId()
76cdf0e10cSrcweir // <--
77cdf0e10cSrcweir {
78cdf0e10cSrcweir     static ::rtl::OUString s_PropNameDefaultListId(
79cdf0e10cSrcweir         RTL_CONSTASCII_USTRINGPARAM("DefaultListId"));
80cdf0e10cSrcweir     {
81cdf0e10cSrcweir         // get the parent list block context (if any); this is a bit ugly...
82cdf0e10cSrcweir         XMLTextListBlockContext * pLB(0);
83cdf0e10cSrcweir         XMLTextListItemContext  * pLI(0);
84cdf0e10cSrcweir         XMLNumberedParaContext  * pNP(0);
85cdf0e10cSrcweir         rTxtImp.GetTextListHelper().ListContextTop(pLB, pLI, pNP);
86cdf0e10cSrcweir         mxParentListBlock = pLB;
87cdf0e10cSrcweir     }
88cdf0e10cSrcweir 	// Inherit style name from parent list, as well as the flags whether
89cdf0e10cSrcweir 	// numbering must be restarted and formats have to be created.
90cdf0e10cSrcweir     OUString sParentListStyleName;
91cdf0e10cSrcweir     // --> OD 2008-11-27 #158694#
92cdf0e10cSrcweir     sal_Bool bParentRestartNumbering( sal_False );
93cdf0e10cSrcweir     // <--
94cdf0e10cSrcweir     if( mxParentListBlock.Is() )
95cdf0e10cSrcweir 	{
96cdf0e10cSrcweir 		XMLTextListBlockContext *pParent =
97cdf0e10cSrcweir                                 (XMLTextListBlockContext *)&mxParentListBlock;
98cdf0e10cSrcweir         msListStyleName = pParent->GetListStyleName();
99cdf0e10cSrcweir         sParentListStyleName = msListStyleName;
100cdf0e10cSrcweir         mxNumRules = pParent->GetNumRules();
101cdf0e10cSrcweir         mnLevel = pParent->GetLevel() + 1;
102cdf0e10cSrcweir         // --> OD 2008-05-07 #refactorlists#
103cdf0e10cSrcweir //        mbRestartNumbering = pParent->IsRestartNumbering();
104cdf0e10cSrcweir         mbRestartNumbering = pParent->IsRestartNumbering() ||
105cdf0e10cSrcweir                              bRestartNumberingAtSubList;
106cdf0e10cSrcweir         // <--
107cdf0e10cSrcweir         // --> OD 2008-11-27 #158694#
108cdf0e10cSrcweir         bParentRestartNumbering = pParent->IsRestartNumbering();
109cdf0e10cSrcweir         // <--
110cdf0e10cSrcweir         mbSetDefaults = pParent->mbSetDefaults;
111cdf0e10cSrcweir         // --> OD 2008-04-22 #refactorlists#
112cdf0e10cSrcweir         msListId = pParent->GetListId();
113cdf0e10cSrcweir         msContinueListId = pParent->GetContinueListId();
114cdf0e10cSrcweir         // <--
115cdf0e10cSrcweir 	}
116cdf0e10cSrcweir 
117cdf0e10cSrcweir     const SvXMLTokenMap& rTokenMap = mrTxtImport.GetTextListBlockAttrTokenMap();
118cdf0e10cSrcweir 
119cdf0e10cSrcweir     // --> OD 2008-05-07 #refactorlists#
120cdf0e10cSrcweir     bool bIsContinueNumberingAttributePresent( false );
121cdf0e10cSrcweir     // <--
122cdf0e10cSrcweir 	sal_Int16 nAttrCount = xAttrList.is() ? xAttrList->getLength() : 0;
123cdf0e10cSrcweir 	for( sal_Int16 i=0; i < nAttrCount; i++ )
124cdf0e10cSrcweir 	{
125cdf0e10cSrcweir 		const OUString& rAttrName = xAttrList->getNameByIndex( i );
126cdf0e10cSrcweir 		const OUString& rValue = xAttrList->getValueByIndex( i );
127cdf0e10cSrcweir 
128cdf0e10cSrcweir 		OUString aLocalName;
129cdf0e10cSrcweir 		sal_uInt16 nPrefix =
130cdf0e10cSrcweir 			GetImport().GetNamespaceMap().GetKeyByAttrName( rAttrName,
131cdf0e10cSrcweir 															&aLocalName );
132cdf0e10cSrcweir 		switch( rTokenMap.Get( nPrefix, aLocalName ) )
133cdf0e10cSrcweir 		{
134cdf0e10cSrcweir 		case XML_TOK_TEXT_LIST_BLOCK_XMLID:
135cdf0e10cSrcweir 			sXmlId = rValue;
136cdf0e10cSrcweir //FIXME: there is no UNO API for lists
137cdf0e10cSrcweir             // --> OD 2008-07-31 #i92221# - xml:id is also the list ID
138cdf0e10cSrcweir             if ( mnLevel == 0 ) // root <list> element
139cdf0e10cSrcweir             {
140cdf0e10cSrcweir                 msListId = rValue;
141cdf0e10cSrcweir             }
142cdf0e10cSrcweir             // <--
143cdf0e10cSrcweir 			break;
144cdf0e10cSrcweir 		case XML_TOK_TEXT_LIST_BLOCK_CONTINUE_NUMBERING:
145cdf0e10cSrcweir             mbRestartNumbering = !IsXMLToken(rValue, XML_TRUE);
146cdf0e10cSrcweir             // --> OD 2008-05-07 #refactorlists#
147cdf0e10cSrcweir             bIsContinueNumberingAttributePresent = true;
148cdf0e10cSrcweir             // <--
149cdf0e10cSrcweir 			break;
150cdf0e10cSrcweir 		case XML_TOK_TEXT_LIST_BLOCK_STYLE_NAME:
151cdf0e10cSrcweir             msListStyleName = rValue;
152cdf0e10cSrcweir 			break;
153cdf0e10cSrcweir         // --> OD 2008-04-22 #refactorlists#
154cdf0e10cSrcweir         case XML_TOK_TEXT_LIST_BLOCK_CONTINUE_LIST:
155cdf0e10cSrcweir             if ( mnLevel == 0 ) // root <list> element
156cdf0e10cSrcweir             {
157cdf0e10cSrcweir                 msContinueListId = rValue;
158cdf0e10cSrcweir             }
159cdf0e10cSrcweir             break;
160cdf0e10cSrcweir 		}
161cdf0e10cSrcweir 	}
162cdf0e10cSrcweir 
163cdf0e10cSrcweir     mxNumRules = XMLTextListsHelper::MakeNumRule(GetImport(), mxNumRules,
164cdf0e10cSrcweir         sParentListStyleName, msListStyleName,
165cdf0e10cSrcweir         mnLevel, &mbRestartNumbering, &mbSetDefaults );
166cdf0e10cSrcweir     if( !mxNumRules.is() )
167cdf0e10cSrcweir         return;
168cdf0e10cSrcweir 
169cdf0e10cSrcweir     // --> OD 2008-04-23 #refactorlists#
170cdf0e10cSrcweir     if ( mnLevel == 0 ) // root <list> element
171cdf0e10cSrcweir     {
172cdf0e10cSrcweir         XMLTextListsHelper& rTextListsHelper( mrTxtImport.GetTextListHelper() );
173cdf0e10cSrcweir         // --> OD 2008-08-15 #i92811#
174cdf0e10cSrcweir         ::rtl::OUString sListStyleDefaultListId;
175cdf0e10cSrcweir         {
176cdf0e10cSrcweir             uno::Reference< beans::XPropertySet > xNumRuleProps( mxNumRules, UNO_QUERY );
177cdf0e10cSrcweir             if ( xNumRuleProps.is() )
178cdf0e10cSrcweir             {
179cdf0e10cSrcweir                 uno::Reference< beans::XPropertySetInfo > xNumRulePropSetInfo(
180cdf0e10cSrcweir                                             xNumRuleProps->getPropertySetInfo());
181cdf0e10cSrcweir                 if (xNumRulePropSetInfo.is() &&
182cdf0e10cSrcweir                     xNumRulePropSetInfo->hasPropertyByName(
183cdf0e10cSrcweir                          s_PropNameDefaultListId))
184cdf0e10cSrcweir                 {
185cdf0e10cSrcweir                     xNumRuleProps->getPropertyValue(s_PropNameDefaultListId)
186cdf0e10cSrcweir                         >>= sListStyleDefaultListId;
187cdf0e10cSrcweir                     DBG_ASSERT( sListStyleDefaultListId.getLength() != 0,
188cdf0e10cSrcweir                                 "no default list id found at numbering rules instance. Serious defect -> please inform OD." );
189cdf0e10cSrcweir                 }
190cdf0e10cSrcweir             }
191cdf0e10cSrcweir         }
192cdf0e10cSrcweir         // <--
193cdf0e10cSrcweir         if ( msListId.getLength() == 0 )  // no text:id property found
194cdf0e10cSrcweir         {
195cdf0e10cSrcweir             sal_Int32 nUPD( 0 );
196cdf0e10cSrcweir             sal_Int32 nBuild( 0 );
197cdf0e10cSrcweir             const bool bBuildIdFound = GetImport().getBuildIds( nUPD, nBuild );
198cdf0e10cSrcweir             if ( rImport.IsTextDocInOOoFileFormat() ||
199cdf0e10cSrcweir                  ( bBuildIdFound && nUPD == 680 ) )
200cdf0e10cSrcweir             {
201cdf0e10cSrcweir                 // handling former documents written by OpenOffice.org:
202cdf0e10cSrcweir                 // use default list id of numbering rules instance, if existing
203cdf0e10cSrcweir                 // --> OD 2008-08-15 #i92811#
204cdf0e10cSrcweir                 if ( sListStyleDefaultListId.getLength() != 0 )
205cdf0e10cSrcweir                 {
206cdf0e10cSrcweir                     msListId = sListStyleDefaultListId;
207cdf0e10cSrcweir                     if ( !bIsContinueNumberingAttributePresent &&
208cdf0e10cSrcweir                          !mbRestartNumbering &&
209cdf0e10cSrcweir                          rTextListsHelper.IsListProcessed( msListId ) )
210cdf0e10cSrcweir                     {
211cdf0e10cSrcweir                         mbRestartNumbering = sal_True;
212cdf0e10cSrcweir                     }
213cdf0e10cSrcweir                 }
214cdf0e10cSrcweir                 // <--
215cdf0e10cSrcweir             }
216cdf0e10cSrcweir             if ( msListId.getLength() == 0 )
217cdf0e10cSrcweir             {
218cdf0e10cSrcweir                 // generate a new list id for the list
219cdf0e10cSrcweir                 msListId = rTextListsHelper.GenerateNewListId();
220cdf0e10cSrcweir             }
221cdf0e10cSrcweir         }
222cdf0e10cSrcweir 
223cdf0e10cSrcweir         if ( bIsContinueNumberingAttributePresent && !mbRestartNumbering &&
224cdf0e10cSrcweir              msContinueListId.getLength() == 0 )
225cdf0e10cSrcweir         {
226cdf0e10cSrcweir             ::rtl::OUString Last( rTextListsHelper.GetLastProcessedListId() );
227cdf0e10cSrcweir             if ( rTextListsHelper.GetListStyleOfLastProcessedList() == msListStyleName
228cdf0e10cSrcweir                  && Last != msListId )
229cdf0e10cSrcweir             {
230cdf0e10cSrcweir                 msContinueListId = Last;
231cdf0e10cSrcweir             }
232cdf0e10cSrcweir         }
233cdf0e10cSrcweir 
234cdf0e10cSrcweir         if ( msContinueListId.getLength() > 0 )
235cdf0e10cSrcweir         {
236cdf0e10cSrcweir             if ( !rTextListsHelper.IsListProcessed( msContinueListId ) )
237cdf0e10cSrcweir             {
238cdf0e10cSrcweir                 msContinueListId = ::rtl::OUString();
239cdf0e10cSrcweir             }
240cdf0e10cSrcweir             else
241cdf0e10cSrcweir             {
242cdf0e10cSrcweir                 // search continue list chain for master list and
243cdf0e10cSrcweir                 // continue the master list.
244cdf0e10cSrcweir                 ::rtl::OUString sTmpStr =
245cdf0e10cSrcweir                     rTextListsHelper.GetContinueListIdOfProcessedList( msContinueListId );
246cdf0e10cSrcweir                 while ( sTmpStr.getLength() > 0 )
247cdf0e10cSrcweir                 {
248cdf0e10cSrcweir                     msContinueListId = sTmpStr;
249cdf0e10cSrcweir 
250cdf0e10cSrcweir                     sTmpStr =
251cdf0e10cSrcweir                         rTextListsHelper.GetContinueListIdOfProcessedList( msContinueListId );
252cdf0e10cSrcweir                 }
253cdf0e10cSrcweir             }
254cdf0e10cSrcweir         }
255cdf0e10cSrcweir 
256cdf0e10cSrcweir         if ( !rTextListsHelper.IsListProcessed( msListId ) )
257cdf0e10cSrcweir         {
258cdf0e10cSrcweir             // --> OD 2008-08-15 #i92811#
259cdf0e10cSrcweir             rTextListsHelper.KeepListAsProcessed(
260cdf0e10cSrcweir                 msListId, msListStyleName, msContinueListId,
261cdf0e10cSrcweir                 sListStyleDefaultListId );
262cdf0e10cSrcweir             // <--
263cdf0e10cSrcweir         }
264cdf0e10cSrcweir     }
265cdf0e10cSrcweir     // <--
266cdf0e10cSrcweir 
267cdf0e10cSrcweir     // Remember this list block.
268cdf0e10cSrcweir     mrTxtImport.GetTextListHelper().PushListContext( this );
269cdf0e10cSrcweir }
270cdf0e10cSrcweir 
~XMLTextListBlockContext()271cdf0e10cSrcweir XMLTextListBlockContext::~XMLTextListBlockContext()
272cdf0e10cSrcweir {
273cdf0e10cSrcweir }
274cdf0e10cSrcweir 
EndElement()275cdf0e10cSrcweir void XMLTextListBlockContext::EndElement()
276cdf0e10cSrcweir {
277cdf0e10cSrcweir 	// Numbering has not to be restarted if it has been restarted within
278cdf0e10cSrcweir 	// a child list.
279cdf0e10cSrcweir 	XMLTextListBlockContext *pParent =
280cdf0e10cSrcweir                                 (XMLTextListBlockContext *)&mxParentListBlock;
281cdf0e10cSrcweir 	if( pParent )
282cdf0e10cSrcweir 	{
283cdf0e10cSrcweir         pParent->mbRestartNumbering = mbRestartNumbering;
284cdf0e10cSrcweir 	}
285cdf0e10cSrcweir 
286cdf0e10cSrcweir 	// Restore current list block.
287cdf0e10cSrcweir     mrTxtImport.GetTextListHelper().PopListContext();
288cdf0e10cSrcweir 
289cdf0e10cSrcweir 	// Any paragraph following the list within the same list item must not
290cdf0e10cSrcweir 	// be numbered.
291cdf0e10cSrcweir     mrTxtImport.GetTextListHelper().SetListItem( 0 );
292cdf0e10cSrcweir }
293cdf0e10cSrcweir 
CreateChildContext(sal_uInt16 nPrefix,const OUString & rLocalName,const Reference<xml::sax::XAttributeList> & xAttrList)294cdf0e10cSrcweir SvXMLImportContext *XMLTextListBlockContext::CreateChildContext(
295cdf0e10cSrcweir 		sal_uInt16 nPrefix,
296cdf0e10cSrcweir 		const OUString& rLocalName,
297cdf0e10cSrcweir 		const Reference< xml::sax::XAttributeList > & xAttrList )
298cdf0e10cSrcweir {
299cdf0e10cSrcweir 	SvXMLImportContext *pContext = 0;
300cdf0e10cSrcweir 
301cdf0e10cSrcweir 	const SvXMLTokenMap& rTokenMap =
302cdf0e10cSrcweir                         mrTxtImport.GetTextListBlockElemTokenMap();
303cdf0e10cSrcweir 	sal_Bool bHeader = sal_False;
304cdf0e10cSrcweir 	switch( rTokenMap.Get( nPrefix, rLocalName ) )
305cdf0e10cSrcweir 	{
306cdf0e10cSrcweir 	case XML_TOK_TEXT_LIST_HEADER:
307cdf0e10cSrcweir 		bHeader = sal_True;
308cdf0e10cSrcweir 	case XML_TOK_TEXT_LIST_ITEM:
309cdf0e10cSrcweir         pContext = new XMLTextListItemContext( GetImport(), mrTxtImport,
310cdf0e10cSrcweir 												nPrefix, rLocalName,
311cdf0e10cSrcweir 							 				 xAttrList, bHeader );
312cdf0e10cSrcweir 		break;
313cdf0e10cSrcweir 	}
314cdf0e10cSrcweir 
315cdf0e10cSrcweir 	if( !pContext )
316cdf0e10cSrcweir 		pContext = new SvXMLImportContext( GetImport(), nPrefix, rLocalName );
317cdf0e10cSrcweir 
318cdf0e10cSrcweir 	return pContext;
319cdf0e10cSrcweir }
320cdf0e10cSrcweir 
321cdf0e10cSrcweir // --> OD 2008-04-22 #refactorlists#
GetListId() const322cdf0e10cSrcweir const ::rtl::OUString& XMLTextListBlockContext::GetListId() const
323cdf0e10cSrcweir {
324cdf0e10cSrcweir     return msListId;
325cdf0e10cSrcweir }
326cdf0e10cSrcweir 
GetContinueListId() const327cdf0e10cSrcweir const ::rtl::OUString& XMLTextListBlockContext::GetContinueListId() const
328cdf0e10cSrcweir {
329cdf0e10cSrcweir     return msContinueListId;
330cdf0e10cSrcweir }
331cdf0e10cSrcweir // <--
332cdf0e10cSrcweir 
333