tdf#130857 qt weld: Implement QtInstanceWidget::strip_mnemonic
[LibreOffice.git] / sc / source / filter / xml / xmlbodyi.cxx
blob2eec7feaab0fd5288802aa13151f7cbe478c2354
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/xmlnamespace.hxx>
48 #include <comphelper/base64.hxx>
49 #include <comphelper/servicehelper.hxx>
50 #include <sal/types.h>
51 #include <sal/log.hxx>
52 #include <rtl/math.hxx>
54 #include <memory>
56 using namespace com::sun::star;
57 using namespace xmloff::token;
59 ScXMLBodyContext::ScXMLBodyContext( ScXMLImport& rImport,
60 const rtl::Reference<sax_fastparser::FastAttributeList>& rAttrList ) :
61 ScXMLImportContext( rImport ),
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( IsTokenInNamespace(nToken, XML_NAMESPACE_TABLE) )
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 = GetScImport().GetScModel()->GetSheetSaveData();
125 if ( pSheetData && pSheetData->HasStartPos() )
127 // stream part to copy ends before the next child element
128 sal_Int64 nEndOffset = GetScImport().GetByteOffset();
129 pSheetData->EndStreamPos( nEndOffset );
132 SvXMLImportContext *pContext = nullptr;
133 sax_fastparser::FastAttributeList *pAttribList =
134 &sax_fastparser::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 std::make_shared<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 return pContext;
196 void SAL_CALL ScXMLBodyContext::characters(const OUString &)
198 ScSheetSaveData* pSheetData = GetScImport().GetScModel()->GetSheetSaveData();
199 if ( pSheetData && pSheetData->HasStartPos() )
201 // stream part to copy ends before any content (whitespace) within the spreadsheet element
202 sal_Int64 nEndOffset = GetScImport().GetByteOffset();
203 pSheetData->EndStreamPos( nEndOffset );
205 // otherwise ignore
208 void SAL_CALL ScXMLBodyContext::endFastElement(sal_Int32 nElement)
210 ScSheetSaveData* pSheetData = GetScImport().GetScModel()->GetSheetSaveData();
211 if ( pSheetData && pSheetData->HasStartPos() )
213 // stream part to copy ends before the closing tag of spreadsheet element
214 sal_Int64 nEndOffset = GetScImport().GetByteOffset();
215 pSheetData->EndStreamPos( nEndOffset );
218 if ( pSheetData )
220 // store the loaded namespaces (for the office:spreadsheet element),
221 // so the prefixes in copied stream fragments remain valid
222 const SvXMLNamespaceMap& rNamespaces = GetImport().GetNamespaceMap();
223 pSheetData->StoreLoadedNamespaces( rNamespaces );
226 if (!bHadCalculationSettings)
228 // #111055#; set calculation settings defaults if there is no calculation settings element
229 rtl::Reference<ScXMLCalculationSettingsContext> pContext( new ScXMLCalculationSettingsContext(GetScImport(), nullptr) );
230 pContext->endFastElement( nElement );
233 ScXMLImport::MutexGuard aGuard(GetScImport());
235 ScMyImpDetectiveOpArray* pDetOpArray = GetScImport().GetDetectiveOpArray();
236 ScDocument* pDoc = GetScImport().GetDocument();
237 ScMyImpDetectiveOp aDetOp;
239 if (!(pDoc && GetScImport().GetModel().is()))
240 return;
242 if (pDetOpArray)
244 pDetOpArray->Sort();
245 while( pDetOpArray->GetFirstOp( aDetOp ) )
247 ScDetOpData aOpData( aDetOp.aPosition, aDetOp.eOpType );
248 pDoc->AddDetectiveOperation( aOpData );
252 if (pChangeTrackingImportHelper)
253 pChangeTrackingImportHelper->CreateChangeTrack(pDoc);
255 // #i37959# handle document protection after the sheet settings
256 if (!bProtected)
257 return;
259 ScDocProtection aProtection;
260 aProtection.setProtected(true);
262 uno::Sequence<sal_Int8> aPass;
263 if (!sPassword.isEmpty())
265 ::comphelper::Base64::decode(aPass, sPassword);
266 aProtection.setPasswordHash(aPass, meHash1, meHash2);
269 pDoc->SetDocProtection(&aProtection);
272 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */