Version 5.2.6.1, tag libreoffice-5.2.6.1
[LibreOffice.git] / sax / source / tools / fastserializer.hxx
blobe73b4021d1cfa81c4fe8c459155e312f83c5bd7d
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 <memory>
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 css::uno::Sequence< ::sal_Int8 > Int8Sequence;
48 typedef css::uno::Sequence< ::sal_Int32 > Int32Sequence;
50 public:
51 explicit FastSaxSerializer(const css::uno::Reference< css::io::XOutputStream >& xOutputStream);
52 ~FastSaxSerializer();
54 css::uno::Reference< css::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 = nullptr );
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 = nullptr );
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( MergeMarks::PREPEND ), mergeTopMarks( MergeMarks::APPEND ), /r, /p
129 and you are done.
131 @param nTag debugging aid to ensure mark and merge match in LIFO order
133 void mark(sal_Int32 nTag, const Int32Sequence& rOrder = Int32Sequence());
135 /** Merge 2 topmost marks.
137 The possibilities: prepend the top before the second top-most
138 mark, append it, append it later or ignore; prepending brings the possibility
139 to switch parts of the output, appending later allows to write some
140 output in advance.
142 Writes the result to the output stream if the mark stack becomes empty
143 by the operation.
145 When the MergeMarks::POSTPONE is specified, the merge happens just
146 before the next merge.
148 @param nTag debugging aid to ensure mark and merge match in LIFO order
150 @see mark()
152 void mergeTopMarks(sal_Int32 nTag,
153 sax_fastparser::MergeMarks eMergeType = sax_fastparser::MergeMarks::APPEND);
155 private:
156 /** Helper class to cache data and write in chunks to XOutputStream or ForMerge::append.
157 * Its flush method needs to be called before touching maMarkStack
158 * to ensure correct order of ForSort methods.
160 CachedOutputStream maCachedOutputStream;
161 css::uno::Reference< css::xml::sax::XFastTokenHandler > mxFastTokenHandler;
163 class ForMerge : public ForMergeBase
165 Int8Sequence maData;
166 Int8Sequence maPostponed;
168 public:
169 sal_Int32 const m_Tag;
170 #ifdef DBG_UTIL
171 // pending close tags, followed by pending open tags
172 std::deque<sal_Int32> m_DebugEndedElements;
173 std::deque<sal_Int32> m_DebugStartedElements;
174 // ... and another buffer for maPostponed ...
175 std::deque<sal_Int32> m_DebugPostponedEndedElements;
176 std::deque<sal_Int32> m_DebugPostponedStartedElements;
177 #endif
179 explicit ForMerge(sal_Int32 const nTag) : m_Tag(nTag) {}
180 virtual ~ForMerge() {}
182 virtual void setCurrentElement( ::sal_Int32 /*nToken*/ ) {}
183 virtual Int8Sequence& getData();
184 #if OSL_DEBUG_LEVEL > 0
185 virtual void print();
186 #endif
188 virtual void prepend( const Int8Sequence &rWhat );
189 virtual void append( const Int8Sequence &rWhat ) override;
190 void postpone( const Int8Sequence &rWhat );
192 protected:
193 void resetData( );
194 static void merge( Int8Sequence &rTop, const Int8Sequence &rMerge, bool bAppend );
197 class ForSort : public ForMerge
199 std::map< ::sal_Int32, Int8Sequence > maData;
200 sal_Int32 mnCurrentElement;
202 Int32Sequence maOrder;
204 public:
205 ForSort(sal_Int32 const nTag, const Int32Sequence& rOrder)
206 : ForMerge(nTag)
207 , mnCurrentElement( 0 )
208 , maOrder( rOrder )
211 void setCurrentElement( ::sal_Int32 nToken ) override;
213 virtual Int8Sequence& getData() override;
215 #if OSL_DEBUG_LEVEL > 0
216 virtual void print() override;
217 #endif
219 virtual void prepend( const Int8Sequence &rWhat ) override;
220 virtual void append( const Int8Sequence &rWhat ) override;
221 private:
222 void sort();
225 std::stack< std::shared_ptr< ForMerge > > maMarkStack;
226 bool mbMarkStackEmpty;
227 // Would be better to use OStringBuffer instead of these two
228 // but then we couldn't get the rtl_String* member :-(
229 rtl_String *mpDoubleStr;
230 sal_Int32 mnDoubleStrCapacity;
231 TokenValueList maTokenValues;
233 #ifdef DBG_UTIL
234 std::stack<sal_Int32> m_DebugStartedElements;
235 #endif
237 void writeTokenValueList();
238 void writeFastAttributeList(FastAttributeList& rAttrList);
240 /** Forward the call to the output stream, or write to the stack.
242 The latter in the case that we are inside a mark().
244 void writeBytes( const css::uno::Sequence< ::sal_Int8 >& aData );
245 void writeBytes( const char* pStr, size_t nLen );
248 } // namespace sax_fastparser
250 #endif
252 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */