LibreOffice Module xmloff (master)  1
XMLFootnoteConfigurationImportContext.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 
22 
23 #include <rtl/ustring.hxx>
24 #include <rtl/ustrbuf.hxx>
25 
26 #include <sax/tools/converter.hxx>
27 
28 #include <xmloff/namespacemap.hxx>
29 #include <xmloff/xmlnamespace.hxx>
30 #include <xmloff/xmltoken.hxx>
31 #include <xmloff/xmlement.hxx>
32 
33 #include <xmloff/families.hxx>
34 #include <xmloff/xmluconv.hxx>
35 #include <xmloff/xmlimp.hxx>
36 #include <com/sun/star/frame/XModel.hpp>
37 #include <com/sun/star/xml/sax/XAttributeList.hpp>
38 #include <com/sun/star/beans/XPropertySet.hpp>
39 #include <com/sun/star/text/XFootnotesSupplier.hpp>
40 #include <com/sun/star/text/XEndnotesSupplier.hpp>
41 #include <com/sun/star/text/FootnoteNumbering.hpp>
42 #include <com/sun/star/style/NumberingType.hpp>
43 
44 
45 using namespace ::com::sun::star::text;
46 using namespace ::com::sun::star::beans;
47 using namespace ::com::sun::star::uno;
48 using namespace ::com::sun::star::style;
49 using namespace ::com::sun::star::xml::sax;
50 using namespace ::xmloff::token;
51 
52 
53 // XMLFootnoteConfigHelper
54 
55 namespace {
56 
58 class XMLFootnoteConfigHelper : public SvXMLImportContext
59 {
60  OUStringBuffer sBuffer;
62  bool bIsBegin;
63 
64 public:
65 
66  XMLFootnoteConfigHelper(
67  SvXMLImport& rImport,
69  bool bBegin);
70 
71  virtual void SAL_CALL endFastElement(sal_Int32 nElement) override;
72 
73  virtual void SAL_CALL characters( const OUString& rChars ) override;
74 };
75 
76 }
77 
78 XMLFootnoteConfigHelper::XMLFootnoteConfigHelper(
79  SvXMLImport& rImport,
81  bool bBegin)
82 : SvXMLImportContext(rImport)
83 , sBuffer()
84 , rConfig(rConfigImport)
85 , bIsBegin(bBegin)
86 {
87 }
88 
89 void XMLFootnoteConfigHelper::endFastElement(sal_Int32 )
90 {
91  if (bIsBegin)
92  {
93  rConfig.SetBeginNotice(sBuffer.makeStringAndClear());
94  }
95  else
96  {
97  rConfig.SetEndNotice(sBuffer.makeStringAndClear());
98  }
99 // rConfig = NULL; // import contexts are ref-counted
100 }
101 
102 void XMLFootnoteConfigHelper::characters( const OUString& rChars )
103 {
104  sBuffer.append(rChars);
105 }
106 
107 
108 // XMLFootnoteConfigurationImportContext
109 
110 constexpr OUStringLiteral gsPropertyAnchorCharStyleName(u"AnchorCharStyleName");
111 constexpr OUStringLiteral gsPropertyCharStyleName(u"CharStyleName");
112 constexpr OUStringLiteral gsPropertyNumberingType(u"NumberingType");
113 constexpr OUStringLiteral gsPropertyPageStyleName(u"PageStyleName");
114 constexpr OUStringLiteral gsPropertyParagraphStyleName(u"ParaStyleName");
115 constexpr OUStringLiteral gsPropertyPrefix(u"Prefix");
116 constexpr OUStringLiteral gsPropertyStartAt(u"StartAt");
117 constexpr OUStringLiteral gsPropertySuffix(u"Suffix");
118 constexpr OUStringLiteral gsPropertyPositionEndOfDoc(u"PositionEndOfDoc");
119 constexpr OUStringLiteral gsPropertyFootnoteCounting(u"FootnoteCounting");
120 constexpr OUStringLiteral gsPropertyEndNotice(u"EndNotice");
121 constexpr OUStringLiteral gsPropertyBeginNotice(u"BeginNotice");
122 
124  SvXMLImport& rImport,
125  sal_Int32 /*nElement*/,
126  const Reference<XFastAttributeList> & xAttrList)
128 , sNumFormat("1")
129 , sNumSync("false")
130 , nOffset(0)
131 , nNumbering(FootnoteNumbering::PER_PAGE)
132 , bPosition(false)
133 , bIsEndnote(false)
134 {
135  for (auto &aIter : sax_fastparser::castToFastAttributeList( xAttrList ))
136  {
137  if( aIter.getToken() == XML_ELEMENT(TEXT, XML_NOTE_CLASS) )
138  {
139  if( IsXMLToken(aIter, XML_ENDNOTE ) )
140  {
141  bIsEndnote = true;
143  }
144  break;
145  }
146  }
147 
148 }
150 {
151 }
152 
154 {
155  { XML_PAGE, FootnoteNumbering::PER_PAGE },
156  { XML_CHAPTER, FootnoteNumbering::PER_CHAPTER },
157  { XML_DOCUMENT, FootnoteNumbering::PER_DOCUMENT },
158  { XML_TOKEN_INVALID, 0 },
159 };
160 
162  const OUString& rValue )
163 {
164  switch (nElement)
165  {
168  break;
171  break;
174  break;
176  sPageStyle = rValue;
177  break;
179  case XML_ELEMENT(TEXT, XML_OFFSET): // for backwards compatibility with SRC630 & earlier
180  {
181  sal_Int32 nTmp;
182  if (::sax::Converter::convertNumber(nTmp, rValue))
183  {
184  nOffset = static_cast<sal_uInt16>(nTmp);
185  }
186  break;
187  }
189  case XML_ELEMENT(TEXT, XML_NUM_PREFIX): // for backwards compatibility with SRC630 & earlier
190  sPrefix = rValue;
191  break;
193  case XML_ELEMENT(TEXT, XML_NUM_SUFFIX): // for backwards compatibility with SRC630 & earlier
194  sSuffix = rValue;
195  break;
197  sNumFormat = rValue;
198  break;
200  sNumSync = rValue;
201  break;
203  {
205  aFootnoteNumberingMap);
206  break;
207  }
209  bPosition = IsXMLToken( rValue, XML_DOCUMENT );
210  break;
211  default:
212  ; // ignore
213  }
214 }
215 
216 css::uno::Reference< css::xml::sax::XFastContextHandler > XMLFootnoteConfigurationImportContext::createFastChildContext(
217  sal_Int32 nElement,
218  const css::uno::Reference< css::xml::sax::XFastAttributeList >& )
219 {
220  css::uno::Reference< css::xml::sax::XFastContextHandler > xContext;
221 
222  if (bIsEndnote)
223  return nullptr;
224 
225  switch (nElement)
226  {
228  xContext = new XMLFootnoteConfigHelper(GetImport(), *this, false);
229  break;
231  xContext = new XMLFootnoteConfigHelper(GetImport(), *this, true);
232  break;
233  }
234 
235  return xContext;
236 }
237 
238 // Rename method <CreateAndInsertLate(..)> to <Finish(..)> (#i40597#)
240 {
241 
242  if (!bOverwrite)
243  return;
244 
245  if (bIsEndnote)
246  {
247  Reference<XEndnotesSupplier> xSupplier(
248  GetImport().GetModel(), UNO_QUERY);
249  if (xSupplier.is())
250  {
251  ProcessSettings(xSupplier->getEndnoteSettings());
252  }
253  }
254  else
255  {
256  Reference<XFootnotesSupplier> xSupplier(
257  GetImport().GetModel(), UNO_QUERY);
258  if (xSupplier.is())
259  {
260  ProcessSettings(xSupplier->getFootnoteSettings());
261  }
262  }
263  // else: ignore (there's only one configuration, so we can only overwrite)
264 }
265 
267  const Reference<XPropertySet> & rConfig)
268 {
269  Any aAny;
270 
271  if (!sCitationStyle.isEmpty())
272  {
273  aAny <<= GetImport().GetStyleDisplayName(
275  rConfig->setPropertyValue(gsPropertyCharStyleName, aAny);
276  }
277 
278  if (!sAnchorStyle.isEmpty())
279  {
280  aAny <<= GetImport().GetStyleDisplayName(
282  rConfig->setPropertyValue(gsPropertyAnchorCharStyleName, aAny);
283  }
284 
285  if (!sPageStyle.isEmpty())
286  {
287  aAny <<= GetImport().GetStyleDisplayName(
289  rConfig->setPropertyValue(gsPropertyPageStyleName, aAny);
290  }
291 
292  if (!sDefaultStyle.isEmpty())
293  {
294  aAny <<= GetImport().GetStyleDisplayName(
296  rConfig->setPropertyValue(gsPropertyParagraphStyleName, aAny);
297  }
298 
299  rConfig->setPropertyValue(gsPropertyPrefix, Any(sPrefix));
300 
301  rConfig->setPropertyValue(gsPropertySuffix, Any(sSuffix));
302 
303  sal_Int16 nNumType = NumberingType::ARABIC;
305  sNumSync );
306  // #i61399: Corrupt file? It contains "Bullet" as numbering style for footnotes.
307  // Okay, even it seems to be corrupt, we will oversee this and set the style to ARABIC
308  if( NumberingType::CHAR_SPECIAL == nNumType )
309  nNumType = NumberingType::ARABIC;
310 
311  rConfig->setPropertyValue(gsPropertyNumberingType, Any(nNumType));
312 
313  rConfig->setPropertyValue(gsPropertyStartAt, Any(nOffset));
314 
315  if (!bIsEndnote)
316  {
317  rConfig->setPropertyValue(gsPropertyPositionEndOfDoc, Any(bPosition));
318  rConfig->setPropertyValue(gsPropertyFootnoteCounting, Any(nNumbering));
319  rConfig->setPropertyValue(gsPropertyEndNotice, Any(sEndNotice));
320  rConfig->setPropertyValue(gsPropertyBeginNotice, Any(sBeginNotice));
321  }
322 }
323 
325  const OUString& sText)
326 {
327  sBeginNotice = sText;
328 }
329 
331  const OUString& sText)
332 {
333  sEndNotice = sText;
334 }
335 
336 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
constexpr OUStringLiteral gsPropertyNumberingType(u"NumberingType")
constexpr OUStringLiteral gsPropertyCharStyleName(u"CharStyleName")
constexpr OUStringLiteral gsPropertyFootnoteCounting(u"FootnoteCounting")
SvXMLImport & GetImport()
Definition: xmlictxt.hxx:56
bool IsXMLToken(std::u16string_view rString, enum XMLTokenEnum eToken)
compare eToken to the string
Definition: xmltoken.cxx:3462
FastAttributeList & castToFastAttributeList(const css::uno::Reference< css::xml::sax::XFastAttributeList > &xAttrList)
constexpr OUStringLiteral gsPropertyAnchorCharStyleName(u"AnchorCharStyleName")
css::uno::Any const & rValue
Definition: ImageStyle.hxx:38
XmlStyleFamily
Definition: families.hxx:47
constexpr OUStringLiteral gsPropertyParagraphStyleName(u"ParaStyleName")
sal_Int16 nNumType
SvXMLEnumMapEntry< sal_Int16 > const aFootnoteNumberingMap[]
bool convertNumFormat(sal_Int16 &rType, const OUString &rNumFormat, std::u16string_view rNumLetterSync, bool bNumberNone=false) const
convert num-format and num-letter-sync values to NumberingType
Definition: xmluconv.cxx:686
void ProcessSettings(const css::uno::Reference< css::beans::XPropertySet > &rConfig)
set configuration at document
XMLFootnoteConfigurationImportContext(SvXMLImport &rImport, sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList > &xAttrList)
virtual void Finish(bool bOverwrite) override
set configuration at document; calls ProcessSettings
constexpr OUStringLiteral gsPropertyPositionEndOfDoc(u"PositionEndOfDoc")
virtual css::uno::Reference< css::xml::sax::XFastContextHandler > SAL_CALL createFastChildContext(sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList > &AttrList) override
for footnotes, also parse begin and end notices
void SetFamily(XmlStyleFamily nSet)
Definition: xmlstyle.hxx:62
constexpr OUStringLiteral gsPropertyEndNotice(u"EndNotice")
constexpr OUStringLiteral gsPropertyPrefix(u"Prefix")
constexpr OUStringLiteral gsPropertyBeginNotice(u"BeginNotice")
import footnote and endnote configuration elements
This class deliberately does not support XWeak, to improve performance when loading large documents...
Definition: xmlictxt.hxx:45
Map an XMLTokenEnum to an enum value.
Definition: ximpshap.hxx:40
virtual void SetAttribute(sal_Int32 nElement, const OUString &rValue) override
parse attributes
OUString GetStyleDisplayName(XmlStyleFamily nFamily, const OUString &rName) const
Definition: xmlimp.cxx:1393
constexpr OUStringLiteral gsPropertyStartAt(u"StartAt")
virtual void SAL_CALL endFastElement(sal_Int32 Element) override
endFastElement is called before a context will be destructed, but after an elements context has been ...
Definition: xmlictxt.cxx:40
Handling of tokens in XML:
#define XML_ELEMENT(prefix, name)
Definition: xmlimp.hxx:96
static bool convertEnum(EnumT &rEnum, std::u16string_view rValue, const SvXMLEnumMapEntry< EnumT > *pMap)
convert string to enum using given enum map, if the enum is not found in the map, this method will re...
Definition: xmluconv.hxx:138
const SvXMLUnitConverter & GetMM100UnitConverter() const
Definition: xmlimp.hxx:395
void SetBeginNotice(const OUString &sText)
for helper class: set begin notice
constexpr OUStringLiteral gsPropertySuffix(u"Suffix")
void SetEndNotice(const OUString &sText)
for helper class: set end notice
constexpr OUStringLiteral gsPropertyPageStyleName(u"PageStyleName")
virtual void SAL_CALL characters(const OUString &aChars) override
This method is called for all characters that are contained in the current element.
Definition: xmlictxt.cxx:70
TEXT
static bool convertNumber(sal_Int32 &rValue, std::u16string_view aString, sal_Int32 nMin=SAL_MIN_INT32, sal_Int32 nMax=SAL_MAX_INT32)
typedef void(CALLTYPE *GetFuncDataPtr)(sal_uInt16 &nNo