LCOV - code coverage report
Current view: top level - oox/source/core - fastparser.cxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 60 68 88.2 %
Date: 2014-04-11 Functions: 14 15 93.3 %
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 <sal/config.h>
      21             : 
      22             : #include <com/sun/star/xml/sax/FastParser.hpp>
      23             : #include "oox/core/fastparser.hxx"
      24             : 
      25             : #include "oox/core/fasttokenhandler.hxx"
      26             : #include "oox/helper/containerhelper.hxx"
      27             : #include "oox/helper/helper.hxx"
      28             : #include "oox/helper/storagebase.hxx"
      29             : #include "oox/token/namespacemap.hxx"
      30             : 
      31             : #include "sax/fastparser.hxx"
      32             : 
      33             : namespace oox {
      34             : namespace core {
      35             : 
      36             : 
      37             : 
      38             : using namespace ::com::sun::star::io;
      39             : using namespace ::com::sun::star::lang;
      40             : using namespace ::com::sun::star::uno;
      41             : using namespace ::com::sun::star::xml::sax;
      42             : 
      43             : 
      44             : 
      45             : 
      46             : namespace {
      47             : 
      48             : class InputStreamCloseGuard
      49             : {
      50             : public:
      51             :     explicit            InputStreamCloseGuard( const Reference< XInputStream >& rxInStream, bool bCloseStream );
      52             :                         ~InputStreamCloseGuard();
      53             : private:
      54             :     Reference< XInputStream > mxInStream;
      55             :     bool                mbCloseStream;
      56             : };
      57             : 
      58        5023 : InputStreamCloseGuard::InputStreamCloseGuard( const Reference< XInputStream >& rxInStream, bool bCloseStream ) :
      59             :     mxInStream( rxInStream ),
      60        5023 :     mbCloseStream( bCloseStream )
      61             : {
      62        5023 : }
      63             : 
      64       10046 : InputStreamCloseGuard::~InputStreamCloseGuard()
      65             : {
      66        5023 :     if( mxInStream.is() && mbCloseStream ) try { mxInStream->closeInput(); } catch( Exception& ) {}
      67        5023 : }
      68             : 
      69             : } // namespace
      70             : 
      71             : 
      72             : 
      73        2771 : FastParser::FastParser( const Reference< XComponentContext >& rxContext ) throw( RuntimeException ) :
      74        2771 :     mrNamespaceMap( StaticNamespaceMap::get() ),
      75        5542 :     mpParser(NULL)
      76             : {
      77             :     // create a fast parser instance
      78        2771 :     mxParser = css::xml::sax::FastParser::create(rxContext);
      79        2771 :     mpParser = dynamic_cast<sax_fastparser::FastSaxParser*>(mxParser.get());
      80             : 
      81             :     // create the fast tokenhandler
      82        2771 :     mxTokenHandler.set( new FastTokenHandler );
      83             : 
      84             :     // create the fast token handler based on the OOXML token list
      85        2771 :     mxParser->setTokenHandler( mxTokenHandler );
      86        2771 : }
      87             : 
      88        2771 : FastParser::~FastParser()
      89             : {
      90        2771 : }
      91             : 
      92       25719 : void FastParser::registerNamespace( sal_Int32 nNamespaceId ) throw( IllegalArgumentException, RuntimeException )
      93             : {
      94       25719 :     if( !mxParser.is() )
      95           0 :         throw RuntimeException();
      96             : 
      97             :     // add handling for OOXML strict here
      98       25719 :     const OUString* pNamespaceUrl = ContainerHelper::getMapElement( mrNamespaceMap.maTransitionalNamespaceMap, nNamespaceId );
      99       25719 :     if( !pNamespaceUrl )
     100           0 :         throw IllegalArgumentException();
     101             : 
     102       25719 :     mxParser->registerNamespace( *pNamespaceUrl, nNamespaceId );
     103             : 
     104             :     //also register the OOXML strict namespaces for the same id
     105       25719 :     const OUString* pNamespaceStrictUrl = ContainerHelper::getMapElement( mrNamespaceMap.maStrictNamespaceMap, nNamespaceId );
     106       25719 :     if(pNamespaceStrictUrl && (*pNamespaceUrl != *pNamespaceStrictUrl))
     107             :     {
     108       10330 :         mxParser->registerNamespace( *pNamespaceStrictUrl, nNamespaceId );
     109             :     }
     110       25719 : }
     111             : 
     112        3951 : void FastParser::setDocumentHandler( const Reference< XFastDocumentHandler >& rxDocHandler ) throw( RuntimeException )
     113             : {
     114        3951 :     if( !mxParser.is() )
     115           0 :         throw RuntimeException();
     116        3951 :     mxParser->setFastDocumentHandler( rxDocHandler );
     117        3951 : }
     118             : 
     119        5023 : void FastParser::parseStream( const InputSource& rInputSource, bool bCloseStream ) throw( SAXException, IOException, RuntimeException )
     120             : {
     121             :     // guard closing the input stream also when exceptions are thrown
     122        5023 :     InputStreamCloseGuard aGuard( rInputSource.aInputStream, bCloseStream );
     123        5023 :     if( !mxParser.is() )
     124           0 :         throw RuntimeException();
     125        5125 :     mxParser->parseStream( rInputSource );
     126        4921 : }
     127             : 
     128        3106 : void FastParser::parseStream( const Reference< XInputStream >& rxInStream, const OUString& rStreamName, bool bCloseStream ) throw( SAXException, IOException, RuntimeException )
     129             : {
     130        3106 :     InputSource aInputSource;
     131        3106 :     aInputSource.sSystemId = rStreamName;
     132        3106 :     aInputSource.aInputStream = rxInStream;
     133        3208 :     parseStream( aInputSource, bCloseStream );
     134        3004 : }
     135             : 
     136        1926 : void FastParser::parseStream( StorageBase& rStorage, const OUString& rStreamName, bool bCloseStream ) throw( SAXException, IOException, RuntimeException )
     137             : {
     138        2028 :     parseStream( rStorage.openInputStream( rStreamName ), rStreamName, bCloseStream );
     139        1824 : }
     140             : 
     141          37 : OUString FastParser::getNamespaceURL( const OUString& rPrefix ) throw( IllegalArgumentException, RuntimeException )
     142             : {
     143          37 :     if( !mxParser.is() )
     144           0 :         throw RuntimeException();
     145          37 :     return mxParser->getNamespaceURL( rPrefix );
     146             : }
     147             : 
     148          38 : bool FastParser::hasNamespaceURL( const OUString& rPrefix ) const
     149             : {
     150          38 :     if (!mxParser.is())
     151           0 :         throw RuntimeException();
     152             : 
     153          38 :     if (!mpParser)
     154           0 :         return false;
     155             : 
     156          38 :     return mpParser->hasNamespaceURL(rPrefix);
     157             : }
     158             : 
     159          37 : sal_Int32 FastParser::getNamespaceId( const OUString& rUrl )
     160             : {
     161        1505 :     for( NamespaceMap::const_iterator aIt = mrNamespaceMap.maTransitionalNamespaceMap.begin(),
     162          37 :             aEnd = mrNamespaceMap.maTransitionalNamespaceMap.end(); aIt != aEnd; ++aIt )
     163        1434 :         if( rUrl  == aIt->second )
     164           3 :             return aIt->first;
     165             : 
     166        1428 :     for( NamespaceMap::const_iterator aIt = mrNamespaceMap.maStrictNamespaceMap.begin(),
     167          34 :             aEnd = mrNamespaceMap.maStrictNamespaceMap.end(); aIt != aEnd; ++aIt )
     168        1360 :         if( rUrl  == aIt->second )
     169           0 :             return aIt->first;
     170             : 
     171          34 :     return 0;
     172             : }
     173             : 
     174             : 
     175             : 
     176             : } // namespace core
     177         177 : } // namespace oox
     178             : 
     179             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10