XmlWriter: support namespaces, writing of base64 attribute

Change-Id: I3c1d885d239178b46578123fd83d3aa1d7ccd023
Reviewed-on: https://gerrit.libreoffice.org/56971
Tested-by: Jenkins
Reviewed-by: Tomaž Vajngerl <quikee@gmail.com>
This commit is contained in:
Tomaž Vajngerl
2018-07-04 17:18:43 +02:00
committed by Tomaž Vajngerl
parent 6db3aeb6e6
commit 72aa159a6f
2 changed files with 44 additions and 5 deletions

View File

@@ -13,6 +13,7 @@
#include <tools/toolsdllapi.h>
#include <tools/stream.hxx>
#include <memory>
#include <vector>
namespace tools
{
@@ -23,8 +24,8 @@ struct XmlWriterImpl;
* all the internal libxml2 workings and uses types that are native for LO
* development.
*
* The codepage used for XML is always "utf-8" and the output is indented so it
* is easier to read.
* The codepage used for XML is always "utf-8" and the output is indented by
* default so it is easier to read.
*
*/
class TOOLS_DLLPUBLIC XmlWriter final
@@ -37,15 +38,18 @@ public:
~XmlWriter();
bool startDocument();
bool startDocument(sal_Int32 nIndent = 2);
void endDocument();
void startElement(const OString& sName);
void startElement(const OString& sPrefix, const OString& sName, const OString& sNamespaceUri);
void endElement();
void attribute(const OString& sTagName, const OString& aValue);
void attribute(const OString& sTagName, const OUString& aValue);
void attribute(const OString& sTagName, sal_Int32 aNumber);
void attributeBase64(const OString& sTagName, std::vector<sal_uInt8> const& rValueInBytes);
void attributeBase64(const OString& sTagName, std::vector<char> const& rValueInBytes);
void content(const OString& sValue);
void content(const OUString& sValue);

View File

@@ -54,14 +54,14 @@ XmlWriter::~XmlWriter()
endDocument();
}
bool XmlWriter::startDocument()
bool XmlWriter::startDocument(sal_Int32 nIndent)
{
xmlOutputBufferPtr xmlOutBuffer
= xmlOutputBufferCreateIO(funcWriteCallback, funcCloseCallback, mpImpl->mpStream, nullptr);
mpImpl->mpWriter = xmlNewTextWriter(xmlOutBuffer);
if (mpImpl->mpWriter == nullptr)
return false;
xmlTextWriterSetIndent(mpImpl->mpWriter, 1);
xmlTextWriterSetIndent(mpImpl->mpWriter, nIndent);
xmlTextWriterStartDocument(mpImpl->mpWriter, nullptr, "UTF-8", nullptr);
return true;
}
@@ -73,6 +73,26 @@ void XmlWriter::endDocument()
mpImpl->mpWriter = nullptr;
}
void XmlWriter::startElement(const OString& sPrefix, const OString& sName,
const OString& sNamespaceUri)
{
xmlChar* xmlName = xmlCharStrdup(sName.getStr());
xmlChar* xmlPrefix = nullptr;
xmlChar* xmlNamespaceUri = nullptr;
if (!sPrefix.isEmpty())
xmlPrefix = xmlCharStrdup(sPrefix.getStr());
if (!sNamespaceUri.isEmpty())
xmlNamespaceUri = xmlCharStrdup(sNamespaceUri.getStr());
xmlTextWriterStartElementNS(mpImpl->mpWriter, xmlPrefix, xmlName, xmlNamespaceUri);
xmlFree(xmlName);
if (!sPrefix.isEmpty())
xmlFree(xmlPrefix);
if (!sNamespaceUri.isEmpty())
xmlFree(xmlNamespaceUri);
}
void XmlWriter::startElement(const OString& sName)
{
xmlChar* xmlName = xmlCharStrdup(sName.getStr());
@@ -82,6 +102,21 @@ void XmlWriter::startElement(const OString& sName)
void XmlWriter::endElement() { xmlTextWriterEndElement(mpImpl->mpWriter); }
void XmlWriter::attributeBase64(const OString& rsName, std::vector<sal_uInt8> const& rValueInBytes)
{
std::vector<char> aSignedBytes(rValueInBytes.begin(), rValueInBytes.end());
attributeBase64(rsName, aSignedBytes);
}
void XmlWriter::attributeBase64(const OString& rsName, std::vector<char> const& rValueInBytes)
{
xmlChar* xmlName = xmlCharStrdup(rsName.getStr());
xmlTextWriterStartAttribute(mpImpl->mpWriter, xmlName);
xmlTextWriterWriteBase64(mpImpl->mpWriter, rValueInBytes.data(), 0, rValueInBytes.size());
xmlTextWriterEndAttribute(mpImpl->mpWriter);
xmlFree(xmlName);
}
void XmlWriter::attribute(const OString& name, const OString& value)
{
xmlChar* xmlName = xmlCharStrdup(name.getStr());