LCOV - code coverage report
Current view: top level - sax/source/tools - fastserializer.hxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 8 8 100.0 %
Date: 2014-11-03 Functions: 9 9 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             : #ifndef INCLUDED_SAX_SOURCE_TOOLS_FASTSERIALIZER_HXX
      21             : #define INCLUDED_SAX_SOURCE_TOOLS_FASTSERIALIZER_HXX
      22             : 
      23             : #include <com/sun/star/xml/sax/XFastTokenHandler.hpp>
      24             : #include <com/sun/star/io/XOutputStream.hpp>
      25             : 
      26             : #include <sax/fastattribs.hxx>
      27             : #include <sax/fshelper.hxx>
      28             : #include <CachedOutputStream.hxx>
      29             : 
      30             : #include <stack>
      31             : #include <map>
      32             : #include <boost/shared_ptr.hpp>
      33             : 
      34             : namespace sax_fastparser {
      35             : 
      36             : struct TokenValue
      37             : {
      38             :     sal_Int32   nToken;
      39             :     const char *pValue;
      40      382462 :     TokenValue(sal_Int32 _nToken, const char *_pValue) : nToken(_nToken), pValue(_pValue) {}
      41             : };
      42             : typedef std::vector<TokenValue> TokenValueList;
      43             : 
      44             : /// Receives notification of sax document events to write into an XOutputStream.
      45             : class FastSaxSerializer
      46             : {
      47             :     typedef ::com::sun::star::uno::Sequence< ::sal_Int8 > Int8Sequence;
      48             :     typedef ::com::sun::star::uno::Sequence< ::sal_Int32 > Int32Sequence;
      49             : 
      50             : public:
      51             :     FastSaxSerializer( const css::uno::Reference< css::io::XOutputStream >& xOutputStream );
      52             :     ~FastSaxSerializer();
      53             : 
      54             :     ::com::sun::star::uno::Reference< ::com::sun::star::io::XOutputStream > getOutputStream();
      55             :     /// called by FSHelper to put data in for writeTokenValueList
      56      585404 :     TokenValueList& getTokenValueList() { return maTokenValues; }
      57             : 
      58             :     /** called by the parser when parsing of an XML stream is started.
      59             :      */
      60             :     void startDocument();
      61             : 
      62             :     /** called by the parser after the last XML element of a stream is processed.
      63             :      */
      64             :     void endDocument();
      65             : 
      66             :     /** receives notification of the beginning of an element.
      67             : 
      68             :         @param Element
      69             :             contains the integer token from the <type>XFastTokenHandler</type>
      70             :             registered at the <type>XFastParser</type>.<br>
      71             : 
      72             :             If the element has a namespace that was registered with the
      73             :             <type>XFastParser</type>, <param>Element</param> contains the integer
      74             :             token of the elements local name from the <type>XFastTokenHandler</type>
      75             :             and the integer token of the namespace combined with an arithmetic
      76             :             <b>or</b> operation.
      77             : 
      78             :         @param pAttrList
      79             :             Contains a <type>FastAttributeList</type> to access the attributes
      80             :             from the element.
      81             : 
      82             :     */
      83             :     void startFastElement( ::sal_Int32 Element, FastAttributeList* pAttrList = NULL );
      84             : 
      85             :     /** receives notification of the end of an known element.
      86             :         @see startFastElement
      87             :      */
      88             :     void endFastElement( ::sal_Int32 Element );
      89             : 
      90             :     /** receives notification of the beginning of a single element.
      91             : 
      92             :         @param Element
      93             :             contains the integer token from the <type>XFastTokenHandler</type>
      94             :             registered at the <type>XFastParser</type>.<br>
      95             : 
      96             :             If the element has a namespace that was registered with the
      97             :             <type>XFastParser</type>, <param>Element</param> contains the integer
      98             :             token of the elements local name from the <type>XFastTokenHandler</type>
      99             :             and the integer token of the namespace combined with an arithmetic
     100             :             <b>or</b> operation.
     101             : 
     102             :         @param pAttrList
     103             :             Contains a <type>FastAttributeList</type> to access the attributes
     104             :             from the element.
     105             : 
     106             :     */
     107             :     void singleFastElement( ::sal_Int32 Element, FastAttributeList* pAttrList = NULL );
     108             : 
     109             :     // C++ helpers
     110             :     void writeId( ::sal_Int32 Element );
     111             :     OString getId( ::sal_Int32 Element );
     112             : 
     113             :     void write( double value );
     114             :     void write( const OUString& s, bool bEscape = false );
     115             :     void write( const OString& s, bool bEscape = false );
     116             :     void write( const char* pStr, sal_Int32 nLen, bool bEscape = false );
     117             : 
     118             : public:
     119             :     /** From now on, don't write directly to the stream, but to top of a stack.
     120             : 
     121             :         This is to be able to change the order of the data being written.
     122             :         If you need to write eg.
     123             :           p, r, rPr, [something], /rPr, t, [text], /t, /r, /p,
     124             :         but get it in order
     125             :           p, r, t, [text], /t, rPr, [something], /rPr, /r, /p,
     126             :         simply do
     127             :           p, r, mark(), t, [text], /t, mark(), rPr, [something], /rPr,
     128             :           mergeTopMarks( MERGE_MARKS_PREPEND ), mergeTopMarks( MERGE_MARKS_APPEND ), /r, /p
     129             :         and you are done.
     130             :      */
     131             :     void mark( const Int32Sequence& aOrder = Int32Sequence() );
     132             : 
     133             :     /** Merge 2 topmost marks.
     134             : 
     135             :         The possibilities: prepend the top before the second top-most
     136             :         mark, append it, append it later or ignore; prepending brings the possibility
     137             :         to switch parts of the output, appending later allows to write some
     138             :         output in advance.
     139             : 
     140             :         Writes the result to the output stream if the mark stack becomes empty
     141             :         by the operation.
     142             : 
     143             :         When the MERGE_MARKS_POSTPONE is specified, the merge happens just
     144             :         before the next merge.
     145             : 
     146             :         @see mark()
     147             :      */
     148             :     void mergeTopMarks( sax_fastparser::MergeMarksEnum eMergeType = sax_fastparser::MERGE_MARKS_APPEND );
     149             : 
     150             : private:
     151             :     /** Helper class to cache data and write in chunks to XOutputStream or ForMerge::append.
     152             :      *  Its flush method needs to be called before touching maMarkStack
     153             :      *  to ensure correct order of ForSort methods.
     154             :      */
     155             :     CachedOutputStream maCachedOutputStream;
     156             :     ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XFastTokenHandler > mxFastTokenHandler;
     157             : 
     158             :     class ForMerge : public ForMergeBase
     159             :     {
     160             :         Int8Sequence maData;
     161             :         Int8Sequence maPostponed;
     162             : 
     163             :     public:
     164      213072 :         ForMerge() : maData(), maPostponed() {}
     165      347122 :         virtual ~ForMerge() {}
     166             : 
     167      168468 :         virtual void setCurrentElement( ::sal_Int32 /*nToken*/ ) {}
     168             :         virtual Int8Sequence& getData();
     169             : #if OSL_DEBUG_LEVEL > 0
     170             :         virtual void print();
     171             : #endif
     172             : 
     173             :         virtual void prepend( const Int8Sequence &rWhat );
     174             :         virtual void append( const Int8Sequence &rWhat ) SAL_OVERRIDE;
     175             :         void postpone( const Int8Sequence &rWhat );
     176             : 
     177             :     protected:
     178             :         void resetData( );
     179             :         static void merge( Int8Sequence &rTop, const Int8Sequence &rMerge, bool bAppend );
     180             :     };
     181             : 
     182      158044 :     class ForSort : public ForMerge
     183             :     {
     184             :         std::map< ::sal_Int32, Int8Sequence > maData;
     185             :         sal_Int32 mnCurrentElement;
     186             : 
     187             :         Int32Sequence maOrder;
     188             : 
     189             :     public:
     190       79022 :         ForSort( const Int32Sequence& aOrder ) :
     191             :             ForMerge(),
     192             :             maData(),
     193             :             mnCurrentElement( 0 ),
     194       79022 :             maOrder( aOrder ) {}
     195             : 
     196             :         void setCurrentElement( ::sal_Int32 nToken ) SAL_OVERRIDE;
     197             : 
     198             :         virtual Int8Sequence& getData() SAL_OVERRIDE;
     199             : 
     200             : #if OSL_DEBUG_LEVEL > 0
     201             :         virtual void print() SAL_OVERRIDE;
     202             : #endif
     203             : 
     204             :         virtual void prepend( const Int8Sequence &rWhat ) SAL_OVERRIDE;
     205             :         virtual void append( const Int8Sequence &rWhat ) SAL_OVERRIDE;
     206             :     private:
     207             :         void sort();
     208             :     };
     209             : 
     210             :     ::std::stack< boost::shared_ptr< ForMerge > > maMarkStack;
     211             :     bool mbMarkStackEmpty;
     212             :     // Would be better to use OStringBuffer instead of these two
     213             :     // but then we couldn't get the rtl_String* member :-(
     214             :     rtl_String *mpDoubleStr;
     215             :     sal_Int32 mnDoubleStrCapacity;
     216             :     TokenValueList maTokenValues;
     217             : 
     218             : #ifdef DBG_UTIL
     219             :     ::std::stack<sal_Int32> m_DebugStartedElements;
     220             : #endif
     221             : 
     222             :     void writeTokenValueList();
     223             :     void writeFastAttributeList(FastAttributeList& rAttrList);
     224             : 
     225             :     /** Forward the call to the output stream, or write to the stack.
     226             : 
     227             :         The latter in the case that we are inside a mark().
     228             :      */
     229             :     void writeBytes( const ::com::sun::star::uno::Sequence< ::sal_Int8 >& aData );
     230             :     void writeBytes( const char* pStr, size_t nLen );
     231             : };
     232             : 
     233             : } // namespace sax_fastparser
     234             : 
     235             : #endif
     236             : 
     237             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10