Version 6.4.0.3, tag libreoffice-6.4.0.3
[LibreOffice.git] / sc / source / filter / xml / xmlbodyi.cxx
blob2b9a5886a049a862095d747bbaa3719969d9f58c
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 <document.hxx>
21 #include <docuno.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>
53 #include <memory>
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 ),
61 sPassword(),
62 meHash1(PASSHASH_SHA1),
63 meHash2(PASSHASH_UNSPECIFIED),
64 bProtected(false),
65 bHadCalculationSettings(false),
66 pChangeTrackingImportHelper(nullptr)
68 ScDocument* pDoc = GetScImport().GetDocument();
69 if (pDoc)
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);
77 if (!nLen)
78 eGrammar = formula::FormulaGrammar::GRAM_PODF;
79 else
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 );
85 if (fVer < 1.2)
86 eGrammar = formula::FormulaGrammar::GRAM_PODF;
88 pDoc->SetStorageGrammar( eGrammar);
91 if ( !rAttrList.is() )
92 return;
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 );
136 switch( nElement )
138 case XML_ELEMENT( TABLE, XML_TRACKED_CHANGES ):
139 pChangeTrackingImportHelper = GetScImport().GetChangeTrackingImportHelper();
140 if (pChangeTrackingImportHelper)
141 pContext = new ScXMLTrackedChangesContext( GetScImport(), pAttribList, pChangeTrackingImportHelper);
142 break;
143 case XML_ELEMENT( TABLE, XML_CALCULATION_SETTINGS ):
144 pContext = new ScXMLCalculationSettingsContext( GetScImport(), pAttribList );
145 bHadCalculationSettings = true;
146 break;
147 case XML_ELEMENT( TABLE, XML_CONTENT_VALIDATIONS ):
148 pContext = new ScXMLContentValidationsContext( GetScImport() );
149 break;
150 case XML_ELEMENT( TABLE, XML_LABEL_RANGES ):
151 pContext = new ScXMLLabelRangesContext( GetScImport() );
152 break;
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() );
159 else
161 pContext = new ScXMLTableContext( GetScImport(), pAttribList );
163 break;
164 case XML_ELEMENT( TABLE, XML_NAMED_EXPRESSIONS ):
165 pContext = new ScXMLNamedExpressionsContext (
166 GetScImport(),
167 new ScXMLNamedExpressionsContext::GlobalInserter(GetScImport()) );
168 break;
169 case XML_ELEMENT( TABLE, XML_DATABASE_RANGES ):
170 pContext = new ScXMLDatabaseRangesContext ( GetScImport() );
171 break;
172 case XML_ELEMENT( CALC_EXT, XML_DATA_MAPPINGS ):
173 pContext = new ScXMLMappingsContext(GetScImport());
174 break;
175 case XML_ELEMENT( TABLE, XML_DATABASE_RANGE ):
176 pContext = new ScXMLDatabaseRangeContext ( GetScImport(),
177 pAttribList );
178 break;
179 case XML_ELEMENT( TABLE, XML_DATA_PILOT_TABLES ):
180 pContext = new ScXMLDataPilotTablesContext ( GetScImport() );
181 break;
182 case XML_ELEMENT( TABLE, XML_CONSOLIDATION ):
183 pContext = new ScXMLConsolidationContext ( GetScImport(), pAttribList );
184 break;
185 case XML_ELEMENT( TABLE, XML_DDE_LINKS ):
186 pContext = new ScXMLDDELinksContext ( GetScImport() );
187 break;
188 case XML_ELEMENT( CALC_EXT, XML_DATA_STREAM_SOURCE ):
189 pContext = new ScXMLDataStreamContext(GetScImport(), pAttribList);
190 break;
193 if( !pContext )
194 pContext = new SvXMLImportContext( GetImport() );
196 return pContext;
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 );
208 // otherwise ignore
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 );
221 if ( pSheetData )
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())
244 if (pDetOpArray)
246 pDetOpArray->Sort();
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
258 if (bProtected)
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: */