Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #ifndef _SAX_FS_HELPER_HXX_
21 : #define _SAX_FS_HELPER_HXX_
22 :
23 : #include <com/sun/star/uno/XReference.hpp>
24 : #include <com/sun/star/io/XOutputStream.hpp>
25 : #include <com/sun/star/xml/sax/XFastTokenHandler.hpp>
26 : #include <stdarg.h>
27 : #include <boost/shared_ptr.hpp>
28 : #include <sax/fastattribs.hxx>
29 :
30 : #define FSNS(namespc, element) ((namespc << 16) | element)
31 : // Backwards compatibility for code that used FSEND to terminate the vararg.
32 : // As soon as no supported LO version has the varargs code, this can be removed entirely
33 : // (otherwise backports might break silently if people didn't add FSEND).
34 : // Ctor is there to get an error when trying to pass it to a vararg by accident.
35 919 : struct FSEND_t { FSEND_t() {}; };
36 919 : static const FSEND_t FSEND = FSEND_t();
37 : const sal_Int32 FSEND_internal = -1; // same as XML_TOKEN_INVALID
38 :
39 : #define SAX_ARGS_ARG( arg1, arg2, convert, num ) arg1##num, arg2##num convert
40 : #define SAX_ARGS_ARG1( arg1, arg2, convert ) SAX_ARGS_ARG( arg1, arg2, convert, 1 )
41 : #define SAX_ARGS_ARG2( arg1, arg2, convert ) SAX_ARGS_ARG1( arg1, arg2, convert ), SAX_ARGS_ARG( arg1, arg2, convert, 2 )
42 : #define SAX_ARGS_ARG3( arg1, arg2, convert ) SAX_ARGS_ARG2( arg1, arg2, convert ), SAX_ARGS_ARG( arg1, arg2, convert, 3 )
43 : #define SAX_ARGS_ARG4( arg1, arg2, convert ) SAX_ARGS_ARG3( arg1, arg2, convert ), SAX_ARGS_ARG( arg1, arg2, convert, 4 )
44 : #define SAX_ARGS_ARG5( arg1, arg2, convert ) SAX_ARGS_ARG4( arg1, arg2, convert ), SAX_ARGS_ARG( arg1, arg2, convert, 5 )
45 : #define SAX_ARGS_ARG6( arg1, arg2, convert ) SAX_ARGS_ARG5( arg1, arg2, convert ), SAX_ARGS_ARG( arg1, arg2, convert, 6 )
46 : #define SAX_ARGS_ARG7( arg1, arg2, convert ) SAX_ARGS_ARG6( arg1, arg2, convert ), SAX_ARGS_ARG( arg1, arg2, convert, 7 )
47 : #define SAX_ARGS_ARG8( arg1, arg2, convert ) SAX_ARGS_ARG7( arg1, arg2, convert ), SAX_ARGS_ARG( arg1, arg2, convert, 8 )
48 : #define SAX_ARGS_ARG9( arg1, arg2, convert ) SAX_ARGS_ARG8( arg1, arg2, convert ), SAX_ARGS_ARG( arg1, arg2, convert, 9 )
49 : #define SAX_ARGS_ARG10( arg1, arg2, convert ) SAX_ARGS_ARG9( arg1, arg2, convert ), SAX_ARGS_ARG( arg1, arg2, convert, 10 )
50 : #define SAX_ARGS_ARG11( arg1, arg2, convert ) SAX_ARGS_ARG10( arg1, arg2, convert ), SAX_ARGS_ARG( arg1, arg2, convert, 11 )
51 : #define SAX_ARGS_ARG12( arg1, arg2, convert ) SAX_ARGS_ARG11( arg1, arg2, convert ), SAX_ARGS_ARG( arg1, arg2, convert, 12 )
52 : #define SAX_ARGS_ARG13( arg1, arg2, convert ) SAX_ARGS_ARG12( arg1, arg2, convert ), SAX_ARGS_ARG( arg1, arg2, convert, 13 )
53 : #define SAX_ARGS_ARG14( arg1, arg2, convert ) SAX_ARGS_ARG13( arg1, arg2, convert ), SAX_ARGS_ARG( arg1, arg2, convert, 14 )
54 : #define SAX_ARGS_ARG15( arg1, arg2, convert ) SAX_ARGS_ARG14( arg1, arg2, convert ), SAX_ARGS_ARG( arg1, arg2, convert, 15 )
55 : #define SAX_ARGS_ARG16( arg1, arg2, convert ) SAX_ARGS_ARG15( arg1, arg2, convert ), SAX_ARGS_ARG( arg1, arg2, convert, 16 )
56 : #define SAX_ARGS_ARG17( arg1, arg2, convert ) SAX_ARGS_ARG16( arg1, arg2, convert ), SAX_ARGS_ARG( arg1, arg2, convert, 17 )
57 : #define SAX_ARGS_ARG18( arg1, arg2, convert ) SAX_ARGS_ARG17( arg1, arg2, convert ), SAX_ARGS_ARG( arg1, arg2, convert, 18 )
58 : #define SAX_ARGS_ARG19( arg1, arg2, convert ) SAX_ARGS_ARG18( arg1, arg2, convert ), SAX_ARGS_ARG( arg1, arg2, convert, 19 )
59 : #define SAX_ARGS_ARG20( arg1, arg2, convert ) SAX_ARGS_ARG19( arg1, arg2, convert ), SAX_ARGS_ARG( arg1, arg2, convert, 20 )
60 : #define SAX_ARGS_ARG21( arg1, arg2, convert ) SAX_ARGS_ARG20( arg1, arg2, convert ), SAX_ARGS_ARG( arg1, arg2, convert, 21 )
61 : #define SAX_ARGS_ARG22( arg1, arg2, convert ) SAX_ARGS_ARG21( arg1, arg2, convert ), SAX_ARGS_ARG( arg1, arg2, convert, 22 )
62 :
63 : namespace sax_fastparser {
64 :
65 : enum MergeMarksEnum { MERGE_MARKS_APPEND = 0, MERGE_MARKS_PREPEND = 1, MERGE_MARKS_POSTPONE = 2 };
66 :
67 : typedef ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XFastAttributeList > XFastAttributeListRef;
68 :
69 : class FastSaxSerializer;
70 :
71 : class SAX_DLLPUBLIC FastSerializerHelper
72 : {
73 : public:
74 :
75 : FastSerializerHelper( const ::com::sun::star::uno::Reference< ::com::sun::star::io::XOutputStream >& xOutputStream, bool bWriteHeader = true );
76 :
77 : ~FastSerializerHelper();
78 :
79 : /// Start an element. After the first argument there can be a number of (attribute, value) pairs.
80 760 : void startElement(sal_Int32 elementTokenId, FSEND_t)
81 760 : { startElementInternal( elementTokenId, FSEND_internal ); }
82 : /// overload
83 206 : void startElement(sal_Int32 elementTokenId, sal_Int32 attribute, const char* value, FSEND_t)
84 206 : { startElementInternal( elementTokenId, attribute, value, FSEND_internal ); }
85 : /// overload
86 : void startElement(sal_Int32 elementTokenId, sal_Int32 attribute, const OString& value, FSEND_t)
87 : { startElementInternal( elementTokenId, attribute, value.getStr(), FSEND_internal ); }
88 : /// Create a single element. After the first argument there can be a number of (attribute, value) pairs.
89 36 : void singleElement(sal_Int32 elementTokenId, FSEND_t)
90 36 : { singleElementInternal( elementTokenId, FSEND_internal ); }
91 : /// overload
92 336 : void singleElement(sal_Int32 elementTokenId, sal_Int32 attribute, const char* value, FSEND_t)
93 336 : { singleElementInternal( elementTokenId, attribute, value, FSEND_internal ); }
94 : /// overload
95 : void singleElement(sal_Int32 elementTokenId, sal_Int32 attribute, const OString& value, FSEND_t)
96 : { singleElementInternal( elementTokenId, attribute, value.getStr(), FSEND_internal ); }
97 : /// Start an element. After the first two arguments there can be a number of (attribute, value) pairs.
98 2883 : void startElementNS(sal_Int32 namespaceTokenId, sal_Int32 elementTokenId, FSEND_t)
99 2883 : { startElementInternal( FSNS( namespaceTokenId, elementTokenId), FSEND_internal ); }
100 : /// overload
101 796 : void startElementNS(sal_Int32 namespaceTokenId, sal_Int32 elementTokenId, sal_Int32 attribute, const char* value, FSEND_t)
102 796 : { startElementInternal( FSNS( namespaceTokenId, elementTokenId), attribute, value, FSEND_internal ); }
103 : /// overload
104 : void startElementNS(sal_Int32 namespaceTokenId, sal_Int32 elementTokenId, sal_Int32 attribute, const OString& value, FSEND_t)
105 : { startElementInternal( FSNS( namespaceTokenId, elementTokenId), attribute, value.getStr(), FSEND_internal ); }
106 : /// Create a single element. After the first two arguments there can be a number of (attribute, value) pairs.
107 354 : void singleElementNS(sal_Int32 namespaceTokenId, sal_Int32 elementTokenId, FSEND_t)
108 354 : { singleElementInternal( FSNS( namespaceTokenId, elementTokenId), FSEND_internal ); }
109 : /// overload
110 3442 : void singleElementNS(sal_Int32 namespaceTokenId, sal_Int32 elementTokenId, sal_Int32 attribute, const char* value, FSEND_t)
111 3442 : { singleElementInternal( FSNS( namespaceTokenId, elementTokenId), attribute, value, FSEND_internal ); }
112 : /// overload
113 1 : void singleElementNS(sal_Int32 namespaceTokenId, sal_Int32 elementTokenId, sal_Int32 attribute, const OString& value, FSEND_t)
114 1 : { singleElementInternal( FSNS( namespaceTokenId, elementTokenId), attribute, value.getStr(), FSEND_internal ); }
115 : void endElement(sal_Int32 elementTokenId);
116 4260 : inline void endElementNS(sal_Int32 namespaceTokenId, sal_Int32 elementTokenId)
117 4260 : { endElement( FSNS( namespaceTokenId, elementTokenId ) ); }
118 :
119 : void singleElement(sal_Int32 elementTokenId, XFastAttributeListRef xAttrList);
120 1531 : inline void singleElementNS(sal_Int32 namespaceTokenId, sal_Int32 elementTokenId, XFastAttributeListRef xAttrList)
121 1531 : { singleElement(FSNS( namespaceTokenId, elementTokenId), xAttrList); }
122 :
123 : void startElement(sal_Int32 elementTokenId, XFastAttributeListRef xAttrList);
124 83 : inline void startElementNS(sal_Int32 namespaceTokenId, sal_Int32 elementTokenId, XFastAttributeListRef xAttrList)
125 83 : { startElement( FSNS( namespaceTokenId, elementTokenId ), xAttrList ); }
126 :
127 : FastSerializerHelper* write(const char* value);
128 : FastSerializerHelper* write(const OUString& value);
129 : FastSerializerHelper* write(sal_Int32 value);
130 : FastSerializerHelper* write(sal_Int64 value);
131 : FastSerializerHelper* write(double value);
132 :
133 : FastSerializerHelper* writeEscaped(const char* value);
134 : FastSerializerHelper* writeEscaped(const OUString& value);
135 :
136 : FastSerializerHelper* writeId(sal_Int32 tokenId);
137 :
138 : ::com::sun::star::uno::Reference< ::com::sun::star::io::XOutputStream > getOutputStream();
139 :
140 : FastAttributeList *createAttrList();
141 :
142 : void mark( ::com::sun::star::uno::Sequence< sal_Int32 > aOrder =
143 : ::com::sun::star::uno::Sequence< sal_Int32 >() );
144 : void mergeTopMarks( MergeMarksEnum eMergeType = MERGE_MARKS_APPEND );
145 : void copyTopMarkPush();
146 : void copyTopMarkPop();
147 :
148 : /*
149 : Now create all the overloads in a typesafe way (i.e. without varargs) by creating a number of overloads
150 : up to a certain reasonable limit (feel free to raise it). This would be a lot easier with C++11 vararg templates.
151 : */
152 : // now overloads for 2 and more pairs
153 : #define SAX_ARGS_FUNC_DECL( argsdecl, argsuse ) \
154 : void startElement(sal_Int32 elementTokenId, argsdecl, FSEND_t) \
155 : { startElementInternal( elementTokenId, argsuse, FSEND_internal ); } \
156 : void singleElement(sal_Int32 elementTokenId, argsdecl, FSEND_t) \
157 : { singleElementInternal( elementTokenId, argsuse, FSEND_internal ); } \
158 : void startElementNS(sal_Int32 namespaceTokenId, sal_Int32 elementTokenId, argsdecl, FSEND_t) \
159 : { startElementInternal( FSNS( namespaceTokenId, elementTokenId), argsuse, FSEND_internal ); } \
160 : void singleElementNS(sal_Int32 namespaceTokenId, sal_Int32 elementTokenId, argsdecl, FSEND_t) \
161 : { singleElementInternal( FSNS( namespaceTokenId, elementTokenId), argsuse, FSEND_internal ); }
162 : #define SAX_ARGS_FUNC_NUM( decl1, decl2, use1, use2, convert, num ) \
163 : SAX_ARGS_FUNC_DECL( SAX_ARGS_ARG##num( decl1, decl2, ), SAX_ARGS_ARG##num( use1, use2, convert ))
164 : #define SAX_ARGS_FUNC_SUBST( type, convert, num ) \
165 : SAX_ARGS_FUNC_NUM( sal_Int32 attribute, type value, attribute, value, convert, num )
166 : #define SAX_ARGS_FUNC( arg, convert ) SAX_ARGS_FUNC_SUBST( arg, convert, 2 ) \
167 : SAX_ARGS_FUNC_SUBST( arg, convert, 3 ) SAX_ARGS_FUNC_SUBST( arg, convert, 4 ) \
168 : SAX_ARGS_FUNC_SUBST( arg, convert, 5 ) SAX_ARGS_FUNC_SUBST( arg, convert, 6 ) \
169 : SAX_ARGS_FUNC_SUBST( arg, convert, 7 ) SAX_ARGS_FUNC_SUBST( arg, convert, 8 ) \
170 : SAX_ARGS_FUNC_SUBST( arg, convert, 9 ) SAX_ARGS_FUNC_SUBST( arg, convert, 10 ) \
171 : SAX_ARGS_FUNC_SUBST( arg, convert, 11 ) SAX_ARGS_FUNC_SUBST( arg, convert, 12 ) \
172 : SAX_ARGS_FUNC_SUBST( arg, convert, 13 ) SAX_ARGS_FUNC_SUBST( arg, convert, 14 ) \
173 : SAX_ARGS_FUNC_SUBST( arg, convert, 15 ) SAX_ARGS_FUNC_SUBST( arg, convert, 16 ) \
174 : SAX_ARGS_FUNC_SUBST( arg, convert, 17 ) SAX_ARGS_FUNC_SUBST( arg, convert, 18 ) \
175 : SAX_ARGS_FUNC_SUBST( arg, convert, 19 ) SAX_ARGS_FUNC_SUBST( arg, convert, 20 ) \
176 : SAX_ARGS_FUNC_SUBST( arg, convert, 21 ) SAX_ARGS_FUNC_SUBST( arg, convert, 22 )
177 1441 : SAX_ARGS_FUNC( const char*, )
178 5 : SAX_ARGS_FUNC( const OString&, .getStr() )
179 : #undef SAX_ARGS_FUNC_DECL
180 : #undef SAX_ARGS_FUNC_NUM
181 : #undef SAX_ARGS_FUNC_SUBST
182 : #undef SAX_ARGS_FUNC
183 :
184 : private:
185 : void startElementInternal(sal_Int32 elementTokenId, ...);
186 : void singleElementInternal(sal_Int32 elementTokenId, ...);
187 :
188 : FastSaxSerializer* mpSerializer;
189 : com::sun::star::uno::Reference<com::sun::star::xml::sax::XFastTokenHandler> mxTokenHandler;
190 :
191 : };
192 :
193 : typedef boost::shared_ptr< FastSerializerHelper > FSHelperPtr;
194 :
195 : }
196 :
197 : #endif // _SAX_FS_HELPER_HXX_
198 :
199 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|