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"
34 #include "XMLDDELinksContext.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>
44 
45 #include <xmloff/xmltoken.hxx>
46 #include <xmloff/xmlnmspe.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 
56 using namespace com::sun::star;
57 using namespace xmloff::token;
58 
61  ScXMLImportContext( rImport ),
62  sPassword(),
63  meHash1(PASSHASH_SHA1),
64  meHash2(PASSHASH_UNSPECIFIED),
65  bProtected(false),
66  bHadCalculationSettings(false),
67  pChangeTrackingImportHelper(nullptr)
68 {
70  if (pDoc)
71  {
72  // ODF 1.1 and earlier => GRAM_PODF; ODF 1.2 and later => GRAM_ODFF;
73  // no version => earlier than 1.2 => GRAM_PODF.
75  const OUString& aVer( rImport.GetODFVersion());
76  sal_Int32 nLen = aVer.getLength();
77  SAL_INFO("sc.filter", "ScXMLBodyContext ODFVersion: nLen: " << nLen << " str : " << aVer);
78  if (!nLen)
80  else
81  {
82  // In case there was a micro version, e.g. "1.2.3", this would
83  // still yield major.minor, but pParsedEnd (5th parameter, not
84  // passed here) would point before string end upon return.
85  double fVer = ::rtl::math::stringToDouble( aVer, '.', 0 );
86  if (fVer < 1.2)
88  }
89  pDoc->SetStorageGrammar( eGrammar);
90  }
91 
92  if ( !rAttrList.is() )
93  return;
94 
95  for (auto &it : *rAttrList)
96  {
97  sal_Int32 nToken = it.getToken();
99  {
100  const sal_Int32 nLocalToken = nToken & TOKEN_MASK;
101  if( nLocalToken == XML_STRUCTURE_PROTECTED )
102  bProtected = IsXMLToken( it, XML_TRUE );
103  else if ( nLocalToken == XML_PROTECTION_KEY )
104  sPassword = it.toString();
105  else if ( nLocalToken == XML_PROTECTION_KEY_DIGEST_ALGORITHM )
107  else if ( nLocalToken == XML_PROTECTION_KEY_DIGEST_ALGORITHM_2 )
109  }
110  else if ( nToken == XML_ELEMENT( LO_EXT, XML_PROTECTION_KEY_DIGEST_ALGORITHM_2 ) )
111  {
113  }
114  }
115 }
116 
118 {
119 }
120 
121 uno::Reference< xml::sax::XFastContextHandler > SAL_CALL
123  const uno::Reference< xml::sax::XFastAttributeList > & xAttrList )
124 {
125  ScSheetSaveData* pSheetData = comphelper::getUnoTunnelImplementation<ScModelObj>(GetScImport().GetModel())->GetSheetSaveData();
126  if ( pSheetData && pSheetData->HasStartPos() )
127  {
128  // stream part to copy ends before the next child element
129  sal_Int32 nEndOffset = GetScImport().GetByteOffset();
130  pSheetData->EndStreamPos( nEndOffset );
131  }
132 
133  SvXMLImportContext *pContext = nullptr;
134  sax_fastparser::FastAttributeList *pAttribList =
136 
137  switch( nElement )
138  {
142  pContext = new ScXMLTrackedChangesContext( GetScImport(), pAttribList, pChangeTrackingImportHelper);
143  break;
145  pContext = new ScXMLCalculationSettingsContext( GetScImport(), pAttribList );
147  break;
149  pContext = new ScXMLContentValidationsContext( GetScImport() );
150  break;
152  pContext = new ScXMLLabelRangesContext( GetScImport() );
153  break;
154  case XML_ELEMENT( TABLE, XML_TABLE ):
155  if (GetScImport().GetTables().GetCurrentSheet() >= MAXTAB)
156  {
158  pContext = new ScXMLEmptyContext(GetScImport() );
159  }
160  else
161  {
162  pContext = new ScXMLTableContext( GetScImport(), pAttribList );
163  }
164  break;
166  pContext = new ScXMLNamedExpressionsContext (
167  GetScImport(),
168  std::make_shared<ScXMLNamedExpressionsContext::GlobalInserter>(GetScImport()) );
169  break;
171  pContext = new ScXMLDatabaseRangesContext ( GetScImport() );
172  break;
173  case XML_ELEMENT( CALC_EXT, XML_DATA_MAPPINGS ):
174  pContext = new ScXMLMappingsContext(GetScImport());
175  break;
177  pContext = new ScXMLDatabaseRangeContext ( GetScImport(),
178  pAttribList );
179  break;
181  pContext = new ScXMLDataPilotTablesContext ( GetScImport() );
182  break;
184  pContext = new ScXMLConsolidationContext ( GetScImport(), pAttribList );
185  break;
187  pContext = new ScXMLDDELinksContext ( GetScImport() );
188  break;
189  case XML_ELEMENT( CALC_EXT, XML_DATA_STREAM_SOURCE ):
190  pContext = new ScXMLDataStreamContext(GetScImport(), pAttribList);
191  break;
192  }
193 
194  return pContext;
195 }
196 
197 void SAL_CALL ScXMLBodyContext::characters(const OUString &)
198 {
199  ScSheetSaveData* pSheetData = comphelper::getUnoTunnelImplementation<ScModelObj>(GetScImport().GetModel())->GetSheetSaveData();
200  if ( pSheetData && pSheetData->HasStartPos() )
201  {
202  // stream part to copy ends before any content (whitespace) within the spreadsheet element
203  sal_Int32 nEndOffset = GetScImport().GetByteOffset();
204  pSheetData->EndStreamPos( nEndOffset );
205  }
206  // otherwise ignore
207 }
208 
209 void SAL_CALL ScXMLBodyContext::endFastElement(sal_Int32 nElement)
210 {
211  ScSheetSaveData* pSheetData = comphelper::getUnoTunnelImplementation<ScModelObj>(GetScImport().GetModel())->GetSheetSaveData();
212  if ( pSheetData && pSheetData->HasStartPos() )
213  {
214  // stream part to copy ends before the closing tag of spreadsheet element
215  sal_Int32 nEndOffset = GetScImport().GetByteOffset();
216  pSheetData->EndStreamPos( nEndOffset );
217  }
218 
219  if ( pSheetData )
220  {
221  // store the loaded namespaces (for the office:spreadsheet element),
222  // so the prefixes in copied stream fragments remain valid
223  const SvXMLNamespaceMap& rNamespaces = GetImport().GetNamespaceMap();
224  pSheetData->StoreLoadedNamespaces( rNamespaces );
225  }
226 
228  {
229  // #111055#; set calculation settings defaults if there is no calculation settings element
231  pContext->endFastElement( nElement );
232  }
233 
235 
237  ScDocument* pDoc = GetScImport().GetDocument();
238  ScMyImpDetectiveOp aDetOp;
239 
240  if (!(pDoc && GetScImport().GetModel().is()))
241  return;
242 
243  if (pDetOpArray)
244  {
245  pDetOpArray->Sort();
246  while( pDetOpArray->GetFirstOp( aDetOp ) )
247  {
248  ScDetOpData aOpData( aDetOp.aPosition, aDetOp.eOpType );
249  pDoc->AddDetectiveOperation( aOpData );
250  }
251  }
252 
255 
256  // #i37959# handle document protection after the sheet settings
257  if (!bProtected)
258  return;
259 
260  std::unique_ptr<ScDocProtection> pProtection(new ScDocProtection);
261  pProtection->setProtected(true);
262 
263  uno::Sequence<sal_Int8> aPass;
264  if (!sPassword.isEmpty())
265  {
267  pProtection->setPasswordHash(aPass, meHash1, meHash2);
268  }
269 
270  pDoc->SetDocProtection(pProtection.get());
271 }
272 
273 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
ScXMLChangeTrackingImportHelper * pChangeTrackingImportHelper
Definition: xmlbodyi.hxx:39
bool IsXMLToken(const OUString &rString, enum XMLTokenEnum eToken)
ScPasswordHash meHash2
Definition: xmlbodyi.hxx:35
virtual ~ScXMLBodyContext() override
Definition: xmlbodyi.cxx:117
bool bHadCalculationSettings
Definition: xmlbodyi.hxx:37
bool HasStartPos() const
Definition: sheetdata.hxx:152
ScDocument * GetDocument()
Definition: xmlimprt.hxx:296
ScXMLBodyContext(ScXMLImport &rImport, const rtl::Reference< sax_fastparser::FastAttributeList > &rAttrList)
Definition: xmlbodyi.cxx:59
SvXMLImport & GetImport()
bool GetFirstOp(ScMyImpDetectiveOp &rDetOp)
SvXMLNamespaceMap & GetNamespaceMap()
FastAttributeList & castToFastAttributeList(const css::uno::Reference< css::xml::sax::XFastAttributeList > &xAttrList)
XML_PROTECTION_KEY
XML_TABLE
XML_DATA_MAPPINGS
ScPasswordHash meHash1
Definition: xmlbodyi.hxx:34
ScPasswordHash getHashTypeFromURI(const OUString &rURI)
void AddDetectiveOperation(const ScDetOpData &rData)
Definition: documen4.cxx:890
XML_DATA_STREAM_SOURCE
This class exists only to provide GetScImport() to its derived classes.
void SetStorageGrammar(formula::FormulaGrammar::Grammar eGrammar)
Should only be GRAM_PODF or GRAM_ODFF.
Definition: documen2.cxx:235
XML_DATABASE_RANGES
static void decode(css::uno::Sequence< sal_Int8 > &aPass, const OUString &sBuffer)
XML_DATA_PILOT_TABLES
void EndStreamPos(sal_Int32 nEndOffset)
Definition: sheetdata.cxx:124
Use this class to manage solar mutex locking instead of calling LockSolarMutex() and UnlockSolarMutex...
Definition: xmlimprt.hxx:392
ScXMLImport & GetScImport()
const SCTAB MAXTAB
Definition: address.hxx:71
XML_PROTECTION_KEY_DIGEST_ALGORITHM_2
XML_TRACKED_CHANGES
TABLE
virtual void SAL_CALL characters(const OUString &aChars) override
Definition: xmlbodyi.cxx:197
OUString sPassword
Definition: xmlbodyi.hxx:33
DefTokenId nToken
Definition: qproform.cxx:400
XML_STRUCTURE_PROTECTED
constexpr bool IsTokenInNamespace(sal_Int32 nToken, sal_uInt16 nNamespacePrefix)
OUString const & GetODFVersion() const
#define SCWARN_IMPORT_SHEET_OVERFLOW
Definition: scerrors.hxx:65
constexpr sal_uInt16 XML_NAMESPACE_TABLE
#define SAL_INFO(area, stream)
ScXMLChangeTrackingImportHelper * GetChangeTrackingImportHelper()
Definition: xmlimprt.cxx:809
#define XML_ELEMENT(prefix, name)
const css::uno::Reference< css::frame::XModel > & GetModel() const
XML_NAMED_EXPRESSIONS
XML_DDE_LINKS
ScMyImpDetectiveOpArray * GetDetectiveOpArray()
Definition: xmlimprt.cxx:1854
XML_DATABASE_RANGE
XML_TRUE
void StoreLoadedNamespaces(const SvXMLNamespaceMap &rNamespaces)
Definition: sheetdata.cxx:184
XML_PROTECTION_KEY_DIGEST_ALGORITHM
SC_DLLPUBLIC void SetDocProtection(const ScDocProtection *pProtect)
Definition: documen3.cxx:1852
constexpr sal_Int32 TOKEN_MASK
XML_CALCULATION_SETTINGS
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:122
virtual void SAL_CALL endFastElement(sal_Int32 nElement) override
Definition: xmlbodyi.cxx:209
sal_Int32 GetByteOffset() const
Definition: xmlimprt.cxx:1730
XML_LABEL_RANGES
XML_CONSOLIDATION
XML_CONTENT_VALIDATIONS
void SetRangeOverflowType(ErrCode nType)
Definition: xmlimprt.cxx:1740