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