Version 6.1.4.1, tag libreoffice-6.1.4.1
[LibreOffice.git] / tools / source / xml / XmlWriter.cxx
blobba2c3936f51bd9f2cc0c75b693a8e12775532be3
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/.
8 */
10 #include <tools/XmlWriter.hxx>
11 #include <o3tl/make_unique.hxx>
13 #include <libxml/xmlwriter.h>
15 namespace tools
17 namespace
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);
28 pStream->Flush();
29 return 0; // 0 or -1 in case of error
32 } // end anonymous namespace
34 struct XmlWriterImpl
36 XmlWriterImpl(SvStream* pStream)
37 : mpStream(pStream)
38 , mpWriter(nullptr)
42 SvStream* mpStream;
43 xmlTextWriterPtr mpWriter;
46 XmlWriter::XmlWriter(SvStream* pStream)
47 : mpImpl(o3tl::make_unique<XmlWriterImpl>(pStream))
51 XmlWriter::~XmlWriter()
53 if (mpImpl && mpImpl->mpWriter != nullptr)
54 endDocument();
57 bool XmlWriter::startDocument()
59 xmlOutputBufferPtr xmlOutBuffer
60 = xmlOutputBufferCreateIO(funcWriteCallback, funcCloseCallback, mpImpl->mpStream, nullptr);
61 mpImpl->mpWriter = xmlNewTextWriter(xmlOutBuffer);
62 if (mpImpl->mpWriter == nullptr)
63 return false;
64 xmlTextWriterSetIndent(mpImpl->mpWriter, 1);
65 xmlTextWriterStartDocument(mpImpl->mpWriter, nullptr, "UTF-8", nullptr);
66 return true;
69 void XmlWriter::endDocument()
71 xmlTextWriterEndDocument(mpImpl->mpWriter);
72 xmlFreeTextWriter(mpImpl->mpWriter);
73 mpImpl->mpWriter = nullptr;
76 void XmlWriter::startElement(const OString& sName)
78 xmlChar* xmlName = xmlCharStrdup(sName.getStr());
79 xmlTextWriterStartElement(mpImpl->mpWriter, xmlName);
80 xmlFree(xmlName);
83 void XmlWriter::endElement() { xmlTextWriterEndElement(mpImpl->mpWriter); }
85 void XmlWriter::attribute(const OString& name, const OString& value)
87 xmlChar* xmlName = xmlCharStrdup(name.getStr());
88 xmlChar* xmlValue = xmlCharStrdup(value.getStr());
89 xmlTextWriterWriteAttribute(mpImpl->mpWriter, xmlName, xmlValue);
90 xmlFree(xmlValue);
91 xmlFree(xmlName);
94 void XmlWriter::attribute(const OString& name, const OUString& value)
96 attribute(name, OUStringToOString(value, RTL_TEXTENCODING_UTF8).getStr());
99 void XmlWriter::attribute(const OString& name, const sal_Int32 aNumber)
101 attribute(name, OUString::number(aNumber));
104 void XmlWriter::content(const OString& sValue)
106 xmlChar* xmlValue = xmlCharStrdup(sValue.getStr());
107 xmlTextWriterWriteString(mpImpl->mpWriter, xmlValue);
108 xmlFree(xmlValue);
111 void XmlWriter::content(const OUString& sValue)
113 content(OUStringToOString(sValue, RTL_TEXTENCODING_UTF8));
116 void XmlWriter::element(const OString& sName)
118 startElement(sName);
119 endElement();
122 } // end tools namespace
124 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */