Avoid potential negative array index access to cached text.
[LibreOffice.git] / xmloff / source / text / xmlcontentcontrolcontext.cxx
blob2a7ef5b2eebe522d41e486b1b25e16115cca5025
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 "xmlcontentcontrolcontext.hxx"
22 #include <com/sun/star/beans/XPropertySet.hpp>
24 #include <sax/tools/converter.hxx>
25 #include <xmloff/xmlimp.hxx>
26 #include <xmloff/xmlnamespace.hxx>
27 #include <xmloff/xmltoken.hxx>
28 #include <comphelper/propertyvalue.hxx>
29 #include <comphelper/sequence.hxx>
31 #include "XMLTextMarkImportContext.hxx"
32 #include "txtparai.hxx"
34 using namespace com::sun::star;
35 using namespace xmloff::token;
37 XMLContentControlContext::XMLContentControlContext(SvXMLImport& rImport, sal_Int32 /*nElement*/,
38 XMLHints_Impl& rHints, bool& rIgnoreLeadingSpace)
39 : SvXMLImportContext(rImport)
40 , m_rHints(rHints)
41 , m_rIgnoreLeadingSpace(rIgnoreLeadingSpace)
42 , m_xStart(GetImport().GetTextImport()->GetCursorAsRange()->getStart())
46 void XMLContentControlContext::startFastElement(
47 sal_Int32 /*nElement*/, const uno::Reference<xml::sax::XFastAttributeList>& xAttrList)
49 for (auto& rIter : sax_fastparser::castToFastAttributeList(xAttrList))
51 bool bTmp = false;
52 sal_Int32 nTmp = 0;
54 switch (rIter.getToken())
56 case XML_ELEMENT(LO_EXT, XML_SHOWING_PLACE_HOLDER):
58 if (sax::Converter::convertBool(bTmp, rIter.toView()))
60 m_bShowingPlaceHolder = bTmp;
62 break;
64 case XML_ELEMENT(LO_EXT, XML_CHECKBOX):
66 if (sax::Converter::convertBool(bTmp, rIter.toView()))
68 m_bCheckbox = bTmp;
70 break;
72 case XML_ELEMENT(LO_EXT, XML_CHECKED):
74 if (sax::Converter::convertBool(bTmp, rIter.toView()))
76 m_bChecked = bTmp;
78 break;
80 case XML_ELEMENT(LO_EXT, XML_CHECKED_STATE):
82 m_aCheckedState = rIter.toString();
83 break;
85 case XML_ELEMENT(LO_EXT, XML_UNCHECKED_STATE):
87 m_aUncheckedState = rIter.toString();
88 break;
90 case XML_ELEMENT(LO_EXT, XML_PICTURE):
92 if (sax::Converter::convertBool(bTmp, rIter.toView()))
94 m_bPicture = bTmp;
96 break;
98 case XML_ELEMENT(LO_EXT, XML_DATE):
100 if (sax::Converter::convertBool(bTmp, rIter.toView()))
102 m_bDate = bTmp;
104 break;
106 case XML_ELEMENT(LO_EXT, XML_DATE_FORMAT):
108 m_aDateFormat = rIter.toString();
109 break;
111 case XML_ELEMENT(LO_EXT, XML_DATE_RFC_LANGUAGE_TAG):
113 m_aDateLanguage = rIter.toString();
114 break;
116 case XML_ELEMENT(LO_EXT, XML_CURRENT_DATE):
118 m_aCurrentDate = rIter.toString();
119 break;
121 case XML_ELEMENT(LO_EXT, XML_PLAIN_TEXT):
123 if (sax::Converter::convertBool(bTmp, rIter.toView()))
125 m_bPlainText = bTmp;
127 break;
129 case XML_ELEMENT(LO_EXT, XML_COMBOBOX):
131 if (sax::Converter::convertBool(bTmp, rIter.toView()))
133 m_bComboBox = bTmp;
135 break;
137 case XML_ELEMENT(LO_EXT, XML_DROPDOWN):
139 if (sax::Converter::convertBool(bTmp, rIter.toView()))
141 m_bDropDown = bTmp;
143 break;
145 case XML_ELEMENT(LO_EXT, XML_ALIAS):
147 m_aAlias = rIter.toString();
148 break;
150 case XML_ELEMENT(LO_EXT, XML_TAG):
152 m_aTag = rIter.toString();
153 break;
155 case XML_ELEMENT(LO_EXT, XML_ID):
157 if (sax::Converter::convertNumber(nTmp, rIter.toView()))
159 m_nId = nTmp;
161 break;
163 case XML_ELEMENT(LO_EXT, XML_TAB_INDEX):
165 if (sax::Converter::convertNumber(nTmp, rIter.toView()))
167 m_nTabIndex = nTmp;
169 break;
171 case XML_ELEMENT(LO_EXT, XML_LOCK):
173 m_aLock = rIter.toString();
174 break;
176 default:
177 XMLOFF_WARN_UNKNOWN("xmloff", rIter);
182 void XMLContentControlContext::endFastElement(sal_Int32)
184 if (!m_xStart.is())
186 SAL_WARN("xmloff.text", "XMLContentControlContext::endFastElement: no m_xStart");
187 return;
190 uno::Reference<text::XTextRange> xEndRange
191 = GetImport().GetTextImport()->GetCursorAsRange()->getStart();
193 // Create range for insertion.
194 uno::Reference<text::XTextCursor> xInsertionCursor
195 = GetImport().GetTextImport()->GetText()->createTextCursorByRange(xEndRange);
196 xInsertionCursor->gotoRange(m_xStart, /*bExpand=*/true);
198 uno::Reference<text::XTextContent> xContentControl
199 = XMLTextMarkImportContext::CreateAndInsertMark(
200 GetImport(), "com.sun.star.text.ContentControl", OUString(), xInsertionCursor);
201 if (!xContentControl.is())
203 SAL_WARN("xmloff.text", "cannot insert content control");
204 return;
207 uno::Reference<beans::XPropertySet> xPropertySet(xContentControl, uno::UNO_QUERY);
208 if (!xPropertySet.is())
210 return;
213 if (m_bShowingPlaceHolder)
215 xPropertySet->setPropertyValue("ShowingPlaceHolder", uno::Any(m_bShowingPlaceHolder));
218 if (m_bCheckbox)
220 xPropertySet->setPropertyValue("Checkbox", uno::Any(m_bCheckbox));
222 if (m_bChecked)
224 xPropertySet->setPropertyValue("Checked", uno::Any(m_bChecked));
226 if (!m_aCheckedState.isEmpty())
228 xPropertySet->setPropertyValue("CheckedState", uno::Any(m_aCheckedState));
230 if (!m_aUncheckedState.isEmpty())
232 xPropertySet->setPropertyValue("UncheckedState", uno::Any(m_aUncheckedState));
234 if (!m_aListItems.empty())
236 xPropertySet->setPropertyValue("ListItems",
237 uno::Any(comphelper::containerToSequence(m_aListItems)));
240 if (m_bPicture)
242 xPropertySet->setPropertyValue("Picture", uno::Any(m_bPicture));
245 if (m_bDate)
247 xPropertySet->setPropertyValue("Date", uno::Any(m_bDate));
249 if (!m_aDateFormat.isEmpty())
251 xPropertySet->setPropertyValue("DateFormat", uno::Any(m_aDateFormat));
253 if (!m_aDateLanguage.isEmpty())
255 xPropertySet->setPropertyValue("DateLanguage", uno::Any(m_aDateLanguage));
257 if (!m_aCurrentDate.isEmpty())
259 xPropertySet->setPropertyValue("CurrentDate", uno::Any(m_aCurrentDate));
262 if (m_bPlainText)
264 xPropertySet->setPropertyValue("PlainText", uno::Any(m_bPlainText));
267 if (m_bComboBox)
269 xPropertySet->setPropertyValue("ComboBox", uno::Any(m_bComboBox));
272 if (m_bDropDown)
274 xPropertySet->setPropertyValue("DropDown", uno::Any(m_bDropDown));
277 if (!m_aAlias.isEmpty())
279 xPropertySet->setPropertyValue("Alias", uno::Any(m_aAlias));
282 if (!m_aTag.isEmpty())
284 xPropertySet->setPropertyValue("Tag", uno::Any(m_aTag));
287 if (m_nId)
289 xPropertySet->setPropertyValue("Id", uno::Any(m_nId));
292 if (m_nTabIndex)
294 xPropertySet->setPropertyValue("TabIndex", uno::Any(m_nTabIndex));
297 if (!m_aLock.isEmpty())
299 xPropertySet->setPropertyValue("Lock", uno::Any(m_aLock));
303 css::uno::Reference<css::xml::sax::XFastContextHandler>
304 XMLContentControlContext::createFastChildContext(
305 sal_Int32 nElement, const uno::Reference<xml::sax::XFastAttributeList>& xAttrList)
307 switch (nElement)
309 case XML_ELEMENT(LO_EXT, XML_LIST_ITEM):
310 return new XMLListItemContext(GetImport(), *this);
311 break;
312 default:
313 break;
316 return XMLImpSpanContext_Impl::CreateSpanContext(GetImport(), nElement, xAttrList, m_rHints,
317 m_rIgnoreLeadingSpace);
320 void XMLContentControlContext::characters(const OUString& rChars)
322 GetImport().GetTextImport()->InsertString(rChars, m_rIgnoreLeadingSpace);
325 void XMLContentControlContext::AppendListItem(const css::beans::PropertyValues& rListItem)
327 m_aListItems.push_back(rListItem);
330 XMLListItemContext::XMLListItemContext(SvXMLImport& rImport,
331 XMLContentControlContext& rContentControl)
332 : SvXMLImportContext(rImport)
333 , m_rContentControl(rContentControl)
337 void XMLListItemContext::startFastElement(
338 sal_Int32 /*nElement*/, const uno::Reference<xml::sax::XFastAttributeList>& xAttrList)
340 OUString aDisplayText;
341 OUString aValue;
343 for (auto& rIter : sax_fastparser::castToFastAttributeList(xAttrList))
345 switch (rIter.getToken())
347 case XML_ELEMENT(LO_EXT, XML_DISPLAY_TEXT):
349 aDisplayText = rIter.toString();
350 break;
352 case XML_ELEMENT(LO_EXT, XML_VALUE):
354 aValue = rIter.toString();
355 break;
357 default:
358 XMLOFF_WARN_UNKNOWN("xmloff", rIter);
362 uno::Sequence<beans::PropertyValue> aListItem = {
363 comphelper::makePropertyValue("DisplayText", uno::Any(aDisplayText)),
364 comphelper::makePropertyValue("Value", uno::Any(aValue)),
366 m_rContentControl.AppendListItem(aListItem);
369 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */