1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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
)
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
))
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
;
64 case XML_ELEMENT(LO_EXT
, XML_CHECKBOX
):
66 if (sax::Converter::convertBool(bTmp
, rIter
.toView()))
72 case XML_ELEMENT(LO_EXT
, XML_CHECKED
):
74 if (sax::Converter::convertBool(bTmp
, rIter
.toView()))
80 case XML_ELEMENT(LO_EXT
, XML_CHECKED_STATE
):
82 m_aCheckedState
= rIter
.toString();
85 case XML_ELEMENT(LO_EXT
, XML_UNCHECKED_STATE
):
87 m_aUncheckedState
= rIter
.toString();
90 case XML_ELEMENT(LO_EXT
, XML_PICTURE
):
92 if (sax::Converter::convertBool(bTmp
, rIter
.toView()))
98 case XML_ELEMENT(LO_EXT
, XML_DATE
):
100 if (sax::Converter::convertBool(bTmp
, rIter
.toView()))
106 case XML_ELEMENT(LO_EXT
, XML_DATE_FORMAT
):
108 m_aDateFormat
= rIter
.toString();
111 case XML_ELEMENT(LO_EXT
, XML_DATE_RFC_LANGUAGE_TAG
):
113 m_aDateLanguage
= rIter
.toString();
116 case XML_ELEMENT(LO_EXT
, XML_CURRENT_DATE
):
118 m_aCurrentDate
= rIter
.toString();
121 case XML_ELEMENT(LO_EXT
, XML_PLAIN_TEXT
):
123 if (sax::Converter::convertBool(bTmp
, rIter
.toView()))
129 case XML_ELEMENT(LO_EXT
, XML_COMBOBOX
):
131 if (sax::Converter::convertBool(bTmp
, rIter
.toView()))
137 case XML_ELEMENT(LO_EXT
, XML_DROPDOWN
):
139 if (sax::Converter::convertBool(bTmp
, rIter
.toView()))
145 case XML_ELEMENT(LO_EXT
, XML_ALIAS
):
147 m_aAlias
= rIter
.toString();
150 case XML_ELEMENT(LO_EXT
, XML_TAG
):
152 m_aTag
= rIter
.toString();
155 case XML_ELEMENT(LO_EXT
, XML_ID
):
157 if (sax::Converter::convertNumber(nTmp
, rIter
.toView()))
163 case XML_ELEMENT(LO_EXT
, XML_TAB_INDEX
):
165 if (sax::Converter::convertNumber(nTmp
, rIter
.toView()))
171 case XML_ELEMENT(LO_EXT
, XML_LOCK
):
173 m_aLock
= rIter
.toString();
177 XMLOFF_WARN_UNKNOWN("xmloff", rIter
);
182 void XMLContentControlContext::endFastElement(sal_Int32
)
186 SAL_WARN("xmloff.text", "XMLContentControlContext::endFastElement: no m_xStart");
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");
207 uno::Reference
<beans::XPropertySet
> xPropertySet(xContentControl
, uno::UNO_QUERY
);
208 if (!xPropertySet
.is())
213 if (m_bShowingPlaceHolder
)
215 xPropertySet
->setPropertyValue("ShowingPlaceHolder", uno::Any(m_bShowingPlaceHolder
));
220 xPropertySet
->setPropertyValue("Checkbox", uno::Any(m_bCheckbox
));
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
)));
242 xPropertySet
->setPropertyValue("Picture", uno::Any(m_bPicture
));
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
));
264 xPropertySet
->setPropertyValue("PlainText", uno::Any(m_bPlainText
));
269 xPropertySet
->setPropertyValue("ComboBox", uno::Any(m_bComboBox
));
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
));
289 xPropertySet
->setPropertyValue("Id", uno::Any(m_nId
));
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
)
309 case XML_ELEMENT(LO_EXT
, XML_LIST_ITEM
):
310 return new XMLListItemContext(GetImport(), *this);
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
;
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();
352 case XML_ELEMENT(LO_EXT
, XML_VALUE
):
354 aValue
= rIter
.toString();
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: */