xref: /aoo4110/main/sc/source/filter/xml/xmlbodyi.cxx (revision b1cdbd2c)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_sc.hxx"
26 
27 
28 // INCLUDE ---------------------------------------------------------------
29 #include <cstdio>
30 
31 #include "document.hxx"
32 #include "docuno.hxx"
33 #include "sheetdata.hxx"
34 
35 #include "xmlbodyi.hxx"
36 #include "xmltabi.hxx"
37 #include "xmlnexpi.hxx"
38 #include "xmldrani.hxx"
39 #include "xmlimprt.hxx"
40 #include "xmldpimp.hxx"
41 #include "xmlcvali.hxx"
42 #include "xmlstyli.hxx"
43 #include "xmllabri.hxx"
44 #include "XMLConsolidationContext.hxx"
45 #include "XMLDDELinksContext.hxx"
46 #include "XMLCalculationSettingsContext.hxx"
47 #include "XMLTrackedChangesContext.hxx"
48 #include "XMLEmptyContext.hxx"
49 #include "scerrors.hxx"
50 #include "tabprotection.hxx"
51 
52 #include <xmloff/xmltkmap.hxx>
53 #include <xmloff/xmltoken.hxx>
54 #include <xmloff/xmlnmspe.hxx>
55 #include <xmloff/nmspmap.hxx>
56 #include <xmloff/xmluconv.hxx>
57 #include <com/sun/star/sheet/XSpreadsheetDocument.hpp>
58 #include <sal/types.h>
59 #include <tools/debug.hxx>
60 
61 #include <memory>
62 
63 using rtl::OUString;
64 
65 using namespace com::sun::star;
66 using namespace xmloff::token;
67 
68 //------------------------------------------------------------------
69 
ScXMLBodyContext(ScXMLImport & rImport,sal_uInt16 nPrfx,const::rtl::OUString & rLName,const uno::Reference<xml::sax::XAttributeList> & xAttrList)70 ScXMLBodyContext::ScXMLBodyContext( ScXMLImport& rImport,
71 											  sal_uInt16 nPrfx,
72 				   	  						  const ::rtl::OUString& rLName,
73 											  const uno::Reference<xml::sax::XAttributeList>& xAttrList ) :
74 	SvXMLImportContext( rImport, nPrfx, rLName ),
75 	sPassword(),
76 	bProtected(sal_False),
77     bHadCalculationSettings(sal_False),
78 	pChangeTrackingImportHelper(NULL)
79 {
80     ScDocument* pDoc = GetScImport().GetDocument();
81     if (pDoc)
82     {
83         // ODF 1.1 and earlier => GRAM_PODF; ODF 1.2 and later => GRAM_ODFF;
84         // no version => earlier than 1.2 => GRAM_PODF.
85         formula::FormulaGrammar::Grammar eGrammar = formula::FormulaGrammar::GRAM_ODFF;
86         OUString aVer( rImport.GetODFVersion());
87         sal_Int32 nLen = aVer.getLength();
88 #if OSL_DEBUG_LEVEL > 1
89         fprintf( stderr, "\n ScXMLBodyContext ODFVersion: nLen: %d, str: %s\n",
90                 (int)nLen, OUStringToOString( aVer, RTL_TEXTENCODING_UTF8).getStr());
91 #endif
92         if (!nLen)
93             eGrammar = formula::FormulaGrammar::GRAM_PODF;
94         else
95         {
96             // In case there was a micro version, e.g. "1.2.3", this would
97             // still yield major.minor, but pParsedEnd (5th parameter, not
98             // passed here) would point before string end upon return.
99             double fVer = ::rtl::math::stringToDouble( aVer, '.', 0, NULL, NULL);
100             if (fVer < 1.2)
101                 eGrammar = formula::FormulaGrammar::GRAM_PODF;
102         }
103         pDoc->SetStorageGrammar( eGrammar);
104     }
105 
106 	sal_Int16 nAttrCount = xAttrList.is() ? xAttrList->getLength() : 0;
107 	for( sal_Int16 i=0; i < nAttrCount; ++i )
108 	{
109 		const rtl::OUString& sAttrName(xAttrList->getNameByIndex( i ));
110 		rtl::OUString aLocalName;
111 		sal_uInt16 nPrefix = GetScImport().GetNamespaceMap().GetKeyByAttrName(
112 											sAttrName, &aLocalName );
113 		const rtl::OUString& sValue(xAttrList->getValueByIndex( i ));
114 
115 		if (nPrefix == XML_NAMESPACE_TABLE)
116 		{
117 			if (IsXMLToken(aLocalName, XML_STRUCTURE_PROTECTED))
118 				bProtected = IsXMLToken(sValue, XML_TRUE);
119 			else if (IsXMLToken(aLocalName, XML_PROTECTION_KEY))
120 				sPassword = sValue;
121 		}
122 	}
123 }
124 
~ScXMLBodyContext()125 ScXMLBodyContext::~ScXMLBodyContext()
126 {
127 }
128 
CreateChildContext(sal_uInt16 nPrefix,const::rtl::OUString & rLocalName,const::com::sun::star::uno::Reference<::com::sun::star::xml::sax::XAttributeList> & xAttrList)129 SvXMLImportContext *ScXMLBodyContext::CreateChildContext( sal_uInt16 nPrefix,
130 									 const ::rtl::OUString& rLocalName,
131 									 const ::com::sun::star::uno::Reference<
132 									  	::com::sun::star::xml::sax::XAttributeList>& xAttrList )
133 {
134     ScSheetSaveData* pSheetData = ScModelObj::getImplementation(GetScImport().GetModel())->GetSheetSaveData();
135     if ( pSheetData && pSheetData->HasStartPos() )
136     {
137         // stream part to copy ends before the next child element
138         sal_Int32 nEndOffset = GetScImport().GetByteOffset();
139         pSheetData->EndStreamPos( nEndOffset );
140     }
141 
142 	SvXMLImportContext *pContext = 0;
143 
144 	const SvXMLTokenMap& rTokenMap = GetScImport().GetBodyElemTokenMap();
145 //    sal_Bool bOrdered = sal_False;
146 //    sal_Bool bHeading = sal_False;
147 	switch( rTokenMap.Get( nPrefix, rLocalName ) )
148 	{
149 //	case XML_TOK_TEXT_H:
150 //		bHeading = sal_True;
151 //	case XML_TOK_TEXT_P:
152 //		pContext = new SwXMLParaContext( GetSwImport(),nPrefix, rLocalName,
153 //										 xAttrList, bHeading );
154 //		break;
155 //	case XML_TOK_TEXT_ORDERED_LIST:
156 //		bOrdered = sal_True;
157 //	case XML_TOK_TEXT_UNORDERED_LIST:
158 //		pContext = new SwXMLListBlockContext( GetSwImport(),nPrefix, rLocalName,
159 //											  xAttrList, bOrdered );
160 //		break;
161 	case XML_TOK_BODY_TRACKED_CHANGES :
162 	{
163 		pChangeTrackingImportHelper = GetScImport().GetChangeTrackingImportHelper();
164 		if (pChangeTrackingImportHelper)
165 			pContext = new ScXMLTrackedChangesContext( GetScImport(), nPrefix, rLocalName, xAttrList, pChangeTrackingImportHelper);
166 	}
167 	break;
168 	case XML_TOK_BODY_CALCULATION_SETTINGS :
169 		pContext = new ScXMLCalculationSettingsContext( GetScImport(), nPrefix, rLocalName, xAttrList );
170         bHadCalculationSettings = sal_True;
171 		break;
172 	case XML_TOK_BODY_CONTENT_VALIDATIONS :
173 		pContext = new ScXMLContentValidationsContext( GetScImport(), nPrefix, rLocalName, xAttrList );
174 		break;
175 	case XML_TOK_BODY_LABEL_RANGES:
176 		pContext = new ScXMLLabelRangesContext( GetScImport(), nPrefix, rLocalName, xAttrList );
177 		break;
178 	case XML_TOK_BODY_TABLE:
179         {
180             if (GetScImport().GetTables().GetCurrentSheet() >= MAXTAB)
181             {
182                 GetScImport().SetRangeOverflowType(SCWARN_IMPORT_SHEET_OVERFLOW);
183                 pContext = new ScXMLEmptyContext(GetScImport(), nPrefix, rLocalName);
184             }
185             else
186             {
187 			    pContext = new ScXMLTableContext( GetScImport(),nPrefix, rLocalName,
188 											      xAttrList );
189             }
190         }
191 		break;
192 	case XML_TOK_BODY_NAMED_EXPRESSIONS:
193 		pContext = new ScXMLNamedExpressionsContext ( GetScImport(), nPrefix, rLocalName,
194 														xAttrList );
195 		static_cast<ScXMLNamedExpressionsContext*>(pContext)->SetScope( MAXTABCOUNT );//workbookname
196 		break;
197 	case XML_TOK_BODY_DATABASE_RANGES:
198 		pContext = new ScXMLDatabaseRangesContext ( GetScImport(), nPrefix, rLocalName,
199 														xAttrList );
200 		break;
201 	case XML_TOK_BODY_DATABASE_RANGE:
202 		pContext = new ScXMLDatabaseRangeContext ( GetScImport(), nPrefix, rLocalName,
203 														xAttrList );
204 		break;
205 	case XML_TOK_BODY_DATA_PILOT_TABLES:
206 		pContext = new ScXMLDataPilotTablesContext ( GetScImport(), nPrefix, rLocalName,
207 														xAttrList );
208 		break;
209 	case XML_TOK_BODY_CONSOLIDATION:
210 		pContext = new ScXMLConsolidationContext ( GetScImport(), nPrefix, rLocalName,
211 														xAttrList );
212 		break;
213 	case XML_TOK_BODY_DDE_LINKS:
214 		pContext = new ScXMLDDELinksContext ( GetScImport(), nPrefix, rLocalName,
215 														xAttrList );
216 		break;
217 	}
218 
219 	if( !pContext )
220 		pContext = new SvXMLImportContext( GetImport(), nPrefix, rLocalName );
221 
222 	return pContext;
223 }
224 
Characters(const OUString &)225 void ScXMLBodyContext::Characters( const OUString& )
226 {
227     ScSheetSaveData* pSheetData = ScModelObj::getImplementation(GetScImport().GetModel())->GetSheetSaveData();
228     if ( pSheetData && pSheetData->HasStartPos() )
229     {
230         // stream part to copy ends before any content (whitespace) within the spreadsheet element
231         sal_Int32 nEndOffset = GetScImport().GetByteOffset();
232         pSheetData->EndStreamPos( nEndOffset );
233     }
234     // otherwise ignore
235 }
236 
EndElement()237 void ScXMLBodyContext::EndElement()
238 {
239     ScSheetSaveData* pSheetData = ScModelObj::getImplementation(GetScImport().GetModel())->GetSheetSaveData();
240     if ( pSheetData && pSheetData->HasStartPos() )
241     {
242         // stream part to copy ends before the closing tag of spreadsheet element
243         sal_Int32 nEndOffset = GetScImport().GetByteOffset();
244         pSheetData->EndStreamPos( nEndOffset );
245     }
246 
247     if ( pSheetData )
248     {
249         // store the loaded namespaces (for the office:spreadsheet element),
250         // so the prefixes in copied stream fragments remain valid
251         const SvXMLNamespaceMap& rNamespaces = GetImport().GetNamespaceMap();
252         pSheetData->StoreLoadedNamespaces( rNamespaces );
253     }
254 
255     if (!bHadCalculationSettings)
256     {
257         // #111055#; set calculation settings defaults if there is no calculation settings element
258 		SvXMLImportContext *pContext = new ScXMLCalculationSettingsContext( GetScImport(), XML_NAMESPACE_TABLE, GetXMLToken(XML_CALCULATION_SETTINGS), NULL );
259         pContext->EndElement();
260     }
261 	GetScImport().LockSolarMutex();
262 	ScMyImpDetectiveOpArray*	pDetOpArray	= GetScImport().GetDetectiveOpArray();
263 	ScDocument*					pDoc		= GetScImport().GetDocument();
264 	ScMyImpDetectiveOp			aDetOp;
265 
266 	if (pDoc && GetScImport().GetModel().is())
267 	{
268 		if (pDetOpArray)
269 		{
270 			pDetOpArray->Sort();
271 			while( pDetOpArray->GetFirstOp( aDetOp ) )
272 			{
273 				ScDetOpData aOpData( aDetOp.aPosition, aDetOp.eOpType );
274 				pDoc->AddDetectiveOperation( aOpData );
275 			}
276 		}
277 
278 		if (pChangeTrackingImportHelper)
279 			pChangeTrackingImportHelper->CreateChangeTrack(GetScImport().GetDocument());
280 
281 #if 0
282         // #i57869# table styles are applied before the contents now
283 
284         std::vector<rtl::OUString> aTableStyleNames(GetScImport().GetTableStyle());
285 		uno::Reference <sheet::XSpreadsheetDocument> xSpreadDoc( GetScImport().GetModel(), uno::UNO_QUERY );
286 		if ( xSpreadDoc.is() && !aTableStyleNames.empty())
287 		{
288 			uno::Reference <container::XIndexAccess> xIndex( xSpreadDoc->getSheets(), uno::UNO_QUERY );
289 			if ( xIndex.is() )
290 			{
291 				sal_Int32 nTableCount = xIndex->getCount();
292                 sal_Int32 nSize(aTableStyleNames.size());
293                 DBG_ASSERT(nTableCount == nSize, "every table should have a style name");
294                 for(sal_uInt32 i = 0; i < nTableCount; i++)
295                 {
296                     if (i < nSize)
297                     {
298 				        uno::Reference <beans::XPropertySet> xProperties(xIndex->getByIndex(i), uno::UNO_QUERY);
299 				        if (xProperties.is())
300 				        {
301                             rtl::OUString sTableStyleName(aTableStyleNames[i]);
302 					        XMLTableStylesContext *pStyles = (XMLTableStylesContext *)GetScImport().GetAutoStyles();
303                             if ( pStyles && sTableStyleName.getLength() )
304 					        {
305     						    XMLTableStyleContext* pStyle = (XMLTableStyleContext *)pStyles->FindStyleChildContext(
306 	    						    XML_STYLE_FAMILY_TABLE_TABLE, sTableStyleName, sal_True);
307 		    				    if (pStyle)
308 			    				    pStyle->FillPropertySet(xProperties);
309 					        }
310 				        }
311                     }
312                 }
313 			}
314 		}
315 #endif
316 
317         // #i37959# handle document protection after the sheet settings
318 		if (bProtected)
319 		{
320             ::std::auto_ptr<ScDocProtection> pProtection(new ScDocProtection);
321             pProtection->setProtected(true);
322 
323 			uno::Sequence<sal_Int8> aPass;
324 			if (sPassword.getLength())
325             {
326 				SvXMLUnitConverter::decodeBase64(aPass, sPassword);
327                 pProtection->setPasswordHash(aPass, PASSHASH_OOO);
328             }
329 
330             pDoc->SetDocProtection(pProtection.get());
331 		}
332 	}
333 	GetScImport().UnlockSolarMutex();
334 }
335 
336