Bump version to 5.0-14
[LibreOffice.git] / sax / source / tools / fastserializer.hxx
blob95e631c355d2b42a04050c52494ed478b369aa39
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #ifndef INCLUDED_SAX_SOURCE_TOOLS_FASTSERIALIZER_HXX
21 #define INCLUDED_SAX_SOURCE_TOOLS_FASTSERIALIZER_HXX
23 #include <com/sun/star/xml/sax/XFastTokenHandler.hpp>
24 #include <com/sun/star/io/XOutputStream.hpp>
26 #include <sax/fastattribs.hxx>
27 #include <sax/fshelper.hxx>
28 #include <CachedOutputStream.hxx>
30 #include <stack>
31 #include <map>
32 #include <boost/shared_ptr.hpp>
34 namespace sax_fastparser {
36 struct TokenValue
38 sal_Int32 nToken;
39 const char *pValue;
40 TokenValue(sal_Int32 _nToken, const char *_pValue) : nToken(_nToken), pValue(_pValue) {}
42 typedef std::vector<TokenValue> TokenValueList;
44 /// Receives notification of sax document events to write into an XOutputStream.
45 class FastSaxSerializer
47 typedef ::com::sun::star::uno::Sequence< ::sal_Int8 > Int8Sequence;
48 typedef ::com::sun::star::uno::Sequence< ::sal_Int32 > Int32Sequence;
50 public:
51 FastSaxSerializer( const css::uno::Reference< css::io::XOutputStream >& xOutputStream );
52 ~FastSaxSerializer();
54 ::com::sun::star::uno::Reference< ::com::sun::star::io::XOutputStream > getOutputStream();
55 /// called by FSHelper to put data in for writeTokenValueList
56 TokenValueList& getTokenValueList() { return maTokenValues; }
58 /** called by the parser when parsing of an XML stream is started.
60 void startDocument();
62 /** called by the parser after the last XML element of a stream is processed.
64 void endDocument();
66 /** receives notification of the beginning of an element.
68 @param Element
69 contains the integer token from the <type>XFastTokenHandler</type>
70 registered at the <type>XFastParser</type>.<br>
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.
78 @param pAttrList
79 Contains a <type>FastAttributeList</type> to access the attributes
80 from the element.
83 void startFastElement( ::sal_Int32 Element, FastAttributeList* pAttrList = NULL );
85 /** receives notification of the end of an known element.
86 @see startFastElement
88 void endFastElement( ::sal_Int32 Element );
90 /** receives notification of the beginning of a single element.
92 @param Element
93 contains the integer token from the <type>XFastTokenHandler</type>
94 registered at the <type>XFastParser</type>.<br>
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.
102 @param pAttrList
103 Contains a <type>FastAttributeList</type> to access the attributes
104 from the element.
107 void singleFastElement( ::sal_Int32 Element, FastAttributeList* pAttrList = NULL );
109 // C++ helpers
110 void writeId( ::sal_Int32 Element );
111 OString getId( ::sal_Int32 Element );
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 );
118 public:
119 /** From now on, don't write directly to the stream, but to top of a stack.
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.
131 void mark( const Int32Sequence& aOrder = Int32Sequence() );
133 /** Merge 2 topmost marks.
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.
140 Writes the result to the output stream if the mark stack becomes empty
141 by the operation.
143 When the MERGE_MARKS_POSTPONE is specified, the merge happens just
144 before the next merge.
146 @see mark()
148 void mergeTopMarks( sax_fastparser::MergeMarksEnum eMergeType = sax_fastparser::MERGE_MARKS_APPEND );
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.
155 CachedOutputStream maCachedOutputStream;
156 ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XFastTokenHandler > mxFastTokenHandler;
158 class ForMerge : public ForMergeBase
160 Int8Sequence maData;
161 Int8Sequence maPostponed;
163 public:
164 ForMerge() : maData(), maPostponed() {}
165 virtual ~ForMerge() {}
167 virtual void setCurrentElement( ::sal_Int32 /*nToken*/ ) {}
168 virtual Int8Sequence& getData();
169 #if OSL_DEBUG_LEVEL > 0
170 virtual void print();
171 #endif
173 virtual void prepend( const Int8Sequence &rWhat );
174 virtual void append( const Int8Sequence &rWhat ) SAL_OVERRIDE;
175 void postpone( const Int8Sequence &rWhat );
177 protected:
178 void resetData( );
179 static void merge( Int8Sequence &rTop, const Int8Sequence &rMerge, bool bAppend );
182 class ForSort : public ForMerge
184 std::map< ::sal_Int32, Int8Sequence > maData;
185 sal_Int32 mnCurrentElement;
187 Int32Sequence maOrder;
189 public:
190 ForSort( const Int32Sequence& aOrder ) :
191 ForMerge(),
192 maData(),
193 mnCurrentElement( 0 ),
194 maOrder( aOrder ) {}
196 void setCurrentElement( ::sal_Int32 nToken ) SAL_OVERRIDE;
198 virtual Int8Sequence& getData() SAL_OVERRIDE;
200 #if OSL_DEBUG_LEVEL > 0
201 virtual void print() SAL_OVERRIDE;
202 #endif
204 virtual void prepend( const Int8Sequence &rWhat ) SAL_OVERRIDE;
205 virtual void append( const Int8Sequence &rWhat ) SAL_OVERRIDE;
206 private:
207 void sort();
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;
218 #ifdef DBG_UTIL
219 ::std::stack<sal_Int32> m_DebugStartedElements;
220 #endif
222 void writeTokenValueList();
223 void writeFastAttributeList(FastAttributeList& rAttrList);
225 /** Forward the call to the output stream, or write to the stack.
227 The latter in the case that we are inside a mark().
229 void writeBytes( const ::com::sun::star::uno::Sequence< ::sal_Int8 >& aData );
230 void writeBytes( const char* pStr, size_t nLen );
233 } // namespace sax_fastparser
235 #endif
237 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */