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