LCOV - code coverage report
Current view: top level - libreoffice/writerfilter/source/ooxml - OOXMLDocumentImpl.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 131 173 75.7 %
Date: 2012-12-27 Functions: 27 34 79.4 %
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 <com/sun/star/xml/sax/XParser.hpp>
      21             : 
      22             : #include <com/sun/star/xml/sax/SAXException.hpp>
      23             : #include <doctok/resourceids.hxx>
      24             : #include <ooxml/resourceids.hxx>
      25             : #include "OOXMLDocumentImpl.hxx"
      26             : #include "OOXMLBinaryObjectReference.hxx"
      27             : #include "OOXMLFastDocumentHandler.hxx"
      28             : #include "OOXMLPropertySetImpl.hxx"
      29             : #include "ooxmlLoggers.hxx"
      30             : 
      31             : #include <iostream>
      32             : 
      33             : using ::com::sun::star::xml::sax::SAXException;
      34             : namespace writerfilter {
      35             : namespace ooxml
      36             : {
      37             : 
      38             : #if OSL_DEBUG_LEVEL > 1
      39             : TagLogger::Pointer_t debug_logger(TagLogger::getInstance("DEBUG"));
      40             : #endif
      41             : 
      42             : using namespace ::std;
      43             : 
      44         121 : OOXMLDocumentImpl::OOXMLDocumentImpl
      45             : (OOXMLStream::Pointer_t pStream)
      46         121 : : mpStream(pStream), mXNoteType(0), mbIsSubstream( false )
      47             : {
      48         121 : }
      49             : 
      50         242 : OOXMLDocumentImpl::~OOXMLDocumentImpl()
      51             : {
      52         242 : }
      53             : 
      54         605 : void OOXMLDocumentImpl::resolveFastSubStream(Stream & rStreamHandler,
      55             :                                              OOXMLStream::StreamType_t nType)
      56             : {
      57             :     OOXMLStream::Pointer_t pStream
      58         605 :         (OOXMLDocumentFactory::createStream(mpStream, nType));
      59         605 :     OOXMLStream::Pointer_t savedStream = mpStream;
      60         605 :     mpStream = pStream;
      61             : 
      62             :     uno::Reference< xml::sax::XFastParser > xParser
      63         605 :         (mpStream->getFastParser());
      64             : 
      65         605 :     if (xParser.is())
      66             :     {
      67         605 :         uno::Reference<uno::XComponentContext> xContext(mpStream->getContext());
      68             :         OOXMLFastDocumentHandler * pDocHandler =
      69         605 :             new OOXMLFastDocumentHandler(xContext);
      70         605 :         pDocHandler->setStream(&rStreamHandler);
      71         605 :         pDocHandler->setDocument(this);
      72         605 :         pDocHandler->setXNoteId(mnXNoteId);
      73             : 
      74             :         uno::Reference < xml::sax::XFastDocumentHandler > xDocumentHandler
      75         605 :             (pDocHandler);
      76             :         uno::Reference < xml::sax::XFastTokenHandler > xTokenHandler
      77         605 :             (mpStream->getFastTokenHandler(xContext));
      78             : 
      79         605 :         xParser->setFastDocumentHandler(xDocumentHandler);
      80         605 :         xParser->setTokenHandler(xTokenHandler);
      81             : 
      82             :         uno::Reference<io::XInputStream> xInputStream =
      83         605 :             pStream->getDocumentStream();
      84             : 
      85         605 :         if (xInputStream.is())
      86             :         {
      87         327 :             struct xml::sax::InputSource oInputSource;
      88         327 :             oInputSource.aInputStream = xInputStream;
      89         327 :             xParser->parseStream(oInputSource);
      90             : 
      91         327 :             xInputStream->closeInput();
      92         605 :         }
      93             :     }
      94             : 
      95         605 :     mpStream = savedStream;
      96         605 : }
      97             : 
      98          27 : void OOXMLDocumentImpl::resolveFastSubStreamWithId(Stream & rStream,
      99             :                                       writerfilter::Reference<Stream>::Pointer_t pStream,
     100             :                       sal_uInt32 nId)
     101             : {
     102          27 :     rStream.substream(nId, pStream);
     103          27 : }
     104             : 
     105         450 : void OOXMLDocumentImpl::setXNoteId(const sal_Int32 nId)
     106             : {
     107         450 :     mnXNoteId = nId;
     108         450 : }
     109             : 
     110           2 : sal_Int32 OOXMLDocumentImpl::getXNoteId() const
     111             : {
     112           2 :     return mnXNoteId;
     113             : }
     114             : 
     115           2 : void OOXMLDocumentImpl::setXNoteType(const Id & nId)
     116             : {
     117           2 :     mXNoteType = nId;
     118           2 : }
     119             : 
     120           0 : const Id & OOXMLDocumentImpl::getXNoteType() const
     121             : {
     122           0 :     return mXNoteType;
     123             : }
     124             : 
     125          36 : const OUString & OOXMLDocumentImpl::getTarget() const
     126             : {
     127          36 :     return mpStream->getTarget();
     128             : }
     129             : 
     130             : writerfilter::Reference<Stream>::Pointer_t
     131          25 : OOXMLDocumentImpl::getSubStream(const OUString & rId)
     132             : {
     133             :     OOXMLStream::Pointer_t pStream
     134          25 :         (OOXMLDocumentFactory::createStream(mpStream, rId));
     135             : 
     136             :     OOXMLDocumentImpl * pTemp;
     137          25 :     writerfilter::Reference<Stream>::Pointer_t pRet( pTemp = new OOXMLDocumentImpl(pStream) );
     138          25 :     pTemp->setModel(mxModel);
     139          25 :     pTemp->setDrawPage(mxDrawPage);
     140          25 :     pTemp->setIsSubstream( true );
     141          25 :     return pRet;
     142             : }
     143             : 
     144             : writerfilter::Reference<Stream>::Pointer_t
     145           2 : OOXMLDocumentImpl::getXNoteStream(OOXMLStream::StreamType_t nType, const Id & rType,
     146             :                                   const sal_Int32 nId)
     147             : {
     148             : #ifdef DEBUG_ELEMENT
     149             :     debug_logger->startElement("getXNoteStream");
     150             :     debug_logger->attribute("id", nId);
     151             :     debug_logger->endElement();
     152             : #endif
     153             : 
     154             :     OOXMLStream::Pointer_t pStream =
     155           2 :         (OOXMLDocumentFactory::createStream(mpStream, nType));
     156           2 :     OOXMLDocumentImpl * pDocument = new OOXMLDocumentImpl(pStream);
     157           2 :     pDocument->setXNoteId(nId);
     158           2 :     pDocument->setXNoteType(rType);
     159             : 
     160           2 :     return writerfilter::Reference<Stream>::Pointer_t(pDocument);
     161             : }
     162             : 
     163           0 : void OOXMLDocumentImpl::resolveFootnote(Stream & rStream,
     164             :                                         const Id & rType,
     165             :                                         const sal_Int32 nNoteId)
     166             : {
     167             :     writerfilter::Reference<Stream>::Pointer_t pStream =
     168           0 :         getXNoteStream(OOXMLStream::FOOTNOTES, rType, nNoteId);
     169             : 
     170             :     Id nId;
     171           0 :     switch (rType)
     172             :     {
     173             :     case NS_ooxml::LN_Value_wordprocessingml_ST_FtnEdn_separator:
     174             :     case NS_ooxml::LN_Value_wordprocessingml_ST_FtnEdn_continuationSeparator:
     175           0 :         nId = rType;
     176           0 :         break;
     177             :     default:
     178           0 :         nId = NS_rtf::LN_footnote;
     179           0 :         break;
     180             :     }
     181             : 
     182           0 :     resolveFastSubStreamWithId(rStream, pStream, nId);
     183           0 : }
     184             : 
     185           0 : void OOXMLDocumentImpl::resolveEndnote(Stream & rStream,
     186             :                                        const Id & rType,
     187             :                                        const sal_Int32 nNoteId)
     188             : {
     189             :     writerfilter::Reference<Stream>::Pointer_t pStream =
     190           0 :         getXNoteStream(OOXMLStream::ENDNOTES, rType, nNoteId);
     191             : 
     192             :     Id nId;
     193           0 :     switch (rType)
     194             :     {
     195             :     case NS_ooxml::LN_Value_wordprocessingml_ST_FtnEdn_separator:
     196             :     case NS_ooxml::LN_Value_wordprocessingml_ST_FtnEdn_continuationSeparator:
     197           0 :         nId = rType;
     198           0 :         break;
     199             :     default:
     200           0 :         nId = NS_rtf::LN_endnote;
     201           0 :         break;
     202             :     }
     203             : 
     204           0 :     resolveFastSubStreamWithId(rStream, pStream, nId);
     205           0 : }
     206             : 
     207           2 : void OOXMLDocumentImpl::resolveComment(Stream & rStream,
     208             :                                        const sal_Int32 nId)
     209             : {
     210             :     writerfilter::Reference<Stream>::Pointer_t pStream =
     211           2 :         getXNoteStream(OOXMLStream::COMMENTS, 0, nId);
     212             : 
     213           2 :     resolveFastSubStreamWithId(rStream, pStream, NS_rtf::LN_annotation);
     214           2 : }
     215             : 
     216           0 : OOXMLPropertySet * OOXMLDocumentImpl::getPicturePropSet
     217             : (const OUString & rId)
     218             : {
     219             :     OOXMLStream::Pointer_t pStream
     220           0 :         (OOXMLDocumentFactory::createStream(mpStream, rId));
     221             : 
     222             :     writerfilter::Reference<BinaryObj>::Pointer_t pPicture
     223           0 :         (new OOXMLBinaryObjectReference(pStream));
     224             : 
     225           0 :     OOXMLValue::Pointer_t pPayloadValue(new OOXMLBinaryValue(pPicture));
     226             : 
     227             :     OOXMLProperty::Pointer_t pPayloadProperty
     228             :         (new OOXMLPropertyImpl(NS_rtf::LN_payload, pPayloadValue,
     229           0 :                                OOXMLPropertyImpl::ATTRIBUTE));
     230             : 
     231           0 :     OOXMLPropertySet::Pointer_t pBlipSet(new OOXMLPropertySetImpl());
     232             : 
     233           0 :     pBlipSet->add(pPayloadProperty);
     234             : 
     235           0 :     OOXMLValue::Pointer_t pBlipValue(new OOXMLPropertySetValue(pBlipSet));
     236             : 
     237             :     OOXMLProperty::Pointer_t pBlipProperty
     238             :         (new OOXMLPropertyImpl(NS_rtf::LN_blip, pBlipValue,
     239           0 :                                OOXMLPropertyImpl::ATTRIBUTE));
     240             : 
     241           0 :     OOXMLPropertySet * pProps = new OOXMLPropertySetImpl();
     242             : 
     243           0 :     pProps->add(pBlipProperty);
     244             : 
     245           0 :     return pProps;
     246             : }
     247             : 
     248           0 : void OOXMLDocumentImpl::resolvePicture(Stream & rStream,
     249             :                                        const OUString & rId)
     250             : {
     251           0 :     OOXMLPropertySet * pProps = getPicturePropSet(rId);
     252             : 
     253           0 :     rStream.props(writerfilter::Reference<Properties>::Pointer_t(pProps));
     254           0 : }
     255             : 
     256           1 : OUString OOXMLDocumentImpl::getTargetForId(const OUString & rId)
     257             : {
     258           1 :     return mpStream->getTargetForId(rId);
     259             : }
     260             : 
     261          14 : void OOXMLDocumentImpl::resolveHeader(Stream & rStream,
     262             :                                       const sal_Int32 type,
     263             :                                       const OUString & rId)
     264             : {
     265             :      writerfilter::Reference<Stream>::Pointer_t pStream =
     266          14 :          getSubStream(rId);
     267          14 :      switch (type)
     268             :      {
     269             :      case NS_ooxml::LN_Value_ST_HrdFtr_even:
     270           2 :          resolveFastSubStreamWithId(rStream, pStream, NS_rtf::LN_headerl);
     271           2 :         break;
     272             :      case NS_ooxml::LN_Value_ST_HrdFtr_default: // here we assume that default is right, but not necessarily true :-(
     273           9 :          resolveFastSubStreamWithId(rStream, pStream, NS_rtf::LN_headerr);
     274           9 :          break;
     275             :      case NS_ooxml::LN_Value_ST_HrdFtr_first:
     276           3 :          resolveFastSubStreamWithId(rStream, pStream, NS_rtf::LN_headerf);
     277           3 :          break;
     278             :      default:
     279           0 :          break;
     280          14 :      }
     281          14 : }
     282             : 
     283          11 : void OOXMLDocumentImpl::resolveFooter(Stream & rStream,
     284             :                                       const sal_Int32 type,
     285             :                                       const OUString & rId)
     286             : {
     287             :      writerfilter::Reference<Stream>::Pointer_t pStream =
     288          11 :          getSubStream(rId);
     289             : 
     290          11 :      switch (type)
     291             :      {
     292             :      case NS_ooxml::LN_Value_ST_HrdFtr_even:
     293           3 :          resolveFastSubStreamWithId(rStream, pStream, NS_rtf::LN_footerl);
     294           3 :          break;
     295             :      case NS_ooxml::LN_Value_ST_HrdFtr_default: // here we assume that default is right, but not necessarily true :-(
     296           5 :          resolveFastSubStreamWithId(rStream, pStream, NS_rtf::LN_footerr);
     297           5 :          break;
     298             :      case NS_ooxml::LN_Value_ST_HrdFtr_first:
     299           3 :          resolveFastSubStreamWithId(rStream, pStream, NS_rtf::LN_footerf);
     300           3 :          break;
     301             :      default:
     302           0 :          break;
     303          11 :      }
     304          11 : }
     305             : 
     306         121 : void OOXMLDocumentImpl::resolve(Stream & rStream)
     307             : {
     308             : #ifdef DEBUG_RESOLVE
     309             :     debug_logger->startElement("OOXMLDocumentImpl.resolve");
     310             : #endif
     311             : 
     312             :     uno::Reference< xml::sax::XFastParser > xParser
     313         121 :         (mpStream->getFastParser());
     314             : 
     315         121 :     if (xParser.is())
     316             :     {
     317         121 :         uno::Reference<uno::XComponentContext> xContext(mpStream->getContext());
     318             : 
     319             :         OOXMLFastDocumentHandler * pDocHandler =
     320         121 :             new OOXMLFastDocumentHandler(xContext);
     321         121 :         pDocHandler->setStream(&rStream);
     322         121 :         pDocHandler->setDocument(this);
     323         121 :         pDocHandler->setXNoteId(mnXNoteId);
     324         121 :         pDocHandler->setIsSubstream( mbIsSubstream );
     325             :         uno::Reference < xml::sax::XFastDocumentHandler > xDocumentHandler
     326         121 :             (pDocHandler);
     327             :         uno::Reference < xml::sax::XFastTokenHandler > xTokenHandler
     328         121 :             (mpStream->getFastTokenHandler(xContext));
     329             : 
     330         121 :         resolveFastSubStream(rStream, OOXMLStream::SETTINGS);
     331         121 :         resolveFastSubStream(rStream, OOXMLStream::THEME);
     332         121 :         resolveFastSubStream(rStream, OOXMLStream::FONTTABLE);
     333         121 :         resolveFastSubStream(rStream, OOXMLStream::STYLES);
     334         121 :         resolveFastSubStream(rStream, OOXMLStream::NUMBERING);
     335             : 
     336         121 :         xParser->setFastDocumentHandler( xDocumentHandler );
     337         121 :         xParser->setTokenHandler( xTokenHandler );
     338             : 
     339         121 :         xml::sax::InputSource aParserInput;
     340         121 :         aParserInput.aInputStream = mpStream->getDocumentStream();
     341             :         try
     342             :         {
     343         121 :             xParser->parseStream(aParserInput);
     344             :         }
     345           1 :         catch (...) {
     346             : #ifdef DEBUG_ELEMENT
     347             :             debug_logger->element("exception");
     348             : #endif
     349         121 :         }
     350         121 :     }
     351             : 
     352             : #ifdef DEBUG_RESOLVE
     353             :     debug_logger->endElement();
     354             : #endif
     355         121 : }
     356             : 
     357           1 : uno::Reference<io::XInputStream> OOXMLDocumentImpl::getInputStreamForId(const OUString & rId)
     358             : {
     359           1 :     OOXMLStream::Pointer_t pStream(OOXMLDocumentFactory::createStream(mpStream, rId));
     360             : 
     361           1 :     return pStream->getDocumentStream();
     362             : }
     363             : 
     364           0 : string OOXMLDocumentImpl::getType() const
     365             : {
     366           0 :     return "OOXMLDocumentImpl";
     367             : }
     368             : 
     369         119 : void OOXMLDocumentImpl::setModel(uno::Reference<frame::XModel> xModel)
     370             : {
     371         119 :     mxModel.set(xModel);
     372         119 : }
     373             : 
     374          36 : uno::Reference<frame::XModel> OOXMLDocumentImpl::getModel()
     375             : {
     376          36 :     return mxModel;
     377             : }
     378             : 
     379         119 : void OOXMLDocumentImpl::setDrawPage(uno::Reference<drawing::XDrawPage> xDrawPage)
     380             : {
     381         119 :     mxDrawPage.set(xDrawPage);
     382         119 : }
     383             : 
     384          36 : uno::Reference<drawing::XDrawPage> OOXMLDocumentImpl::getDrawPage()
     385             : {
     386          36 :     return mxDrawPage;
     387             : }
     388             : 
     389           0 : uno::Reference<io::XInputStream> OOXMLDocumentImpl::getInputStream()
     390             : {
     391           0 :     return mpStream->getDocumentStream();
     392             : }
     393             : 
     394          36 : uno::Reference<io::XInputStream> OOXMLDocumentImpl::getStorageStream()
     395             : {
     396          36 :     return mpStream->getStorageStream();
     397             : }
     398             : 
     399          21 : void OOXMLDocumentImpl::setShapeContext( uno::Reference<xml::sax::XFastShapeContextHandler> xContext )
     400             : {
     401          21 :     mxShapeContext = xContext;
     402          21 : }
     403             : 
     404          36 : uno::Reference<xml::sax::XFastShapeContextHandler> OOXMLDocumentImpl::getShapeContext( )
     405             : {
     406          36 :     return mxShapeContext;
     407             : }
     408             : 
     409             : OOXMLDocument *
     410          94 : OOXMLDocumentFactory::createDocument
     411             : (OOXMLStream::Pointer_t pStream)
     412             : {
     413          94 :     return new OOXMLDocumentImpl(pStream);
     414             : }
     415             : 
     416          15 : }}
     417             : 
     418             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10