LibreOffice Module sc (master)  1
xmltransformationi.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 
10 #include "xmlimprt.hxx"
11 #include "xmltransformationi.hxx"
12 
13 #include <xmloff/xmltoken.hxx>
14 #include <xmloff/xmlnamespace.hxx>
15 
16 #include <datamapper.hxx>
17 #include <document.hxx>
18 
19 using namespace com::sun::star;
20 using namespace xmloff::token;
21 
23  : ScXMLImportContext(rImport)
24 {
25 }
26 
28 
29 uno::Reference<xml::sax::XFastContextHandler>
31  sal_Int32 nElement, const uno::Reference<xml::sax::XFastAttributeList>& xAttrList)
32 {
33  SvXMLImportContext* pContext = nullptr;
36 
37  switch (nElement)
38  {
40  {
41  pContext = new ScXMLColumnRemoveContext(GetScImport(), pAttribList);
42  }
43  break;
45  {
46  pContext = new ScXMLColumnSplitContext(GetScImport(), pAttribList);
47  }
48  break;
50  {
51  pContext = new ScXMLColumnMergeContext(GetScImport(), pAttribList);
52  }
53  break;
55  {
56  pContext = new ScXMLColumnSortContext(GetScImport(), pAttribList);
57  }
58  break;
60  {
61  pContext = new ScXMLColumnTextContext(GetScImport(), pAttribList);
62  }
63  break;
65  {
66  pContext = new ScXMLColumnAggregateContext(GetScImport(), pAttribList);
67  }
68  break;
70  {
71  pContext = new ScXMLColumnNumberContext(GetScImport(), pAttribList);
72  }
73  break;
75  {
76  pContext = new ScXMLColumnRemoveNullContext(GetScImport(), pAttribList);
77  }
78  break;
79  }
80 
81  return pContext;
82 }
83 
86  : ScXMLImportContext(rImport)
87 {
88 }
89 
91 {
93  auto& rDataSources = pDoc->GetExternalDataMapper().getDataSources();
94  if (!rDataSources.empty())
95  {
96  rDataSources[rDataSources.size() - 1].AddDataTransformation(
97  std::make_shared<sc::ColumnRemoveTransformation>(maColumns));
98  }
99 }
100 
101 uno::Reference<xml::sax::XFastContextHandler>
103  sal_Int32 nElement, const uno::Reference<xml::sax::XFastAttributeList>& xAttrList)
104 {
107 
108  const rtl::Reference<sax_fastparser::FastAttributeList>& rAttrList = pAttribList;
109 
110  switch (nElement)
111  {
112  case XML_ELEMENT(CALC_EXT, XML_COLUMN):
113  {
114  if (rAttrList.is())
115  {
116  for (auto& aIter : *rAttrList)
117  {
118  switch (aIter.getToken())
119  {
120  case XML_ELEMENT(CALC_EXT, XML_COLUMN):
121  {
122  maColumns.insert(aIter.toInt32());
123  }
124  break;
125  }
126  }
127  }
128  }
129  break;
130  }
131 
132  return new SvXMLImportContext(GetImport());
133 }
134 
137  : ScXMLImportContext(rImport)
138 {
139  SCCOL mnCol = 0;
140  OUString cSeparator;
141 
142  if (rAttrList.is())
143  {
144  for (auto& aIter : *rAttrList)
145  {
146  switch (aIter.getToken())
147  {
148  case XML_ELEMENT(CALC_EXT, XML_COLUMN):
149  {
150  mnCol = aIter.toInt32();
151  }
152  break;
153  case XML_ELEMENT(CALC_EXT, XML_SEPARATOR):
154  {
155  cSeparator = aIter.toString();
156  }
157  break;
158  }
159  }
160  }
161 
162  if (mnCol > 0)
163  {
164  ScDocument* pDoc = GetScImport().GetDocument();
165  auto& rDataSources = pDoc->GetExternalDataMapper().getDataSources();
166  if (!rDataSources.empty())
167  {
168  rDataSources[rDataSources.size() - 1].AddDataTransformation(
169  std::make_shared<sc::SplitColumnTransformation>(mnCol, cSeparator.toChar()));
170  }
171  }
172 }
173 
175 
178  : ScXMLImportContext(rImport)
179 {
180  if (!rAttrList.is())
181  return;
182 
183  for (auto& aIter : *rAttrList)
184  {
185  switch (aIter.getToken())
186  {
187  case XML_ELEMENT(CALC_EXT, XML_MERGE_STRING):
188  {
189  maMergeString = aIter.toString();
190  }
191  break;
192  }
193  }
194 }
195 
197 {
198  ScDocument* pDoc = GetScImport().GetDocument();
199  auto& rDataSources = pDoc->GetExternalDataMapper().getDataSources();
200  if (!rDataSources.empty())
201  {
202  rDataSources[rDataSources.size() - 1].AddDataTransformation(
203  std::make_shared<sc::MergeColumnTransformation>(maColumns, maMergeString));
204  }
205 }
206 
207 uno::Reference<xml::sax::XFastContextHandler>
209  sal_Int32 nElement, const uno::Reference<xml::sax::XFastAttributeList>& xAttrList)
210 {
211  switch (nElement)
212  {
213  case XML_ELEMENT(CALC_EXT, XML_COLUMN):
214  {
215  for (auto& aIter : sax_fastparser::castToFastAttributeList(xAttrList))
216  {
217  switch (aIter.getToken())
218  {
219  case XML_ELEMENT(CALC_EXT, XML_COLUMN):
220  {
221  maColumns.insert(aIter.toInt32());
222  }
223  break;
224  }
225  }
226  }
227  break;
228  }
229  return new SvXMLImportContext(GetImport());
230 }
231 
233  ScXMLImport& rImport, const rtl::Reference<sax_fastparser::FastAttributeList>& /*rAttrList*/)
234  : ScXMLImportContext(rImport)
235 {
236 }
237 
239 
240 /*
241 uno::Reference<xml::sax::XFastContextHandler>
242  SAL_CALL ScXMLColumnSortContext::createFastChildContext(
243  sal_Int32 nElement, const uno::Reference<xml::sax::XFastAttributeList>& xAttrList)
244 {
245 
246 }
247 */
248 
251  : ScXMLImportContext(rImport)
252  , maType(sc::TEXT_TRANSFORM_TYPE::TO_LOWER)
253 {
254  OUString aType;
255 
256  if (rAttrList.is())
257  {
258  for (auto& aIter : *rAttrList)
259  {
260  switch (aIter.getToken())
261  {
262  case XML_ELEMENT(CALC_EXT, XML_TYPE):
263  {
264  aType = aIter.toString();
265  }
266  break;
267  }
268  }
269  }
270 
271  if (!aType.isEmpty())
272  {
273  if (aType == "lowercase")
275  else if (aType == "uppercase")
277  else if (aType == "capitalize")
279  else if (aType == "trim")
281  }
282 }
283 
285 {
286  ScDocument* pDoc = GetScImport().GetDocument();
287  auto& rDataSources = pDoc->GetExternalDataMapper().getDataSources();
288  if (!rDataSources.empty())
289  {
290  rDataSources[rDataSources.size() - 1].AddDataTransformation(
291  std::make_shared<sc::TextTransformation>(maColumns, maType));
292  }
293 }
294 
295 uno::Reference<xml::sax::XFastContextHandler>
297  sal_Int32 nElement, const uno::Reference<xml::sax::XFastAttributeList>& xAttrList)
298 {
299  switch (nElement)
300  {
301  case XML_ELEMENT(CALC_EXT, XML_COLUMN):
302  {
303  for (auto& aIter : sax_fastparser::castToFastAttributeList(xAttrList))
304  {
305  switch (aIter.getToken())
306  {
307  case XML_ELEMENT(CALC_EXT, XML_COLUMN):
308  {
309  maColumns.insert(aIter.toInt32());
310  }
311  break;
312  }
313  }
314  }
315  break;
316  }
317  return new SvXMLImportContext(GetImport());
318 }
319 
322  : ScXMLImportContext(rImport)
323  , maType(sc::AGGREGATE_FUNCTION::SUM)
324 {
325  OUString aType;
326 
327  if (rAttrList.is())
328  {
329  for (auto& aIter : *rAttrList)
330  {
331  switch (aIter.getToken())
332  {
333  case XML_ELEMENT(CALC_EXT, XML_TYPE):
334  {
335  aType = aIter.toString();
336  }
337  break;
338  }
339  }
340  }
341 
342  if (!aType.isEmpty())
343  {
344  if (aType == "sum")
346  else if (aType == "average")
348  else if (aType == "min")
350  else if (aType == "max")
352  }
353 }
354 
356 {
357  ScDocument* pDoc = GetScImport().GetDocument();
358  auto& rDataSources = pDoc->GetExternalDataMapper().getDataSources();
359  if (!rDataSources.empty())
360  {
361  rDataSources[rDataSources.size() - 1].AddDataTransformation(
362  std::make_shared<sc::AggregateFunction>(maColumns, maType));
363  }
364 }
365 
366 uno::Reference<xml::sax::XFastContextHandler>
368  sal_Int32 nElement, const uno::Reference<xml::sax::XFastAttributeList>& xAttrList)
369 {
370  switch (nElement)
371  {
372  case XML_ELEMENT(CALC_EXT, XML_COLUMN):
373  {
374  for (auto& aIter : sax_fastparser::castToFastAttributeList(xAttrList))
375  {
376  switch (aIter.getToken())
377  {
378  case XML_ELEMENT(CALC_EXT, XML_COLUMN):
379  {
380  maColumns.insert(aIter.toInt32());
381  }
382  break;
383  }
384  }
385  }
386  break;
387  }
388  return new SvXMLImportContext(GetImport());
389 }
390 
393  : ScXMLImportContext(rImport)
395  , maPrecision(0)
396 {
397  OUString aType;
398  if (rAttrList.is())
399  {
400  for (auto& aIter : *rAttrList)
401  {
402  switch (aIter.getToken())
403  {
404  case XML_ELEMENT(CALC_EXT, XML_TYPE):
405  {
406  aType = aIter.toString();
407  }
408  break;
409  case XML_ELEMENT(CALC_EXT, XML_PRECISION):
410  {
411  maPrecision = aIter.toInt32();
412  }
413  break;
414  }
415  }
416  }
417 
418  if (aType.isEmpty())
419  return;
420 
421  if (aType == "round")
423  else if (aType == "round-up")
425  else if (aType == "round-down")
427  else if (aType == "abs")
429  else if (aType == "log")
431  else if (aType == "log-base-10")
433  else if (aType == "cube")
435  else if (aType == "number-square")
437  else if (aType == "square-root")
439  else if (aType == "exponential")
441  else if (aType == "even")
443  else if (aType == "odd")
445  else if (aType == "sign")
447 }
448 
450 {
451  ScDocument* pDoc = GetScImport().GetDocument();
452  auto& rDataSources = pDoc->GetExternalDataMapper().getDataSources();
453  if (!rDataSources.empty())
454  {
455  rDataSources[rDataSources.size() - 1].AddDataTransformation(
456  std::make_shared<sc::NumberTransformation>(maColumns, maType, maPrecision));
457  }
458 }
459 
460 uno::Reference<xml::sax::XFastContextHandler>
462  sal_Int32 nElement, const uno::Reference<xml::sax::XFastAttributeList>& xAttrList)
463 {
464  switch (nElement)
465  {
466  case XML_ELEMENT(CALC_EXT, XML_COLUMN):
467  {
468  for (auto& aIter : sax_fastparser::castToFastAttributeList(xAttrList))
469  {
470  switch (aIter.getToken())
471  {
472  case XML_ELEMENT(CALC_EXT, XML_COLUMN):
473  {
474  maColumns.insert(aIter.toInt32());
475  }
476  break;
477  }
478  }
479  }
480  break;
481  }
482  return new SvXMLImportContext(GetImport());
483 }
484 
487  : ScXMLImportContext(rImport)
488 {
489  if (!rAttrList.is())
490  return;
491 
492  for (auto& aIter : *rAttrList)
493  {
494  switch (aIter.getToken())
495  {
496  case XML_ELEMENT(CALC_EXT, XML_REPLACE_STRING):
497  {
498  maReplaceString = aIter.toString();
499  }
500  break;
501  }
502  }
503 }
504 
506 {
507  ScDocument* pDoc = GetScImport().GetDocument();
508  auto& rDataSources = pDoc->GetExternalDataMapper().getDataSources();
509  if (!rDataSources.empty())
510  {
511  rDataSources[rDataSources.size() - 1].AddDataTransformation(
512  std::make_shared<sc::ReplaceNullTransformation>(maColumns, maReplaceString));
513  }
514 }
515 
516 uno::Reference<xml::sax::XFastContextHandler>
518  sal_Int32 nElement, const uno::Reference<xml::sax::XFastAttributeList>& xAttrList)
519 {
520  switch (nElement)
521  {
522  case XML_ELEMENT(CALC_EXT, XML_COLUMN):
523  {
524  for (auto& aIter : sax_fastparser::castToFastAttributeList(xAttrList))
525  {
526  switch (aIter.getToken())
527  {
528  case XML_ELEMENT(CALC_EXT, XML_COLUMN):
529  {
530  maColumns.insert(aIter.toInt32());
531  }
532  break;
533  }
534  }
535  }
536  break;
537  }
538  return new SvXMLImportContext(GetImport());
539 }
540 
543  : ScXMLImportContext(rImport)
544  , maType(sc::DATETIME_TRANSFORMATION_TYPE::DATE_STRING)
545 {
546  if (rAttrList.is())
547  {
548  for (auto& aIter : *rAttrList)
549  {
550  switch (aIter.getToken())
551  {
552  case XML_ELEMENT(CALC_EXT, XML_TYPE):
553  {
554  aType = aIter.toString();
555  }
556  break;
557  }
558  }
559  }
560 
561  if (aType.isEmpty())
562  return;
563 
564  if (aType == "date-string")
566  else if (aType == "year")
568  else if (aType == "start-of-year")
570  else if (aType == "end-of-year")
572  else if (aType == "month")
574  else if (aType == "month-name")
576  else if (aType == "start-of-month")
578  else if (aType == "end-of-month")
580  else if (aType == "day")
582  else if (aType == "day-of-week")
584  else if (aType == "day-of-year")
586  else if (aType == "quarter")
588  else if (aType == "start-of-quarter")
590  else if (aType == "end-of-quarter")
592  else if (aType == "time")
594  else if (aType == "hour")
596  else if (aType == "minute")
598  else if (aType == "seconds")
600 }
601 
603 {
604  ScDocument* pDoc = GetScImport().GetDocument();
605  auto& rDataSources = pDoc->GetExternalDataMapper().getDataSources();
606  if (!rDataSources.empty())
607  {
608  rDataSources[rDataSources.size() - 1].AddDataTransformation(
609  std::make_shared<sc::DateTimeTransformation>(maColumns, maType));
610  }
611 }
612 
613 uno::Reference<xml::sax::XFastContextHandler> SAL_CALL ScXMLDateTimeContext::createFastChildContext(
614  sal_Int32 nElement, const uno::Reference<xml::sax::XFastAttributeList>& xAttrList)
615 {
616  switch (nElement)
617  {
618  case XML_ELEMENT(CALC_EXT, XML_COLUMN):
619  {
620  for (auto& aIter : sax_fastparser::castToFastAttributeList(xAttrList))
621  {
622  switch (aIter.getToken())
623  {
624  case XML_ELEMENT(CALC_EXT, XML_COLUMN):
625  {
626  maColumns.insert(aIter.toInt32());
627  break;
628  }
629  }
630  }
631  }
632  break;
633  }
634  return new SvXMLImportContext(GetImport());
635 }
636 
637 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
OUString maType
ScXMLColumnAggregateContext(ScXMLImport &rImport, const rtl::Reference< sax_fastparser::FastAttributeList > &rAttrList)
virtual ~ScXMLColumnSplitContext() override
virtual css::uno::Reference< css::xml::sax::XFastContextHandler > SAL_CALL createFastChildContext(sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList > &xAttrList) override
sc::AGGREGATE_FUNCTION maType
XML_SEPARATOR
virtual css::uno::Reference< css::xml::sax::XFastContextHandler > SAL_CALL createFastChildContext(sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList > &xAttrList) override
XML_COLUMN
ScDocument * GetDocument()
Definition: xmlimprt.hxx:206
ScXMLColumnSplitContext(ScXMLImport &rImport, const rtl::Reference< sax_fastparser::FastAttributeList > &rAttrList)
SvXMLImport & GetImport()
sal_Int32 mnCol
virtual ~ScXMLTransformationsContext() override
virtual css::uno::Reference< css::xml::sax::XFastContextHandler > SAL_CALL createFastChildContext(sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList > &xAttrList) override
XML_COLUMN_REPLACENULL_TRANSFORMATION
FastAttributeList & castToFastAttributeList(const css::uno::Reference< css::xml::sax::XFastAttributeList > &xAttrList)
virtual ~ScXMLColumnAggregateContext() override
virtual ~ScXMLColumnMergeContext() override
XML_COLUMN_AGGREGATE_TRANSFORMATION
ScXMLColumnMergeContext(ScXMLImport &rImport, const rtl::Reference< sax_fastparser::FastAttributeList > &rAttrList)
virtual css::uno::Reference< css::xml::sax::XFastContextHandler > SAL_CALL createFastChildContext(sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList > &xAttrList) override
XML_TYPE
SC_DLLPUBLIC sc::ExternalDataMapper & GetExternalDataMapper()
ScXMLColumnRemoveNullContext(ScXMLImport &rImport, const rtl::Reference< sax_fastparser::FastAttributeList > &rAttrList)
virtual ~ScXMLColumnRemoveNullContext() override
DATETIME_TRANSFORMATION_TYPE
This class exists only to provide GetScImport() to its derived classes.
XML_PRECISION
const std::vector< ExternalDataSource > & getDataSources() const
sc::DATETIME_TRANSFORMATION_TYPE maType
sal_Int16 SCCOL
Definition: types.hxx:22
virtual css::uno::Reference< css::xml::sax::XFastContextHandler > SAL_CALL createFastChildContext(sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList > &xAttrList) override
virtual ~ScXMLColumnTextContext() override
XML_COLUMN_TEXT_TRANSFORMATION
virtual css::uno::Reference< css::xml::sax::XFastContextHandler > SAL_CALL createFastChildContext(sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList > &xAttrList) override
SvXMLImportContext(SvXMLImport &rImport)
ScXMLImport & GetScImport()
ScXMLColumnSortContext(ScXMLImport &rImport, const rtl::Reference< sax_fastparser::FastAttributeList > &rAttrList)
XML_MERGE_STRING
ScXMLTransformationsContext(ScXMLImport &rImport)
std::set< SCCOL > maColumns
XML_COLUMN_MERGE_TRANSFORMATION
XML_COLUMN_SPLIT_TRANSFORMATION
ScXMLColumnRemoveContext(ScXMLImport &rImport, const rtl::Reference< sax_fastparser::FastAttributeList > &rAttrList)
ScXMLColumnNumberContext(ScXMLImport &rImport, const rtl::Reference< sax_fastparser::FastAttributeList > &rAttrList)
sc::NUMBER_TRANSFORM_TYPE maType
virtual css::uno::Reference< css::xml::sax::XFastContextHandler > SAL_CALL createFastChildContext(sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList > &xAttrList) override
virtual ~ScXMLColumnNumberContext() override
ScXMLDateTimeContext(ScXMLImport &rImport, const rtl::Reference< sax_fastparser::FastAttributeList > &rAttrList)
sc::TEXT_TRANSFORM_TYPE maType
#define XML_ELEMENT(prefix, name)
XML_COLUMN_NUMBER_TRANSFORMATION
virtual ~ScXMLColumnRemoveContext() override
virtual css::uno::Reference< css::xml::sax::XFastContextHandler > SAL_CALL createFastChildContext(sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList > &xAttrList) override
#define ROUND(x)
std::set< SCCOL > maColumns
virtual ~ScXMLColumnSortContext() override
virtual ~ScXMLDateTimeContext() override
XML_COLUMN_SORT_TRANSFORMATION
XML_COLUMN_REMOVE_TRANSFORMATION
XML_REPLACE_STRING
ScXMLColumnTextContext(ScXMLImport &rImport, const rtl::Reference< sax_fastparser::FastAttributeList > &rAttrList)