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