fix baseline build (old cairo) - 'cairo_rectangle_int_t' does not name a type
[LibreOffice.git] / sc / source / filter / xml / xmlexternaltabi.cxx
blobfc90e5d28f2cca62c69d1233189751dcc15d38c7
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/.
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 "xmlexternaltabi.hxx"
21 #include "xmlimprt.hxx"
22 #include "xmltabi.hxx"
23 #include "xmlstyli.hxx"
25 #include "token.hxx"
26 #include "document.hxx"
27 #include <documentimport.hxx>
29 #include <svl/sharedstringpool.hxx>
30 #include <xmloff/nmspmap.hxx>
31 #include <xmloff/xmlnmspe.hxx>
32 #include <xmloff/xmltoken.hxx>
33 #include <xmloff/xmluconv.hxx>
35 #include <sax/tools/converter.hxx>
37 #include <com/sun/star/util/NumberFormat.hpp>
39 using namespace ::com::sun::star;
41 using ::com::sun::star::uno::Reference;
42 using ::com::sun::star::xml::sax::XAttributeList;
44 ScXMLExternalRefTabSourceContext::ScXMLExternalRefTabSourceContext(
45 ScXMLImport& rImport, sal_uInt16 nPrefix, const OUString& rLName,
46 const Reference<XAttributeList>& xAttrList, ScXMLExternalTabData& rRefInfo ) :
47 SvXMLImportContext( rImport, nPrefix, rLName ),
48 mrScImport(rImport),
49 mrExternalRefInfo(rRefInfo)
51 using namespace ::xmloff::token;
53 sal_Int16 nAttrCount = xAttrList.is() ? xAttrList->getLength() : 0;
54 for (sal_Int16 i = 0; i < nAttrCount; ++i)
56 const OUString& sAttrName = xAttrList->getNameByIndex(i);
57 OUString aLocalName;
58 sal_uInt16 nAttrPrefix = mrScImport.GetNamespaceMap().GetKeyByAttrName(sAttrName, &aLocalName);
59 const OUString& sValue = xAttrList->getValueByIndex(i);
60 if (nAttrPrefix == XML_NAMESPACE_XLINK)
62 if (IsXMLToken(aLocalName, XML_HREF))
63 maRelativeUrl = sValue;
65 else if (nAttrPrefix == XML_NAMESPACE_TABLE)
67 if (IsXMLToken(aLocalName, XML_TABLE_NAME))
68 maTableName = sValue;
69 else if (IsXMLToken(aLocalName, XML_FILTER_NAME))
70 maFilterName = sValue;
71 else if (IsXMLToken(aLocalName, XML_FILTER_OPTIONS))
72 maFilterOptions = sValue;
77 ScXMLExternalRefTabSourceContext::~ScXMLExternalRefTabSourceContext()
81 SvXMLImportContext* ScXMLExternalRefTabSourceContext::CreateChildContext(
82 sal_uInt16 nPrefix, const OUString& rLocalName, const Reference<XAttributeList>& /*xAttrList*/ )
84 return new SvXMLImportContext(GetImport(), nPrefix, rLocalName);
87 /**
88 * Make sure the URL is a valid relative URL, mainly to avoid storing
89 * absolute URL as relative URL by accident. For now, we only check the first
90 * three characters which are assumed to be always '../', because the relative
91 * URL for an external document is always in reference to the content.xml
92 * fragment of the original document.
94 static bool lcl_isValidRelativeURL(const OUString& rUrl)
96 sal_Int32 n = ::std::min( rUrl.getLength(), static_cast<sal_Int32>(3));
97 if (n < 3)
98 return false;
99 const sal_Unicode* p = rUrl.getStr();
100 for (sal_Int32 i = 0; i < n; ++i)
102 sal_Unicode c = p[i];
103 if (i < 2 && c != '.')
104 // the path must begin with '..'
105 return false;
106 else if (i == 2 && c != '/')
107 // a '/' path separator must follow
108 return false;
110 return true;
113 void ScXMLExternalRefTabSourceContext::EndElement()
115 ScDocument* pDoc = mrScImport.GetDocument();
116 if (!pDoc)
117 return;
119 ScExternalRefManager* pRefMgr = pDoc->GetExternalRefManager();
120 if (lcl_isValidRelativeURL(maRelativeUrl))
121 pRefMgr->setRelativeFileName(mrExternalRefInfo.mnFileId, maRelativeUrl);
122 pRefMgr->setFilterData(mrExternalRefInfo.mnFileId, maFilterName, maFilterOptions);
125 ScXMLExternalRefRowsContext::ScXMLExternalRefRowsContext(
126 ScXMLImport& rImport, sal_uInt16 nPrefix, const OUString& rLName,
127 const Reference<XAttributeList>& /* xAttrList */, ScXMLExternalTabData& rRefInfo ) :
128 SvXMLImportContext( rImport, nPrefix, rLName ),
129 mrScImport(rImport),
130 mrExternalRefInfo(rRefInfo)
134 ScXMLExternalRefRowsContext::~ScXMLExternalRefRowsContext()
138 SvXMLImportContext* ScXMLExternalRefRowsContext::CreateChildContext(
139 sal_uInt16 nPrefix, const OUString& rLocalName, const Reference<XAttributeList>& xAttrList )
141 // #i101319# row elements inside group, rows or header-rows
142 // are treated like row elements directly in the table element
144 const SvXMLTokenMap& rTokenMap = mrScImport.GetTableRowsElemTokenMap();
145 sal_uInt16 nToken = rTokenMap.Get(nPrefix, rLocalName);
146 switch (nToken)
148 case XML_TOK_TABLE_ROWS_ROW_GROUP:
149 case XML_TOK_TABLE_ROWS_HEADER_ROWS:
150 case XML_TOK_TABLE_ROWS_ROWS:
151 return new ScXMLExternalRefRowsContext(
152 mrScImport, nPrefix, rLocalName, xAttrList, mrExternalRefInfo);
153 case XML_TOK_TABLE_ROWS_ROW:
154 return new ScXMLExternalRefRowContext(
155 mrScImport, nPrefix, rLocalName, xAttrList, mrExternalRefInfo);
156 default:
159 return new SvXMLImportContext(GetImport(), nPrefix, rLocalName);
162 void ScXMLExternalRefRowsContext::EndElement()
166 ScXMLExternalRefRowContext::ScXMLExternalRefRowContext(
167 ScXMLImport& rImport, sal_uInt16 nPrefix, const OUString& rLName,
168 const Reference<XAttributeList>& xAttrList, ScXMLExternalTabData& rRefInfo ) :
169 SvXMLImportContext( rImport, nPrefix, rLName ),
170 mrScImport(rImport),
171 mrExternalRefInfo(rRefInfo),
172 mnRepeatRowCount(1)
174 mrExternalRefInfo.mnCol = 0;
176 sal_Int16 nAttrCount(xAttrList.is() ? xAttrList->getLength() : 0);
177 const SvXMLTokenMap& rAttrTokenMap = mrScImport.GetTableRowAttrTokenMap();
178 for( sal_Int16 i=0; i < nAttrCount; ++i )
180 const OUString& sAttrName = xAttrList->getNameByIndex(i);
181 OUString aLocalName;
182 sal_uInt16 nAttrPrefix = mrScImport.GetNamespaceMap().GetKeyByAttrName(sAttrName, &aLocalName);
183 const OUString& sValue = xAttrList->getValueByIndex(i);
185 switch (rAttrTokenMap.Get(nAttrPrefix, aLocalName))
187 case XML_TOK_TABLE_ROW_ATTR_REPEATED:
189 mnRepeatRowCount = std::max(sValue.toInt32(), static_cast<sal_Int32>(1));
191 break;
196 ScXMLExternalRefRowContext::~ScXMLExternalRefRowContext()
200 SvXMLImportContext* ScXMLExternalRefRowContext::CreateChildContext(
201 sal_uInt16 nPrefix, const OUString& rLocalName, const Reference<XAttributeList>& xAttrList )
203 const SvXMLTokenMap& rTokenMap = mrScImport.GetTableRowElemTokenMap();
204 sal_uInt16 nToken = rTokenMap.Get(nPrefix, rLocalName);
205 if (nToken == XML_TOK_TABLE_ROW_CELL || nToken == XML_TOK_TABLE_ROW_COVERED_CELL)
206 return new ScXMLExternalRefCellContext(mrScImport, nPrefix, rLocalName, xAttrList, mrExternalRefInfo);
208 return new SvXMLImportContext(GetImport(), nPrefix, rLocalName);
211 void ScXMLExternalRefRowContext::EndElement()
213 ScExternalRefCache::TableTypeRef pTab = mrExternalRefInfo.mpCacheTable;
215 for (sal_Int32 i = 1; i < mnRepeatRowCount; ++i)
217 // Performance: duplicates of a non-existent row will still not exist.
218 // Don't find that out for every cell.
219 // External references often are a sparse matrix.
220 if (i == 1 && !pTab->hasRow( mrExternalRefInfo.mnRow))
222 mrExternalRefInfo.mnRow += mnRepeatRowCount;
223 return;
226 for (sal_Int32 j = 0; j < mrExternalRefInfo.mnCol; ++j)
228 ScExternalRefCache::TokenRef pToken = pTab->getCell(
229 static_cast<SCCOL>(j), static_cast<SCROW>(mrExternalRefInfo.mnRow));
231 if (pToken.get())
233 pTab->setCell(static_cast<SCCOL>(j),
234 static_cast<SCROW>(mrExternalRefInfo.mnRow+i), pToken);
238 mrExternalRefInfo.mnRow += mnRepeatRowCount;
241 ScXMLExternalRefCellContext::ScXMLExternalRefCellContext(
242 ScXMLImport& rImport, sal_uInt16 nPrefix, const OUString& rLName,
243 const Reference<XAttributeList>& xAttrList, ScXMLExternalTabData& rRefInfo ) :
244 SvXMLImportContext( rImport, nPrefix, rLName ),
245 mrScImport(rImport),
246 mrExternalRefInfo(rRefInfo),
247 mfCellValue(0.0),
248 mnRepeatCount(1),
249 mnNumberFormat(-1),
250 mnCellType(::com::sun::star::util::NumberFormat::UNDEFINED),
251 mbIsNumeric(false),
252 mbIsEmpty(true)
254 using namespace ::xmloff::token;
256 sal_Int16 nAttrCount = xAttrList.is() ? xAttrList->getLength() : 0;
257 const SvXMLTokenMap& rTokenMap = rImport.GetTableRowCellAttrTokenMap();
258 for (sal_Int16 i = 0; i < nAttrCount; ++i)
260 OUString aLocalName;
261 sal_uInt16 nAttrPrefix = rImport.GetNamespaceMap().GetKeyByAttrName(
262 xAttrList->getNameByIndex(i), &aLocalName);
264 const OUString& sValue = xAttrList->getValueByIndex(i);
265 sal_uInt16 nToken = rTokenMap.Get(nAttrPrefix, aLocalName);
267 switch (nToken)
269 case XML_TOK_TABLE_ROW_CELL_ATTR_STYLE_NAME:
271 XMLTableStylesContext* pStyles = static_cast<XMLTableStylesContext*>(mrScImport.GetAutoStyles());
272 const XMLTableStyleContext* pStyle = static_cast<const XMLTableStyleContext*>(
273 pStyles->FindStyleChildContext(XML_STYLE_FAMILY_TABLE_CELL, sValue, true));
274 if (pStyle)
275 mnNumberFormat = const_cast<XMLTableStyleContext*>(pStyle)->GetNumberFormat();
277 break;
278 case XML_TOK_TABLE_ROW_CELL_ATTR_REPEATED:
280 mnRepeatCount = ::std::max(sValue.toInt32(), static_cast<sal_Int32>(1));
282 break;
283 case XML_TOK_TABLE_ROW_CELL_ATTR_VALUE_TYPE:
285 mnCellType = mrScImport.GetCellType(sValue);
287 break;
288 case XML_TOK_TABLE_ROW_CELL_ATTR_VALUE:
290 if (!sValue.isEmpty())
292 ::sax::Converter::convertDouble(mfCellValue, sValue);
293 mbIsNumeric = true;
294 mbIsEmpty = false;
297 break;
298 case XML_TOK_TABLE_ROW_CELL_ATTR_DATE_VALUE:
300 if (!sValue.isEmpty() && mrScImport.SetNullDateOnUnitConverter())
302 mrScImport.GetMM100UnitConverter().convertDateTime(mfCellValue, sValue);
303 mbIsNumeric = true;
304 mbIsEmpty = false;
307 break;
308 case XML_TOK_TABLE_ROW_CELL_ATTR_TIME_VALUE:
310 if (!sValue.isEmpty())
312 ::sax::Converter::convertDuration(mfCellValue, sValue);
313 mbIsNumeric = true;
314 mbIsEmpty = false;
317 break;
318 case XML_TOK_TABLE_ROW_CELL_ATTR_STRING_VALUE:
320 if (!sValue.isEmpty())
322 maCellString = sValue;
323 mbIsNumeric = false;
324 mbIsEmpty = false;
327 break;
328 case XML_TOK_TABLE_ROW_CELL_ATTR_BOOLEAN_VALUE:
330 if (!sValue.isEmpty())
332 mfCellValue = IsXMLToken(sValue, XML_TRUE) ? 1.0 : 0.0;
333 mbIsNumeric = true;
334 mbIsEmpty = false;
337 break;
338 default:
344 ScXMLExternalRefCellContext::~ScXMLExternalRefCellContext()
348 SvXMLImportContext* ScXMLExternalRefCellContext::CreateChildContext(
349 sal_uInt16 nPrefix, const OUString& rLocalName, const Reference<XAttributeList>& xAttrList )
351 const SvXMLTokenMap& rTokenMap = mrScImport.GetTableRowCellElemTokenMap();
352 sal_uInt16 nToken = rTokenMap.Get(nPrefix, rLocalName);
353 if (nToken == XML_TOK_TABLE_ROW_CELL_P)
354 return new ScXMLExternalRefCellTextContext(mrScImport, nPrefix, rLocalName, xAttrList, *this);
356 return new SvXMLImportContext(GetImport(), nPrefix, rLocalName);
359 void ScXMLExternalRefCellContext::EndElement()
361 if (!maCellString.isEmpty())
362 mbIsEmpty = false;
364 for (sal_Int32 i = 0; i < mnRepeatCount; ++i, ++mrExternalRefInfo.mnCol)
366 if (mbIsEmpty)
367 continue;
369 ScExternalRefCache::TokenRef aToken;
370 if (mbIsNumeric)
371 aToken.reset(new formula::FormulaDoubleToken(mfCellValue));
372 else
374 ScDocument& rDoc = mrScImport.GetDoc().getDoc();
375 svl::SharedString aSS = rDoc.GetSharedStringPool().intern(maCellString);
376 aToken.reset(new formula::FormulaStringToken(aSS));
379 sal_uInt32 nNumFmt = mnNumberFormat >= 0 ? static_cast<sal_uInt32>(mnNumberFormat) : 0;
380 mrExternalRefInfo.mpCacheTable->setCell(
381 static_cast<SCCOL>(mrExternalRefInfo.mnCol),
382 static_cast<SCROW>(mrExternalRefInfo.mnRow),
383 aToken, nNumFmt);
387 void ScXMLExternalRefCellContext::SetCellString(const OUString& rStr)
389 maCellString = rStr;
392 ScXMLExternalRefCellTextContext::ScXMLExternalRefCellTextContext(
393 ScXMLImport& rImport, sal_uInt16 nPrefix, const OUString& rLName,
394 const Reference<XAttributeList>& /*xAttrList*/,
395 ScXMLExternalRefCellContext& rParent ) :
396 SvXMLImportContext( rImport, nPrefix, rLName ),
397 mrParent(rParent)
401 ScXMLExternalRefCellTextContext::~ScXMLExternalRefCellTextContext()
405 SvXMLImportContext* ScXMLExternalRefCellTextContext::CreateChildContext(
406 sal_uInt16 nPrefix, const OUString& rLocalName, const Reference<XAttributeList>& /*xAttrList*/ )
408 return new SvXMLImportContext(GetImport(), nPrefix, rLocalName);
411 void ScXMLExternalRefCellTextContext::Characters(const OUString& rChar)
413 maCellStrBuf.append(rChar);
416 void ScXMLExternalRefCellTextContext::EndElement()
418 mrParent.SetCellString(maCellStrBuf.makeStringAndClear());
421 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */