1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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"
31 #include <string_view>
35 namespace sax_fastparser
{
41 TokenValue(sal_Int32 _nToken
, const char *_pValue
) : nToken(_nToken
), pValue(_pValue
) {}
43 typedef std::vector
<TokenValue
> TokenValueList
;
45 /// Receives notification of sax document events to write into an XOutputStream.
46 class FastSaxSerializer
48 typedef css::uno::Sequence
< ::sal_Int8
> Int8Sequence
;
49 typedef css::uno::Sequence
< ::sal_Int32
> Int32Sequence
;
52 explicit FastSaxSerializer(const css::uno::Reference
< css::io::XOutputStream
>& xOutputStream
);
55 css::uno::Reference
< css::io::XOutputStream
> const & getOutputStream() const;
56 /// called by FSHelper to put data in for writeTokenValueList
57 TokenValueList
& getTokenValueList() { return maTokenValues
; }
59 /** called by the parser when parsing of an XML stream is started.
63 /** called by the parser after the last XML element of a stream is processed.
67 /** receives notification of the beginning of an element.
70 contains the integer token from the <type>XFastTokenHandler</type>
71 registered at the <type>XFastParser</type>.<br>
73 If the element has a namespace that was registered with the
74 <type>XFastParser</type>, <param>Element</param> contains the integer
75 token of the elements local name from the <type>XFastTokenHandler</type>
76 and the integer token of the namespace combined with an arithmetic
80 Contains a <type>FastAttributeList</type> to access the attributes
84 void startFastElement( ::sal_Int32 Element
, FastAttributeList
const * pAttrList
= nullptr );
86 /** receives notification of the end of a known element.
89 void endFastElement( ::sal_Int32 Element
);
91 /** receives notification of the beginning of a single element.
94 contains the integer token from the <type>XFastTokenHandler</type>
95 registered at the <type>XFastParser</type>.<br>
97 If the element has a namespace that was registered with the
98 <type>XFastParser</type>, <param>Element</param> contains the integer
99 token of the elements local name from the <type>XFastTokenHandler</type>
100 and the integer token of the namespace combined with an arithmetic
104 Contains a <type>FastAttributeList</type> to access the attributes
108 void singleFastElement( ::sal_Int32 Element
, FastAttributeList
const * pAttrList
= nullptr );
111 void writeId( ::sal_Int32 Element
);
112 OString
getId( ::sal_Int32 Element
);
114 void write( double value
);
115 void write( std::u16string_view s
, bool bEscape
= false );
116 void write( std::string_view s
, bool bEscape
= false );
117 void write( const char* pStr
, sal_Int32 nLen
, bool bEscape
= false );
119 // strings with _xHHHH_ are escaped with _x005F unless this is disabled
120 void setAllowXEscape(bool bSet
) { mbXescape
= bSet
; }
123 /** From now on, don't write directly to the stream, but to top of a stack.
125 This is to be able to change the order of the data being written.
126 If you need to write eg.
127 p, r, rPr, [something], /rPr, t, [text], /t, /r, /p,
129 p, r, t, [text], /t, rPr, [something], /rPr, /r, /p,
131 p, r, mark(), t, [text], /t, mark(), rPr, [something], /rPr,
132 mergeTopMarks( MergeMarks::PREPEND ), mergeTopMarks( MergeMarks::APPEND ), /r, /p
135 @param nTag debugging aid to ensure mark and merge match in LIFO order
137 void mark(sal_Int32 nTag
, const Int32Sequence
& rOrder
);
139 /** Merge 2 topmost marks.
141 The possibilities: prepend the top before the second top-most
142 mark, append it, append it later or ignore; prepending brings the possibility
143 to switch parts of the output, appending later allows to write some
146 Writes the result to the output stream if the mark stack becomes empty
149 When the MergeMarks::POSTPONE is specified, the merge happens just
150 before the next merge.
152 @param nTag debugging aid to ensure mark and merge match in LIFO order
156 void mergeTopMarks(sal_Int32 nTag
,
157 sax_fastparser::MergeMarks eMergeType
);
160 /** Helper class to cache data and write in chunks to XOutputStream or ForMerge::append.
161 * Its flush method needs to be called before touching maMarkStack
162 * to ensure correct order of ForSort methods.
164 CachedOutputStream maCachedOutputStream
;
165 css::uno::Reference
< css::xml::sax::XFastTokenHandler
> mxFastTokenHandler
;
167 class ForMerge
: public ForMergeBase
170 Int8Sequence maPostponed
;
173 sal_Int32
const m_Tag
;
175 // pending close tags, followed by pending open tags
176 std::deque
<sal_Int32
> m_DebugEndedElements
;
177 std::deque
<sal_Int32
> m_DebugStartedElements
;
178 // ... and another buffer for maPostponed ...
179 std::deque
<sal_Int32
> m_DebugPostponedEndedElements
;
180 std::deque
<sal_Int32
> m_DebugPostponedStartedElements
;
183 explicit ForMerge(sal_Int32
const nTag
) : m_Tag(nTag
) {}
185 virtual void setCurrentElement( ::sal_Int32
/*nToken*/ ) {}
186 virtual Int8Sequence
& getData();
187 #if OSL_DEBUG_LEVEL > 0
188 virtual void print();
191 virtual void prepend( const Int8Sequence
&rWhat
);
192 virtual void append( const css::uno::Sequence
<sal_Int8
> &rWhat
) override
;
193 void postpone( const Int8Sequence
&rWhat
);
197 static void merge( Int8Sequence
&rTop
, const Int8Sequence
&rMerge
, bool bAppend
);
200 class ForSort
: public ForMerge
202 std::map
< ::sal_Int32
, Int8Sequence
> maData
;
203 sal_Int32 mnCurrentElement
;
205 Int32Sequence maOrder
;
208 ForSort(sal_Int32
const nTag
, const Int32Sequence
& rOrder
)
210 , mnCurrentElement( 0 )
214 void setCurrentElement( ::sal_Int32 nToken
) override
;
216 virtual Int8Sequence
& getData() override
;
218 #if OSL_DEBUG_LEVEL > 0
219 virtual void print() override
;
222 virtual void prepend( const Int8Sequence
&rWhat
) override
;
223 virtual void append( const css::uno::Sequence
<sal_Int8
> &rWhat
) override
;
228 std::stack
< std::shared_ptr
< ForMerge
> > maMarkStack
;
229 bool mbMarkStackEmpty
;
230 // Would be better to use OStringBuffer instead of these two
231 // but then we couldn't get the rtl_String* member :-(
232 rtl_String
*mpDoubleStr
;
233 sal_Int32 mnDoubleStrCapacity
;
234 TokenValueList maTokenValues
;
235 bool mbXescape
; ///< whether to escape invalid XML characters as _xHHHH_ in write(const char*,sal_Int32,true)
239 std::stack
<sal_Int32
> m_DebugStartedElements
;
242 void writeTokenValueList();
243 void writeFastAttributeList(FastAttributeList
const & rAttrList
);
245 /** Forward the call to the output stream, or write to the stack.
247 The latter in the case that we are inside a mark().
249 void writeBytes( const css::uno::Sequence
< ::sal_Int8
>& aData
);
250 void writeBytes( const char* pStr
, size_t nLen
);
253 } // namespace sax_fastparser
257 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */