LibreOffice Module sc (master) 1
xmlbodyi.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20#include <document.hxx>
21#include <docuno.hxx>
22#include <sheetdata.hxx>
23
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"
38#include "XMLEmptyContext.hxx"
40#include <scerrors.hxx>
41#include <tabprotection.hxx>
42#include "datastreamimport.hxx"
43#include <sax/fastattribs.hxx>
44
45#include <xmloff/xmltoken.hxx>
47
48#include <comphelper/base64.hxx>
50#include <sal/types.h>
51#include <sal/log.hxx>
52#include <rtl/math.hxx>
53
54#include <memory>
55
56using namespace com::sun::star;
57using namespace xmloff::token;
58
61 ScXMLImportContext( rImport ),
62 meHash1(PASSHASH_SHA1),
63 meHash2(PASSHASH_UNSPECIFIED),
64 bProtected(false),
65 bHadCalculationSettings(false),
66 pChangeTrackingImportHelper(nullptr)
67{
69 if (pDoc)
70 {
71 // ODF 1.1 and earlier => GRAM_PODF; ODF 1.2 and later => GRAM_ODFF;
72 // no version => earlier than 1.2 => GRAM_PODF.
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)
79 else
80 {
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)
87 }
88 pDoc->SetStorageGrammar( eGrammar);
89 }
90
91 if ( !rAttrList.is() )
92 return;
93
94 for (auto &it : *rAttrList)
95 {
96 sal_Int32 nToken = it.getToken();
98 {
99 const sal_Int32 nLocalToken = nToken & TOKEN_MASK;
100 if( nLocalToken == XML_STRUCTURE_PROTECTED )
102 else if ( nLocalToken == XML_PROTECTION_KEY )
103 sPassword = it.toString();
104 else if ( nLocalToken == XML_PROTECTION_KEY_DIGEST_ALGORITHM )
106 else if ( nLocalToken == XML_PROTECTION_KEY_DIGEST_ALGORITHM_2 )
108 }
110 {
112 }
113 }
114}
115
117{
118}
119
120uno::Reference< xml::sax::XFastContextHandler > SAL_CALL
122 const uno::Reference< xml::sax::XFastAttributeList > & xAttrList )
123{
124 ScSheetSaveData* pSheetData = comphelper::getFromUnoTunnel<ScModelObj>(GetScImport().GetModel())->GetSheetSaveData();
125 if ( pSheetData && pSheetData->HasStartPos() )
126 {
127 // stream part to copy ends before the next child element
128 sal_Int32 nEndOffset = GetScImport().GetByteOffset();
129 pSheetData->EndStreamPos( nEndOffset );
130 }
131
132 SvXMLImportContext *pContext = nullptr;
135
136 switch( nElement )
137 {
142 break;
144 pContext = new ScXMLCalculationSettingsContext( GetScImport(), pAttribList );
146 break;
149 break;
151 pContext = new ScXMLLabelRangesContext( GetScImport() );
152 break;
153 case XML_ELEMENT( TABLE, XML_TABLE ):
154 if (GetScImport().GetTables().GetCurrentSheet() >= MAXTAB)
155 {
157 pContext = new ScXMLEmptyContext(GetScImport() );
158 }
159 else
160 {
161 pContext = new ScXMLTableContext( GetScImport(), pAttribList );
162 }
163 break;
165 pContext = new ScXMLNamedExpressionsContext (
166 GetScImport(),
167 std::make_shared<ScXMLNamedExpressionsContext::GlobalInserter>(GetScImport()) );
168 break;
170 pContext = new ScXMLDatabaseRangesContext ( GetScImport() );
171 break;
172 case XML_ELEMENT( CALC_EXT, XML_DATA_MAPPINGS ):
173 pContext = new ScXMLMappingsContext(GetScImport());
174 break;
176 pContext = new ScXMLDatabaseRangeContext ( GetScImport(),
177 pAttribList );
178 break;
180 pContext = new ScXMLDataPilotTablesContext ( GetScImport() );
181 break;
183 pContext = new ScXMLConsolidationContext ( GetScImport(), pAttribList );
184 break;
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;
191 }
192
193 return pContext;
194}
195
196void SAL_CALL ScXMLBodyContext::characters(const OUString &)
197{
198 ScSheetSaveData* pSheetData = comphelper::getFromUnoTunnel<ScModelObj>(GetScImport().GetModel())->GetSheetSaveData();
199 if ( pSheetData && pSheetData->HasStartPos() )
200 {
201 // stream part to copy ends before any content (whitespace) within the spreadsheet element
202 sal_Int32 nEndOffset = GetScImport().GetByteOffset();
203 pSheetData->EndStreamPos( nEndOffset );
204 }
205 // otherwise ignore
206}
207
208void SAL_CALL ScXMLBodyContext::endFastElement(sal_Int32 nElement)
209{
210 ScSheetSaveData* pSheetData = comphelper::getFromUnoTunnel<ScModelObj>(GetScImport().GetModel())->GetSheetSaveData();
211 if ( pSheetData && pSheetData->HasStartPos() )
212 {
213 // stream part to copy ends before the closing tag of spreadsheet element
214 sal_Int32 nEndOffset = GetScImport().GetByteOffset();
215 pSheetData->EndStreamPos( nEndOffset );
216 }
217
218 if ( pSheetData )
219 {
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 );
224 }
225
227 {
228 // #111055#; set calculation settings defaults if there is no calculation settings element
230 pContext->endFastElement( nElement );
231 }
232
234
237 ScMyImpDetectiveOp aDetOp;
238
239 if (!(pDoc && GetScImport().GetModel().is()))
240 return;
241
242 if (pDetOpArray)
243 {
244 pDetOpArray->Sort();
245 while( pDetOpArray->GetFirstOp( aDetOp ) )
246 {
247 ScDetOpData aOpData( aDetOp.aPosition, aDetOp.eOpType );
248 pDoc->AddDetectiveOperation( aOpData );
249 }
250 }
251
254
255 // #i37959# handle document protection after the sheet settings
256 if (!bProtected)
257 return;
258
259 ScDocProtection aProtection;
260 aProtection.setProtected(true);
261
262 uno::Sequence<sal_Int8> aPass;
263 if (!sPassword.isEmpty())
264 {
266 aProtection.setPasswordHash(aPass, meHash1, meHash2);
267 }
268
269 pDoc->SetDocProtection(&aProtection);
270}
271
272/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
const SCTAB MAXTAB
Definition: address.hxx:70
virtual void setPasswordHash(const css::uno::Sequence< sal_Int8 > &aPassword, ScPasswordHash eHash, ScPasswordHash eHash2=PASSHASH_UNSPECIFIED) override
virtual void setProtected(bool bProtected) override
void AddDetectiveOperation(const ScDetOpData &rData)
Definition: documen4.cxx:892
void SetStorageGrammar(formula::FormulaGrammar::Grammar eGrammar)
Should only be GRAM_PODF or GRAM_ODFF.
Definition: documen2.cxx:251
SC_DLLPUBLIC void SetDocProtection(const ScDocProtection *pProtect)
Definition: documen3.cxx:1886
bool GetFirstOp(ScMyImpDetectiveOp &rDetOp)
bool HasStartPos() const
Definition: sheetdata.hxx:152
void StoreLoadedNamespaces(const SvXMLNamespaceMap &rNamespaces)
Definition: sheetdata.cxx:184
void EndStreamPos(sal_Int32 nEndOffset)
Definition: sheetdata.cxx:124
virtual ~ScXMLBodyContext() override
Definition: xmlbodyi.cxx:116
ScPasswordHash meHash1
Definition: xmlbodyi.hxx:33
ScXMLChangeTrackingImportHelper * pChangeTrackingImportHelper
Definition: xmlbodyi.hxx:38
ScPasswordHash meHash2
Definition: xmlbodyi.hxx:34
virtual void SAL_CALL endFastElement(sal_Int32 nElement) override
Definition: xmlbodyi.cxx:208
virtual void SAL_CALL characters(const OUString &aChars) override
Definition: xmlbodyi.cxx:196
bool bHadCalculationSettings
Definition: xmlbodyi.hxx:36
virtual css::uno::Reference< css::xml::sax::XFastContextHandler > SAL_CALL createFastChildContext(sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList > &xAttrList) override
Definition: xmlbodyi.cxx:121
OUString sPassword
Definition: xmlbodyi.hxx:32
ScXMLBodyContext(ScXMLImport &rImport, const rtl::Reference< sax_fastparser::FastAttributeList > &rAttrList)
Definition: xmlbodyi.cxx:59
This class exists only to provide GetScImport() to its derived classes.
ScXMLImport & GetScImport()
Use this class to manage solar mutex locking instead of calling LockSolarMutex() and UnlockSolarMutex...
Definition: xmlimprt.hxx:289
ScMyImpDetectiveOpArray * GetDetectiveOpArray()
Definition: xmlimprt.cxx:1674
ScXMLChangeTrackingImportHelper * GetChangeTrackingImportHelper()
Definition: xmlimprt.cxx:589
ScDocument * GetDocument()
Definition: xmlimprt.hxx:205
void SetRangeOverflowType(ErrCode nType)
Definition: xmlimprt.cxx:1560
sal_Int32 GetByteOffset() const
Definition: xmlimprt.cxx:1550
SvXMLImport & GetImport()
static void decode(css::uno::Sequence< sal_Int8 > &aPass, std::u16string_view sBuffer)
#define SAL_INFO(area, stream)
ScPasswordHash getHashTypeFromURI(std::u16string_view rURI)
TABLE
FastAttributeList & castToFastAttributeList(const css::uno::Reference< css::xml::sax::XFastAttributeList > &xAttrList)
XML_CONSOLIDATION
XML_DATA_STREAM_SOURCE
XML_TABLE
XML_TRACKED_CHANGES
XML_CONTENT_VALIDATIONS
XML_NAMED_EXPRESSIONS
XML_TRUE
XML_STRUCTURE_PROTECTED
XML_DATABASE_RANGE
XML_DDE_LINKS
XML_PROTECTION_KEY_DIGEST_ALGORITHM
XML_PROTECTION_KEY_DIGEST_ALGORITHM_2
XML_DATA_PILOT_TABLES
XML_PROTECTION_KEY
XML_CALCULATION_SETTINGS
XML_DATA_MAPPINGS
XML_LABEL_RANGES
XML_DATABASE_RANGES
bool IsXMLToken(std::u16string_view rString, enum XMLTokenEnum eToken)
DefTokenId nToken
Definition: qproform.cxx:397
#define SCWARN_IMPORT_SHEET_OVERFLOW
Definition: scerrors.hxx:64
@ PASSHASH_UNSPECIFIED
@ PASSHASH_SHA1
#define XML_ELEMENT(prefix, name)
constexpr bool IsTokenInNamespace(sal_Int32 nToken, sal_uInt16 nNamespacePrefix)
constexpr sal_Int32 TOKEN_MASK
constexpr sal_uInt16 XML_NAMESPACE_TABLE