Avoid potential negative array index access to cached text.
[LibreOffice.git] / writerfilter / source / dmapper / SdtHelper.hxx
blob85b95a48818b6123947100efb80a1a31f7f00949
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/.
8 */
10 #pragma once
12 #include <vector>
13 #include <optional>
14 #include <unordered_map>
16 #include <com/sun/star/beans/PropertyValue.hpp>
17 #include <com/sun/star/text/XTextRange.hpp>
18 #include <com/sun/star/uno/Sequence.hxx>
19 #include <com/sun/star/xml/dom/XDocument.hpp>
21 #include <rtl/ustrbuf.hxx>
22 #include <tools/ref.hxx>
24 namespace com::sun::star
26 namespace uno
28 class XComponentContext;
30 namespace awt
32 struct Size;
33 class XControlModel;
37 namespace writerfilter::dmapper
39 class DomainMapper_Impl;
41 enum class SdtControlType
43 datePicker,
44 dropDown,
45 plainText,
46 richText,
47 checkBox,
48 picture,
49 comboBox,
50 unsupported, // Sdt block is defined, but we still do not support such type of field
51 unknown
54 /**
55 * Helper to create form controls from w:sdt tokens.
57 * w:sdt tokens can't be imported as form fields, as w:sdt supports
58 * e.g. date picking as well.
60 class SdtHelper final : public virtual SvRefBase
62 DomainMapper_Impl& m_rDM_Impl;
63 css::uno::Reference<css::uno::XComponentContext> m_xComponentContext;
65 /// Items of the drop-down control: <w:listItem w:value="...">.
66 std::vector<OUString> m_aDropDownItems;
67 /// Display texts of a drop-down control: <w:listItem w:displayText="...">.
68 std::vector<OUString> m_aDropDownDisplayTexts;
69 /// Type of sdt control
70 SdtControlType m_aControlType;
71 sal_uInt32 m_nSdtType = 0;
72 /// Pieces of the default text -- currently used only by the dropdown control.
73 OUStringBuffer m_aSdtTexts;
74 /// Date ISO string contained in the w:date element, used by the date control.
75 OUStringBuffer m_sDate;
76 /// Date format string as it comes from the ooxml document.
77 OUStringBuffer m_sDateFormat;
79 /// <w:dataBinding w:prefixMappings="">
80 OUString m_sDataBindingPrefixMapping;
81 /// <w:dataBinding w:xpath="">
82 OUString m_sDataBindingXPath;
83 /// <w:dataBinding w:storeItemID="">
84 OUString m_sDataBindingStoreItemID;
86 /// Start range of the date or plain text field
87 css::uno::Reference<css::text::XTextRange> m_xFieldStartRange;
88 /// Locale string as it comes from the ooxml document.
89 OUStringBuffer m_sLocale;
90 /// Grab bag to store unsupported SDTs, aiming to save them back on export.
91 std::vector<css::beans::PropertyValue> m_aGrabBag;
92 /// Has inserted texts for plain text control
93 bool m_bHasUnusedText;
95 bool m_bHasElements;
96 /// The last stored SDT element is outside paragraphs.
97 bool m_bOutsideAParagraph;
99 /// Storage for all properties documents as xml::dom::XDocument for later querying xpath for data
100 std::unordered_map<OUString, css::uno::Reference<css::xml::dom::XDocument>> m_xPropertiesXMLs;
102 /// Check if m_xPropertiesXMLs is initialized and loaded (need extra flag to distinguish
103 /// empty sequence from not yet initialized)
104 bool m_bPropertiesXMLsLoaded;
106 /// Current contents are placeholder text.
107 bool m_bShowingPlcHdr = false;
109 /// If this is a checkbox, is the checkbox checked?
110 bool m_bChecked = false;
112 /// If this is a checkbox, the value of a checked checkbox.
113 OUString m_aCheckedState;
115 /// If this is a checkbox, the value of an unchecked checkbox.
116 OUString m_aUncheckedState;
118 /// Create and append the drawing::XControlShape, containing the various models.
119 void createControlShape(css::awt::Size aSize,
120 css::uno::Reference<css::awt::XControlModel> const& xControlModel,
121 const css::uno::Sequence<css::beans::PropertyValue>& rGrabBag);
123 void loadPropertiesXMLs();
125 /// <w:placeholder>'s <w:docPart w:val="...">.
126 OUString m_aPlaceholderDocPart;
128 /// <w:sdtPr>'s <w15:color w:val="...">.
129 OUString m_aColor;
131 /// <w:sdtPr>'s <w15:appearance w:val="...">.
132 OUString m_aAppearance;
134 /// <w:sdtPr>'s <w:alias w:val="...">.
135 OUString m_aAlias;
137 /// <w:sdtPr>'s <w:tag w:val="...">.
138 OUString m_aTag;
140 /// <w:sdtPr>'s <w:id w:val="...">.
141 sal_Int32 m_nId = 0;
143 /// <w:sdtPr>'s <w:tabIndex w:val="...">.
144 sal_uInt32 m_nTabIndex = 0;
146 /// <w:sdtPr>'s <w:lock w:val="...">.
147 OUString m_aLock;
149 public:
150 explicit SdtHelper(DomainMapper_Impl& rDM_Impl,
151 css::uno::Reference<css::uno::XComponentContext> xContext);
152 ~SdtHelper() override;
154 std::vector<OUString>& getDropDownItems() { return m_aDropDownItems; }
155 std::vector<OUString>& getDropDownDisplayTexts() { return m_aDropDownDisplayTexts; }
156 OUStringBuffer& getSdtTexts() { return m_aSdtTexts; }
158 OUStringBuffer& getDate() { return m_sDate; }
160 OUStringBuffer& getDateFormat() { return m_sDateFormat; }
162 void setDataBindingPrefixMapping(const OUString& sValue)
164 m_sDataBindingPrefixMapping = sValue;
166 const OUString& GetDataBindingPrefixMapping() const { return m_sDataBindingPrefixMapping; }
168 void setDataBindingXPath(const OUString& sValue) { m_sDataBindingXPath = sValue; }
169 const OUString& GetDataBindingXPath() const { return m_sDataBindingXPath; }
171 void setDataBindingStoreItemID(const OUString& sValue) { m_sDataBindingStoreItemID = sValue; }
172 const OUString& GetDataBindingStoreItemID() const { return m_sDataBindingStoreItemID; }
174 bool isFieldStartRangeSet() const { return m_xFieldStartRange.is(); }
175 void setFieldStartRange(const css::uno::Reference<css::text::XTextRange>& xStartRange)
177 m_xFieldStartRange = xStartRange;
180 OUStringBuffer& getLocale() { return m_sLocale; }
181 /// If createControlShape() was ever called.
182 bool hasElements() const { return m_bHasElements; }
184 void setOutsideAParagraph(bool bOutsideAParagraph)
186 m_bOutsideAParagraph = bOutsideAParagraph;
189 bool isOutsideAParagraph() const { return m_bOutsideAParagraph; }
191 SdtControlType getControlType() { return m_aControlType; }
192 void setControlType(SdtControlType aType) { m_aControlType = aType; }
194 void SetSdtType(sal_uInt32 nSdtType) { m_nSdtType = nSdtType; }
195 sal_uInt32 GetSdtType() const { return m_nSdtType; }
197 /// Create drop-down control from w:sdt's w:dropDownList.
198 void createDropDownControl();
199 /// Create date control from w:sdt's w:date.
200 void createDateContentControl();
202 void createPlainTextControl();
204 void appendToInteropGrabBag(const css::beans::PropertyValue& rValue);
205 css::uno::Sequence<css::beans::PropertyValue> getInteropGrabBagAndClear();
206 bool isInteropGrabBagEmpty() const;
207 bool containedInInteropGrabBag(const OUString& rValueName);
208 sal_Int32 getInteropGrabBagSize() const;
210 void setHasUnusedText(bool bHasUnusedText) { m_bHasUnusedText = bHasUnusedText; }
211 bool hasUnusedText() const { return m_bHasUnusedText; }
213 void SetShowingPlcHdr();
214 bool GetShowingPlcHdr() const;
216 void SetChecked();
217 bool GetChecked() const;
218 void SetCheckedState(const OUString& rCheckedState);
219 const OUString& GetCheckedState() const;
220 void SetUncheckedState(const OUString& rUncheckedState);
221 const OUString& GetUncheckedState() const;
223 /// Clear all collected attributes for further reuse
224 void clear();
226 void SetPlaceholderDocPart(const OUString& rPlaceholderDocPart);
227 const OUString& GetPlaceholderDocPart() const;
229 void SetColor(const OUString& rColor);
230 const OUString& GetColor() const;
232 void SetAppearance(const OUString& rAppearance);
233 const OUString& GetAppearance() const;
235 void SetAlias(const OUString& rAlias);
236 const OUString& GetAlias() const;
238 void SetTag(const OUString& rTag);
239 const OUString& GetTag() const;
241 void SetId(sal_Int32 nId);
242 sal_Int32 GetId() const;
244 void SetTabIndex(sal_uInt32 nTabIndex);
245 sal_uInt32 GetTabIndex() const;
247 void SetLock(const OUString& rLock);
248 const OUString& GetLock() const;
250 std::optional<OUString> getValueFromDataBinding();
253 } // namespace writerfilter::dmapper
255 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */