1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 <document.hxx>
22 #include <sheetdata.hxx>
24 #include "xmlbodyi.hxx"
25 #include "xmltabi.hxx"
26 #include "xmlnexpi.hxx"
27 #include "xmldrani.hxx"
28 #include "xmlimprt.hxx"
29 #include "xmldpimp.hxx"
30 #include "xmlcvali.hxx"
31 #include "xmllabri.hxx"
32 #include "xmlmappingi.hxx"
33 #include "XMLConsolidationContext.hxx"
34 #include "XMLDDELinksContext.hxx"
35 #include "XMLCalculationSettingsContext.hxx"
36 #include "XMLTrackedChangesContext.hxx"
37 #include "XMLChangeTrackingImportHelper.hxx"
38 #include "XMLEmptyContext.hxx"
39 #include "XMLDetectiveContext.hxx"
40 #include <scerrors.hxx>
41 #include <tabprotection.hxx>
42 #include "datastreamimport.hxx"
43 #include <sax/fastattribs.hxx>
45 #include <xmloff/xmltoken.hxx>
46 #include <xmloff/xmlnmspe.hxx>
48 #include <comphelper/base64.hxx>
49 #include <comphelper/servicehelper.hxx>
50 #include <sal/types.h>
51 #include <sal/log.hxx>
55 using namespace com::sun::star
;
56 using namespace xmloff::token
;
58 ScXMLBodyContext::ScXMLBodyContext( ScXMLImport
& rImport
,
59 const rtl::Reference
<sax_fastparser::FastAttributeList
>& rAttrList
) :
60 ScXMLImportContext( rImport
),
62 meHash1(PASSHASH_SHA1
),
63 meHash2(PASSHASH_UNSPECIFIED
),
65 bHadCalculationSettings(false),
66 pChangeTrackingImportHelper(nullptr)
68 ScDocument
* pDoc
= GetScImport().GetDocument();
71 // ODF 1.1 and earlier => GRAM_PODF; ODF 1.2 and later => GRAM_ODFF;
72 // no version => earlier than 1.2 => GRAM_PODF.
73 formula::FormulaGrammar::Grammar eGrammar
= formula::FormulaGrammar::GRAM_ODFF
;
74 const OUString
& aVer( rImport
.GetODFVersion());
75 sal_Int32 nLen
= aVer
.getLength();
76 SAL_INFO("sc.filter", "ScXMLBodyContext ODFVersion: nLen: " << nLen
<< " str : " << aVer
);
78 eGrammar
= formula::FormulaGrammar::GRAM_PODF
;
81 // In case there was a micro version, e.g. "1.2.3", this would
82 // still yield major.minor, but pParsedEnd (5th parameter, not
83 // passed here) would point before string end upon return.
84 double fVer
= ::rtl::math::stringToDouble( aVer
, '.', 0 );
86 eGrammar
= formula::FormulaGrammar::GRAM_PODF
;
88 pDoc
->SetStorageGrammar( eGrammar
);
91 if ( !rAttrList
.is() )
94 for (auto &it
: *rAttrList
)
96 sal_Int32 nToken
= it
.getToken();
97 if( NAMESPACE_TOKEN( XML_NAMESPACE_TABLE
) == ( nToken
& NMSP_MASK
) )
99 const sal_Int32 nLocalToken
= nToken
& TOKEN_MASK
;
100 if( nLocalToken
== XML_STRUCTURE_PROTECTED
)
101 bProtected
= IsXMLToken( it
, XML_TRUE
);
102 else if ( nLocalToken
== XML_PROTECTION_KEY
)
103 sPassword
= it
.toString();
104 else if ( nLocalToken
== XML_PROTECTION_KEY_DIGEST_ALGORITHM
)
105 meHash1
= ScPassHashHelper::getHashTypeFromURI( it
.toString() );
106 else if ( nLocalToken
== XML_PROTECTION_KEY_DIGEST_ALGORITHM_2
)
107 meHash2
= ScPassHashHelper::getHashTypeFromURI( it
.toString() );
109 else if ( nToken
== XML_ELEMENT( LO_EXT
, XML_PROTECTION_KEY_DIGEST_ALGORITHM_2
) )
111 meHash2
= ScPassHashHelper::getHashTypeFromURI( it
.toString() );
116 ScXMLBodyContext::~ScXMLBodyContext()
120 uno::Reference
< xml::sax::XFastContextHandler
> SAL_CALL
121 ScXMLBodyContext::createFastChildContext( sal_Int32 nElement
,
122 const uno::Reference
< xml::sax::XFastAttributeList
> & xAttrList
)
124 ScSheetSaveData
* pSheetData
= comphelper::getUnoTunnelImplementation
<ScModelObj
>(GetScImport().GetModel())->GetSheetSaveData();
125 if ( pSheetData
&& pSheetData
->HasStartPos() )
127 // stream part to copy ends before the next child element
128 sal_Int32 nEndOffset
= GetScImport().GetByteOffset();
129 pSheetData
->EndStreamPos( nEndOffset
);
132 SvXMLImportContext
*pContext
= nullptr;
133 sax_fastparser::FastAttributeList
*pAttribList
=
134 sax_fastparser::FastAttributeList::castToFastAttributeList( xAttrList
);
138 case XML_ELEMENT( TABLE
, XML_TRACKED_CHANGES
):
139 pChangeTrackingImportHelper
= GetScImport().GetChangeTrackingImportHelper();
140 if (pChangeTrackingImportHelper
)
141 pContext
= new ScXMLTrackedChangesContext( GetScImport(), pAttribList
, pChangeTrackingImportHelper
);
143 case XML_ELEMENT( TABLE
, XML_CALCULATION_SETTINGS
):
144 pContext
= new ScXMLCalculationSettingsContext( GetScImport(), pAttribList
);
145 bHadCalculationSettings
= true;
147 case XML_ELEMENT( TABLE
, XML_CONTENT_VALIDATIONS
):
148 pContext
= new ScXMLContentValidationsContext( GetScImport() );
150 case XML_ELEMENT( TABLE
, XML_LABEL_RANGES
):
151 pContext
= new ScXMLLabelRangesContext( GetScImport() );
153 case XML_ELEMENT( TABLE
, XML_TABLE
):
154 if (GetScImport().GetTables().GetCurrentSheet() >= MAXTAB
)
156 GetScImport().SetRangeOverflowType(SCWARN_IMPORT_SHEET_OVERFLOW
);
157 pContext
= new ScXMLEmptyContext(GetScImport() );
161 pContext
= new ScXMLTableContext( GetScImport(), pAttribList
);
164 case XML_ELEMENT( TABLE
, XML_NAMED_EXPRESSIONS
):
165 pContext
= new ScXMLNamedExpressionsContext (
167 new ScXMLNamedExpressionsContext::GlobalInserter(GetScImport()) );
169 case XML_ELEMENT( TABLE
, XML_DATABASE_RANGES
):
170 pContext
= new ScXMLDatabaseRangesContext ( GetScImport() );
172 case XML_ELEMENT( CALC_EXT
, XML_DATA_MAPPINGS
):
173 pContext
= new ScXMLMappingsContext(GetScImport());
175 case XML_ELEMENT( TABLE
, XML_DATABASE_RANGE
):
176 pContext
= new ScXMLDatabaseRangeContext ( GetScImport(),
179 case XML_ELEMENT( TABLE
, XML_DATA_PILOT_TABLES
):
180 pContext
= new ScXMLDataPilotTablesContext ( GetScImport() );
182 case XML_ELEMENT( TABLE
, XML_CONSOLIDATION
):
183 pContext
= new ScXMLConsolidationContext ( GetScImport(), pAttribList
);
185 case XML_ELEMENT( TABLE
, XML_DDE_LINKS
):
186 pContext
= new ScXMLDDELinksContext ( GetScImport() );
188 case XML_ELEMENT( CALC_EXT
, XML_DATA_STREAM_SOURCE
):
189 pContext
= new ScXMLDataStreamContext(GetScImport(), pAttribList
);
194 pContext
= new SvXMLImportContext( GetImport() );
199 void SAL_CALL
ScXMLBodyContext::characters(const OUString
&)
201 ScSheetSaveData
* pSheetData
= comphelper::getUnoTunnelImplementation
<ScModelObj
>(GetScImport().GetModel())->GetSheetSaveData();
202 if ( pSheetData
&& pSheetData
->HasStartPos() )
204 // stream part to copy ends before any content (whitespace) within the spreadsheet element
205 sal_Int32 nEndOffset
= GetScImport().GetByteOffset();
206 pSheetData
->EndStreamPos( nEndOffset
);
211 void SAL_CALL
ScXMLBodyContext::endFastElement(sal_Int32 nElement
)
213 ScSheetSaveData
* pSheetData
= comphelper::getUnoTunnelImplementation
<ScModelObj
>(GetScImport().GetModel())->GetSheetSaveData();
214 if ( pSheetData
&& pSheetData
->HasStartPos() )
216 // stream part to copy ends before the closing tag of spreadsheet element
217 sal_Int32 nEndOffset
= GetScImport().GetByteOffset();
218 pSheetData
->EndStreamPos( nEndOffset
);
223 // store the loaded namespaces (for the office:spreadsheet element),
224 // so the prefixes in copied stream fragments remain valid
225 const SvXMLNamespaceMap
& rNamespaces
= GetImport().GetNamespaceMap();
226 pSheetData
->StoreLoadedNamespaces( rNamespaces
);
229 if (!bHadCalculationSettings
)
231 // #111055#; set calculation settings defaults if there is no calculation settings element
232 rtl::Reference
<ScXMLCalculationSettingsContext
> pContext( new ScXMLCalculationSettingsContext(GetScImport(), nullptr) );
233 pContext
->endFastElement( nElement
);
236 ScXMLImport::MutexGuard
aGuard(GetScImport());
238 ScMyImpDetectiveOpArray
* pDetOpArray
= GetScImport().GetDetectiveOpArray();
239 ScDocument
* pDoc
= GetScImport().GetDocument();
240 ScMyImpDetectiveOp aDetOp
;
242 if (pDoc
&& GetScImport().GetModel().is())
247 while( pDetOpArray
->GetFirstOp( aDetOp
) )
249 ScDetOpData
aOpData( aDetOp
.aPosition
, aDetOp
.eOpType
);
250 pDoc
->AddDetectiveOperation( aOpData
);
254 if (pChangeTrackingImportHelper
)
255 pChangeTrackingImportHelper
->CreateChangeTrack(GetScImport().GetDocument());
257 // #i37959# handle document protection after the sheet settings
260 std::unique_ptr
<ScDocProtection
> pProtection(new ScDocProtection
);
261 pProtection
->setProtected(true);
263 uno::Sequence
<sal_Int8
> aPass
;
264 if (!sPassword
.isEmpty())
266 ::comphelper::Base64::decode(aPass
, sPassword
);
267 pProtection
->setPasswordHash(aPass
, meHash1
, meHash2
);
270 pDoc
->SetDocProtection(pProtection
.get());
275 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */