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_FSHELPER_HXX
21 #define INCLUDED_SAX_FSHELPER_HXX
23 #include <com/sun/star/io/XOutputStream.hpp>
24 #include <sax/fastattribs.hxx>
28 #define FSNS(namespc, element) ((namespc << 16) | element)
29 // Backwards compatibility for code that used FSEND to terminate the vararg.
30 // As soon as no supported LO version has the varargs code, this can be removed entirely
31 // (otherwise backports might break silently if people didn't add FSEND).
32 // Ctor is there to get an error when trying to pass it to a vararg by accident.
33 struct FSEND_t
{ FSEND_t() {}; };
34 static const FSEND_t FSEND
= FSEND_t();
35 const sal_Int32 FSEND_internal
= -1; // same as XML_TOKEN_INVALID
37 #define SAX_ARGS_ARG( arg1, arg2, convert, num ) arg1##num, arg2##num convert
38 #define SAX_ARGS_ARG1( arg1, arg2, convert ) SAX_ARGS_ARG( arg1, arg2, convert, 1 )
39 #define SAX_ARGS_ARG2( arg1, arg2, convert ) SAX_ARGS_ARG1( arg1, arg2, convert ), SAX_ARGS_ARG( arg1, arg2, convert, 2 )
40 #define SAX_ARGS_ARG3( arg1, arg2, convert ) SAX_ARGS_ARG2( arg1, arg2, convert ), SAX_ARGS_ARG( arg1, arg2, convert, 3 )
41 #define SAX_ARGS_ARG4( arg1, arg2, convert ) SAX_ARGS_ARG3( arg1, arg2, convert ), SAX_ARGS_ARG( arg1, arg2, convert, 4 )
42 #define SAX_ARGS_ARG5( arg1, arg2, convert ) SAX_ARGS_ARG4( arg1, arg2, convert ), SAX_ARGS_ARG( arg1, arg2, convert, 5 )
43 #define SAX_ARGS_ARG6( arg1, arg2, convert ) SAX_ARGS_ARG5( arg1, arg2, convert ), SAX_ARGS_ARG( arg1, arg2, convert, 6 )
44 #define SAX_ARGS_ARG7( arg1, arg2, convert ) SAX_ARGS_ARG6( arg1, arg2, convert ), SAX_ARGS_ARG( arg1, arg2, convert, 7 )
45 #define SAX_ARGS_ARG8( arg1, arg2, convert ) SAX_ARGS_ARG7( arg1, arg2, convert ), SAX_ARGS_ARG( arg1, arg2, convert, 8 )
46 #define SAX_ARGS_ARG9( arg1, arg2, convert ) SAX_ARGS_ARG8( arg1, arg2, convert ), SAX_ARGS_ARG( arg1, arg2, convert, 9 )
47 #define SAX_ARGS_ARG10( arg1, arg2, convert ) SAX_ARGS_ARG9( arg1, arg2, convert ), SAX_ARGS_ARG( arg1, arg2, convert, 10 )
48 #define SAX_ARGS_ARG11( arg1, arg2, convert ) SAX_ARGS_ARG10( arg1, arg2, convert ), SAX_ARGS_ARG( arg1, arg2, convert, 11 )
49 #define SAX_ARGS_ARG12( arg1, arg2, convert ) SAX_ARGS_ARG11( arg1, arg2, convert ), SAX_ARGS_ARG( arg1, arg2, convert, 12 )
50 #define SAX_ARGS_ARG13( arg1, arg2, convert ) SAX_ARGS_ARG12( arg1, arg2, convert ), SAX_ARGS_ARG( arg1, arg2, convert, 13 )
51 #define SAX_ARGS_ARG14( arg1, arg2, convert ) SAX_ARGS_ARG13( arg1, arg2, convert ), SAX_ARGS_ARG( arg1, arg2, convert, 14 )
52 #define SAX_ARGS_ARG15( arg1, arg2, convert ) SAX_ARGS_ARG14( arg1, arg2, convert ), SAX_ARGS_ARG( arg1, arg2, convert, 15 )
53 #define SAX_ARGS_ARG16( arg1, arg2, convert ) SAX_ARGS_ARG15( arg1, arg2, convert ), SAX_ARGS_ARG( arg1, arg2, convert, 16 )
54 #define SAX_ARGS_ARG17( arg1, arg2, convert ) SAX_ARGS_ARG16( arg1, arg2, convert ), SAX_ARGS_ARG( arg1, arg2, convert, 17 )
55 #define SAX_ARGS_ARG18( arg1, arg2, convert ) SAX_ARGS_ARG17( arg1, arg2, convert ), SAX_ARGS_ARG( arg1, arg2, convert, 18 )
56 #define SAX_ARGS_ARG19( arg1, arg2, convert ) SAX_ARGS_ARG18( arg1, arg2, convert ), SAX_ARGS_ARG( arg1, arg2, convert, 19 )
57 #define SAX_ARGS_ARG20( arg1, arg2, convert ) SAX_ARGS_ARG19( arg1, arg2, convert ), SAX_ARGS_ARG( arg1, arg2, convert, 20 )
58 #define SAX_ARGS_ARG21( arg1, arg2, convert ) SAX_ARGS_ARG20( arg1, arg2, convert ), SAX_ARGS_ARG( arg1, arg2, convert, 21 )
59 #define SAX_ARGS_ARG22( arg1, arg2, convert ) SAX_ARGS_ARG21( arg1, arg2, convert ), SAX_ARGS_ARG( arg1, arg2, convert, 22 )
61 namespace sax_fastparser
{
63 enum class MergeMarks
{ APPEND
= 0, PREPEND
= 1, POSTPONE
= 2};
65 typedef css::uno::Reference
< css::xml::sax::XFastAttributeList
> XFastAttributeListRef
;
67 class FastSaxSerializer
;
69 class SAX_DLLPUBLIC FastSerializerHelper
73 FastSerializerHelper( const css::uno::Reference
< css::io::XOutputStream
>& xOutputStream
, bool bWriteHeader
);
75 ~FastSerializerHelper();
77 /// Start an element. After the first argument there can be a number of (attribute, value) pairs.
78 void startElement(sal_Int32 elementTokenId
, FSEND_t
)
79 { startElementInternal( elementTokenId
, FSEND_internal
); }
81 void startElement(sal_Int32 elementTokenId
, sal_Int32 attribute
, const char* value
, FSEND_t
)
82 { startElementInternal( elementTokenId
, attribute
, value
, FSEND_internal
); }
84 void startElement(sal_Int32 elementTokenId
, sal_Int32 attribute
, const OString
& value
, FSEND_t
)
85 { startElementInternal( elementTokenId
, attribute
, value
.getStr(), FSEND_internal
); }
86 /// Create a single element. After the first argument there can be a number of (attribute, value) pairs.
87 void singleElement(sal_Int32 elementTokenId
, FSEND_t
)
88 { singleElementInternal( elementTokenId
, FSEND_internal
); }
90 void singleElement(sal_Int32 elementTokenId
, sal_Int32 attribute
, const char* value
, FSEND_t
)
91 { singleElementInternal( elementTokenId
, attribute
, value
, FSEND_internal
); }
93 void singleElement(sal_Int32 elementTokenId
, sal_Int32 attribute
, const OString
& value
, FSEND_t
)
94 { singleElementInternal( elementTokenId
, attribute
, value
.getStr(), FSEND_internal
); }
95 /// Start an element. After the first two arguments there can be a number of (attribute, value) pairs.
96 void startElementNS(sal_Int32 namespaceTokenId
, sal_Int32 elementTokenId
, FSEND_t
)
97 { startElementInternal( FSNS( namespaceTokenId
, elementTokenId
), FSEND_internal
); }
99 void startElementNS(sal_Int32 namespaceTokenId
, sal_Int32 elementTokenId
, sal_Int32 attribute
, const char* value
, FSEND_t
)
100 { startElementInternal( FSNS( namespaceTokenId
, elementTokenId
), attribute
, value
, FSEND_internal
); }
102 void startElementNS(sal_Int32 namespaceTokenId
, sal_Int32 elementTokenId
, sal_Int32 attribute
, const OString
& value
, FSEND_t
)
103 { startElementInternal( FSNS( namespaceTokenId
, elementTokenId
), attribute
, value
.getStr(), FSEND_internal
); }
104 /// Create a single element. After the first two arguments there can be a number of (attribute, value) pairs.
105 void singleElementNS(sal_Int32 namespaceTokenId
, sal_Int32 elementTokenId
, FSEND_t
)
106 { singleElementInternal( FSNS( namespaceTokenId
, elementTokenId
), FSEND_internal
); }
108 void singleElementNS(sal_Int32 namespaceTokenId
, sal_Int32 elementTokenId
, sal_Int32 attribute
, const char* value
, FSEND_t
)
109 { singleElementInternal( FSNS( namespaceTokenId
, elementTokenId
), attribute
, value
, FSEND_internal
); }
111 void singleElementNS(sal_Int32 namespaceTokenId
, sal_Int32 elementTokenId
, sal_Int32 attribute
, const OString
& value
, FSEND_t
)
112 { singleElementInternal( FSNS( namespaceTokenId
, elementTokenId
), attribute
, value
.getStr(), FSEND_internal
); }
113 void endElement(sal_Int32 elementTokenId
);
114 void endElementNS(sal_Int32 namespaceTokenId
, sal_Int32 elementTokenId
)
115 { endElement( FSNS( namespaceTokenId
, elementTokenId
) ); }
117 void singleElement(sal_Int32 elementTokenId
, const XFastAttributeListRef
& xAttrList
);
118 void singleElementNS(sal_Int32 namespaceTokenId
, sal_Int32 elementTokenId
, XFastAttributeListRef
const & xAttrList
)
119 { singleElement(FSNS( namespaceTokenId
, elementTokenId
), xAttrList
); }
121 void startElement(sal_Int32 elementTokenId
, const XFastAttributeListRef
& xAttrList
);
122 void startElementNS(sal_Int32 namespaceTokenId
, sal_Int32 elementTokenId
, XFastAttributeListRef
const & xAttrList
)
123 { startElement( FSNS( namespaceTokenId
, elementTokenId
), xAttrList
); }
125 FastSerializerHelper
* write(const char* value
);
126 FastSerializerHelper
* write(const OUString
& value
);
127 FastSerializerHelper
* write(sal_Int32 value
);
128 FastSerializerHelper
* write(sal_Int64 value
);
129 FastSerializerHelper
* write(double value
);
131 FastSerializerHelper
* writeEscaped(const char* value
);
132 FastSerializerHelper
* writeEscaped(const OUString
& value
);
134 FastSerializerHelper
* writeId(sal_Int32 tokenId
);
136 css::uno::Reference
< css::io::XOutputStream
> getOutputStream();
138 static FastAttributeList
*createAttrList();
140 void mark(sal_Int32 nTag
,
141 const css::uno::Sequence
< sal_Int32
>& rOrder
=
142 css::uno::Sequence
< sal_Int32
>() );
143 void mergeTopMarks(sal_Int32 nTag
,
144 MergeMarks eMergeType
= MergeMarks::APPEND
);
147 Now create all the overloads in a typesafe way (i.e. without varargs) by creating a number of overloads
148 up to a certain reasonable limit (feel free to raise it). This would be a lot easier with C++11 vararg templates.
150 // now overloads for 2 and more pairs
151 #define SAX_ARGS_FUNC_DECL( argsdecl, argsuse ) \
152 void startElement(sal_Int32 elementTokenId, argsdecl, FSEND_t) \
153 { startElementInternal( elementTokenId, argsuse, FSEND_internal ); } \
154 void singleElement(sal_Int32 elementTokenId, argsdecl, FSEND_t) \
155 { singleElementInternal( elementTokenId, argsuse, FSEND_internal ); } \
156 void startElementNS(sal_Int32 namespaceTokenId, sal_Int32 elementTokenId, argsdecl, FSEND_t) \
157 { startElementInternal( FSNS( namespaceTokenId, elementTokenId), argsuse, FSEND_internal ); } \
158 void singleElementNS(sal_Int32 namespaceTokenId, sal_Int32 elementTokenId, argsdecl, FSEND_t) \
159 { singleElementInternal( FSNS( namespaceTokenId, elementTokenId), argsuse, FSEND_internal ); }
160 #define SAX_ARGS_FUNC_NUM( decl1, decl2, use1, use2, convert, num ) \
161 SAX_ARGS_FUNC_DECL( SAX_ARGS_ARG##num( decl1, decl2, ), SAX_ARGS_ARG##num( use1, use2, convert ))
162 #define SAX_ARGS_FUNC_SUBST( type, convert, num ) \
163 SAX_ARGS_FUNC_NUM( sal_Int32 attribute, type value, attribute, value, convert, num )
164 #define SAX_ARGS_FUNC( arg, convert ) SAX_ARGS_FUNC_SUBST( arg, convert, 2 ) \
165 SAX_ARGS_FUNC_SUBST( arg, convert, 3 ) SAX_ARGS_FUNC_SUBST( arg, convert, 4 ) \
166 SAX_ARGS_FUNC_SUBST( arg, convert, 5 ) SAX_ARGS_FUNC_SUBST( arg, convert, 6 ) \
167 SAX_ARGS_FUNC_SUBST( arg, convert, 7 ) SAX_ARGS_FUNC_SUBST( arg, convert, 8 ) \
168 SAX_ARGS_FUNC_SUBST( arg, convert, 9 ) SAX_ARGS_FUNC_SUBST( arg, convert, 10 ) \
169 SAX_ARGS_FUNC_SUBST( arg, convert, 11 ) SAX_ARGS_FUNC_SUBST( arg, convert, 12 ) \
170 SAX_ARGS_FUNC_SUBST( arg, convert, 13 ) SAX_ARGS_FUNC_SUBST( arg, convert, 14 ) \
171 SAX_ARGS_FUNC_SUBST( arg, convert, 15 ) SAX_ARGS_FUNC_SUBST( arg, convert, 16 ) \
172 SAX_ARGS_FUNC_SUBST( arg, convert, 17 ) SAX_ARGS_FUNC_SUBST( arg, convert, 18 ) \
173 SAX_ARGS_FUNC_SUBST( arg, convert, 19 ) SAX_ARGS_FUNC_SUBST( arg, convert, 20 ) \
174 SAX_ARGS_FUNC_SUBST( arg, convert, 21 ) SAX_ARGS_FUNC_SUBST( arg, convert, 22 )
175 SAX_ARGS_FUNC( const char*, )
176 SAX_ARGS_FUNC( const OString
&, .getStr() )
177 #undef SAX_ARGS_FUNC_DECL
178 #undef SAX_ARGS_FUNC_NUM
179 #undef SAX_ARGS_FUNC_SUBST
183 void startElementInternal(sal_Int32 elementTokenId
, ...);
184 void singleElementInternal(sal_Int32 elementTokenId
, ...);
186 FastSaxSerializer
* mpSerializer
;
189 typedef std::shared_ptr
< FastSerializerHelper
> FSHelperPtr
;
193 #endif // INCLUDED_SAX_FSHELPER_HXX
195 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */