Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / include / sax / fshelper.hxx
blob74e2ed4e3254c439aab5ae1d818a53216b2ac466
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_FSHELPER_HXX
21 #define INCLUDED_SAX_FSHELPER_HXX
23 #include <com/sun/star/xml/sax/XFastAttributeList.hpp>
24 #include <com/sun/star/uno/Reference.hxx>
25 #include <com/sun/star/uno/Sequence.hxx>
26 #include <rtl/ustring.hxx>
27 #include <rtl/ref.hxx>
28 #include <sax/saxdllapi.h>
29 #include <optional>
30 #include <memory>
31 #include <string_view>
32 #include <utility>
34 namespace com::sun::star::io { class XOutputStream; }
35 namespace sax_fastparser { class FastAttributeList; }
37 constexpr sal_Int32 FSNS(sal_Int32 namespc, sal_Int32 element) { return (namespc << 16) | element; }
39 namespace sax_fastparser {
41 enum class MergeMarks { APPEND = 0, PREPEND = 1, POSTPONE = 2};
43 class FastSaxSerializer;
45 class SAX_DLLPUBLIC FastSerializerHelper
47 public:
49 FastSerializerHelper( const css::uno::Reference< css::io::XOutputStream >& xOutputStream, bool bWriteHeader );
51 ~FastSerializerHelper();
53 void startDocument();
54 void endDocument();
56 /// Start an element. After the first argument there can be a number of (attribute, value) pairs.
57 template<typename... Args>
58 void startElement(sal_Int32 elementTokenId, sal_Int32 attribute, const char* value, Args &&... args)
60 if (value)
61 pushAttributeValue(attribute, value);
62 startElement(elementTokenId, std::forward<Args>(args)...);
64 template<typename... Args>
65 void startElement(sal_Int32 elementTokenId, sal_Int32 attribute,
66 const std::optional<OString>& value, Args&&... args)
68 if (value)
69 pushAttributeValue(attribute, *value);
70 startElement(elementTokenId, std::forward<Args>(args)...);
72 template<typename... Args>
73 void startElement(sal_Int32 elementTokenId, sal_Int32 attribute,
74 const std::optional<OUString>& value, Args&&... args)
76 std::optional<OString> opt;
77 if (value)
78 opt = value->toUtf8();
79 startElement(elementTokenId, attribute, opt, std::forward<Args>(args)...);
81 void startElement(sal_Int32 elementTokenId);
83 /// Start an element. After the first two arguments there can be a number of (attribute, value) pairs.
84 template<typename... Args>
85 void startElementNS(sal_Int32 namespaceTokenId, sal_Int32 elementTokenId, Args &&... args)
87 startElement(FSNS(namespaceTokenId, elementTokenId), std::forward<Args>(args)...);
90 /// Create a single element. After the first argument there can be a number of (attribute, value) pairs.
91 template<typename... Args>
92 void singleElement(sal_Int32 elementTokenId, sal_Int32 attribute, const char* value, Args &&... args)
94 if (value)
95 pushAttributeValue(attribute, value);
96 singleElement(elementTokenId, std::forward<Args>(args)...);
98 template<typename... Args>
99 void singleElement(sal_Int32 elementTokenId, sal_Int32 attribute,
100 const std::optional<OString>& value, Args&&... args)
102 if (value)
103 pushAttributeValue(attribute, *value);
104 singleElement(elementTokenId, std::forward<Args>(args)...);
106 template<typename... Args>
107 void singleElement(sal_Int32 elementTokenId, sal_Int32 attribute,
108 const std::optional<OUString>& value, Args&&... args)
110 std::optional<OString> opt;
111 if (value)
112 opt = value->toUtf8();
113 singleElement(elementTokenId, attribute, opt, std::forward<Args>(args)...);
115 void singleElement(sal_Int32 elementTokenId);
117 /// Create a single element. After the first two arguments there can be a number of (attribute, value) pairs.
118 template<typename... Args>
119 void singleElementNS(sal_Int32 namespaceTokenId, sal_Int32 elementTokenId, Args &&... args)
121 singleElement(FSNS(namespaceTokenId, elementTokenId), std::forward<Args>(args)...);
124 void endElement(sal_Int32 elementTokenId);
125 void endElementNS(sal_Int32 namespaceTokenId, sal_Int32 elementTokenId)
126 { endElement( FSNS( namespaceTokenId, elementTokenId ) ); }
128 void singleElement(sal_Int32 elementTokenId, const rtl::Reference<FastAttributeList>& xAttrList);
130 void startElement(sal_Int32 elementTokenId, const rtl::Reference<FastAttributeList>& xAttrList);
132 FastSerializerHelper* write(const char* value);
133 FastSerializerHelper* write(const OString& value);
134 FastSerializerHelper* write(std::u16string_view value);
135 FastSerializerHelper* write(sal_Int32 value);
136 FastSerializerHelper* write(sal_Int64 value);
137 FastSerializerHelper* write(double value);
139 FastSerializerHelper* writeEscaped(const char* value);
140 FastSerializerHelper* writeEscaped(std::u16string_view value);
142 FastSerializerHelper* writeId(sal_Int32 tokenId);
144 css::uno::Reference< css::io::XOutputStream > const & getOutputStream() const;
146 static rtl::Reference<FastAttributeList> createAttrList();
148 void mark(sal_Int32 nTag,
149 const css::uno::Sequence< sal_Int32 >& rOrder =
150 css::uno::Sequence< sal_Int32 >() );
151 void mergeTopMarks(sal_Int32 nTag,
152 MergeMarks eMergeType = MergeMarks::APPEND );
154 void setAllowXEscape(bool bSet);
156 private:
157 void pushAttributeValue( sal_Int32 attribute, const char* value );
158 void pushAttributeValue( sal_Int32 attribute, const OString& value );
160 std::unique_ptr<FastSaxSerializer> mpSerializer;
163 typedef std::shared_ptr< FastSerializerHelper > FSHelperPtr;
165 // Helpers to make intention to pass optional attributes to *Element functions explicit, instead of
166 // using `(condition) ? value.toUtf8().getStr() : nullptr` syntax.
167 inline const char* UseIf(const char* s, bool bUse) { return bUse ? s : nullptr; }
168 // OString, OUString
169 template<class TString>
170 std::optional<TString> UseIf(const TString& s, bool bUse)
172 return bUse ? std::optional<TString>(s) : std::optional<TString>();
177 #endif // INCLUDED_SAX_FSHELPER_HXX
179 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */