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 try
114 {
116 sParentListStyleName, msListStyleName,
118 if( !mxNumRules.is() )
119 return;
120
121 if ( mnLevel != 0 ) // root <list> element
122 return;
123
124 XMLTextListsHelper& rTextListsHelper( mrTxtImport.GetTextListHelper() );
125 // Inconsistent behavior regarding lists (#i92811#)
126 OUString sListStyleDefaultListId;
127 {
128 uno::Reference< beans::XPropertySet > xNumRuleProps( mxNumRules, UNO_QUERY );
129 if ( xNumRuleProps.is() )
130 {
131 uno::Reference< beans::XPropertySetInfo > xNumRulePropSetInfo(
132 xNumRuleProps->getPropertySetInfo());
133 if (xNumRulePropSetInfo.is() &&
134 xNumRulePropSetInfo->hasPropertyByName(
135 s_PropNameDefaultListId))
136 {
137 xNumRuleProps->getPropertyValue(s_PropNameDefaultListId)
138 >>= sListStyleDefaultListId;
139 SAL_WARN_IF( sListStyleDefaultListId.isEmpty(), "xmloff",
140 "no default list id found at numbering rules instance. Serious defect." );
141 }
142 }
143 }
144 if ( msListId.isEmpty() ) // no text:id property found
145 {
146 sal_Int32 nUPD( 0 );
147 sal_Int32 nBuild( 0 );
148 const bool bBuildIdFound = GetImport().getBuildIds( nUPD, nBuild );
149 if ( rImport.IsTextDocInOOoFileFormat() ||
150 ( bBuildIdFound && nUPD == 680 ) )
151 {
152 /* handling former documents written by OpenOffice.org:
153 use default list id of numbering rules instance, if existing
154 (#i92811#)
155 */
156 if ( !sListStyleDefaultListId.isEmpty() )
157 {
158 msListId = sListStyleDefaultListId;
159 if ( !bIsContinueNumberingAttributePresent &&
161 rTextListsHelper.IsListProcessed( msListId ) )
162 {
163 mbRestartNumbering = true;
164 }
165 }
166 }
167 if ( msListId.isEmpty() )
168 {
169 // generate a new list id for the list
170 msListId = rTextListsHelper.GenerateNewListId();
171 }
172 }
173
174 if ( bIsContinueNumberingAttributePresent && !mbRestartNumbering &&
175 msContinueListId.isEmpty() )
176 {
177 const OUString& Last( rTextListsHelper.GetLastProcessedListId() );
178 if ( rTextListsHelper.GetListStyleOfLastProcessedList() == msListStyleName
179 && Last != msListId )
180 {
182 }
183 }
184
185 bool bContinueNumbering = bIsContinueNumberingAttributePresent && !mbRestartNumbering;
186 if (msContinueListId.isEmpty() && bContinueNumbering && GetImport().IsMSO())
187 {
188 // No "continue list" id, but continue numbering was requested. Connect to the last list of
189 // the same list style in the Word case, even if there was a different list in the meantime.
191 }
192
193 if ( !msContinueListId.isEmpty() )
194 {
195 if ( !rTextListsHelper.IsListProcessed( msContinueListId ) )
196 {
197 msContinueListId.clear();
198 }
199 else
200 {
201 // search continue list chain for master list and
202 // continue the master list.
203 OUString sTmpStr =
205 while ( !sTmpStr.isEmpty() )
206 {
207 msContinueListId = sTmpStr;
208
209 sTmpStr =
211 }
212 }
213 }
214
215 if ( !rTextListsHelper.IsListProcessed( msListId ) )
216 {
217 // Inconsistent behavior regarding lists (#i92811#)
218 rTextListsHelper.KeepListAsProcessed(
220 sListStyleDefaultListId );
221 }
222 }
223 catch (uno::Exception&)
224 {
225 // pop ourselves if anything goes wrong to avoid use-after-free
227 throw;
228 }
229}
230
232{
233}
234
236{
237 // Numbering has not to be restarted if it has been restarted within
238 // a child list.
240 if( pParent )
241 {
243 }
244
245 // Restore current list block.
247
248 // Any paragraph following the list within the same list item must not
249 // be numbered.
251}
252
253css::uno::Reference< css::xml::sax::XFastContextHandler > XMLTextListBlockContext::createFastChildContext(
254 sal_Int32 nElement,
255 const css::uno::Reference< css::xml::sax::XFastAttributeList >& xAttrList )
256{
257 SvXMLImportContext *pContext = nullptr;
258
259 bool bHeader = false;
260 switch( nElement )
261 {
263 bHeader = true;
264 [[fallthrough]];
267 xAttrList, bHeader );
268 break;
269 default:
270 XMLOFF_WARN_UNKNOWN_ELEMENT("xmloff", nElement);
271 }
272
273 return pContext;
274}
275
276
277/* 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
XMLTextListsHelper & GetTextListHelper()
Definition: txtimp.cxx:332
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:513
bool IsXMLToken(std::u16string_view rString, enum XMLTokenEnum eToken)
compare eToken to the string
Definition: xmltoken.cxx:3597
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