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 "XMLDetectiveContext.hxx"
41 : #include "scerrors.hxx"
42 : #include "tabprotection.hxx"
43 : #include "datastreamimport.hxx"
44 :
45 : #include <xmloff/xmltkmap.hxx>
46 : #include <xmloff/xmltoken.hxx>
47 : #include <xmloff/xmlnmspe.hxx>
48 : #include <xmloff/nmspmap.hxx>
49 :
50 : #include <sax/tools/converter.hxx>
51 : #include <com/sun/star/sheet/XSpreadsheetDocument.hpp>
52 : #include <sal/types.h>
53 :
54 : #include <boost/scoped_ptr.hpp>
55 :
56 : using namespace com::sun::star;
57 : using namespace xmloff::token;
58 :
59 197 : 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 197 : pChangeTrackingImportHelper(NULL)
70 : {
71 197 : ScDocument* pDoc = GetScImport().GetDocument();
72 197 : 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 197 : formula::FormulaGrammar::Grammar eGrammar = formula::FormulaGrammar::GRAM_ODFF;
77 197 : OUString aVer( rImport.GetODFVersion());
78 197 : 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 197 : 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 197 : double fVer = ::rtl::math::stringToDouble( aVer, '.', 0, NULL, NULL);
91 197 : if (fVer < 1.2)
92 5 : eGrammar = formula::FormulaGrammar::GRAM_PODF;
93 : }
94 197 : pDoc->SetStorageGrammar( eGrammar);
95 : }
96 :
97 197 : sal_Int16 nAttrCount = xAttrList.is() ? xAttrList->getLength() : 0;
98 197 : 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 : else if(nPrefix == XML_NAMESPACE_LO_EXT)
118 : {
119 0 : if (IsXMLToken(aLocalName, XML_PROTECTION_KEY_DIGEST_ALGORITHM_2))
120 0 : meHash2 = ScPassHashHelper::getHashTypeFromURI(sValue);
121 : }
122 0 : }
123 197 : }
124 :
125 394 : ScXMLBodyContext::~ScXMLBodyContext()
126 : {
127 394 : }
128 :
129 524 : SvXMLImportContext *ScXMLBodyContext::CreateChildContext( sal_uInt16 nPrefix,
130 : const OUString& rLocalName,
131 : const ::com::sun::star::uno::Reference<
132 : ::com::sun::star::xml::sax::XAttributeList>& xAttrList )
133 : {
134 524 : ScSheetSaveData* pSheetData = ScModelObj::getImplementation(GetScImport().GetModel())->GetSheetSaveData();
135 524 : if ( pSheetData && pSheetData->HasStartPos() )
136 : {
137 : // stream part to copy ends before the next child element
138 254 : sal_Int32 nEndOffset = GetScImport().GetByteOffset();
139 254 : pSheetData->EndStreamPos( nEndOffset );
140 : }
141 :
142 524 : SvXMLImportContext *pContext = 0;
143 :
144 524 : const SvXMLTokenMap& rTokenMap = GetScImport().GetBodyElemTokenMap();
145 524 : switch( rTokenMap.Get( nPrefix, rLocalName ) )
146 : {
147 : case XML_TOK_BODY_TRACKED_CHANGES :
148 1 : pChangeTrackingImportHelper = GetScImport().GetChangeTrackingImportHelper();
149 1 : if (pChangeTrackingImportHelper)
150 1 : pContext = new ScXMLTrackedChangesContext( GetScImport(), nPrefix, rLocalName, xAttrList, pChangeTrackingImportHelper);
151 1 : break;
152 : case XML_TOK_BODY_CALCULATION_SETTINGS :
153 28 : pContext = new ScXMLCalculationSettingsContext( GetScImport(), nPrefix, rLocalName, xAttrList );
154 28 : bHadCalculationSettings = true;
155 28 : break;
156 : case XML_TOK_BODY_CONTENT_VALIDATIONS :
157 1 : pContext = new ScXMLContentValidationsContext( GetScImport(), nPrefix, rLocalName, xAttrList );
158 1 : break;
159 : case XML_TOK_BODY_LABEL_RANGES:
160 0 : pContext = new ScXMLLabelRangesContext( GetScImport(), nPrefix, rLocalName, xAttrList );
161 0 : break;
162 : case XML_TOK_BODY_TABLE:
163 337 : if (GetScImport().GetTables().GetCurrentSheet() >= MAXTAB)
164 : {
165 0 : GetScImport().SetRangeOverflowType(SCWARN_IMPORT_SHEET_OVERFLOW);
166 0 : pContext = new ScXMLEmptyContext(GetScImport(), nPrefix, rLocalName);
167 : }
168 : else
169 : {
170 : pContext = new ScXMLTableContext( GetScImport(),nPrefix, rLocalName,
171 337 : xAttrList );
172 : }
173 337 : break;
174 : case XML_TOK_BODY_NAMED_EXPRESSIONS:
175 : pContext = new ScXMLNamedExpressionsContext (
176 : GetScImport(), nPrefix, rLocalName, xAttrList,
177 143 : new ScXMLNamedExpressionsContext::GlobalInserter(GetScImport()) );
178 143 : break;
179 : case XML_TOK_BODY_DATABASE_RANGES:
180 : pContext = new ScXMLDatabaseRangesContext ( GetScImport(), nPrefix, rLocalName,
181 7 : xAttrList );
182 7 : break;
183 : case XML_TOK_BODY_DATABASE_RANGE:
184 : pContext = new ScXMLDatabaseRangeContext ( GetScImport(), nPrefix, rLocalName,
185 0 : xAttrList );
186 0 : break;
187 : case XML_TOK_BODY_DATA_PILOT_TABLES:
188 : pContext = new ScXMLDataPilotTablesContext ( GetScImport(), nPrefix, rLocalName,
189 7 : xAttrList );
190 7 : break;
191 : case XML_TOK_BODY_CONSOLIDATION:
192 : pContext = new ScXMLConsolidationContext ( GetScImport(), nPrefix, rLocalName,
193 0 : xAttrList );
194 0 : break;
195 : case XML_TOK_BODY_DDE_LINKS:
196 : pContext = new ScXMLDDELinksContext ( GetScImport(), nPrefix, rLocalName,
197 0 : xAttrList );
198 0 : break;
199 : case XML_TOK_BODY_DATA_STREAM_SOURCE:
200 0 : pContext = new ScXMLDataStreamContext(GetScImport(), nPrefix, rLocalName, xAttrList);
201 0 : break;
202 : }
203 :
204 524 : if( !pContext )
205 0 : pContext = new SvXMLImportContext( GetImport(), nPrefix, rLocalName );
206 :
207 524 : return pContext;
208 : }
209 :
210 58 : void ScXMLBodyContext::Characters( const OUString& )
211 : {
212 58 : ScSheetSaveData* pSheetData = ScModelObj::getImplementation(GetScImport().GetModel())->GetSheetSaveData();
213 58 : if ( pSheetData && pSheetData->HasStartPos() )
214 : {
215 : // stream part to copy ends before any content (whitespace) within the spreadsheet element
216 16 : sal_Int32 nEndOffset = GetScImport().GetByteOffset();
217 16 : pSheetData->EndStreamPos( nEndOffset );
218 : }
219 : // otherwise ignore
220 58 : }
221 :
222 197 : void ScXMLBodyContext::EndElement()
223 : {
224 197 : ScSheetSaveData* pSheetData = ScModelObj::getImplementation(GetScImport().GetModel())->GetSheetSaveData();
225 197 : if ( pSheetData && pSheetData->HasStartPos() )
226 : {
227 : // stream part to copy ends before the closing tag of spreadsheet element
228 17 : sal_Int32 nEndOffset = GetScImport().GetByteOffset();
229 17 : pSheetData->EndStreamPos( nEndOffset );
230 : }
231 :
232 197 : if ( pSheetData )
233 : {
234 : // store the loaded namespaces (for the office:spreadsheet element),
235 : // so the prefixes in copied stream fragments remain valid
236 197 : const SvXMLNamespaceMap& rNamespaces = GetImport().GetNamespaceMap();
237 197 : pSheetData->StoreLoadedNamespaces( rNamespaces );
238 : }
239 :
240 197 : if (!bHadCalculationSettings)
241 : {
242 : // #111055#; set calculation settings defaults if there is no calculation settings element
243 169 : ScXMLCalculationSettingsContext aContext( GetScImport(), XML_NAMESPACE_TABLE, GetXMLToken(XML_CALCULATION_SETTINGS), NULL );
244 169 : aContext.EndElement();
245 : }
246 :
247 197 : ScXMLImport::MutexGuard aGuard(GetScImport());
248 :
249 197 : ScMyImpDetectiveOpArray* pDetOpArray = GetScImport().GetDetectiveOpArray();
250 197 : ScDocument* pDoc = GetScImport().GetDocument();
251 197 : ScMyImpDetectiveOp aDetOp;
252 :
253 197 : if (pDoc && GetScImport().GetModel().is())
254 : {
255 197 : if (pDetOpArray)
256 : {
257 197 : pDetOpArray->Sort();
258 404 : while( pDetOpArray->GetFirstOp( aDetOp ) )
259 : {
260 10 : ScDetOpData aOpData( aDetOp.aPosition, aDetOp.eOpType );
261 10 : pDoc->AddDetectiveOperation( aOpData );
262 : }
263 : }
264 :
265 197 : if (pChangeTrackingImportHelper)
266 1 : pChangeTrackingImportHelper->CreateChangeTrack(GetScImport().GetDocument());
267 :
268 : // #i37959# handle document protection after the sheet settings
269 197 : if (bProtected)
270 : {
271 0 : boost::scoped_ptr<ScDocProtection> pProtection(new ScDocProtection);
272 0 : pProtection->setProtected(true);
273 :
274 0 : uno::Sequence<sal_Int8> aPass;
275 0 : if (!sPassword.isEmpty())
276 : {
277 0 : ::sax::Converter::decodeBase64(aPass, sPassword);
278 0 : pProtection->setPasswordHash(aPass, meHash1, meHash2);
279 : }
280 :
281 0 : pDoc->SetDocProtection(pProtection.get());
282 : }
283 197 : }
284 353 : }
285 :
286 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|