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/.
10 #include <tools/stream.hxx>
11 #include <tools/XmlWriter.hxx>
13 #include <libxml/xmlwriter.h>
19 int funcWriteCallback(void* pContext
, const char* sBuffer
, int nLen
)
21 SvStream
* pStream
= static_cast<SvStream
*>(pContext
);
22 return static_cast<int>(pStream
->WriteBytes(sBuffer
, nLen
));
25 int funcCloseCallback(void* pContext
)
27 SvStream
* pStream
= static_cast<SvStream
*>(pContext
);
29 return 0; // 0 or -1 in case of error
33 requires(sizeof(T
) == sizeof(char)) void attributeBase64_impl(xmlTextWriterPtr writer
,
34 const char* name
, const T
* value
,
37 (void)xmlTextWriterStartAttribute(writer
, BAD_CAST(name
));
38 (void)xmlTextWriterWriteBase64(writer
, reinterpret_cast<const char*>(value
), 0, size
);
39 (void)xmlTextWriterEndAttribute(writer
);
41 } // end anonymous namespace
45 XmlWriterImpl(SvStream
* pStream
)
48 , mbWriteXmlHeader(true)
53 xmlTextWriterPtr mpWriter
;
54 bool mbWriteXmlHeader
;
57 XmlWriter::XmlWriter(SvStream
* pStream
)
58 : mpImpl(std::make_unique
<XmlWriterImpl
>(pStream
))
62 XmlWriter::~XmlWriter()
64 if (mpImpl
&& mpImpl
->mpWriter
!= nullptr)
68 bool XmlWriter::startDocument(sal_Int32 nIndent
, bool bWriteXmlHeader
)
70 mpImpl
->mbWriteXmlHeader
= bWriteXmlHeader
;
71 xmlCharEncodingHandlerPtr pEncodingHandler
= xmlGetCharEncodingHandler(XML_CHAR_ENCODING_UTF8
);
72 xmlOutputBufferPtr xmlOutBuffer
= xmlOutputBufferCreateIO(funcWriteCallback
, funcCloseCallback
,
73 mpImpl
->mpStream
, pEncodingHandler
);
74 mpImpl
->mpWriter
= xmlNewTextWriter(xmlOutBuffer
);
75 if (mpImpl
->mpWriter
== nullptr)
77 xmlTextWriterSetIndent(mpImpl
->mpWriter
, nIndent
);
78 if (mpImpl
->mbWriteXmlHeader
)
79 (void)xmlTextWriterStartDocument(mpImpl
->mpWriter
, nullptr, "UTF-8", nullptr);
83 void XmlWriter::endDocument()
85 if (mpImpl
->mbWriteXmlHeader
)
86 (void)xmlTextWriterEndDocument(mpImpl
->mpWriter
);
87 xmlFreeTextWriter(mpImpl
->mpWriter
);
88 mpImpl
->mpWriter
= nullptr;
91 void XmlWriter::startElement(const OString
& sPrefix
, const OString
& sName
,
92 const OString
& sNamespaceUri
)
94 xmlChar
* xmlName
= BAD_CAST(sName
.getStr());
95 xmlChar
* xmlPrefix
= nullptr;
96 xmlChar
* xmlNamespaceUri
= nullptr;
97 if (!sPrefix
.isEmpty())
98 xmlPrefix
= BAD_CAST(sPrefix
.getStr());
99 if (!sNamespaceUri
.isEmpty())
100 xmlNamespaceUri
= BAD_CAST(sNamespaceUri
.getStr());
102 (void)xmlTextWriterStartElementNS(mpImpl
->mpWriter
, xmlPrefix
, xmlName
, xmlNamespaceUri
);
105 void XmlWriter::startElement(const char* pName
)
107 xmlChar
* xmlName
= BAD_CAST(pName
);
108 (void)xmlTextWriterStartElement(mpImpl
->mpWriter
, xmlName
);
111 void XmlWriter::endElement() { (void)xmlTextWriterEndElement(mpImpl
->mpWriter
); }
113 void XmlWriter::attributeBase64(const char* pName
, std::vector
<sal_uInt8
> const& rValueInBytes
)
115 attributeBase64_impl(mpImpl
->mpWriter
, pName
, rValueInBytes
.data(), rValueInBytes
.size());
118 void XmlWriter::attributeBase64(const char* pName
, std::vector
<char> const& rValueInBytes
)
120 attributeBase64_impl(mpImpl
->mpWriter
, pName
, rValueInBytes
.data(), rValueInBytes
.size());
123 void XmlWriter::attribute(const char* name
, const OString
& value
)
125 xmlChar
* xmlName
= BAD_CAST(name
);
126 xmlChar
* xmlValue
= BAD_CAST(value
.getStr());
127 (void)xmlTextWriterWriteAttribute(mpImpl
->mpWriter
, xmlName
, xmlValue
);
130 void XmlWriter::attribute(const char* name
, std::u16string_view value
)
132 attribute(name
, OUStringToOString(value
, RTL_TEXTENCODING_UTF8
));
135 void XmlWriter::attribute(const char* name
, const sal_Int64 aNumber
)
137 attribute(name
, OString::number(aNumber
));
140 void XmlWriter::attributeDouble(const char* name
, const double aNumber
)
142 attribute(name
, OString::number(aNumber
));
145 void XmlWriter::content(const OString
& sValue
)
147 xmlChar
* xmlValue
= BAD_CAST(sValue
.getStr());
148 (void)xmlTextWriterWriteString(mpImpl
->mpWriter
, xmlValue
);
151 void XmlWriter::content(std::u16string_view sValue
)
153 content(OUStringToOString(sValue
, RTL_TEXTENCODING_UTF8
));
156 void XmlWriter::element(const char* sName
)
162 } // end tools namespace
164 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */