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
15 namespace tools
16 {
17 namespace
18 {
funcWriteCallback(void * pContext,const char * sBuffer,int nLen)19 int 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
funcCloseCallback(void * pContext)25 int 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
34 struct XmlWriterImpl
35 {
XmlWriterImpltools::XmlWriterImpl36 XmlWriterImpl(SvStream* pStream)
37 : mpStream(pStream)
38 , mpWriter(nullptr)
39 {
40 }
41
42 SvStream* const mpStream;
43 xmlTextWriterPtr mpWriter;
44 };
45
XmlWriter(SvStream * pStream)46 XmlWriter::XmlWriter(SvStream* pStream)
47 : mpImpl(std::make_unique<XmlWriterImpl>(pStream))
48 {
49 }
50
~XmlWriter()51 XmlWriter::~XmlWriter()
52 {
53 if (mpImpl && mpImpl->mpWriter != nullptr)
54 endDocument();
55 }
56
startDocument(sal_Int32 nIndent)57 bool XmlWriter::startDocument(sal_Int32 nIndent)
58 {
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, nIndent);
65 xmlTextWriterStartDocument(mpImpl->mpWriter, nullptr, "UTF-8", nullptr);
66 return true;
67 }
68
endDocument()69 void XmlWriter::endDocument()
70 {
71 xmlTextWriterEndDocument(mpImpl->mpWriter);
72 xmlFreeTextWriter(mpImpl->mpWriter);
73 mpImpl->mpWriter = nullptr;
74 }
75
startElement(const OString & sPrefix,const OString & sName,const OString & sNamespaceUri)76 void XmlWriter::startElement(const OString& sPrefix, const OString& sName,
77 const OString& sNamespaceUri)
78 {
79 xmlChar* xmlName = xmlCharStrdup(sName.getStr());
80 xmlChar* xmlPrefix = nullptr;
81 xmlChar* xmlNamespaceUri = nullptr;
82 if (!sPrefix.isEmpty())
83 xmlPrefix = xmlCharStrdup(sPrefix.getStr());
84 if (!sNamespaceUri.isEmpty())
85 xmlNamespaceUri = xmlCharStrdup(sNamespaceUri.getStr());
86
87 xmlTextWriterStartElementNS(mpImpl->mpWriter, xmlPrefix, xmlName, xmlNamespaceUri);
88
89 xmlFree(xmlName);
90 if (!sPrefix.isEmpty())
91 xmlFree(xmlPrefix);
92 if (!sNamespaceUri.isEmpty())
93 xmlFree(xmlNamespaceUri);
94 }
95
startElement(const OString & sName)96 void XmlWriter::startElement(const OString& sName)
97 {
98 xmlChar* xmlName = xmlCharStrdup(sName.getStr());
99 xmlTextWriterStartElement(mpImpl->mpWriter, xmlName);
100 xmlFree(xmlName);
101 }
102
endElement()103 void XmlWriter::endElement() { xmlTextWriterEndElement(mpImpl->mpWriter); }
104
attributeBase64(const OString & rsName,std::vector<sal_uInt8> const & rValueInBytes)105 void XmlWriter::attributeBase64(const OString& rsName, std::vector<sal_uInt8> const& rValueInBytes)
106 {
107 std::vector<char> aSignedBytes(rValueInBytes.begin(), rValueInBytes.end());
108 attributeBase64(rsName, aSignedBytes);
109 }
110
attributeBase64(const OString & rsName,std::vector<char> const & rValueInBytes)111 void XmlWriter::attributeBase64(const OString& rsName, std::vector<char> const& rValueInBytes)
112 {
113 xmlChar* xmlName = xmlCharStrdup(rsName.getStr());
114 xmlTextWriterStartAttribute(mpImpl->mpWriter, xmlName);
115 xmlTextWriterWriteBase64(mpImpl->mpWriter, rValueInBytes.data(), 0, rValueInBytes.size());
116 xmlTextWriterEndAttribute(mpImpl->mpWriter);
117 xmlFree(xmlName);
118 }
119
attribute(const OString & name,const OString & value)120 void XmlWriter::attribute(const OString& name, const OString& value)
121 {
122 xmlChar* xmlName = xmlCharStrdup(name.getStr());
123 xmlChar* xmlValue = xmlCharStrdup(value.getStr());
124 xmlTextWriterWriteAttribute(mpImpl->mpWriter, xmlName, xmlValue);
125 xmlFree(xmlValue);
126 xmlFree(xmlName);
127 }
128
attribute(const OString & name,const OUString & value)129 void XmlWriter::attribute(const OString& name, const OUString& value)
130 {
131 attribute(name, OUStringToOString(value, RTL_TEXTENCODING_UTF8).getStr());
132 }
133
attribute(const OString & name,const sal_Int32 aNumber)134 void XmlWriter::attribute(const OString& name, const sal_Int32 aNumber)
135 {
136 attribute(name, OUString::number(aNumber));
137 }
138
attributeDouble(const OString & name,const double aNumber)139 void XmlWriter::attributeDouble(const OString& name, const double aNumber)
140 {
141 attribute(name, OUString::number(aNumber));
142 }
143
content(const OString & sValue)144 void XmlWriter::content(const OString& sValue)
145 {
146 xmlChar* xmlValue = xmlCharStrdup(sValue.getStr());
147 xmlTextWriterWriteString(mpImpl->mpWriter, xmlValue);
148 xmlFree(xmlValue);
149 }
150
content(const OUString & sValue)151 void XmlWriter::content(const OUString& sValue)
152 {
153 content(OUStringToOString(sValue, RTL_TEXTENCODING_UTF8));
154 }
155
element(const OString & sName)156 void XmlWriter::element(const OString& sName)
157 {
158 startElement(sName);
159 endElement();
160 }
161
162 } // end tools namespace
163
164 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
165