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 , rConfig(rConfigImport)
84 , bIsBegin(bBegin)
85 {
86 }
87 
88 void XMLFootnoteConfigHelper::endFastElement(sal_Int32 )
89 {
90  if (bIsBegin)
91  {
92  rConfig.SetBeginNotice(sBuffer.makeStringAndClear());
93  }
94  else
95  {
96  rConfig.SetEndNotice(sBuffer.makeStringAndClear());
97  }
98 // rConfig = NULL; // import contexts are ref-counted
99 }
100 
101 void XMLFootnoteConfigHelper::characters( const OUString& rChars )
102 {
103  sBuffer.append(rChars);
104 }
105 
106 
107 // XMLFootnoteConfigurationImportContext
108 
109 constexpr OUStringLiteral gsPropertyAnchorCharStyleName(u"AnchorCharStyleName");
110 constexpr OUStringLiteral gsPropertyCharStyleName(u"CharStyleName");
111 constexpr OUStringLiteral gsPropertyNumberingType(u"NumberingType");
112 constexpr OUStringLiteral gsPropertyPageStyleName(u"PageStyleName");
113 constexpr OUStringLiteral gsPropertyParagraphStyleName(u"ParaStyleName");
114 constexpr OUStringLiteral gsPropertyPrefix(u"Prefix");
115 constexpr OUStringLiteral gsPropertyStartAt(u"StartAt");
116 constexpr OUStringLiteral gsPropertySuffix(u"Suffix");
117 constexpr OUStringLiteral gsPropertyPositionEndOfDoc(u"PositionEndOfDoc");
118 constexpr OUStringLiteral gsPropertyFootnoteCounting(u"FootnoteCounting");
119 constexpr OUStringLiteral gsPropertyEndNotice(u"EndNotice");
120 constexpr OUStringLiteral gsPropertyBeginNotice(u"BeginNotice");
121 
123  SvXMLImport& rImport,
124  sal_Int32 /*nElement*/,
125  const Reference<XFastAttributeList> & xAttrList)
127 , sNumFormat("1")
128 , sNumSync("false")
129 , nOffset(0)
130 , nNumbering(FootnoteNumbering::PER_PAGE)
131 , bPosition(false)
132 , bIsEndnote(false)
133 {
134  for (auto &aIter : sax_fastparser::castToFastAttributeList( xAttrList ))
135  {
136  if( aIter.getToken() == XML_ELEMENT(TEXT, XML_NOTE_CLASS) )
137  {
138  if( IsXMLToken(aIter, XML_ENDNOTE ) )
139  {
140  bIsEndnote = true;
142  }
143  break;
144  }
145  }
146 
147 }
149 {
150 }
151 
153 {
154  { XML_PAGE, FootnoteNumbering::PER_PAGE },
155  { XML_CHAPTER, FootnoteNumbering::PER_CHAPTER },
156  { XML_DOCUMENT, FootnoteNumbering::PER_DOCUMENT },
157  { XML_TOKEN_INVALID, 0 },
158 };
159 
161  const OUString& rValue )
162 {
163  switch (nElement)
164  {
166  sCitationStyle = rValue;
167  break;
169  sAnchorStyle = rValue;
170  break;
172  sDefaultStyle = rValue;
173  break;
175  sPageStyle = rValue;
176  break;
178  case XML_ELEMENT(TEXT, XML_OFFSET): // for backwards compatibility with SRC630 & earlier
179  {
180  sal_Int32 nTmp;
181  if (::sax::Converter::convertNumber(nTmp, rValue))
182  {
183  nOffset = static_cast<sal_uInt16>(nTmp);
184  }
185  break;
186  }
188  case XML_ELEMENT(TEXT, XML_NUM_PREFIX): // for backwards compatibility with SRC630 & earlier
189  sPrefix = rValue;
190  break;
192  case XML_ELEMENT(TEXT, XML_NUM_SUFFIX): // for backwards compatibility with SRC630 & earlier
193  sSuffix = rValue;
194  break;
196  sNumFormat = rValue;
197  break;
199  sNumSync = rValue;
200  break;
202  {
204  aFootnoteNumberingMap);
205  break;
206  }
208  bPosition = IsXMLToken( rValue, XML_DOCUMENT );
209  break;
210  default:
211  ; // ignore
212  }
213 }
214 
215 css::uno::Reference< css::xml::sax::XFastContextHandler > XMLFootnoteConfigurationImportContext::createFastChildContext(
216  sal_Int32 nElement,
217  const css::uno::Reference< css::xml::sax::XFastAttributeList >& )
218 {
219  css::uno::Reference< css::xml::sax::XFastContextHandler > xContext;
220 
221  if (bIsEndnote)
222  return nullptr;
223 
224  switch (nElement)
225  {
227  xContext = new XMLFootnoteConfigHelper(GetImport(), *this, false);
228  break;
230  xContext = new XMLFootnoteConfigHelper(GetImport(), *this, true);
231  break;
232  }
233 
234  return xContext;
235 }
236 
237 // Rename method <CreateAndInsertLate(..)> to <Finish(..)> (#i40597#)
239 {
240 
241  if (!bOverwrite)
242  return;
243 
244  if (bIsEndnote)
245  {
246  Reference<XEndnotesSupplier> xSupplier(
247  GetImport().GetModel(), UNO_QUERY);
248  if (xSupplier.is())
249  {
250  ProcessSettings(xSupplier->getEndnoteSettings());
251  }
252  }
253  else
254  {
255  Reference<XFootnotesSupplier> xSupplier(
256  GetImport().GetModel(), UNO_QUERY);
257  if (xSupplier.is())
258  {
259  ProcessSettings(xSupplier->getFootnoteSettings());
260  }
261  }
262  // else: ignore (there's only one configuration, so we can only overwrite)
263 }
264 
266  const Reference<XPropertySet> & rConfig)
267 {
268  Any aAny;
269 
270  if (!sCitationStyle.isEmpty())
271  {
272  aAny <<= GetImport().GetStyleDisplayName(
274  rConfig->setPropertyValue(gsPropertyCharStyleName, aAny);
275  }
276 
277  if (!sAnchorStyle.isEmpty())
278  {
279  aAny <<= GetImport().GetStyleDisplayName(
281  rConfig->setPropertyValue(gsPropertyAnchorCharStyleName, aAny);
282  }
283 
284  if (!sPageStyle.isEmpty())
285  {
286  aAny <<= GetImport().GetStyleDisplayName(
288  rConfig->setPropertyValue(gsPropertyPageStyleName, aAny);
289  }
290 
291  if (!sDefaultStyle.isEmpty())
292  {
293  aAny <<= GetImport().GetStyleDisplayName(
295  rConfig->setPropertyValue(gsPropertyParagraphStyleName, aAny);
296  }
297 
298  rConfig->setPropertyValue(gsPropertyPrefix, Any(sPrefix));
299 
300  rConfig->setPropertyValue(gsPropertySuffix, Any(sSuffix));
301 
302  sal_Int16 nNumType = NumberingType::ARABIC;
304  sNumSync );
305  // #i61399: Corrupt file? It contains "Bullet" as numbering style for footnotes.
306  // Okay, even it seems to be corrupt, we will oversee this and set the style to ARABIC
307  if( NumberingType::CHAR_SPECIAL == nNumType )
308  nNumType = NumberingType::ARABIC;
309 
310  rConfig->setPropertyValue(gsPropertyNumberingType, Any(nNumType));
311 
312  rConfig->setPropertyValue(gsPropertyStartAt, Any(nOffset));
313 
314  if (!bIsEndnote)
315  {
316  rConfig->setPropertyValue(gsPropertyPositionEndOfDoc, Any(bPosition));
317  rConfig->setPropertyValue(gsPropertyFootnoteCounting, Any(nNumbering));
318  rConfig->setPropertyValue(gsPropertyEndNotice, Any(sEndNotice));
319  rConfig->setPropertyValue(gsPropertyBeginNotice, Any(sBeginNotice));
320  }
321 }
322 
324  const OUString& sText)
325 {
326  sBeginNotice = sText;
327 }
328 
330  const OUString& sText)
331 {
332  sEndNotice = sText;
333 }
334 
335 /* 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:59
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)
constexpr OUStringLiteral gsPropertyAnchorCharStyleName(u"AnchorCharStyleName")
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:701
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:44
Map an XMLTokenEnum to an enum value.
Definition: ximpshap.hxx:39
virtual void SetAttribute(sal_Int32 nElement, const OUString &rValue) override
parse attributes
OUString GetStyleDisplayName(XmlStyleFamily nFamily, const OUString &rName) const
Definition: xmlimp.cxx:1406
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:97
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:145
const SvXMLUnitConverter & GetMM100UnitConverter() const
Definition: xmlimp.hxx:400
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