Avoid potential negative array index access to cached text.
[LibreOffice.git] / xmloff / source / text / XMLLineNumberingImportContext.cxx
blob0ba08972df2faab82d8d7c819ebbbdef5780aa25
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 .
20 #include <XMLLineNumberingImportContext.hxx>
21 #include "XMLLineNumberingSeparatorImportContext.hxx"
22 #include <com/sun/star/beans/XPropertySet.hpp>
23 #include <com/sun/star/frame/XModel.hpp>
24 #include <com/sun/star/text/XLineNumberingProperties.hpp>
25 #include <com/sun/star/style/LineNumberPosition.hpp>
26 #include <com/sun/star/style/NumberingType.hpp>
27 #include <sax/tools/converter.hxx>
28 #include <xmloff/xmlimp.hxx>
29 #include <xmloff/xmluconv.hxx>
30 #include <xmloff/xmlnamespace.hxx>
31 #include <xmloff/namespacemap.hxx>
32 #include <xmloff/xmltoken.hxx>
33 #include <xmloff/xmlement.hxx>
36 using namespace ::com::sun::star;
37 using namespace ::com::sun::star::uno;
38 using namespace ::com::sun::star::style;
39 using namespace ::xmloff::token;
41 using ::com::sun::star::beans::XPropertySet;
42 using ::com::sun::star::xml::sax::XFastAttributeList;
43 using ::com::sun::star::text::XLineNumberingProperties;
46 constexpr OUStringLiteral gsCharStyleName(u"CharStyleName");
47 constexpr OUStringLiteral gsCountEmptyLines(u"CountEmptyLines");
48 constexpr OUStringLiteral gsCountLinesInFrames(u"CountLinesInFrames");
49 constexpr OUStringLiteral gsDistance(u"Distance");
50 constexpr OUStringLiteral gsInterval(u"Interval");
51 constexpr OUStringLiteral gsSeparatorText(u"SeparatorText");
52 constexpr OUStringLiteral gsNumberPosition(u"NumberPosition");
53 constexpr OUStringLiteral gsNumberingType(u"NumberingType");
54 constexpr OUStringLiteral gsIsOn(u"IsOn");
55 constexpr OUStringLiteral gsRestartAtEachPage(u"RestartAtEachPage");
56 constexpr OUStringLiteral gsSeparatorInterval(u"SeparatorInterval");
58 XMLLineNumberingImportContext::XMLLineNumberingImportContext(
59 SvXMLImport& rImport)
60 : SvXMLStyleContext(rImport, XmlStyleFamily::TEXT_LINENUMBERINGCONFIG)
61 , sNumFormat(GetXMLToken(XML_1))
62 , sNumLetterSync(GetXMLToken(XML_FALSE))
63 , nOffset(-1)
64 , nNumberPosition(style::LineNumberPosition::LEFT)
65 , nIncrement(-1)
66 , nSeparatorIncrement(-1)
67 , bNumberLines(true)
68 , bCountEmptyLines(true)
69 , bCountInFloatingFrames(false)
70 , bRestartNumbering(false)
74 XMLLineNumberingImportContext::~XMLLineNumberingImportContext()
78 void XMLLineNumberingImportContext::SetAttribute( sal_Int32 nElement,
79 const OUString& rValue )
81 bool bTmp(false);
82 sal_Int32 nTmp;
84 switch (nElement)
86 case XML_ELEMENT(TEXT, XML_STYLE_NAME):
87 sStyleName = rValue;
88 break;
90 case XML_ELEMENT(TEXT, XML_NUMBER_LINES):
91 if (::sax::Converter::convertBool(bTmp, rValue))
93 bNumberLines = bTmp;
95 break;
97 case XML_ELEMENT(TEXT, XML_COUNT_EMPTY_LINES):
98 if (::sax::Converter::convertBool(bTmp, rValue))
100 bCountEmptyLines = bTmp;
102 break;
104 case XML_ELEMENT(TEXT, XML_COUNT_IN_TEXT_BOXES):
105 if (::sax::Converter::convertBool(bTmp, rValue))
107 bCountInFloatingFrames = bTmp;
109 break;
111 case XML_ELEMENT(TEXT, XML_RESTART_ON_PAGE):
112 if (::sax::Converter::convertBool(bTmp, rValue))
114 bRestartNumbering = bTmp;
116 break;
118 case XML_ELEMENT(TEXT, XML_OFFSET):
119 if (GetImport().GetMM100UnitConverter().
120 convertMeasureToCore(nTmp, rValue))
122 nOffset = nTmp;
124 break;
126 case XML_ELEMENT(STYLE, XML_NUM_FORMAT):
127 sNumFormat = rValue;
128 break;
130 case XML_ELEMENT(STYLE, XML_NUM_LETTER_SYNC):
131 sNumLetterSync = rValue;
132 break;
134 case XML_ELEMENT(TEXT, XML_NUMBER_POSITION):
136 static const SvXMLEnumMapEntry<sal_Int16> aLineNumberPositionMap[] =
138 { XML_LEFT, style::LineNumberPosition::LEFT },
139 { XML_RIGHT, style::LineNumberPosition::RIGHT },
140 { XML_INSIDE, style::LineNumberPosition::INSIDE },
141 { XML_OUTSIDE, style::LineNumberPosition::OUTSIDE },
142 { XML_TOKEN_INVALID, 0 }
145 (void)SvXMLUnitConverter::convertEnum(nNumberPosition, rValue,
146 aLineNumberPositionMap);
147 break;
150 case XML_ELEMENT(TEXT, XML_INCREMENT):
151 if (::sax::Converter::convertNumber(nTmp, rValue, 0))
153 nIncrement = static_cast<sal_Int16>(nTmp);
155 break;
159 void XMLLineNumberingImportContext::CreateAndInsert(bool)
161 // insert and block mode is handled in insertStyleFamily
163 // we'll try to get the LineNumberingProperties
164 Reference<XLineNumberingProperties> xSupplier(GetImport().GetModel(),
165 UNO_QUERY);
166 if (!xSupplier.is())
167 return;
169 Reference<XPropertySet> xLineNumbering =
170 xSupplier->getLineNumberingProperties();
172 if (!xLineNumbering.is())
173 return;
175 Any aAny;
177 // set style name (if it exists)
178 if ( GetImport().GetStyles()->FindStyleChildContext(
179 XmlStyleFamily::TEXT_TEXT, sStyleName ) != nullptr )
181 aAny <<= GetImport().GetStyleDisplayName(
182 XmlStyleFamily::TEXT_TEXT, sStyleName );
183 xLineNumbering->setPropertyValue(gsCharStyleName, aAny);
186 xLineNumbering->setPropertyValue(gsSeparatorText, Any(sSeparator));
187 xLineNumbering->setPropertyValue(gsDistance, Any(nOffset));
188 xLineNumbering->setPropertyValue(gsNumberPosition, Any(nNumberPosition));
190 if (nIncrement >= 0)
192 xLineNumbering->setPropertyValue(gsInterval, Any(nIncrement));
195 if (nSeparatorIncrement >= 0)
197 xLineNumbering->setPropertyValue(gsSeparatorInterval, Any(nSeparatorIncrement));
200 xLineNumbering->setPropertyValue(gsIsOn, Any(bNumberLines));
201 xLineNumbering->setPropertyValue(gsCountEmptyLines, Any(bCountEmptyLines));
202 xLineNumbering->setPropertyValue(gsCountLinesInFrames, Any(bCountInFloatingFrames));
203 xLineNumbering->setPropertyValue(gsRestartAtEachPage, Any(bRestartNumbering));
205 sal_Int16 nNumType = NumberingType::ARABIC;
206 GetImport().GetMM100UnitConverter().convertNumFormat( nNumType,
207 sNumFormat,
208 sNumLetterSync );
209 xLineNumbering->setPropertyValue(gsNumberingType, Any(nNumType));
212 css::uno::Reference< css::xml::sax::XFastContextHandler > XMLLineNumberingImportContext::createFastChildContext(
213 sal_Int32 nElement,
214 const css::uno::Reference< css::xml::sax::XFastAttributeList >& )
216 if ( nElement == XML_ELEMENT(TEXT, XML_LINENUMBERING_SEPARATOR) )
217 return new XMLLineNumberingSeparatorImportContext(GetImport(), *this);
218 return nullptr;
221 void XMLLineNumberingImportContext::SetSeparatorText(
222 const OUString& sText)
224 sSeparator = sText;
227 void XMLLineNumberingImportContext::SetSeparatorIncrement(
228 sal_Int16 nIncr)
230 nSeparatorIncrement = nIncr;
233 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */