LibreOffice Module sax (master) 1
fshelper.hxx
Go to the documentation of this file.
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 INCLUDED_SAX_FSHELPER_HXX
21#define INCLUDED_SAX_FSHELPER_HXX
22
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>
33
34namespace com::sun::star::io { class XOutputStream; }
35namespace sax_fastparser { class FastAttributeList; }
36
37constexpr sal_Int32 FSNS(sal_Int32 namespc, sal_Int32 element) { return (namespc << 16) | element; }
38
39namespace sax_fastparser {
40
41enum class MergeMarks { APPEND = 0, PREPEND = 1, POSTPONE = 2};
42
43class FastSaxSerializer;
44
46{
47public:
48
49 FastSerializerHelper( const css::uno::Reference< css::io::XOutputStream >& xOutputStream, bool bWriteHeader );
50
52
53 void startDocument();
54 void endDocument();
55
57 template<typename... Args>
58 void startElement(sal_Int32 elementTokenId, sal_Int32 attribute, const char* value, Args &&... args)
59 {
60 if (value)
61 pushAttributeValue(attribute, value);
62 startElement(elementTokenId, std::forward<Args>(args)...);
63 }
64 template<typename... Args>
65 void startElement(sal_Int32 elementTokenId, sal_Int32 attribute,
66 const std::optional<OString>& value, Args&&... args)
67 {
68 if (value)
69 pushAttributeValue(attribute, *value);
70 startElement(elementTokenId, std::forward<Args>(args)...);
71 }
72 template<typename... Args>
73 void startElement(sal_Int32 elementTokenId, sal_Int32 attribute,
74 const std::optional<OUString>& value, Args&&... args)
75 {
76 std::optional<OString> opt;
77 if (value)
78 opt = value->toUtf8();
79 startElement(elementTokenId, attribute, opt, std::forward<Args>(args)...);
80 }
81 void startElement(sal_Int32 elementTokenId);
82
84 template<typename... Args>
85 void startElementNS(sal_Int32 namespaceTokenId, sal_Int32 elementTokenId, Args &&... args)
86 {
87 startElement(FSNS(namespaceTokenId, elementTokenId), std::forward<Args>(args)...);
88 }
89
91 template<typename... Args>
92 void singleElement(sal_Int32 elementTokenId, sal_Int32 attribute, const char* value, Args &&... args)
93 {
94 if (value)
95 pushAttributeValue(attribute, value);
96 singleElement(elementTokenId, std::forward<Args>(args)...);
97 }
98 template<typename... Args>
99 void singleElement(sal_Int32 elementTokenId, sal_Int32 attribute,
100 const std::optional<OString>& value, Args&&... args)
101 {
102 if (value)
103 pushAttributeValue(attribute, *value);
104 singleElement(elementTokenId, std::forward<Args>(args)...);
105 }
106 template<typename... Args>
107 void singleElement(sal_Int32 elementTokenId, sal_Int32 attribute,
108 const std::optional<OUString>& value, Args&&... args)
109 {
110 std::optional<OString> opt;
111 if (value)
112 opt = value->toUtf8();
113 singleElement(elementTokenId, attribute, opt, std::forward<Args>(args)...);
114 }
115 void singleElement(sal_Int32 elementTokenId);
116
118 template<typename... Args>
119 void singleElementNS(sal_Int32 namespaceTokenId, sal_Int32 elementTokenId, Args &&... args)
120 {
121 singleElement(FSNS(namespaceTokenId, elementTokenId), std::forward<Args>(args)...);
122 }
123
124 void endElement(sal_Int32 elementTokenId);
125 void endElementNS(sal_Int32 namespaceTokenId, sal_Int32 elementTokenId)
126 { endElement( FSNS( namespaceTokenId, elementTokenId ) ); }
127
128 void singleElement(sal_Int32 elementTokenId, const rtl::Reference<FastAttributeList>& xAttrList);
129
130 void startElement(sal_Int32 elementTokenId, const rtl::Reference<FastAttributeList>& xAttrList);
131
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);
138
139 FastSerializerHelper* writeEscaped(const char* value);
140 FastSerializerHelper* writeEscaped(std::u16string_view value);
141
142 FastSerializerHelper* writeId(sal_Int32 tokenId);
143
144 css::uno::Reference< css::io::XOutputStream > const & getOutputStream() const;
145
146 static rtl::Reference<FastAttributeList> createAttrList();
147
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 );
153
154 void setAllowXEscape(bool bSet);
155
156private:
157 void pushAttributeValue( sal_Int32 attribute, const char* value );
158 void pushAttributeValue( sal_Int32 attribute, const OString& value );
159
160 std::unique_ptr<FastSaxSerializer> mpSerializer;
161};
162
163typedef std::shared_ptr< FastSerializerHelper > FSHelperPtr;
164
165// Helpers to make intention to pass optional attributes to *Element functions explicit, instead of
166// using `(condition) ? value.toUtf8().getStr() : nullptr` syntax.
167inline const char* UseIf(const char* s, bool bUse) { return bUse ? s : nullptr; }
168// OString, OUString
169template<class TString>
170std::optional<TString> UseIf(const TString& s, bool bUse)
171{
172 return bUse ? std::optional<TString>(s) : std::optional<TString>();
173}
174
175}
176
177#endif // INCLUDED_SAX_FSHELPER_HXX
178
179/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
void startElementNS(sal_Int32 namespaceTokenId, sal_Int32 elementTokenId, Args &&... args)
Start an element. After the first two arguments there can be a number of (attribute,...
Definition: fshelper.hxx:85
void singleElementNS(sal_Int32 namespaceTokenId, sal_Int32 elementTokenId, Args &&... args)
Create a single element. After the first two arguments there can be a number of (attribute,...
Definition: fshelper.hxx:119
void endElementNS(sal_Int32 namespaceTokenId, sal_Int32 elementTokenId)
Definition: fshelper.hxx:125
void singleElement(sal_Int32 elementTokenId, sal_Int32 attribute, const std::optional< OString > &value, Args &&... args)
Definition: fshelper.hxx:99
void singleElement(sal_Int32 elementTokenId, sal_Int32 attribute, const char *value, Args &&... args)
Create a single element. After the first argument there can be a number of (attribute,...
Definition: fshelper.hxx:92
std::unique_ptr< FastSaxSerializer > mpSerializer
Definition: fshelper.hxx:160
void startElement(sal_Int32 elementTokenId, sal_Int32 attribute, const char *value, Args &&... args)
Start an element. After the first argument there can be a number of (attribute, value) pairs.
Definition: fshelper.hxx:58
void startElement(sal_Int32 elementTokenId, sal_Int32 attribute, const std::optional< OString > &value, Args &&... args)
Definition: fshelper.hxx:65
void singleElement(sal_Int32 elementTokenId, sal_Int32 attribute, const std::optional< OUString > &value, Args &&... args)
Definition: fshelper.hxx:107
void startElement(sal_Int32 elementTokenId, sal_Int32 attribute, const std::optional< OUString > &value, Args &&... args)
Definition: fshelper.hxx:73
Any value
constexpr sal_Int32 FSNS(sal_Int32 namespc, sal_Int32 element)
Definition: fshelper.hxx:37
args
const char * UseIf(const char *s, bool bUse)
Definition: fshelper.hxx:167
std::shared_ptr< FastSerializerHelper > FSHelperPtr
bool getOutputStream(ProgramOptions const &options, OString const &extension, std::ostream **ppOutputStream, OString &targetSourceFileName, OString &tmpSourceFileName)
sal_Int32 attribute
#define SAX_DLLPUBLIC
Definition: saxdllapi.h:28
APPEND