LibreOffice Module xmloff (master)  1
xmlcontentcontrolcontext.cxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
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  *
9  * This file incorporates work covered by the following license notice:
10  *
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 .
18  */
19 
21 
22 #include <com/sun/star/beans/XPropertySet.hpp>
23 
24 #include <sax/tools/converter.hxx>
25 #include <xmloff/xmlimp.hxx>
26 #include <xmloff/xmlnamespace.hxx>
27 #include <xmloff/xmltoken.hxx>
29 #include <comphelper/sequence.hxx>
30 
32 #include "txtparai.hxx"
33 
34 using namespace com::sun::star;
35 using namespace xmloff::token;
36 
38  XMLHints_Impl& rHints, bool& rIgnoreLeadingSpace)
39  : SvXMLImportContext(rImport)
40  , m_rHints(rHints)
41  , m_rIgnoreLeadingSpace(rIgnoreLeadingSpace)
42  , m_xStart(GetImport().GetTextImport()->GetCursorAsRange()->getStart())
43 {
44 }
45 
47  sal_Int32 /*nElement*/, const uno::Reference<xml::sax::XFastAttributeList>& xAttrList)
48 {
49  for (auto& rIter : sax_fastparser::castToFastAttributeList(xAttrList))
50  {
51  bool bTmp = false;
52 
53  switch (rIter.getToken())
54  {
56  {
57  if (sax::Converter::convertBool(bTmp, rIter.toView()))
58  {
59  m_bShowingPlaceHolder = bTmp;
60  }
61  break;
62  }
63  case XML_ELEMENT(LO_EXT, XML_CHECKBOX):
64  {
65  if (sax::Converter::convertBool(bTmp, rIter.toView()))
66  {
67  m_bCheckbox = bTmp;
68  }
69  break;
70  }
71  case XML_ELEMENT(LO_EXT, XML_CHECKED):
72  {
73  if (sax::Converter::convertBool(bTmp, rIter.toView()))
74  {
75  m_bChecked = bTmp;
76  }
77  break;
78  }
79  case XML_ELEMENT(LO_EXT, XML_CHECKED_STATE):
80  {
81  m_aCheckedState = rIter.toString();
82  break;
83  }
84  case XML_ELEMENT(LO_EXT, XML_UNCHECKED_STATE):
85  {
86  m_aUncheckedState = rIter.toString();
87  break;
88  }
89  case XML_ELEMENT(LO_EXT, XML_PICTURE):
90  {
91  if (sax::Converter::convertBool(bTmp, rIter.toView()))
92  {
93  m_bPicture = bTmp;
94  }
95  break;
96  }
97  case XML_ELEMENT(LO_EXT, XML_DATE):
98  {
99  if (sax::Converter::convertBool(bTmp, rIter.toView()))
100  {
101  m_bDate = bTmp;
102  }
103  break;
104  }
105  case XML_ELEMENT(LO_EXT, XML_DATE_FORMAT):
106  {
107  m_aDateFormat = rIter.toString();
108  break;
109  }
111  {
112  m_aDateLanguage = rIter.toString();
113  break;
114  }
115  case XML_ELEMENT(LO_EXT, XML_CURRENT_DATE):
116  {
117  m_aCurrentDate = rIter.toString();
118  break;
119  }
120  default:
121  XMLOFF_WARN_UNKNOWN("xmloff", rIter);
122  }
123  }
124 }
125 
127 {
128  if (!m_xStart.is())
129  {
130  SAL_WARN("xmloff.text", "XMLContentControlContext::endFastElement: no m_xStart");
131  return;
132  }
133 
134  uno::Reference<text::XTextRange> xEndRange
135  = GetImport().GetTextImport()->GetCursorAsRange()->getStart();
136 
137  // Create range for insertion.
138  uno::Reference<text::XTextCursor> xInsertionCursor
139  = GetImport().GetTextImport()->GetText()->createTextCursorByRange(xEndRange);
140  xInsertionCursor->gotoRange(m_xStart, /*bExpand=*/true);
141 
142  uno::Reference<text::XTextContent> xContentControl
144  GetImport(), "com.sun.star.text.ContentControl", OUString(), xInsertionCursor);
145  if (!xContentControl.is())
146  {
147  SAL_WARN("xmloff.text", "cannot insert content control");
148  return;
149  }
150 
151  uno::Reference<beans::XPropertySet> xPropertySet(xContentControl, uno::UNO_QUERY);
152  if (!xPropertySet.is())
153  {
154  return;
155  }
156 
158  {
159  xPropertySet->setPropertyValue("ShowingPlaceHolder", uno::Any(m_bShowingPlaceHolder));
160  }
161 
162  if (m_bCheckbox)
163  {
164  xPropertySet->setPropertyValue("Checkbox", uno::Any(m_bCheckbox));
165  }
166  if (m_bChecked)
167  {
168  xPropertySet->setPropertyValue("Checked", uno::Any(m_bChecked));
169  }
170  if (!m_aCheckedState.isEmpty())
171  {
172  xPropertySet->setPropertyValue("CheckedState", uno::Any(m_aCheckedState));
173  }
174  if (!m_aUncheckedState.isEmpty())
175  {
176  xPropertySet->setPropertyValue("UncheckedState", uno::Any(m_aUncheckedState));
177  }
178  if (!m_aListItems.empty())
179  {
180  xPropertySet->setPropertyValue("ListItems",
182  }
183 
184  if (m_bPicture)
185  {
186  xPropertySet->setPropertyValue("Picture", uno::Any(m_bPicture));
187  }
188 
189  if (m_bDate)
190  {
191  xPropertySet->setPropertyValue("Date", uno::Any(m_bDate));
192  }
193  if (!m_aDateFormat.isEmpty())
194  {
195  xPropertySet->setPropertyValue("DateFormat", uno::Any(m_aDateFormat));
196  }
197  if (!m_aDateLanguage.isEmpty())
198  {
199  xPropertySet->setPropertyValue("DateLanguage", uno::Any(m_aDateLanguage));
200  }
201  if (!m_aCurrentDate.isEmpty())
202  {
203  xPropertySet->setPropertyValue("CurrentDate", uno::Any(m_aCurrentDate));
204  }
205 }
206 
207 css::uno::Reference<css::xml::sax::XFastContextHandler>
209  sal_Int32 nElement, const uno::Reference<xml::sax::XFastAttributeList>& xAttrList)
210 {
211  switch (nElement)
212  {
213  case XML_ELEMENT(LO_EXT, XML_LIST_ITEM):
214  return new XMLListItemContext(GetImport(), *this);
215  break;
216  default:
217  break;
218  }
219 
220  return XMLImpSpanContext_Impl::CreateSpanContext(GetImport(), nElement, xAttrList, m_rHints,
222 }
223 
224 void XMLContentControlContext::characters(const OUString& rChars)
225 {
226  GetImport().GetTextImport()->InsertString(rChars, m_rIgnoreLeadingSpace);
227 }
228 
229 void XMLContentControlContext::AppendListItem(const css::beans::PropertyValues& rListItem)
230 {
231  m_aListItems.push_back(rListItem);
232 }
233 
235  XMLContentControlContext& rContentControl)
236  : SvXMLImportContext(rImport)
237  , m_rContentControl(rContentControl)
238 {
239 }
240 
242  sal_Int32 /*nElement*/, const uno::Reference<xml::sax::XFastAttributeList>& xAttrList)
243 {
244  OUString aDisplayText;
245  OUString aValue;
246 
247  for (auto& rIter : sax_fastparser::castToFastAttributeList(xAttrList))
248  {
249  switch (rIter.getToken())
250  {
251  case XML_ELEMENT(LO_EXT, XML_DISPLAY_TEXT):
252  {
253  aDisplayText = rIter.toString();
254  break;
255  }
256  case XML_ELEMENT(LO_EXT, XML_VALUE):
257  {
258  aValue = rIter.toString();
259  break;
260  }
261  default:
262  XMLOFF_WARN_UNKNOWN("xmloff", rIter);
263  }
264  }
265 
266  uno::Sequence<beans::PropertyValue> aListItem = {
267  comphelper::makePropertyValue("DisplayText", uno::Any(aDisplayText)),
268  comphelper::makePropertyValue("Value", uno::Any(aValue)),
269  };
271 }
272 
273 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
XMLListItemContext(SvXMLImport &rImport, XMLContentControlContext &rContentControl)
css::beans::PropertyValue makePropertyValue(const OUString &rName, T &&rValue)
SvXMLImport & GetImport()
Definition: xmlictxt.hxx:60
void SAL_CALL characters(const OUString &rChars) override
This method is called for all characters that are contained in the current element.
css::uno::Reference< css::text::XTextRange > m_xStart
FastAttributeList & castToFastAttributeList(const css::uno::Reference< css::xml::sax::XFastAttributeList > &xAttrList)
rtl::Reference< XMLTextImportHelper > const & GetTextImport()
Definition: xmlimp.hxx:601
XMLContentControlContext & m_rContentControl
static css::uno::Reference< css::text::XTextContent > CreateAndInsertMark(SvXMLImport &rImport, const OUString &sServiceName, const OUString &sMarkName, const css::uno::Reference< css::text::XTextRange > &rRange, const OUString &i_rXmlId=OUString())
void SAL_CALL startFastElement(sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList > &xAttrList) override
#define XMLOFF_WARN_UNKNOWN(area, rIter)
Definition: xmlictxt.hxx:114
void AppendListItem(const css::beans::PropertyValues &rListItem)
void SAL_CALL startFastElement(sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList > &xAttrList) override
static css::uno::Reference< css::xml::sax::XFastContextHandler > CreateSpanContext(SvXMLImport &rImport, sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList > &xAttrList, XMLHints_Impl &rHints, bool &rIgnLeadSpace, sal_uInt8 nStarFontsConvFlags=0)
Definition: txtparai.cxx:1335
static bool convertBool(bool &rBool, std::u16string_view rString)
This class deliberately does not support XWeak, to improve performance when loading large documents...
Definition: xmlictxt.hxx:45
XMLContentControlContext(SvXMLImport &rImport, sal_Int32 nElement, XMLHints_Impl &rHints, bool &rIgnoreLeadingSpace)
css::uno::Sequence< DstElementType > containerToSequence(const SrcType &i_Container)
Handling of tokens in XML:
#define XML_ELEMENT(prefix, name)
Definition: xmlimp.hxx:97
css::uno::Reference< css::xml::sax::XFastContextHandler > SAL_CALL createFastChildContext(sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList > &rAttrList) override
void SAL_CALL endFastElement(sal_Int32 nElement) override
endFastElement is called before a context will be destructed, but after an elements context has been ...
#define SAL_WARN(area, stream)
std::vector< css::beans::PropertyValues > m_aListItems