LibreOffice Module tools (master) 1
XmlWriter.cxx
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
10#include <tools/stream.hxx>
11#include <tools/XmlWriter.hxx>
12
13#include <libxml/xmlwriter.h>
14
15namespace tools
16{
17namespace
18{
19int funcWriteCallback(void* pContext, const char* sBuffer, int nLen)
20{
21 SvStream* pStream = static_cast<SvStream*>(pContext);
22 return static_cast<int>(pStream->WriteBytes(sBuffer, nLen));
23}
24
25int funcCloseCallback(void* pContext)
26{
27 SvStream* pStream = static_cast<SvStream*>(pContext);
28 pStream->Flush();
29 return 0; // 0 or -1 in case of error
30}
31
32} // end anonymous namespace
33
35{
37 : mpStream(pStream)
38 , mpWriter(nullptr)
39 , mbWriteXmlHeader(true)
40 {
41 }
42
46};
47
49 : mpImpl(std::make_unique<XmlWriterImpl>(pStream))
50{
51}
52
54{
55 if (mpImpl && mpImpl->mpWriter != nullptr)
57}
58
59bool XmlWriter::startDocument(sal_Int32 nIndent, bool bWriteXmlHeader)
60{
61 mpImpl->mbWriteXmlHeader = bWriteXmlHeader;
62 xmlCharEncodingHandlerPtr pEncodingHandler = xmlGetCharEncodingHandler(XML_CHAR_ENCODING_UTF8);
63 xmlOutputBufferPtr xmlOutBuffer = xmlOutputBufferCreateIO(funcWriteCallback, funcCloseCallback,
64 mpImpl->mpStream, pEncodingHandler);
65 mpImpl->mpWriter = xmlNewTextWriter(xmlOutBuffer);
66 if (mpImpl->mpWriter == nullptr)
67 return false;
68 xmlTextWriterSetIndent(mpImpl->mpWriter, nIndent);
69 if (mpImpl->mbWriteXmlHeader)
70 (void)xmlTextWriterStartDocument(mpImpl->mpWriter, nullptr, "UTF-8", nullptr);
71 return true;
72}
73
75{
76 if (mpImpl->mbWriteXmlHeader)
77 (void)xmlTextWriterEndDocument(mpImpl->mpWriter);
78 xmlFreeTextWriter(mpImpl->mpWriter);
79 mpImpl->mpWriter = nullptr;
80}
81
82void XmlWriter::startElement(const OString& sPrefix, const OString& sName,
83 const OString& sNamespaceUri)
84{
85 xmlChar* xmlName = BAD_CAST(sName.getStr());
86 xmlChar* xmlPrefix = nullptr;
87 xmlChar* xmlNamespaceUri = nullptr;
88 if (!sPrefix.isEmpty())
89 xmlPrefix = BAD_CAST(sPrefix.getStr());
90 if (!sNamespaceUri.isEmpty())
91 xmlNamespaceUri = BAD_CAST(sNamespaceUri.getStr());
92
93 (void)xmlTextWriterStartElementNS(mpImpl->mpWriter, xmlPrefix, xmlName, xmlNamespaceUri);
94}
95
96void XmlWriter::startElement(const char* pName)
97{
98 xmlChar* xmlName = BAD_CAST(pName);
99 (void)xmlTextWriterStartElement(mpImpl->mpWriter, xmlName);
100}
101
102void XmlWriter::startElement(const OString& rName)
103{
104 xmlChar* xmlName = BAD_CAST(rName.getStr());
105 (void)xmlTextWriterStartElement(mpImpl->mpWriter, xmlName);
106}
107
108void XmlWriter::endElement() { (void)xmlTextWriterEndElement(mpImpl->mpWriter); }
109
110void XmlWriter::attributeBase64(const char* pName, std::vector<sal_uInt8> const& rValueInBytes)
111{
112 std::vector<char> aSignedBytes(rValueInBytes.begin(), rValueInBytes.end());
113 attributeBase64(pName, aSignedBytes);
114}
115
116void XmlWriter::attributeBase64(const char* pName, std::vector<char> const& rValueInBytes)
117{
118 xmlChar* xmlName = BAD_CAST(pName);
119 (void)xmlTextWriterStartAttribute(mpImpl->mpWriter, xmlName);
120 (void)xmlTextWriterWriteBase64(mpImpl->mpWriter, rValueInBytes.data(), 0, rValueInBytes.size());
121 (void)xmlTextWriterEndAttribute(mpImpl->mpWriter);
122}
123
124void XmlWriter::attribute(const char* name, const OString& value)
125{
126 xmlChar* xmlName = BAD_CAST(name);
127 xmlChar* xmlValue = BAD_CAST(value.getStr());
128 (void)xmlTextWriterWriteAttribute(mpImpl->mpWriter, xmlName, xmlValue);
129}
130
131void XmlWriter::attribute(const OString& name, const OString& value)
132{
133 xmlChar* xmlName = BAD_CAST(name.getStr());
134 xmlChar* xmlValue = BAD_CAST(value.getStr());
135 (void)xmlTextWriterWriteAttribute(mpImpl->mpWriter, xmlName, xmlValue);
136}
137
138void XmlWriter::attribute(const char* name, std::u16string_view value)
139{
140 attribute(name, OUStringToOString(value, RTL_TEXTENCODING_UTF8));
141}
142
143void XmlWriter::attribute(const char* name, const sal_Int32 aNumber)
144{
145 attribute(name, OString::number(aNumber));
146}
147
148void XmlWriter::attributeDouble(const char* name, const double aNumber)
149{
150 attribute(name, OString::number(aNumber));
151}
152
153void XmlWriter::content(const OString& sValue)
154{
155 xmlChar* xmlValue = BAD_CAST(sValue.getStr());
156 (void)xmlTextWriterWriteString(mpImpl->mpWriter, xmlValue);
157}
158
159void XmlWriter::content(std::u16string_view sValue)
160{
161 content(OUStringToOString(sValue, RTL_TEXTENCODING_UTF8));
162}
163
164void XmlWriter::element(const char* sName)
165{
167 endElement();
168}
169
170} // end tools namespace
171
172/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
const char * pName
std::size_t WriteBytes(const void *pData, std::size_t nSize)
Definition: stream.cxx:1192
void Flush()
Call FlushBuffer() and then call flush on the underlying OS stream.
Definition: stream.cxx:1337
void content(const OString &sValue)
Definition: XmlWriter.cxx:153
bool startDocument(sal_Int32 nIndent=2, bool bWriteXmlHeader=true)
Definition: XmlWriter.cxx:59
void endDocument()
Definition: XmlWriter.cxx:74
std::unique_ptr< XmlWriterImpl > mpImpl
Definition: XmlWriter.hxx:37
void attributeDouble(const char *sTagName, double aNumber)
Definition: XmlWriter.cxx:148
void attribute(const char *sTagName, const OString &aValue)
Definition: XmlWriter.cxx:124
void attributeBase64(const char *sTagName, std::vector< sal_uInt8 > const &rValueInBytes)
Definition: XmlWriter.cxx:110
void element(const char *sName)
Definition: XmlWriter.cxx:164
XmlWriter(SvStream *pStream)
Definition: XmlWriter.cxx:48
void startElement(const char *sName)
Definition: XmlWriter.cxx:96
Any value
struct _xmlTextWriter * xmlTextWriterPtr
OUString sName
OUString sPrefix
const char * name
OString OUStringToOString(std::u16string_view str, ConnectionSettings const *settings)
Note: this class is a true marvel of engineering: because the author could not decide whether it's be...
XmlWriterImpl(SvStream *pStream)
Definition: XmlWriter.cxx:36
xmlTextWriterPtr mpWriter
Definition: XmlWriter.cxx:44
SvStream * mpStream
Definition: XmlWriter.cxx:43