LCOV - code coverage report
Current view: top level - sc/source/filter/xml - xmlbodyi.cxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 70 113 61.9 %
Date: 2014-11-03 Functions: 8 8 100.0 %
Legend: Lines: hit not hit

          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         322 : 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         322 :     pChangeTrackingImportHelper(NULL)
      70             : {
      71         322 :     ScDocument* pDoc = GetScImport().GetDocument();
      72         322 :     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         322 :         formula::FormulaGrammar::Grammar eGrammar = formula::FormulaGrammar::GRAM_ODFF;
      77         322 :         OUString aVer( rImport.GetODFVersion());
      78         322 :         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         322 :         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         322 :             double fVer = ::rtl::math::stringToDouble( aVer, '.', 0, NULL, NULL);
      91         322 :             if (fVer < 1.2)
      92          10 :                 eGrammar = formula::FormulaGrammar::GRAM_PODF;
      93             :         }
      94         322 :         pDoc->SetStorageGrammar( eGrammar);
      95             :     }
      96             : 
      97         322 :     sal_Int16 nAttrCount = xAttrList.is() ? xAttrList->getLength() : 0;
      98         322 :     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         322 : }
     119             : 
     120         644 : ScXMLBodyContext::~ScXMLBodyContext()
     121             : {
     122         644 : }
     123             : 
     124         866 : 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         866 :     ScSheetSaveData* pSheetData = ScModelObj::getImplementation(GetScImport().GetModel())->GetSheetSaveData();
     130         866 :     if ( pSheetData && pSheetData->HasStartPos() )
     131             :     {
     132             :         // stream part to copy ends before the next child element
     133         424 :         sal_Int32 nEndOffset = GetScImport().GetByteOffset();
     134         424 :         pSheetData->EndStreamPos( nEndOffset );
     135             :     }
     136             : 
     137         866 :     SvXMLImportContext *pContext = 0;
     138             : 
     139         866 :     const SvXMLTokenMap& rTokenMap = GetScImport().GetBodyElemTokenMap();
     140         866 :     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          40 :         pContext = new ScXMLCalculationSettingsContext( GetScImport(), nPrefix, rLocalName, xAttrList );
     149          40 :         bHadCalculationSettings = true;
     150          40 :         break;
     151             :     case XML_TOK_BODY_CONTENT_VALIDATIONS :
     152           2 :         pContext = new ScXMLContentValidationsContext( GetScImport(), nPrefix, rLocalName, xAttrList );
     153           2 :         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         582 :         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         582 :                                               xAttrList );
     167             :         }
     168         582 :         break;
     169             :     case XML_TOK_BODY_NAMED_EXPRESSIONS:
     170             :         pContext = new ScXMLNamedExpressionsContext (
     171             :             GetScImport(), nPrefix, rLocalName, xAttrList,
     172         216 :             new ScXMLNamedExpressionsContext::GlobalInserter(GetScImport()) );
     173         216 :         break;
     174             :     case XML_TOK_BODY_DATABASE_RANGES:
     175             :         pContext = new ScXMLDatabaseRangesContext ( GetScImport(), nPrefix, rLocalName,
     176          16 :                                                         xAttrList );
     177          16 :         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          10 :                                                         xAttrList );
     185          10 :         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         866 :     if( !pContext )
     200           0 :         pContext = new SvXMLImportContext( GetImport(), nPrefix, rLocalName );
     201             : 
     202         866 :     return pContext;
     203             : }
     204             : 
     205         128 : void ScXMLBodyContext::Characters( const OUString& )
     206             : {
     207         128 :     ScSheetSaveData* pSheetData = ScModelObj::getImplementation(GetScImport().GetModel())->GetSheetSaveData();
     208         128 :     if ( pSheetData && pSheetData->HasStartPos() )
     209             :     {
     210             :         // stream part to copy ends before any content (whitespace) within the spreadsheet element
     211          34 :         sal_Int32 nEndOffset = GetScImport().GetByteOffset();
     212          34 :         pSheetData->EndStreamPos( nEndOffset );
     213             :     }
     214             :     // otherwise ignore
     215         128 : }
     216             : 
     217         322 : void ScXMLBodyContext::EndElement()
     218             : {
     219         322 :     ScSheetSaveData* pSheetData = ScModelObj::getImplementation(GetScImport().GetModel())->GetSheetSaveData();
     220         322 :     if ( pSheetData && pSheetData->HasStartPos() )
     221             :     {
     222             :         // stream part to copy ends before the closing tag of spreadsheet element
     223          34 :         sal_Int32 nEndOffset = GetScImport().GetByteOffset();
     224          34 :         pSheetData->EndStreamPos( nEndOffset );
     225             :     }
     226             : 
     227         322 :     if ( pSheetData )
     228             :     {
     229             :         // store the loaded namespaces (for the office:spreadsheet element),
     230             :         // so the prefixes in copied stream fragments remain valid
     231         322 :         const SvXMLNamespaceMap& rNamespaces = GetImport().GetNamespaceMap();
     232         322 :         pSheetData->StoreLoadedNamespaces( rNamespaces );
     233             :     }
     234             : 
     235         322 :     if (!bHadCalculationSettings)
     236             :     {
     237             :         // #111055#; set calculation settings defaults if there is no calculation settings element
     238         282 :         ScXMLCalculationSettingsContext aContext( GetScImport(), XML_NAMESPACE_TABLE, GetXMLToken(XML_CALCULATION_SETTINGS), NULL );
     239         282 :         aContext.EndElement();
     240             :     }
     241             : 
     242         322 :     ScXMLImport::MutexGuard aGuard(GetScImport());
     243             : 
     244         322 :     ScMyImpDetectiveOpArray*    pDetOpArray = GetScImport().GetDetectiveOpArray();
     245         322 :     ScDocument*                 pDoc        = GetScImport().GetDocument();
     246         322 :     ScMyImpDetectiveOp          aDetOp;
     247             : 
     248         322 :     if (pDoc && GetScImport().GetModel().is())
     249             :     {
     250         322 :         if (pDetOpArray)
     251             :         {
     252         322 :             pDetOpArray->Sort();
     253         644 :             while( pDetOpArray->GetFirstOp( aDetOp ) )
     254             :             {
     255           0 :                 ScDetOpData aOpData( aDetOp.aPosition, aDetOp.eOpType );
     256           0 :                 pDoc->AddDetectiveOperation( aOpData );
     257             :             }
     258             :         }
     259             : 
     260         322 :         if (pChangeTrackingImportHelper)
     261           0 :             pChangeTrackingImportHelper->CreateChangeTrack(GetScImport().GetDocument());
     262             : 
     263             :         // #i37959# handle document protection after the sheet settings
     264         322 :         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         322 :     }
     279         550 : }
     280             : 
     281             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10