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 , 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);
61  mxParentListBlock = pLB;
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;
75  mbSetDefaults = pParent->mbSetDefaults;
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  if ( !msContinueListId.isEmpty() )
185  {
186  if ( !rTextListsHelper.IsListProcessed( msContinueListId ) )
187  {
188  msContinueListId.clear();
189  }
190  else
191  {
192  // search continue list chain for master list and
193  // continue the master list.
194  OUString sTmpStr =
195  rTextListsHelper.GetContinueListIdOfProcessedList( msContinueListId );
196  while ( !sTmpStr.isEmpty() )
197  {
198  msContinueListId = sTmpStr;
199 
200  sTmpStr =
201  rTextListsHelper.GetContinueListIdOfProcessedList( msContinueListId );
202  }
203  }
204  }
205 
206  if ( !rTextListsHelper.IsListProcessed( msListId ) )
207  {
208  // Inconsistent behavior regarding lists (#i92811#)
209  rTextListsHelper.KeepListAsProcessed(
211  sListStyleDefaultListId );
212  }
213 }
214 
216 {
217 }
218 
220 {
221  // Numbering has not to be restarted if it has been restarted within
222  // a child list.
224  if( pParent )
225  {
227  }
228 
229  // Restore current list block.
231 
232  // Any paragraph following the list within the same list item must not
233  // be numbered.
235 }
236 
237 css::uno::Reference< css::xml::sax::XFastContextHandler > XMLTextListBlockContext::createFastChildContext(
238  sal_Int32 nElement,
239  const css::uno::Reference< css::xml::sax::XFastAttributeList >& xAttrList )
240 {
241  SvXMLImportContext *pContext = nullptr;
242 
243  bool bHeader = false;
244  switch( nElement )
245  {
247  bHeader = true;
248  [[fallthrough]];
250  pContext = new XMLTextListItemContext( GetImport(), mrTxtImport,
251  xAttrList, bHeader );
252  break;
253  default:
254  XMLOFF_WARN_UNKNOWN_ELEMENT("xmloff", nElement);
255  }
256 
257  return pContext;
258 }
259 
260 
261 /* 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:3508
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:72
virtual ~XMLTextListBlockContext() override
bool getBuildIds(sal_Int32 &rUPD, sal_Int32 &rBuild) const
returns the upd and build id (f.e.
Definition: xmlimp.cxx:1814
bool IsTextDocInOOoFileFormat() const
Definition: xmlimp.cxx:1804
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:49
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:87
Handling of tokens in XML:
void PopListContext()
pop the list context stack
Definition: txtlists.cxx:65
#define XML_ELEMENT(prefix, name)
Definition: xmlimp.hxx:97
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:400