Line data Source code
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 :
20 : #include "xmlfilti.hxx"
21 : #include "xmlimprt.hxx"
22 : #include "docuno.hxx"
23 : #include "convuno.hxx"
24 : #include "XMLConverter.hxx"
25 : #include "rangeutl.hxx"
26 : #include "queryentry.hxx"
27 :
28 : #include <xmloff/xmltkmap.hxx>
29 : #include <xmloff/nmspmap.hxx>
30 : #include <xmloff/xmltoken.hxx>
31 :
32 : using namespace com::sun::star;
33 : using namespace xmloff::token;
34 :
35 : using ::com::sun::star::uno::Reference;
36 : using ::com::sun::star::xml::sax::XAttributeList;
37 :
38 8 : ScXMLFilterContext::ConnStackItem::ConnStackItem(bool bOr) : mbOr(bOr), mnCondCount(0) {}
39 :
40 8 : ScXMLFilterContext::ScXMLFilterContext( ScXMLImport& rImport,
41 : sal_uInt16 nPrfx,
42 : const rtl::OUString& rLName,
43 : const Reference<XAttributeList>& xAttrList,
44 : ScQueryParam& rParam,
45 : ScXMLDatabaseRangeContext* pTempDatabaseRangeContext) :
46 : SvXMLImportContext( rImport, nPrfx, rLName ),
47 : mrQueryParam(rParam),
48 : pDatabaseRangeContext(pTempDatabaseRangeContext),
49 : bSkipDuplicates(false),
50 : bCopyOutputData(false),
51 8 : bConditionSourceRange(false)
52 : {
53 8 : ScDocument* pDoc(GetScImport().GetDocument());
54 :
55 8 : sal_Int16 nAttrCount = xAttrList.is() ? xAttrList->getLength() : 0;
56 8 : const SvXMLTokenMap& rAttrTokenMap = GetScImport().GetFilterAttrTokenMap();
57 8 : for( sal_Int16 i=0; i < nAttrCount; ++i )
58 : {
59 0 : const rtl::OUString& sAttrName(xAttrList->getNameByIndex( i ));
60 0 : rtl::OUString aLocalName;
61 0 : sal_uInt16 nPrefix = GetScImport().GetNamespaceMap().GetKeyByAttrName(
62 0 : sAttrName, &aLocalName );
63 0 : const rtl::OUString& sValue(xAttrList->getValueByIndex( i ));
64 :
65 0 : switch( rAttrTokenMap.Get( nPrefix, aLocalName ) )
66 : {
67 : case XML_TOK_FILTER_ATTR_TARGET_RANGE_ADDRESS :
68 : {
69 0 : ScRange aScRange;
70 0 : sal_Int32 nOffset(0);
71 0 : if (ScRangeStringConverter::GetRangeFromString( aScRange, sValue, pDoc, ::formula::FormulaGrammar::CONV_OOO, nOffset ))
72 : {
73 0 : ScUnoConversion::FillApiAddress( aOutputPosition, aScRange.aStart );
74 0 : bCopyOutputData = true;
75 : }
76 : }
77 0 : break;
78 : case XML_TOK_FILTER_ATTR_CONDITION_SOURCE_RANGE_ADDRESS :
79 : {
80 0 : sal_Int32 nOffset(0);
81 0 : if (ScRangeStringConverter::GetRangeFromString( aConditionSourceRangeAddress, sValue, pDoc, ::formula::FormulaGrammar::CONV_OOO, nOffset ))
82 0 : bConditionSourceRange = true;
83 : }
84 0 : break;
85 : case XML_TOK_FILTER_ATTR_CONDITION_SOURCE :
86 : {
87 : // not supported by StarOffice
88 : }
89 0 : break;
90 : case XML_TOK_FILTER_ATTR_DISPLAY_DUPLICATES :
91 : {
92 0 : bSkipDuplicates = !IsXMLToken(sValue, XML_TRUE);
93 : }
94 0 : break;
95 : }
96 0 : }
97 8 : }
98 :
99 16 : ScXMLFilterContext::~ScXMLFilterContext()
100 : {
101 16 : }
102 :
103 8 : SvXMLImportContext *ScXMLFilterContext::CreateChildContext( sal_uInt16 nPrefix,
104 : const ::rtl::OUString& rLName,
105 : const ::com::sun::star::uno::Reference<
106 : ::com::sun::star::xml::sax::XAttributeList>& xAttrList )
107 : {
108 8 : SvXMLImportContext *pContext(0);
109 :
110 8 : const SvXMLTokenMap& rTokenMap(GetScImport().GetFilterElemTokenMap());
111 8 : switch( rTokenMap.Get( nPrefix, rLName ) )
112 : {
113 : case XML_TOK_FILTER_AND:
114 : {
115 : pContext = new ScXMLAndContext(
116 2 : GetScImport(), nPrefix, rLName, xAttrList, mrQueryParam, this);
117 : }
118 2 : break;
119 : case XML_TOK_FILTER_OR:
120 : {
121 : pContext = new ScXMLOrContext(
122 6 : GetScImport(), nPrefix, rLName, xAttrList, mrQueryParam, this);
123 : }
124 6 : break;
125 : case XML_TOK_FILTER_CONDITION:
126 : {
127 : pContext = new ScXMLConditionContext(
128 0 : GetScImport(), nPrefix, rLName, xAttrList, mrQueryParam, this);
129 : }
130 0 : break;
131 : }
132 :
133 8 : if( !pContext )
134 0 : pContext = new SvXMLImportContext( GetImport(), nPrefix, rLName );
135 :
136 8 : return pContext;
137 : }
138 :
139 8 : void ScXMLFilterContext::EndElement()
140 : {
141 8 : mrQueryParam.bInplace = !bCopyOutputData;
142 8 : mrQueryParam.bDuplicate = !bSkipDuplicates;
143 :
144 8 : if (bCopyOutputData)
145 : {
146 0 : mrQueryParam.nDestCol = aOutputPosition.Column;
147 0 : mrQueryParam.nDestRow = aOutputPosition.Row;
148 0 : mrQueryParam.nDestTab = aOutputPosition.Sheet;
149 : }
150 :
151 8 : if (bConditionSourceRange)
152 0 : pDatabaseRangeContext->SetFilterConditionSourceRangeAddress(aConditionSourceRangeAddress);
153 8 : }
154 :
155 8 : void ScXMLFilterContext::OpenConnection(bool b)
156 : {
157 8 : maConnStack.push_back(ConnStackItem(b));
158 8 : }
159 :
160 8 : void ScXMLFilterContext::CloseConnection()
161 : {
162 8 : maConnStack.pop_back();
163 8 : }
164 :
165 10 : bool ScXMLFilterContext::GetConnection()
166 : {
167 : // For condition items in each stack, the first one gets the connection of
168 : // the last stack, while the rest of them get that of the current stack.
169 :
170 10 : if (maConnStack.empty())
171 : // This should never happen.
172 0 : return true;
173 :
174 10 : ConnStackItem& rItem = maConnStack.back();
175 10 : if (rItem.mnCondCount)
176 : // secondary item gets the current connection.
177 0 : return rItem.mbOr;
178 :
179 10 : if (maConnStack.size() < 2)
180 : // There is no last stack. Likely the first condition in the first
181 : // stack whose connection is not used.
182 10 : return true;
183 :
184 0 : ++rItem.mnCondCount;
185 0 : std::vector<ConnStackItem>::reverse_iterator itr = maConnStack.rbegin();
186 0 : ++itr;
187 0 : return itr->mbOr; // connection of the last stack.
188 : }
189 :
190 2 : ScXMLAndContext::ScXMLAndContext( ScXMLImport& rImport,
191 : sal_uInt16 nPrfx,
192 : const rtl::OUString& rLName,
193 : const Reference<XAttributeList>& /* xAttrList */,
194 : ScQueryParam& rParam,
195 : ScXMLFilterContext* pTempFilterContext) :
196 : SvXMLImportContext( rImport, nPrfx, rLName ),
197 : mrQueryParam(rParam),
198 2 : pFilterContext(pTempFilterContext)
199 : {
200 2 : pFilterContext->OpenConnection(false);
201 2 : }
202 :
203 4 : ScXMLAndContext::~ScXMLAndContext()
204 : {
205 4 : }
206 :
207 2 : SvXMLImportContext *ScXMLAndContext::CreateChildContext( sal_uInt16 nPrefix,
208 : const ::rtl::OUString& rLName,
209 : const ::com::sun::star::uno::Reference<
210 : ::com::sun::star::xml::sax::XAttributeList>& xAttrList )
211 : {
212 2 : SvXMLImportContext *pContext(0);
213 :
214 2 : const SvXMLTokenMap& rTokenMap(GetScImport().GetFilterElemTokenMap());
215 2 : switch( rTokenMap.Get( nPrefix, rLName ) )
216 : {
217 : case XML_TOK_FILTER_OR:
218 : {
219 : // not supported in StarOffice
220 : }
221 0 : break;
222 : case XML_TOK_FILTER_CONDITION:
223 : {
224 : pContext = new ScXMLConditionContext(
225 2 : GetScImport(), nPrefix, rLName, xAttrList, mrQueryParam, pFilterContext);
226 : }
227 2 : break;
228 : }
229 :
230 2 : if( !pContext )
231 0 : pContext = new SvXMLImportContext( GetImport(), nPrefix, rLName );
232 :
233 2 : return pContext;
234 : }
235 :
236 2 : void ScXMLAndContext::EndElement()
237 : {
238 2 : pFilterContext->CloseConnection();
239 2 : }
240 :
241 6 : ScXMLOrContext::ScXMLOrContext( ScXMLImport& rImport,
242 : sal_uInt16 nPrfx,
243 : const rtl::OUString& rLName,
244 : const Reference<
245 : ::com::sun::star::xml::sax::XAttributeList>& /* xAttrList */,
246 : ScQueryParam& rParam,
247 : ScXMLFilterContext* pTempFilterContext) :
248 : SvXMLImportContext( rImport, nPrfx, rLName ),
249 : mrQueryParam(rParam),
250 6 : pFilterContext(pTempFilterContext)
251 : {
252 6 : pFilterContext->OpenConnection(true);
253 6 : }
254 :
255 12 : ScXMLOrContext::~ScXMLOrContext()
256 : {
257 12 : }
258 :
259 8 : SvXMLImportContext *ScXMLOrContext::CreateChildContext( sal_uInt16 nPrefix,
260 : const ::rtl::OUString& rLName,
261 : const ::com::sun::star::uno::Reference<
262 : ::com::sun::star::xml::sax::XAttributeList>& xAttrList )
263 : {
264 8 : SvXMLImportContext *pContext(0);
265 :
266 8 : const SvXMLTokenMap& rTokenMap(GetScImport().GetFilterElemTokenMap());
267 8 : switch( rTokenMap.Get( nPrefix, rLName ) )
268 : {
269 : case XML_TOK_FILTER_AND:
270 : {
271 : pContext = new ScXMLAndContext(
272 0 : GetScImport(), nPrefix, rLName, xAttrList, mrQueryParam, pFilterContext);
273 : }
274 0 : break;
275 : case XML_TOK_FILTER_CONDITION:
276 : {
277 : pContext = new ScXMLConditionContext(
278 8 : GetScImport(), nPrefix, rLName, xAttrList, mrQueryParam, pFilterContext);
279 : }
280 8 : break;
281 : }
282 :
283 8 : if( !pContext )
284 0 : pContext = new SvXMLImportContext( GetImport(), nPrefix, rLName );
285 :
286 8 : return pContext;
287 : }
288 :
289 6 : void ScXMLOrContext::EndElement()
290 : {
291 6 : pFilterContext->CloseConnection();
292 6 : }
293 :
294 10 : ScXMLConditionContext::ScXMLConditionContext(
295 : ScXMLImport& rImport, sal_uInt16 nPrfx, const rtl::OUString& rLName,
296 : const Reference<XAttributeList>& xAttrList,
297 : ScQueryParam& rParam,
298 : ScXMLFilterContext* pTempFilterContext) :
299 : SvXMLImportContext( rImport, nPrfx, rLName ),
300 : mrQueryParam(rParam),
301 : pFilterContext(pTempFilterContext),
302 10 : bIsCaseSensitive(false)
303 : {
304 10 : sDataType = GetXMLToken(XML_TEXT);
305 :
306 10 : sal_Int16 nAttrCount(xAttrList.is() ? xAttrList->getLength() : 0);
307 10 : const SvXMLTokenMap& rAttrTokenMap(GetScImport().GetFilterConditionAttrTokenMap());
308 40 : for( sal_Int16 i=0; i < nAttrCount; ++i )
309 : {
310 30 : const rtl::OUString& sAttrName(xAttrList->getNameByIndex( i ));
311 30 : rtl::OUString aLocalName;
312 30 : sal_uInt16 nPrefix = GetScImport().GetNamespaceMap().GetKeyByAttrName(
313 30 : sAttrName, &aLocalName );
314 30 : const rtl::OUString& sValue(xAttrList->getValueByIndex( i ));
315 :
316 30 : switch( rAttrTokenMap.Get( nPrefix, aLocalName ) )
317 : {
318 : case XML_TOK_CONDITION_ATTR_FIELD_NUMBER :
319 : {
320 10 : nField = sValue.toInt32();
321 : }
322 10 : break;
323 : case XML_TOK_CONDITION_ATTR_CASE_SENSITIVE :
324 : {
325 0 : bIsCaseSensitive = IsXMLToken(sValue, XML_TRUE);
326 : }
327 0 : break;
328 : case XML_TOK_CONDITION_ATTR_DATA_TYPE :
329 : {
330 0 : sDataType = sValue;
331 : }
332 0 : break;
333 : case XML_TOK_CONDITION_ATTR_VALUE :
334 : {
335 10 : sConditionValue = sValue;
336 : }
337 10 : break;
338 : case XML_TOK_CONDITION_ATTR_OPERATOR :
339 : {
340 10 : sOperator = sValue;
341 : }
342 10 : break;
343 : }
344 30 : }
345 10 : }
346 :
347 20 : ScXMLConditionContext::~ScXMLConditionContext()
348 : {
349 20 : }
350 :
351 16 : SvXMLImportContext *ScXMLConditionContext::CreateChildContext( sal_uInt16 nPrefix,
352 : const ::rtl::OUString& rLName,
353 : const Reference<XAttributeList>& xAttrList )
354 : {
355 16 : SvXMLImportContext *pContext = NULL;
356 :
357 16 : const SvXMLTokenMap& rTokenMap(GetScImport().GetFilterConditionElemTokenMap());
358 16 : switch( rTokenMap.Get( nPrefix, rLName ) )
359 : {
360 : case XML_TOK_CONDITION_FILTER_SET_ITEM:
361 : {
362 : pContext = new ScXMLSetItemContext(
363 16 : GetScImport(), nPrefix, rLName, xAttrList, *this);
364 : }
365 16 : break;
366 : }
367 :
368 16 : if( !pContext )
369 0 : pContext = new SvXMLImportContext( GetImport(), nPrefix, rLName );
370 :
371 16 : return pContext;
372 : }
373 :
374 10 : void ScXMLConditionContext::GetOperator(
375 : const rtl::OUString& aOpStr, ScQueryParam& rParam, ScQueryEntry& rEntry) const
376 : {
377 10 : rParam.bRegExp = false;
378 10 : if (IsXMLToken(aOpStr, XML_MATCH))
379 : {
380 0 : rParam.bRegExp = true;
381 0 : rEntry.eOp = SC_EQUAL;
382 : }
383 10 : else if (IsXMLToken(aOpStr, XML_NOMATCH))
384 : {
385 0 : rParam.bRegExp = true;
386 0 : rEntry.eOp = SC_NOT_EQUAL;
387 : }
388 10 : else if (aOpStr.compareToAscii("=") == 0)
389 10 : rEntry.eOp = SC_EQUAL;
390 0 : else if (aOpStr.compareToAscii("!=") == 0)
391 0 : rEntry.eOp = SC_NOT_EQUAL;
392 0 : else if (IsXMLToken(aOpStr, XML_BOTTOM_PERCENT))
393 0 : rEntry.eOp = SC_BOTPERC;
394 0 : else if (IsXMLToken(aOpStr, XML_BOTTOM_VALUES))
395 0 : rEntry.eOp = SC_BOTVAL;
396 0 : else if (IsXMLToken(aOpStr, XML_EMPTY))
397 0 : rEntry.SetQueryByEmpty();
398 0 : else if (aOpStr.compareToAscii(">") == 0)
399 0 : rEntry.eOp = SC_GREATER;
400 0 : else if (aOpStr.compareToAscii(">=") == 0)
401 0 : rEntry.eOp = SC_GREATER_EQUAL;
402 0 : else if (aOpStr.compareToAscii("<") == 0)
403 0 : rEntry.eOp = SC_LESS;
404 0 : else if (aOpStr.compareToAscii("<=") == 0)
405 0 : rEntry.eOp = SC_LESS_EQUAL;
406 0 : else if (IsXMLToken(aOpStr, XML_NOEMPTY))
407 0 : rEntry.SetQueryByNonEmpty();
408 0 : else if (IsXMLToken(aOpStr, XML_TOP_PERCENT))
409 0 : rEntry.eOp = SC_TOPPERC;
410 0 : else if (IsXMLToken(aOpStr, XML_TOP_VALUES))
411 0 : rEntry.eOp = SC_TOPVAL;
412 0 : else if (IsXMLToken(aOpStr, XML_CONTAINS))
413 0 : rEntry.eOp = SC_CONTAINS;
414 0 : else if (IsXMLToken(aOpStr, XML_DOES_NOT_CONTAIN))
415 0 : rEntry.eOp = SC_DOES_NOT_CONTAIN;
416 0 : else if (IsXMLToken(aOpStr, XML_BEGINS_WITH))
417 0 : rEntry.eOp = SC_BEGINS_WITH;
418 0 : else if (IsXMLToken(aOpStr, XML_DOES_NOT_BEGIN_WITH))
419 0 : rEntry.eOp = SC_DOES_NOT_BEGIN_WITH;
420 0 : else if (IsXMLToken(aOpStr, XML_ENDS_WITH))
421 0 : rEntry.eOp = SC_ENDS_WITH;
422 0 : else if (IsXMLToken(aOpStr, XML_DOES_NOT_END_WITH))
423 0 : rEntry.eOp = SC_DOES_NOT_END_WITH;
424 10 : }
425 :
426 16 : void ScXMLConditionContext::AddSetItem(const ScQueryEntry::Item& rItem)
427 : {
428 16 : maQueryItems.push_back(rItem);
429 16 : }
430 :
431 10 : void ScXMLConditionContext::EndElement()
432 : {
433 10 : ScQueryEntry& rEntry = mrQueryParam.AppendEntry();
434 :
435 : // We currently don't support per-condition case sensitivity.
436 10 : mrQueryParam.bCaseSens = bIsCaseSensitive;
437 :
438 10 : rEntry.bDoQuery = true;
439 10 : rEntry.eConnect = pFilterContext->GetConnection() ? SC_OR : SC_AND;
440 :
441 10 : GetOperator(sOperator, mrQueryParam, rEntry);
442 10 : SCCOLROW nStartPos = mrQueryParam.bByRow ? mrQueryParam.nCol1 : mrQueryParam.nRow1;
443 10 : rEntry.nField = nField + nStartPos;
444 :
445 10 : if (maQueryItems.empty())
446 : {
447 4 : ScQueryEntry::Item& rItem = rEntry.GetQueryItem();
448 4 : if (IsXMLToken(sDataType, XML_NUMBER))
449 : {
450 0 : rItem.mfVal = sConditionValue.toDouble();
451 0 : rItem.meType = ScQueryEntry::ByValue;
452 : }
453 : else
454 : {
455 4 : rItem.maString = sConditionValue;
456 4 : rItem.meType = ScQueryEntry::ByString;
457 : }
458 : }
459 : else
460 6 : rEntry.GetQueryItems().swap(maQueryItems);
461 10 : }
462 :
463 0 : const ScXMLImport& ScXMLSetItemContext::GetScImport() const
464 : {
465 0 : return static_cast<const ScXMLImport&>(GetImport());
466 : }
467 :
468 32 : ScXMLImport& ScXMLSetItemContext::GetScImport()
469 : {
470 32 : return static_cast<ScXMLImport&>(GetImport());
471 : }
472 :
473 16 : ScXMLSetItemContext::ScXMLSetItemContext(
474 : ScXMLImport& rImport, sal_uInt16 nPrfx, const rtl::OUString& rLName,
475 : const Reference<XAttributeList>& xAttrList, ScXMLConditionContext& rParent) :
476 16 : SvXMLImportContext(rImport, nPrfx, rLName)
477 : {
478 16 : sal_Int32 nAttrCount = xAttrList.is() ? xAttrList->getLength() : 0;
479 16 : const SvXMLTokenMap& rAttrTokenMap = GetScImport().GetFilterSetItemAttrTokenMap();
480 32 : for (sal_Int32 i = 0; i < nAttrCount; ++i)
481 : {
482 16 : const rtl::OUString& sAttrName = xAttrList->getNameByIndex(i);
483 16 : rtl::OUString aLocalName;
484 : sal_uInt16 nPrefix =
485 16 : GetScImport().GetNamespaceMap().GetKeyByAttrName(sAttrName, &aLocalName);
486 :
487 16 : const rtl::OUString& sValue = xAttrList->getValueByIndex(i);
488 :
489 16 : switch (rAttrTokenMap.Get(nPrefix, aLocalName))
490 : {
491 : case XML_TOK_FILTER_SET_ITEM_ATTR_VALUE:
492 : {
493 16 : ScQueryEntry::Item aItem;
494 16 : aItem.maString = sValue;
495 16 : aItem.meType = ScQueryEntry::ByString;
496 16 : aItem.mfVal = 0.0;
497 16 : rParent.AddSetItem(aItem);
498 : }
499 16 : break;
500 : }
501 16 : }
502 16 : }
503 :
504 0 : SvXMLImportContext* ScXMLSetItemContext::CreateChildContext(
505 : sal_uInt16 nPrefix, const ::rtl::OUString& rLName,
506 : const Reference<XAttributeList>& /*xAttrList*/ )
507 : {
508 0 : return new SvXMLImportContext( GetImport(), nPrefix, rLName );;
509 : }
510 :
511 32 : ScXMLSetItemContext::~ScXMLSetItemContext()
512 : {
513 32 : }
514 :
515 16 : void ScXMLSetItemContext::EndElement()
516 : {
517 16 : }
518 :
519 : //==========================================================================
520 :
521 2 : ScXMLDPFilterContext::ScXMLDPFilterContext( ScXMLImport& rImport,
522 : sal_uInt16 nPrfx,
523 : const ::rtl::OUString& rLName,
524 : const ::com::sun::star::uno::Reference<
525 : ::com::sun::star::xml::sax::XAttributeList>& xAttrList,
526 : ScXMLDataPilotTableContext* pTempDataPilotTableContext) :
527 : SvXMLImportContext( rImport, nPrfx, rLName ),
528 : pDataPilotTable(pTempDataPilotTableContext),
529 : aFilterFields(),
530 : nFilterFieldCount(0),
531 : bSkipDuplicates(false),
532 : bCopyOutputData(false),
533 : bUseRegularExpressions(false),
534 : bConnectionOr(true),
535 : bNextConnectionOr(true),
536 2 : bConditionSourceRange(false)
537 : {
538 2 : ScDocument* pDoc(GetScImport().GetDocument());
539 :
540 2 : sal_Int16 nAttrCount(xAttrList.is() ? xAttrList->getLength() : 0);
541 2 : const SvXMLTokenMap& rAttrTokenMap(GetScImport().GetFilterAttrTokenMap());
542 4 : for( sal_Int16 i=0; i < nAttrCount; ++i )
543 : {
544 2 : const rtl::OUString& sAttrName(xAttrList->getNameByIndex( i ));
545 2 : rtl::OUString aLocalName;
546 2 : sal_uInt16 nPrefix(GetScImport().GetNamespaceMap().GetKeyByAttrName(
547 2 : sAttrName, &aLocalName ));
548 2 : const rtl::OUString& sValue(xAttrList->getValueByIndex( i ));
549 :
550 2 : switch( rAttrTokenMap.Get( nPrefix, aLocalName ) )
551 : {
552 : case XML_TOK_FILTER_ATTR_TARGET_RANGE_ADDRESS :
553 : {
554 0 : ScRange aScRange;
555 0 : sal_Int32 nOffset(0);
556 0 : if (ScRangeStringConverter::GetRangeFromString( aScRange, sValue, pDoc, ::formula::FormulaGrammar::CONV_OOO, nOffset ))
557 : {
558 0 : aOutputPosition = aScRange.aStart;
559 0 : bCopyOutputData = true;
560 : }
561 : }
562 0 : break;
563 : case XML_TOK_FILTER_ATTR_CONDITION_SOURCE_RANGE_ADDRESS :
564 : {
565 2 : sal_Int32 nOffset(0);
566 2 : if(ScRangeStringConverter::GetRangeFromString( aConditionSourceRangeAddress, sValue, pDoc, ::formula::FormulaGrammar::CONV_OOO, nOffset ))
567 0 : bConditionSourceRange = true;
568 : }
569 2 : break;
570 : case XML_TOK_FILTER_ATTR_CONDITION_SOURCE :
571 : {
572 : // not supported by StarOffice
573 : }
574 0 : break;
575 : case XML_TOK_FILTER_ATTR_DISPLAY_DUPLICATES :
576 : {
577 0 : bSkipDuplicates = !IsXMLToken(sValue, XML_TRUE);
578 : }
579 0 : break;
580 : }
581 2 : }
582 2 : }
583 :
584 4 : ScXMLDPFilterContext::~ScXMLDPFilterContext()
585 : {
586 4 : }
587 :
588 2 : SvXMLImportContext *ScXMLDPFilterContext::CreateChildContext( sal_uInt16 nPrefix,
589 : const ::rtl::OUString& rLName,
590 : const ::com::sun::star::uno::Reference<
591 : ::com::sun::star::xml::sax::XAttributeList>& xAttrList )
592 : {
593 2 : SvXMLImportContext *pContext(0);
594 :
595 2 : const SvXMLTokenMap& rTokenMap(GetScImport().GetFilterElemTokenMap());
596 2 : switch( rTokenMap.Get( nPrefix, rLName ) )
597 : {
598 : case XML_TOK_FILTER_AND:
599 : {
600 : pContext = new ScXMLDPAndContext( GetScImport(), nPrefix,
601 2 : rLName, xAttrList, this);
602 : }
603 2 : break;
604 : case XML_TOK_FILTER_OR:
605 : {
606 : pContext = new ScXMLDPOrContext( GetScImport(), nPrefix,
607 0 : rLName, xAttrList, this);
608 : }
609 0 : break;
610 : case XML_TOK_FILTER_CONDITION:
611 : {
612 : pContext = new ScXMLDPConditionContext( GetScImport(), nPrefix,
613 0 : rLName, xAttrList, this);
614 : }
615 0 : break;
616 : }
617 :
618 2 : if( !pContext )
619 0 : pContext = new SvXMLImportContext( GetImport(), nPrefix, rLName );
620 :
621 2 : return pContext;
622 : }
623 :
624 2 : void ScXMLDPFilterContext::EndElement()
625 : {
626 2 : aFilterFields.bRegExp = bUseRegularExpressions;
627 2 : aFilterFields.bCaseSens = bIsCaseSensitive;
628 2 : aFilterFields.bDuplicate = !bSkipDuplicates;
629 2 : if (bCopyOutputData)
630 0 : pDataPilotTable->SetFilterOutputPosition(aOutputPosition);
631 :
632 2 : pDataPilotTable->SetSourceQueryParam(aFilterFields);
633 2 : if (bConditionSourceRange)
634 0 : pDataPilotTable->SetFilterSourceRange(aConditionSourceRangeAddress);
635 2 : }
636 :
637 4 : void ScXMLDPFilterContext::AddFilterField (const ScQueryEntry& aFilterField)
638 : {
639 4 : aFilterFields.Resize(nFilterFieldCount + 1);
640 4 : ScQueryEntry& rEntry(aFilterFields.GetEntry(nFilterFieldCount));
641 4 : rEntry = aFilterField;
642 4 : rEntry.bDoQuery = true;
643 4 : ++nFilterFieldCount;
644 4 : }
645 :
646 2 : ScXMLDPAndContext::ScXMLDPAndContext( ScXMLImport& rImport,
647 : sal_uInt16 nPrfx,
648 : const ::rtl::OUString& rLName,
649 : const ::com::sun::star::uno::Reference<
650 : ::com::sun::star::xml::sax::XAttributeList>& /* xAttrList */,
651 : ScXMLDPFilterContext* pTempFilterContext) :
652 2 : SvXMLImportContext( rImport, nPrfx, rLName )
653 : {
654 2 : pFilterContext = pTempFilterContext;
655 2 : pFilterContext->OpenConnection(false);
656 2 : }
657 :
658 4 : ScXMLDPAndContext::~ScXMLDPAndContext()
659 : {
660 4 : }
661 :
662 4 : SvXMLImportContext *ScXMLDPAndContext::CreateChildContext( sal_uInt16 nPrefix,
663 : const ::rtl::OUString& rLName,
664 : const ::com::sun::star::uno::Reference<
665 : ::com::sun::star::xml::sax::XAttributeList>& xAttrList )
666 : {
667 4 : SvXMLImportContext *pContext(0);
668 :
669 4 : const SvXMLTokenMap& rTokenMap(GetScImport().GetFilterElemTokenMap());
670 4 : switch( rTokenMap.Get( nPrefix, rLName ) )
671 : {
672 : case XML_TOK_FILTER_OR:
673 : {
674 : // not supported in StarOffice
675 : }
676 0 : break;
677 : case XML_TOK_FILTER_CONDITION:
678 : {
679 : pContext = new ScXMLDPConditionContext( GetScImport(), nPrefix,
680 4 : rLName, xAttrList, pFilterContext);
681 : }
682 4 : break;
683 : }
684 :
685 4 : if( !pContext )
686 0 : pContext = new SvXMLImportContext( GetImport(), nPrefix, rLName );
687 :
688 4 : return pContext;
689 : }
690 :
691 2 : void ScXMLDPAndContext::EndElement()
692 : {
693 2 : pFilterContext->CloseConnection();
694 2 : }
695 :
696 0 : ScXMLDPOrContext::ScXMLDPOrContext( ScXMLImport& rImport,
697 : sal_uInt16 nPrfx,
698 : const ::rtl::OUString& rLName,
699 : const ::com::sun::star::uno::Reference<
700 : ::com::sun::star::xml::sax::XAttributeList>& /* xAttrList */,
701 : ScXMLDPFilterContext* pTempFilterContext) :
702 : SvXMLImportContext( rImport, nPrfx, rLName ),
703 0 : pFilterContext(pTempFilterContext)
704 : {
705 0 : pFilterContext->OpenConnection(true);
706 0 : }
707 :
708 0 : ScXMLDPOrContext::~ScXMLDPOrContext()
709 : {
710 0 : }
711 :
712 0 : SvXMLImportContext *ScXMLDPOrContext::CreateChildContext( sal_uInt16 nPrefix,
713 : const ::rtl::OUString& rLName,
714 : const ::com::sun::star::uno::Reference<
715 : ::com::sun::star::xml::sax::XAttributeList>& xAttrList )
716 : {
717 0 : SvXMLImportContext *pContext(0);
718 :
719 0 : const SvXMLTokenMap& rTokenMap(GetScImport().GetFilterElemTokenMap());
720 0 : switch( rTokenMap.Get( nPrefix, rLName ) )
721 : {
722 : case XML_TOK_FILTER_AND:
723 : {
724 : pContext = new ScXMLDPAndContext( GetScImport(), nPrefix,
725 0 : rLName, xAttrList, pFilterContext);
726 : }
727 0 : break;
728 : case XML_TOK_FILTER_CONDITION:
729 : {
730 : pContext = new ScXMLDPConditionContext( GetScImport(), nPrefix,
731 0 : rLName, xAttrList, pFilterContext);
732 : }
733 0 : break;
734 : }
735 :
736 0 : if( !pContext )
737 0 : pContext = new SvXMLImportContext( GetImport(), nPrefix, rLName );
738 :
739 0 : return pContext;
740 : }
741 :
742 0 : void ScXMLDPOrContext::EndElement()
743 : {
744 0 : pFilterContext->CloseConnection();
745 0 : }
746 :
747 4 : ScXMLDPConditionContext::ScXMLDPConditionContext( ScXMLImport& rImport,
748 : sal_uInt16 nPrfx,
749 : const ::rtl::OUString& rLName,
750 : const ::com::sun::star::uno::Reference<
751 : ::com::sun::star::xml::sax::XAttributeList>& xAttrList,
752 : ScXMLDPFilterContext* pTempFilterContext) :
753 : SvXMLImportContext( rImport, nPrfx, rLName ),
754 : pFilterContext(pTempFilterContext),
755 4 : sDataType(GetXMLToken(XML_TEXT)),
756 8 : bIsCaseSensitive(false)
757 : {
758 :
759 4 : sal_Int16 nAttrCount(xAttrList.is() ? xAttrList->getLength() : 0);
760 4 : const SvXMLTokenMap& rAttrTokenMap(GetScImport().GetFilterConditionAttrTokenMap());
761 20 : for( sal_Int16 i=0; i < nAttrCount; ++i )
762 : {
763 16 : const rtl::OUString& sAttrName(xAttrList->getNameByIndex( i ));
764 16 : rtl::OUString aLocalName;
765 16 : sal_uInt16 nPrefix(GetScImport().GetNamespaceMap().GetKeyByAttrName(
766 16 : sAttrName, &aLocalName ));
767 16 : const rtl::OUString& sValue(xAttrList->getValueByIndex( i ));
768 :
769 16 : switch( rAttrTokenMap.Get( nPrefix, aLocalName ) )
770 : {
771 : case XML_TOK_CONDITION_ATTR_FIELD_NUMBER :
772 : {
773 4 : nField = sValue.toInt32();
774 : }
775 4 : break;
776 : case XML_TOK_CONDITION_ATTR_CASE_SENSITIVE :
777 : {
778 0 : bIsCaseSensitive = IsXMLToken(sValue, XML_TRUE);
779 : }
780 0 : break;
781 : case XML_TOK_CONDITION_ATTR_DATA_TYPE :
782 : {
783 4 : sDataType = sValue;
784 : }
785 4 : break;
786 : case XML_TOK_CONDITION_ATTR_VALUE :
787 : {
788 4 : sConditionValue = sValue;
789 : }
790 4 : break;
791 : case XML_TOK_CONDITION_ATTR_OPERATOR :
792 : {
793 4 : sOperator = sValue;
794 : }
795 4 : break;
796 : }
797 16 : }
798 4 : }
799 :
800 8 : ScXMLDPConditionContext::~ScXMLDPConditionContext()
801 : {
802 8 : }
803 :
804 0 : SvXMLImportContext *ScXMLDPConditionContext::CreateChildContext( sal_uInt16 nPrefix,
805 : const ::rtl::OUString& rLName,
806 : const ::com::sun::star::uno::Reference<
807 : ::com::sun::star::xml::sax::XAttributeList>& /* xAttrList */ )
808 : {
809 0 : return new SvXMLImportContext( GetImport(), nPrefix, rLName );
810 : }
811 :
812 4 : void ScXMLDPConditionContext::getOperatorXML(
813 : const rtl::OUString sTempOperator, ScQueryOp& aFilterOperator, bool& bUseRegularExpressions) const
814 : {
815 4 : bUseRegularExpressions = false;
816 4 : if (IsXMLToken(sTempOperator, XML_MATCH))
817 : {
818 0 : bUseRegularExpressions = true;
819 0 : aFilterOperator = SC_EQUAL;
820 : }
821 4 : else if (IsXMLToken(sTempOperator, XML_NOMATCH))
822 : {
823 0 : bUseRegularExpressions = true;
824 0 : aFilterOperator = SC_NOT_EQUAL;
825 : }
826 4 : else if (sTempOperator.compareToAscii("=") == 0)
827 0 : aFilterOperator = SC_EQUAL;
828 4 : else if (sTempOperator.compareToAscii("!=") == 0)
829 0 : aFilterOperator = SC_NOT_EQUAL;
830 4 : else if (IsXMLToken(sTempOperator, XML_BOTTOM_PERCENT))
831 0 : aFilterOperator = SC_BOTPERC;
832 4 : else if (IsXMLToken(sTempOperator, XML_BOTTOM_VALUES))
833 0 : aFilterOperator = SC_BOTVAL;
834 4 : else if (sTempOperator.compareToAscii(">") == 0)
835 2 : aFilterOperator = SC_GREATER;
836 2 : else if (sTempOperator.compareToAscii(">=") == 0)
837 0 : aFilterOperator = SC_GREATER_EQUAL;
838 2 : else if (sTempOperator.compareToAscii("<") == 0)
839 0 : aFilterOperator = SC_LESS;
840 2 : else if (sTempOperator.compareToAscii("<=") == 0)
841 2 : aFilterOperator = SC_LESS_EQUAL;
842 0 : else if (IsXMLToken(sTempOperator, XML_TOP_PERCENT))
843 0 : aFilterOperator = SC_TOPPERC;
844 0 : else if (IsXMLToken(sTempOperator, XML_TOP_VALUES))
845 0 : aFilterOperator = SC_TOPVAL;
846 4 : }
847 :
848 4 : void ScXMLDPConditionContext::EndElement()
849 : {
850 4 : ScQueryEntry aFilterField;
851 4 : if (pFilterContext->GetConnection())
852 2 : aFilterField.eConnect = SC_OR;
853 : else
854 2 : aFilterField.eConnect = SC_AND;
855 4 : pFilterContext->SetIsCaseSensitive(bIsCaseSensitive);
856 4 : if (IsXMLToken(sOperator, XML_EMPTY))
857 0 : aFilterField.SetQueryByEmpty();
858 4 : else if (IsXMLToken(sOperator, XML_NOEMPTY))
859 0 : aFilterField.SetQueryByNonEmpty();
860 : else
861 : {
862 4 : bool bUseRegularExpressions = false;
863 4 : getOperatorXML(sOperator, aFilterField.eOp, bUseRegularExpressions);
864 4 : pFilterContext->SetUseRegularExpressions(bUseRegularExpressions);
865 4 : aFilterField.nField = nField;
866 4 : ScQueryEntry::Item& rItem = aFilterField.GetQueryItem();
867 4 : if (IsXMLToken(sDataType, XML_NUMBER))
868 : {
869 4 : rItem.mfVal = sConditionValue.toDouble();
870 4 : rItem.maString = sConditionValue;
871 4 : rItem.meType = ScQueryEntry::ByValue;
872 : }
873 : else
874 : {
875 0 : rItem.maString = sConditionValue;
876 0 : rItem.meType = ScQueryEntry::ByString;
877 0 : rItem.mfVal = 0.0;
878 : }
879 : }
880 4 : pFilterContext->AddFilterField(aFilterField);
881 106 : }
882 :
883 :
884 :
885 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|