Line data Source code
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 <cstdio>
21 :
22 : #include "document.hxx"
23 : #include "docuno.hxx"
24 : #include "sheetdata.hxx"
25 :
26 : #include "xmlbodyi.hxx"
27 : #include "xmltabi.hxx"
28 : #include "xmlnexpi.hxx"
29 : #include "xmldrani.hxx"
30 : #include "xmlimprt.hxx"
31 : #include "xmldpimp.hxx"
32 : #include "xmlcvali.hxx"
33 : #include "xmlstyli.hxx"
34 : #include "xmllabri.hxx"
35 : #include "XMLConsolidationContext.hxx"
36 : #include "XMLDDELinksContext.hxx"
37 : #include "XMLCalculationSettingsContext.hxx"
38 : #include "XMLTrackedChangesContext.hxx"
39 : #include "XMLEmptyContext.hxx"
40 : #include "scerrors.hxx"
41 : #include "tabprotection.hxx"
42 : #include "datastreamimport.hxx"
43 :
44 : #include <xmloff/xmltkmap.hxx>
45 : #include <xmloff/xmltoken.hxx>
46 : #include <xmloff/xmlnmspe.hxx>
47 : #include <xmloff/nmspmap.hxx>
48 :
49 : #include <sax/tools/converter.hxx>
50 : #include <com/sun/star/sheet/XSpreadsheetDocument.hpp>
51 : #include <sal/types.h>
52 :
53 : #include <boost/scoped_ptr.hpp>
54 :
55 :
56 : using namespace com::sun::star;
57 : using namespace xmloff::token;
58 :
59 107 : ScXMLBodyContext::ScXMLBodyContext( ScXMLImport& rImport,
60 : sal_uInt16 nPrfx,
61 : const OUString& rLName,
62 : const uno::Reference<xml::sax::XAttributeList>& xAttrList ) :
63 : SvXMLImportContext( rImport, nPrfx, rLName ),
64 : sPassword(),
65 : meHash1(PASSHASH_SHA1),
66 : meHash2(PASSHASH_UNSPECIFIED),
67 : bProtected(false),
68 : bHadCalculationSettings(false),
69 107 : pChangeTrackingImportHelper(NULL)
70 : {
71 107 : ScDocument* pDoc = GetScImport().GetDocument();
72 107 : if (pDoc)
73 : {
74 : // ODF 1.1 and earlier => GRAM_PODF; ODF 1.2 and later => GRAM_ODFF;
75 : // no version => earlier than 1.2 => GRAM_PODF.
76 107 : formula::FormulaGrammar::Grammar eGrammar = formula::FormulaGrammar::GRAM_ODFF;
77 107 : OUString aVer( rImport.GetODFVersion());
78 107 : sal_Int32 nLen = aVer.getLength();
79 : #if OSL_DEBUG_LEVEL > 1
80 : fprintf( stderr, "\n ScXMLBodyContext ODFVersion: nLen: %d, str: %s\n",
81 : (int)nLen, OUStringToOString( aVer, RTL_TEXTENCODING_UTF8).getStr());
82 : #endif
83 107 : if (!nLen)
84 0 : eGrammar = formula::FormulaGrammar::GRAM_PODF;
85 : else
86 : {
87 : // In case there was a micro version, e.g. "1.2.3", this would
88 : // still yield major.minor, but pParsedEnd (5th parameter, not
89 : // passed here) would point before string end upon return.
90 107 : double fVer = ::rtl::math::stringToDouble( aVer, '.', 0, NULL, NULL);
91 107 : if (fVer < 1.2)
92 5 : eGrammar = formula::FormulaGrammar::GRAM_PODF;
93 : }
94 107 : pDoc->SetStorageGrammar( eGrammar);
95 : }
96 :
97 107 : sal_Int16 nAttrCount = xAttrList.is() ? xAttrList->getLength() : 0;
98 107 : for( sal_Int16 i=0; i < nAttrCount; ++i )
99 : {
100 0 : const OUString& sAttrName(xAttrList->getNameByIndex( i ));
101 0 : OUString aLocalName;
102 0 : sal_uInt16 nPrefix = GetScImport().GetNamespaceMap().GetKeyByAttrName(
103 0 : sAttrName, &aLocalName );
104 0 : const OUString& sValue(xAttrList->getValueByIndex( i ));
105 :
106 0 : if (nPrefix == XML_NAMESPACE_TABLE)
107 : {
108 0 : if (IsXMLToken(aLocalName, XML_STRUCTURE_PROTECTED))
109 0 : bProtected = IsXMLToken(sValue, XML_TRUE);
110 0 : else if (IsXMLToken(aLocalName, XML_PROTECTION_KEY))
111 0 : sPassword = sValue;
112 0 : else if (IsXMLToken(aLocalName, XML_PROTECTION_KEY_DIGEST_ALGORITHM))
113 0 : meHash1 = ScPassHashHelper::getHashTypeFromURI(sValue);
114 0 : else if (IsXMLToken(aLocalName, XML_PROTECTION_KEY_DIGEST_ALGORITHM_2))
115 0 : meHash2 = ScPassHashHelper::getHashTypeFromURI(sValue);
116 : }
117 0 : }
118 107 : }
119 :
120 214 : ScXMLBodyContext::~ScXMLBodyContext()
121 : {
122 214 : }
123 :
124 339 : SvXMLImportContext *ScXMLBodyContext::CreateChildContext( sal_uInt16 nPrefix,
125 : const OUString& rLocalName,
126 : const ::com::sun::star::uno::Reference<
127 : ::com::sun::star::xml::sax::XAttributeList>& xAttrList )
128 : {
129 339 : ScSheetSaveData* pSheetData = ScModelObj::getImplementation(GetScImport().GetModel())->GetSheetSaveData();
130 339 : if ( pSheetData && pSheetData->HasStartPos() )
131 : {
132 : // stream part to copy ends before the next child element
133 180 : sal_Int32 nEndOffset = GetScImport().GetByteOffset();
134 180 : pSheetData->EndStreamPos( nEndOffset );
135 : }
136 :
137 339 : SvXMLImportContext *pContext = 0;
138 :
139 339 : const SvXMLTokenMap& rTokenMap = GetScImport().GetBodyElemTokenMap();
140 339 : switch( rTokenMap.Get( nPrefix, rLocalName ) )
141 : {
142 : case XML_TOK_BODY_TRACKED_CHANGES :
143 0 : pChangeTrackingImportHelper = GetScImport().GetChangeTrackingImportHelper();
144 0 : if (pChangeTrackingImportHelper)
145 0 : pContext = new ScXMLTrackedChangesContext( GetScImport(), nPrefix, rLocalName, xAttrList, pChangeTrackingImportHelper);
146 0 : break;
147 : case XML_TOK_BODY_CALCULATION_SETTINGS :
148 17 : pContext = new ScXMLCalculationSettingsContext( GetScImport(), nPrefix, rLocalName, xAttrList );
149 17 : bHadCalculationSettings = true;
150 17 : break;
151 : case XML_TOK_BODY_CONTENT_VALIDATIONS :
152 1 : pContext = new ScXMLContentValidationsContext( GetScImport(), nPrefix, rLocalName, xAttrList );
153 1 : break;
154 : case XML_TOK_BODY_LABEL_RANGES:
155 0 : pContext = new ScXMLLabelRangesContext( GetScImport(), nPrefix, rLocalName, xAttrList );
156 0 : break;
157 : case XML_TOK_BODY_TABLE:
158 222 : if (GetScImport().GetTables().GetCurrentSheet() >= MAXTAB)
159 : {
160 0 : GetScImport().SetRangeOverflowType(SCWARN_IMPORT_SHEET_OVERFLOW);
161 0 : pContext = new ScXMLEmptyContext(GetScImport(), nPrefix, rLocalName);
162 : }
163 : else
164 : {
165 : pContext = new ScXMLTableContext( GetScImport(),nPrefix, rLocalName,
166 222 : xAttrList );
167 : }
168 222 : break;
169 : case XML_TOK_BODY_NAMED_EXPRESSIONS:
170 : pContext = new ScXMLNamedExpressionsContext (
171 : GetScImport(), nPrefix, rLocalName, xAttrList,
172 88 : new ScXMLNamedExpressionsContext::GlobalInserter(GetScImport()) );
173 88 : break;
174 : case XML_TOK_BODY_DATABASE_RANGES:
175 : pContext = new ScXMLDatabaseRangesContext ( GetScImport(), nPrefix, rLocalName,
176 6 : xAttrList );
177 6 : break;
178 : case XML_TOK_BODY_DATABASE_RANGE:
179 : pContext = new ScXMLDatabaseRangeContext ( GetScImport(), nPrefix, rLocalName,
180 0 : xAttrList );
181 0 : break;
182 : case XML_TOK_BODY_DATA_PILOT_TABLES:
183 : pContext = new ScXMLDataPilotTablesContext ( GetScImport(), nPrefix, rLocalName,
184 5 : xAttrList );
185 5 : break;
186 : case XML_TOK_BODY_CONSOLIDATION:
187 : pContext = new ScXMLConsolidationContext ( GetScImport(), nPrefix, rLocalName,
188 0 : xAttrList );
189 0 : break;
190 : case XML_TOK_BODY_DDE_LINKS:
191 : pContext = new ScXMLDDELinksContext ( GetScImport(), nPrefix, rLocalName,
192 0 : xAttrList );
193 0 : break;
194 : case XML_TOK_BODY_DATA_STREAM_SOURCE:
195 0 : pContext = new ScXMLDataStreamContext(GetScImport(), nPrefix, rLocalName, xAttrList);
196 0 : break;
197 : }
198 :
199 339 : if( !pContext )
200 0 : pContext = new SvXMLImportContext( GetImport(), nPrefix, rLocalName );
201 :
202 339 : return pContext;
203 : }
204 :
205 56 : void ScXMLBodyContext::Characters( const OUString& )
206 : {
207 56 : ScSheetSaveData* pSheetData = ScModelObj::getImplementation(GetScImport().GetModel())->GetSheetSaveData();
208 56 : if ( pSheetData && pSheetData->HasStartPos() )
209 : {
210 : // stream part to copy ends before any content (whitespace) within the spreadsheet element
211 15 : sal_Int32 nEndOffset = GetScImport().GetByteOffset();
212 15 : pSheetData->EndStreamPos( nEndOffset );
213 : }
214 : // otherwise ignore
215 56 : }
216 :
217 107 : void ScXMLBodyContext::EndElement()
218 : {
219 107 : ScSheetSaveData* pSheetData = ScModelObj::getImplementation(GetScImport().GetModel())->GetSheetSaveData();
220 107 : if ( pSheetData && pSheetData->HasStartPos() )
221 : {
222 : // stream part to copy ends before the closing tag of spreadsheet element
223 11 : sal_Int32 nEndOffset = GetScImport().GetByteOffset();
224 11 : pSheetData->EndStreamPos( nEndOffset );
225 : }
226 :
227 107 : if ( pSheetData )
228 : {
229 : // store the loaded namespaces (for the office:spreadsheet element),
230 : // so the prefixes in copied stream fragments remain valid
231 107 : const SvXMLNamespaceMap& rNamespaces = GetImport().GetNamespaceMap();
232 107 : pSheetData->StoreLoadedNamespaces( rNamespaces );
233 : }
234 :
235 107 : if (!bHadCalculationSettings)
236 : {
237 : // #111055#; set calculation settings defaults if there is no calculation settings element
238 90 : SvXMLImportContext *pContext = new ScXMLCalculationSettingsContext( GetScImport(), XML_NAMESPACE_TABLE, GetXMLToken(XML_CALCULATION_SETTINGS), NULL );
239 90 : pContext->EndElement();
240 : }
241 :
242 107 : ScXMLImport::MutexGuard aGuard(GetScImport());
243 :
244 107 : ScMyImpDetectiveOpArray* pDetOpArray = GetScImport().GetDetectiveOpArray();
245 107 : ScDocument* pDoc = GetScImport().GetDocument();
246 107 : ScMyImpDetectiveOp aDetOp;
247 :
248 107 : if (pDoc && GetScImport().GetModel().is())
249 : {
250 107 : if (pDetOpArray)
251 : {
252 107 : pDetOpArray->Sort();
253 214 : while( pDetOpArray->GetFirstOp( aDetOp ) )
254 : {
255 0 : ScDetOpData aOpData( aDetOp.aPosition, aDetOp.eOpType );
256 0 : pDoc->AddDetectiveOperation( aOpData );
257 : }
258 : }
259 :
260 107 : if (pChangeTrackingImportHelper)
261 0 : pChangeTrackingImportHelper->CreateChangeTrack(GetScImport().GetDocument());
262 :
263 : // #i37959# handle document protection after the sheet settings
264 107 : if (bProtected)
265 : {
266 0 : boost::scoped_ptr<ScDocProtection> pProtection(new ScDocProtection);
267 0 : pProtection->setProtected(true);
268 :
269 0 : uno::Sequence<sal_Int8> aPass;
270 0 : if (!sPassword.isEmpty())
271 : {
272 0 : ::sax::Converter::decodeBase64(aPass, sPassword);
273 0 : pProtection->setPasswordHash(aPass, meHash1, meHash2);
274 : }
275 :
276 0 : pDoc->SetDocProtection(pProtection.get());
277 : }
278 107 : }
279 209 : }
280 :
281 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|