1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: xmlexternaltabi.cxx,v $
10 * $Revision: 1.1.2.5 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_sc.hxx"
36 // INCLUDE ---------------------------------------------------------------
38 #include "xmlexternaltabi.hxx"
39 #include "xmlimprt.hxx"
40 #include "xmltabi.hxx"
41 #include "xmlstyli.hxx"
44 #include "document.hxx"
46 #include <xmloff/nmspmap.hxx>
47 #include <xmloff/xmlnmspe.hxx>
48 #include <xmloff/xmltoken.hxx>
49 #include <xmloff/xmluconv.hxx>
50 #include <com/sun/star/util/NumberFormat.hpp>
52 using namespace ::com::sun::star
;
54 using ::rtl::OUString
;
55 using ::com::sun::star::uno::Reference
;
56 using ::com::sun::star::xml::sax::XAttributeList
;
58 // ============================================================================
60 ScXMLExternalRefTabSourceContext::ScXMLExternalRefTabSourceContext(
61 ScXMLImport
& rImport
, USHORT nPrefix
, const OUString
& rLName
,
62 const Reference
<XAttributeList
>& xAttrList
, ScXMLExternalTabData
& rRefInfo
) :
63 SvXMLImportContext( rImport
, nPrefix
, rLName
),
65 mrExternalRefInfo(rRefInfo
)
67 using namespace ::xmloff::token
;
69 sal_Int16 nAttrCount
= xAttrList
.is() ? xAttrList
->getLength() : 0;
70 for (sal_Int16 i
= 0; i
< nAttrCount
; ++i
)
72 const rtl::OUString
& sAttrName
= xAttrList
->getNameByIndex(i
);
73 rtl::OUString aLocalName
;
74 sal_uInt16 nAttrPrefix
= mrScImport
.GetNamespaceMap().GetKeyByAttrName(sAttrName
, &aLocalName
);
75 const rtl::OUString
& sValue
= xAttrList
->getValueByIndex(i
);
76 if (nAttrPrefix
== XML_NAMESPACE_XLINK
)
78 if (IsXMLToken(aLocalName
, XML_HREF
))
79 maRelativeUrl
= sValue
;
81 else if (nAttrPrefix
== XML_NAMESPACE_TABLE
)
83 if (IsXMLToken(aLocalName
, XML_TABLE_NAME
))
85 else if (IsXMLToken(aLocalName
, XML_FILTER_NAME
))
86 maFilterName
= sValue
;
87 else if (IsXMLToken(aLocalName
, XML_FILTER_OPTIONS
))
88 maFilterOptions
= sValue
;
93 ScXMLExternalRefTabSourceContext::~ScXMLExternalRefTabSourceContext()
97 SvXMLImportContext
* ScXMLExternalRefTabSourceContext::CreateChildContext(
98 USHORT nPrefix
, const OUString
& rLocalName
, const Reference
<XAttributeList
>& /*xAttrList*/ )
100 return new SvXMLImportContext(GetImport(), nPrefix
, rLocalName
);
103 void ScXMLExternalRefTabSourceContext::EndElement()
105 ScDocument
* pDoc
= mrScImport
.GetDocument();
109 ScExternalRefManager
* pRefMgr
= pDoc
->GetExternalRefManager();
110 if (!maRelativeUrl
.equals(mrExternalRefInfo
.maFileUrl
))
111 pRefMgr
->setRelativeFileName(mrExternalRefInfo
.mnFileId
, maRelativeUrl
);
112 pRefMgr
->setFilterData(mrExternalRefInfo
.mnFileId
, maFilterName
, maFilterOptions
);
115 // ============================================================================
117 ScXMLExternalRefRowsContext::ScXMLExternalRefRowsContext(
118 ScXMLImport
& rImport
, USHORT nPrefix
, const OUString
& rLName
,
119 const Reference
<XAttributeList
>& /* xAttrList */, ScXMLExternalTabData
& rRefInfo
) :
120 SvXMLImportContext( rImport
, nPrefix
, rLName
),
122 mrExternalRefInfo(rRefInfo
)
126 ScXMLExternalRefRowsContext::~ScXMLExternalRefRowsContext()
130 SvXMLImportContext
* ScXMLExternalRefRowsContext::CreateChildContext(
131 USHORT nPrefix
, const OUString
& rLocalName
, const Reference
<XAttributeList
>& xAttrList
)
133 // #i101319# row elements inside group, rows or header-rows
134 // are treated like row elements directly in the table element
136 const SvXMLTokenMap
& rTokenMap
= mrScImport
.GetTableRowsElemTokenMap();
137 sal_uInt16 nToken
= rTokenMap
.Get(nPrefix
, rLocalName
);
140 case XML_TOK_TABLE_ROWS_ROW_GROUP
:
141 case XML_TOK_TABLE_ROWS_HEADER_ROWS
:
142 case XML_TOK_TABLE_ROWS_ROWS
:
143 return new ScXMLExternalRefRowsContext(
144 mrScImport
, nPrefix
, rLocalName
, xAttrList
, mrExternalRefInfo
);
145 case XML_TOK_TABLE_ROWS_ROW
:
146 return new ScXMLExternalRefRowContext(
147 mrScImport
, nPrefix
, rLocalName
, xAttrList
, mrExternalRefInfo
);
151 return new SvXMLImportContext(GetImport(), nPrefix
, rLocalName
);
154 void ScXMLExternalRefRowsContext::EndElement()
158 // ============================================================================
160 ScXMLExternalRefRowContext::ScXMLExternalRefRowContext(
161 ScXMLImport
& rImport
, USHORT nPrefix
, const OUString
& rLName
,
162 const Reference
<XAttributeList
>& xAttrList
, ScXMLExternalTabData
& rRefInfo
) :
163 SvXMLImportContext( rImport
, nPrefix
, rLName
),
165 mrExternalRefInfo(rRefInfo
),
168 mrExternalRefInfo
.mnCol
= 0;
170 sal_Int16
nAttrCount(xAttrList
.is() ? xAttrList
->getLength() : 0);
171 const SvXMLTokenMap
& rAttrTokenMap
= mrScImport
.GetTableRowAttrTokenMap();
172 for( sal_Int16 i
=0; i
< nAttrCount
; ++i
)
174 const rtl::OUString
& sAttrName
= xAttrList
->getNameByIndex(i
);
175 rtl::OUString aLocalName
;
176 sal_uInt16 nAttrPrefix
= mrScImport
.GetNamespaceMap().GetKeyByAttrName(sAttrName
, &aLocalName
);
177 const rtl::OUString
& sValue
= xAttrList
->getValueByIndex(i
);
179 switch (rAttrTokenMap
.Get(nAttrPrefix
, aLocalName
))
181 case XML_TOK_TABLE_ROW_ATTR_REPEATED
:
183 mnRepeatRowCount
= std::max(sValue
.toInt32(), static_cast<sal_Int32
>(1));
190 ScXMLExternalRefRowContext::~ScXMLExternalRefRowContext()
194 SvXMLImportContext
* ScXMLExternalRefRowContext::CreateChildContext(
195 USHORT nPrefix
, const OUString
& rLocalName
, const Reference
<XAttributeList
>& xAttrList
)
197 const SvXMLTokenMap
& rTokenMap
= mrScImport
.GetTableRowElemTokenMap();
198 sal_uInt16 nToken
= rTokenMap
.Get(nPrefix
, rLocalName
);
199 if (nToken
== XML_TOK_TABLE_ROW_CELL
|| nToken
== XML_TOK_TABLE_ROW_COVERED_CELL
)
200 return new ScXMLExternalRefCellContext(mrScImport
, nPrefix
, rLocalName
, xAttrList
, mrExternalRefInfo
);
202 return new SvXMLImportContext(GetImport(), nPrefix
, rLocalName
);
205 void ScXMLExternalRefRowContext::EndElement()
207 ScExternalRefCache::TableTypeRef pTab
= mrExternalRefInfo
.mpCacheTable
;
209 for (sal_Int32 i
= 1; i
< mnRepeatRowCount
; ++i
)
211 // Performance: duplicates of a non-existent row will still not exist.
212 // Don't find that out for every cell.
213 // External references often are a sparse matrix.
214 if (i
== 1 && !pTab
->hasRow( mrExternalRefInfo
.mnRow
))
216 mrExternalRefInfo
.mnRow
+= mnRepeatRowCount
;
220 for (sal_Int32 j
= 0; j
< mrExternalRefInfo
.mnCol
; ++j
)
222 ScExternalRefCache::TokenRef pToken
= pTab
->getCell(
223 static_cast<SCCOL
>(j
), static_cast<SCROW
>(mrExternalRefInfo
.mnRow
));
227 pTab
->setCell(static_cast<SCCOL
>(j
),
228 static_cast<SCROW
>(mrExternalRefInfo
.mnRow
+i
), pToken
);
232 mrExternalRefInfo
.mnRow
+= mnRepeatRowCount
;
235 // ============================================================================
237 ScXMLExternalRefCellContext::ScXMLExternalRefCellContext(
238 ScXMLImport
& rImport
, USHORT nPrefix
, const OUString
& rLName
,
239 const Reference
<XAttributeList
>& xAttrList
, ScXMLExternalTabData
& rRefInfo
) :
240 SvXMLImportContext( rImport
, nPrefix
, rLName
),
242 mrExternalRefInfo(rRefInfo
),
246 mnCellType(::com::sun::star::util::NumberFormat::UNDEFINED
),
250 using namespace ::xmloff::token
;
252 sal_Int16 nAttrCount
= xAttrList
.is() ? xAttrList
->getLength() : 0;
253 const SvXMLTokenMap
& rTokenMap
= rImport
.GetTableRowCellAttrTokenMap();
254 for (sal_Int16 i
= 0; i
< nAttrCount
; ++i
)
257 sal_uInt16 nAttrPrefix
= rImport
.GetNamespaceMap().GetKeyByAttrName(
258 xAttrList
->getNameByIndex(i
), &aLocalName
);
260 const rtl::OUString
& sValue
= xAttrList
->getValueByIndex(i
);
261 sal_uInt16 nToken
= rTokenMap
.Get(nAttrPrefix
, aLocalName
);
265 case XML_TOK_TABLE_ROW_CELL_ATTR_STYLE_NAME
:
267 XMLTableStylesContext
* pStyles
= static_cast<XMLTableStylesContext
*>(mrScImport
.GetAutoStyles());
268 const XMLTableStyleContext
* pStyle
= static_cast<const XMLTableStyleContext
*>(
269 pStyles
->FindStyleChildContext(XML_STYLE_FAMILY_TABLE_CELL
, sValue
, true));
271 mnNumberFormat
= const_cast<XMLTableStyleContext
*>(pStyle
)->GetNumberFormat();
274 case XML_TOK_TABLE_ROW_CELL_ATTR_REPEATED
:
276 mnRepeatCount
= ::std::max(sValue
.toInt32(), static_cast<sal_Int32
>(1));
279 case XML_TOK_TABLE_ROW_CELL_ATTR_VALUE_TYPE
:
281 mnCellType
= mrScImport
.GetCellType(sValue
);
284 case XML_TOK_TABLE_ROW_CELL_ATTR_VALUE
:
286 if (sValue
.getLength())
288 mrScImport
.GetMM100UnitConverter().convertDouble(mfCellValue
, sValue
);
294 case XML_TOK_TABLE_ROW_CELL_ATTR_DATE_VALUE
:
296 if (sValue
.getLength() && mrScImport
.SetNullDateOnUnitConverter())
298 mrScImport
.GetMM100UnitConverter().convertDateTime(mfCellValue
, sValue
);
304 case XML_TOK_TABLE_ROW_CELL_ATTR_TIME_VALUE
:
306 if (sValue
.getLength())
308 mrScImport
.GetMM100UnitConverter().convertTime(mfCellValue
, sValue
);
314 case XML_TOK_TABLE_ROW_CELL_ATTR_STRING_VALUE
:
316 if (sValue
.getLength())
318 maCellString
= sValue
;
324 case XML_TOK_TABLE_ROW_CELL_ATTR_BOOLEAN_VALUE
:
326 if (sValue
.getLength())
328 mfCellValue
= IsXMLToken(sValue
, XML_TRUE
) ? 1.0 : 0.0;
340 ScXMLExternalRefCellContext::~ScXMLExternalRefCellContext()
344 SvXMLImportContext
* ScXMLExternalRefCellContext::CreateChildContext(
345 USHORT nPrefix
, const OUString
& rLocalName
, const Reference
<XAttributeList
>& xAttrList
)
347 const SvXMLTokenMap
& rTokenMap
= mrScImport
.GetTableRowCellElemTokenMap();
348 sal_uInt16 nToken
= rTokenMap
.Get(nPrefix
, rLocalName
);
349 if (nToken
== XML_TOK_TABLE_ROW_CELL_P
)
350 return new ScXMLExternalRefCellTextContext(mrScImport
, nPrefix
, rLocalName
, xAttrList
, maCellString
);
352 return new SvXMLImportContext(GetImport(), nPrefix
, rLocalName
);
355 void ScXMLExternalRefCellContext::EndElement()
357 if (maCellString
.getLength())
360 for (sal_Int32 i
= 0; i
< mnRepeatCount
; ++i
, ++mrExternalRefInfo
.mnCol
)
365 ScExternalRefCache::TokenRef aToken
;
367 aToken
.reset(new formula::FormulaDoubleToken(mfCellValue
));
369 aToken
.reset(new formula::FormulaStringToken(maCellString
));
371 sal_uInt32 nNumFmt
= mnNumberFormat
>= 0 ? static_cast<sal_uInt32
>(mnNumberFormat
) : 0;
372 mrExternalRefInfo
.mpCacheTable
->setCell(
373 static_cast<SCCOL
>(mrExternalRefInfo
.mnCol
),
374 static_cast<SCROW
>(mrExternalRefInfo
.mnRow
),
379 // ============================================================================
381 ScXMLExternalRefCellTextContext::ScXMLExternalRefCellTextContext(
382 ScXMLImport
& rImport
, USHORT nPrefix
, const OUString
& rLName
,
383 const Reference
<XAttributeList
>& /*xAttrList*/, OUString
& rCellString
) :
384 SvXMLImportContext( rImport
, nPrefix
, rLName
),
386 mrCellString(rCellString
)
390 ScXMLExternalRefCellTextContext::~ScXMLExternalRefCellTextContext()
394 SvXMLImportContext
* ScXMLExternalRefCellTextContext::CreateChildContext(
395 USHORT nPrefix
, const OUString
& rLocalName
, const Reference
<XAttributeList
>& /*xAttrList*/ )
397 return new SvXMLImportContext(GetImport(), nPrefix
, rLocalName
);
400 void ScXMLExternalRefCellTextContext::Characters(const OUString
& rChar
)
402 mrCellString
= rChar
;
405 void ScXMLExternalRefCellTextContext::EndElement()