Avoid potential negative array index access to cached text.
[LibreOffice.git] / xmloff / source / text / XMLFootnoteConfigurationImportContext.cxx
blob8e67b2d90964444ede35e4f7b8d7de534d28eab2
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
21 #include <XMLFootnoteConfigurationImportContext.hxx>
23 #include <rtl/ustring.hxx>
24 #include <rtl/ustrbuf.hxx>
26 #include <sax/tools/converter.hxx>
28 #include <xmloff/namespacemap.hxx>
29 #include <xmloff/xmlnamespace.hxx>
30 #include <xmloff/xmltoken.hxx>
31 #include <xmloff/xmlement.hxx>
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>
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;
53 // XMLFootnoteConfigHelper
55 namespace {
57 /// local helper class for import of quo-vadis and ergo-sum elements
58 class XMLFootnoteConfigHelper : public SvXMLImportContext
60 OUStringBuffer sBuffer;
61 XMLFootnoteConfigurationImportContext& rConfig;
62 bool bIsBegin;
64 public:
66 XMLFootnoteConfigHelper(
67 SvXMLImport& rImport,
68 XMLFootnoteConfigurationImportContext& rConfigImport,
69 bool bBegin);
71 virtual void SAL_CALL endFastElement(sal_Int32 nElement) override;
73 virtual void SAL_CALL characters( const OUString& rChars ) override;
78 XMLFootnoteConfigHelper::XMLFootnoteConfigHelper(
79 SvXMLImport& rImport,
80 XMLFootnoteConfigurationImportContext& rConfigImport,
81 bool bBegin)
82 : SvXMLImportContext(rImport)
83 , rConfig(rConfigImport)
84 , bIsBegin(bBegin)
88 void XMLFootnoteConfigHelper::endFastElement(sal_Int32 )
90 if (bIsBegin)
92 rConfig.SetBeginNotice(sBuffer.makeStringAndClear());
94 else
96 rConfig.SetEndNotice(sBuffer.makeStringAndClear());
98 // rConfig = NULL; // import contexts are ref-counted
101 void XMLFootnoteConfigHelper::characters( const OUString& rChars )
103 sBuffer.append(rChars);
107 // XMLFootnoteConfigurationImportContext
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");
122 XMLFootnoteConfigurationImportContext::XMLFootnoteConfigurationImportContext(
123 SvXMLImport& rImport,
124 sal_Int32 /*nElement*/,
125 const Reference<XFastAttributeList> & xAttrList)
126 : SvXMLStyleContext(rImport, XmlStyleFamily::TEXT_FOOTNOTECONFIG)
127 , sNumFormat("1")
128 , sNumSync("false")
129 , nOffset(0)
130 , nNumbering(FootnoteNumbering::PER_PAGE)
131 , bPosition(false)
132 , bIsEndnote(false)
134 for (auto &aIter : sax_fastparser::castToFastAttributeList( xAttrList ))
136 if( aIter.getToken() == XML_ELEMENT(TEXT, XML_NOTE_CLASS) )
138 if( IsXMLToken(aIter, XML_ENDNOTE ) )
140 bIsEndnote = true;
141 SetFamily( XmlStyleFamily::TEXT_FOOTNOTECONFIG );
143 break;
148 XMLFootnoteConfigurationImportContext::~XMLFootnoteConfigurationImportContext()
152 SvXMLEnumMapEntry<sal_Int16> const aFootnoteNumberingMap[] =
154 { XML_PAGE, FootnoteNumbering::PER_PAGE },
155 { XML_CHAPTER, FootnoteNumbering::PER_CHAPTER },
156 { XML_DOCUMENT, FootnoteNumbering::PER_DOCUMENT },
157 { XML_TOKEN_INVALID, 0 },
160 void XMLFootnoteConfigurationImportContext::SetAttribute( sal_Int32 nElement,
161 const OUString& rValue )
163 switch (nElement)
165 case XML_ELEMENT(TEXT, XML_CITATION_STYLE_NAME):
166 sCitationStyle = rValue;
167 break;
168 case XML_ELEMENT(TEXT, XML_CITATION_BODY_STYLE_NAME):
169 sAnchorStyle = rValue;
170 break;
171 case XML_ELEMENT(TEXT, XML_DEFAULT_STYLE_NAME):
172 sDefaultStyle = rValue;
173 break;
174 case XML_ELEMENT(TEXT, XML_MASTER_PAGE_NAME):
175 sPageStyle = rValue;
176 break;
177 case XML_ELEMENT(TEXT, XML_START_VALUE):
178 case XML_ELEMENT(TEXT, XML_OFFSET): // for backwards compatibility with SRC630 & earlier
180 sal_Int32 nTmp;
181 if (::sax::Converter::convertNumber(nTmp, rValue))
183 nOffset = static_cast<sal_uInt16>(nTmp);
185 break;
187 case XML_ELEMENT(STYLE, XML_NUM_PREFIX):
188 case XML_ELEMENT(TEXT, XML_NUM_PREFIX): // for backwards compatibility with SRC630 & earlier
189 sPrefix = rValue;
190 break;
191 case XML_ELEMENT(STYLE, XML_NUM_SUFFIX):
192 case XML_ELEMENT(TEXT, XML_NUM_SUFFIX): // for backwards compatibility with SRC630 & earlier
193 sSuffix = rValue;
194 break;
195 case XML_ELEMENT(STYLE, XML_NUM_FORMAT):
196 sNumFormat = rValue;
197 break;
198 case XML_ELEMENT(STYLE, XML_NUM_LETTER_SYNC):
199 sNumSync = rValue;
200 break;
201 case XML_ELEMENT(TEXT, XML_START_NUMBERING_AT):
203 (void)SvXMLUnitConverter::convertEnum(nNumbering, rValue,
204 aFootnoteNumberingMap);
205 break;
207 case XML_ELEMENT(TEXT, XML_FOOTNOTES_POSITION):
208 bPosition = IsXMLToken( rValue, XML_DOCUMENT );
209 break;
210 default:
211 ; // ignore
215 css::uno::Reference< css::xml::sax::XFastContextHandler > XMLFootnoteConfigurationImportContext::createFastChildContext(
216 sal_Int32 nElement,
217 const css::uno::Reference< css::xml::sax::XFastAttributeList >& )
219 css::uno::Reference< css::xml::sax::XFastContextHandler > xContext;
221 if (bIsEndnote)
222 return nullptr;
224 switch (nElement)
226 case XML_ELEMENT(TEXT, XML_FOOTNOTE_CONTINUATION_NOTICE_FORWARD):
227 xContext = new XMLFootnoteConfigHelper(GetImport(), *this, false);
228 break;
229 case XML_ELEMENT(TEXT, XML_FOOTNOTE_CONTINUATION_NOTICE_BACKWARD):
230 xContext = new XMLFootnoteConfigHelper(GetImport(), *this, true);
231 break;
234 return xContext;
237 // Rename method <CreateAndInsertLate(..)> to <Finish(..)> (#i40597#)
238 void XMLFootnoteConfigurationImportContext::Finish( bool bOverwrite )
241 if (!bOverwrite)
242 return;
244 if (bIsEndnote)
246 Reference<XEndnotesSupplier> xSupplier(
247 GetImport().GetModel(), UNO_QUERY);
248 if (xSupplier.is())
250 ProcessSettings(xSupplier->getEndnoteSettings());
253 else
255 Reference<XFootnotesSupplier> xSupplier(
256 GetImport().GetModel(), UNO_QUERY);
257 if (xSupplier.is())
259 ProcessSettings(xSupplier->getFootnoteSettings());
262 // else: ignore (there's only one configuration, so we can only overwrite)
265 void XMLFootnoteConfigurationImportContext::ProcessSettings(
266 const Reference<XPropertySet> & rConfig)
268 Any aAny;
270 if (!sCitationStyle.isEmpty())
272 aAny <<= GetImport().GetStyleDisplayName(
273 XmlStyleFamily::TEXT_TEXT, sCitationStyle );
274 rConfig->setPropertyValue(gsPropertyCharStyleName, aAny);
277 if (!sAnchorStyle.isEmpty())
279 aAny <<= GetImport().GetStyleDisplayName(
280 XmlStyleFamily::TEXT_TEXT, sAnchorStyle );
281 rConfig->setPropertyValue(gsPropertyAnchorCharStyleName, aAny);
284 if (!sPageStyle.isEmpty())
286 aAny <<= GetImport().GetStyleDisplayName(
287 XmlStyleFamily::MASTER_PAGE, sPageStyle );
288 rConfig->setPropertyValue(gsPropertyPageStyleName, aAny);
291 if (!sDefaultStyle.isEmpty())
293 aAny <<= GetImport().GetStyleDisplayName(
294 XmlStyleFamily::TEXT_PARAGRAPH, sDefaultStyle );
295 rConfig->setPropertyValue(gsPropertyParagraphStyleName, aAny);
298 rConfig->setPropertyValue(gsPropertyPrefix, Any(sPrefix));
300 rConfig->setPropertyValue(gsPropertySuffix, Any(sSuffix));
302 sal_Int16 nNumType = NumberingType::ARABIC;
303 GetImport().GetMM100UnitConverter().convertNumFormat( nNumType, sNumFormat,
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;
310 rConfig->setPropertyValue(gsPropertyNumberingType, Any(nNumType));
312 rConfig->setPropertyValue(gsPropertyStartAt, Any(nOffset));
314 if (!bIsEndnote)
316 rConfig->setPropertyValue(gsPropertyPositionEndOfDoc, Any(bPosition));
317 rConfig->setPropertyValue(gsPropertyFootnoteCounting, Any(nNumbering));
318 rConfig->setPropertyValue(gsPropertyEndNotice, Any(sEndNotice));
319 rConfig->setPropertyValue(gsPropertyBeginNotice, Any(sBeginNotice));
323 void XMLFootnoteConfigurationImportContext::SetBeginNotice(
324 const OUString& sText)
326 sBeginNotice = sText;
329 void XMLFootnoteConfigurationImportContext::SetEndNotice(
330 const OUString& sText)
332 sEndNotice = sText;
335 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */