LibreOffice Module xmloff (master) 1
XMLTextListBlockContext.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#include <com/sun/star/container/XIndexReplace.hpp>
21#include <com/sun/star/style/XStyle.hpp>
22#include <com/sun/star/beans/XPropertySet.hpp>
23#include <xmloff/xmlimp.hxx>
26#include <xmloff/xmltoken.hxx>
29#include <txtlists.hxx>
30#include <sal/log.hxx>
31
32
33using namespace ::com::sun::star;
34using namespace ::com::sun::star::uno;
35using namespace ::com::sun::star::container;
36using namespace ::com::sun::star::style;
37using namespace ::com::sun::star::beans;
38using namespace ::xmloff::token;
39
40
41// OD 2008-05-07 #refactorlists#
42// add optional parameter <bRestartNumberingAtSubList> and its handling
44 SvXMLImport& rImport,
45 XMLTextImportHelper& rTxtImp,
46 const Reference< xml::sax::XFastAttributeList > & xAttrList,
47 const bool bRestartNumberingAtSubList )
48: SvXMLImportContext( rImport )
49, mrTxtImport( rTxtImp )
50, mnLevel( 0 )
51, mbRestartNumbering( false )
52, mbSetDefaults( false )
53{
54 static constexpr OUStringLiteral s_PropNameDefaultListId = u"DefaultListId";
55 {
56 // get the parent list block context (if any); this is a bit ugly...
57 XMLTextListBlockContext * pLB(nullptr);
58 XMLTextListItemContext * pLI(nullptr);
59 XMLNumberedParaContext * pNP(nullptr);
60 rTxtImp.GetTextListHelper().ListContextTop(pLB, pLI, pNP);
62 }
63 // Inherit style name from parent list, as well as the flags whether
64 // numbering must be restarted and formats have to be created.
65 OUString sParentListStyleName;
66 if( mxParentListBlock.is() )
67 {
70 sParentListStyleName = msListStyleName;
71 mxNumRules = pParent->GetNumRules();
72 mnLevel = pParent->GetLevel() + 1;
74 bRestartNumberingAtSubList;
76 msListId = pParent->GetListId();
78 }
79
80 bool bIsContinueNumberingAttributePresent( false );
81 for( auto& aIter : sax_fastparser::castToFastAttributeList(xAttrList) )
82 {
83 switch( aIter.getToken() )
84 {
85 case XML_ELEMENT(XML, XML_ID):
86//FIXME: there is no UNO API for lists
87 // xml:id is also the list ID (#i92221#)
88 if ( mnLevel == 0 ) // root <list> element
89 {
90 msListId = aIter.toString();
91 }
92 break;
95 bIsContinueNumberingAttributePresent = true;
96 break;
98 msListStyleName = aIter.toString();
99 break;
101 if ( mnLevel == 0 ) // root <list> element
102 {
103 msContinueListId = aIter.toString();
104 }
105 break;
106 default:
107 XMLOFF_WARN_UNKNOWN("xmloff", aIter);
108 }
109 }
110
111 // Remember this list block.
113
115 sParentListStyleName, msListStyleName,
117 if( !mxNumRules.is() )
118 return;
119
120 if ( mnLevel != 0 ) // root <list> element
121 return;
122
123 XMLTextListsHelper& rTextListsHelper( mrTxtImport.GetTextListHelper() );
124 // Inconsistent behavior regarding lists (#i92811#)
125 OUString sListStyleDefaultListId;
126 {
127 uno::Reference< beans::XPropertySet > xNumRuleProps( mxNumRules, UNO_QUERY );
128 if ( xNumRuleProps.is() )
129 {
130 uno::Reference< beans::XPropertySetInfo > xNumRulePropSetInfo(
131 xNumRuleProps->getPropertySetInfo());
132 if (xNumRulePropSetInfo.is() &&
133 xNumRulePropSetInfo->hasPropertyByName(
134 s_PropNameDefaultListId))
135 {
136 xNumRuleProps->getPropertyValue(s_PropNameDefaultListId)
137 >>= sListStyleDefaultListId;
138 SAL_WARN_IF( sListStyleDefaultListId.isEmpty(), "xmloff",
139 "no default list id found at numbering rules instance. Serious defect." );
140 }
141 }
142 }
143 if ( msListId.isEmpty() ) // no text:id property found
144 {
145 sal_Int32 nUPD( 0 );
146 sal_Int32 nBuild( 0 );
147 const bool bBuildIdFound = GetImport().getBuildIds( nUPD, nBuild );
148 if ( rImport.IsTextDocInOOoFileFormat() ||
149 ( bBuildIdFound && nUPD == 680 ) )
150 {
151 /* handling former documents written by OpenOffice.org:
152 use default list id of numbering rules instance, if existing
153 (#i92811#)
154 */
155 if ( !sListStyleDefaultListId.isEmpty() )
156 {
157 msListId = sListStyleDefaultListId;
158 if ( !bIsContinueNumberingAttributePresent &&
160 rTextListsHelper.IsListProcessed( msListId ) )
161 {
162 mbRestartNumbering = true;
163 }
164 }
165 }
166 if ( msListId.isEmpty() )
167 {
168 // generate a new list id for the list
169 msListId = rTextListsHelper.GenerateNewListId();
170 }
171 }
172
173 if ( bIsContinueNumberingAttributePresent && !mbRestartNumbering &&
174 msContinueListId.isEmpty() )
175 {
176 const OUString& Last( rTextListsHelper.GetLastProcessedListId() );
177 if ( rTextListsHelper.GetListStyleOfLastProcessedList() == msListStyleName
178 && Last != msListId )
179 {
181 }
182 }
183
184 bool bContinueNumbering = bIsContinueNumberingAttributePresent && !mbRestartNumbering;
185 if (msContinueListId.isEmpty() && bContinueNumbering && GetImport().IsMSO())
186 {
187 // No "continue list" id, but continue numbering was requested. Connect to the last list of
188 // the same list style in the Word case, even if there was a different list in the meantime.
190 }
191
192 if ( !msContinueListId.isEmpty() )
193 {
194 if ( !rTextListsHelper.IsListProcessed( msContinueListId ) )
195 {
196 msContinueListId.clear();
197 }
198 else
199 {
200 // search continue list chain for master list and
201 // continue the master list.
202 OUString sTmpStr =
204 while ( !sTmpStr.isEmpty() )
205 {
206 msContinueListId = sTmpStr;
207
208 sTmpStr =
210 }
211 }
212 }
213
214 if ( !rTextListsHelper.IsListProcessed( msListId ) )
215 {
216 // Inconsistent behavior regarding lists (#i92811#)
217 rTextListsHelper.KeepListAsProcessed(
219 sListStyleDefaultListId );
220 }
221}
222
224{
225}
226
228{
229 // Numbering has not to be restarted if it has been restarted within
230 // a child list.
232 if( pParent )
233 {
235 }
236
237 // Restore current list block.
239
240 // Any paragraph following the list within the same list item must not
241 // be numbered.
243}
244
245css::uno::Reference< css::xml::sax::XFastContextHandler > XMLTextListBlockContext::createFastChildContext(
246 sal_Int32 nElement,
247 const css::uno::Reference< css::xml::sax::XFastAttributeList >& xAttrList )
248{
249 SvXMLImportContext *pContext = nullptr;
250
251 bool bHeader = false;
252 switch( nElement )
253 {
255 bHeader = true;
256 [[fallthrough]];
259 xAttrList, bHeader );
260 break;
261 default:
262 XMLOFF_WARN_UNKNOWN_ELEMENT("xmloff", nElement);
263 }
264
265 return pContext;
266}
267
268
269/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
This class deliberately does not support XWeak, to improve performance when loading large documents.
Definition: xmlictxt.hxx:48
SvXMLImport & GetImport()
Definition: xmlictxt.hxx:60
bool IsTextDocInOOoFileFormat() const
Definition: xmlimp.cxx:1815
bool getBuildIds(sal_Int32 &rUPD, sal_Int32 &rBuild) const
returns the upd and build id (f.e.
Definition: xmlimp.cxx:1825
XMLTextListsHelper & GetTextListHelper()
Definition: txtimp.cxx:333
const css::uno::Reference< css::container::XIndexReplace > & GetNumRules() const
virtual css::uno::Reference< css::xml::sax::XFastContextHandler > SAL_CALL createFastChildContext(sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList > &xAttrList) override
virtual ~XMLTextListBlockContext() override
rtl::Reference< XMLTextListBlockContext > mxParentListBlock
css::uno::Reference< css::container::XIndexReplace > mxNumRules
const OUString & GetContinueListId() const
virtual void SAL_CALL endFastElement(sal_Int32 nElement) override
endFastElement is called before a context will be destructed, but after an elements context has been ...
const OUString & GetListId() const
XMLTextListBlockContext(SvXMLImport &rImport, XMLTextImportHelper &rTxtImp, const css::uno::Reference< css::xml::sax::XFastAttributeList > &xAttrList, const bool bRestartNumberingAtSubList=false)
XMLTextImportHelper & mrTxtImport
void PopListContext()
pop the list context stack
Definition: txtlists.cxx:65
const OUString & GetLastProcessedListId() const
Definition: txtlists.hxx:71
void SetListItem(XMLTextListItemContext *pListItem)
set list item on top of the list context stack
Definition: txtlists.cxx:87
void KeepListAsProcessed(const OUString &sListId, const OUString &sListStyleName, const OUString &sContinueListId, const OUString &sListStyleDefaultListId=OUString())
Definition: txtlists.cxx:103
void PushListContext(XMLTextListBlockContext *i_pListBlock)
list stack for importing:
Definition: txtlists.cxx:49
OUString GenerateNewListId() const
Definition: txtlists.cxx:194
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
void ListContextTop(XMLTextListBlockContext *&o_pListBlockContext, XMLTextListItemContext *&o_pListItemContext, XMLNumberedParaContext *&o_pNumberedParagraphContext)
peek at the top of the list context stack
Definition: txtlists.cxx:72
bool IsListProcessed(const OUString &sListId) const
Definition: txtlists.cxx:153
OUString GetContinueListIdOfProcessedList(const OUString &sListId) const
Definition: txtlists.cxx:178
const OUString & GetListStyleOfLastProcessedList() const
Definition: txtlists.hxx:72
OUString GetLastIdOfStyleName(const OUString &sListStyleName) const
Looks up the last list id of a given list style, by name.
Definition: txtlists.cxx:483
sal_Int16 mnLevel
float u
#define SAL_WARN_IF(condition, area, stream)
FastAttributeList & castToFastAttributeList(const css::uno::Reference< css::xml::sax::XFastAttributeList > &xAttrList)
Handling of tokens in XML:
@ XML_CONTINUE_NUMBERING
Definition: xmltoken.hxx:511
bool IsXMLToken(std::u16string_view rString, enum XMLTokenEnum eToken)
compare eToken to the string
Definition: xmltoken.cxx:3573
TEXT
#define XMLOFF_WARN_UNKNOWN_ELEMENT(area, token)
Definition: xmlictxt.hxx:120
#define XMLOFF_WARN_UNKNOWN(area, rIter)
Definition: xmlictxt.hxx:114
#define XML_ELEMENT(prefix, name)
Definition: xmlimp.hxx:97