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>
34 namespace sax_fastparser
{
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
;
51 explicit FastSaxSerializer(const css::uno::Reference
< css::io::XOutputStream
>& xOutputStream
);
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.
62 /** called by the parser after the last XML element of a stream is processed.
66 /** receives notification of the beginning of an 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
79 Contains a <type>FastAttributeList</type> to access the attributes
83 void startFastElement( ::sal_Int32 Element
, FastAttributeList
* pAttrList
= nullptr );
85 /** receives notification of the end of an known element.
88 void endFastElement( ::sal_Int32 Element
);
90 /** receives notification of the beginning of a single 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
103 Contains a <type>FastAttributeList</type> to access the attributes
107 void singleFastElement( ::sal_Int32 Element
, FastAttributeList
* pAttrList
= nullptr );
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 );
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,
125 p, r, t, [text], /t, rPr, [something], /rPr, /r, /p,
127 p, r, mark(), t, [text], /t, mark(), rPr, [something], /rPr,
128 mergeTopMarks( MergeMarks::PREPEND ), mergeTopMarks( MergeMarks::APPEND ), /r, /p
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
142 Writes the result to the output stream if the mark stack becomes empty
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
152 void mergeTopMarks(sal_Int32 nTag
,
153 sax_fastparser::MergeMarks eMergeType
= sax_fastparser::MergeMarks::APPEND
);
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
166 Int8Sequence maPostponed
;
169 sal_Int32
const m_Tag
;
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
;
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();
188 virtual void prepend( const Int8Sequence
&rWhat
);
189 virtual void append( const Int8Sequence
&rWhat
) override
;
190 void postpone( const Int8Sequence
&rWhat
);
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
;
205 ForSort(sal_Int32
const nTag
, const Int32Sequence
& rOrder
)
207 , mnCurrentElement( 0 )
211 void setCurrentElement( ::sal_Int32 nToken
) override
;
213 virtual Int8Sequence
& getData() override
;
215 #if OSL_DEBUG_LEVEL > 0
216 virtual void print() override
;
219 virtual void prepend( const Int8Sequence
&rWhat
) override
;
220 virtual void append( const Int8Sequence
&rWhat
) override
;
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
;
234 std::stack
<sal_Int32
> m_DebugStartedElements
;
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
252 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */