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:
committed by
Tomaž Vajngerl
parent
6db3aeb6e6
commit
72aa159a6f
@@ -13,6 +13,7 @@
|
|||||||
#include <tools/toolsdllapi.h>
|
#include <tools/toolsdllapi.h>
|
||||||
#include <tools/stream.hxx>
|
#include <tools/stream.hxx>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
namespace tools
|
namespace tools
|
||||||
{
|
{
|
||||||
@@ -23,8 +24,8 @@ struct XmlWriterImpl;
|
|||||||
* all the internal libxml2 workings and uses types that are native for LO
|
* all the internal libxml2 workings and uses types that are native for LO
|
||||||
* development.
|
* development.
|
||||||
*
|
*
|
||||||
* The codepage used for XML is always "utf-8" and the output is indented so it
|
* The codepage used for XML is always "utf-8" and the output is indented by
|
||||||
* is easier to read.
|
* default so it is easier to read.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
class TOOLS_DLLPUBLIC XmlWriter final
|
class TOOLS_DLLPUBLIC XmlWriter final
|
||||||
@@ -37,15 +38,18 @@ public:
|
|||||||
|
|
||||||
~XmlWriter();
|
~XmlWriter();
|
||||||
|
|
||||||
bool startDocument();
|
bool startDocument(sal_Int32 nIndent = 2);
|
||||||
void endDocument();
|
void endDocument();
|
||||||
|
|
||||||
void startElement(const OString& sName);
|
void startElement(const OString& sName);
|
||||||
|
void startElement(const OString& sPrefix, const OString& sName, const OString& sNamespaceUri);
|
||||||
void endElement();
|
void endElement();
|
||||||
|
|
||||||
void attribute(const OString& sTagName, const OString& aValue);
|
void attribute(const OString& sTagName, const OString& aValue);
|
||||||
void attribute(const OString& sTagName, const OUString& aValue);
|
void attribute(const OString& sTagName, const OUString& aValue);
|
||||||
void attribute(const OString& sTagName, sal_Int32 aNumber);
|
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 OString& sValue);
|
||||||
void content(const OUString& sValue);
|
void content(const OUString& sValue);
|
||||||
|
@@ -54,14 +54,14 @@ XmlWriter::~XmlWriter()
|
|||||||
endDocument();
|
endDocument();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool XmlWriter::startDocument()
|
bool XmlWriter::startDocument(sal_Int32 nIndent)
|
||||||
{
|
{
|
||||||
xmlOutputBufferPtr xmlOutBuffer
|
xmlOutputBufferPtr xmlOutBuffer
|
||||||
= xmlOutputBufferCreateIO(funcWriteCallback, funcCloseCallback, mpImpl->mpStream, nullptr);
|
= xmlOutputBufferCreateIO(funcWriteCallback, funcCloseCallback, mpImpl->mpStream, nullptr);
|
||||||
mpImpl->mpWriter = xmlNewTextWriter(xmlOutBuffer);
|
mpImpl->mpWriter = xmlNewTextWriter(xmlOutBuffer);
|
||||||
if (mpImpl->mpWriter == nullptr)
|
if (mpImpl->mpWriter == nullptr)
|
||||||
return false;
|
return false;
|
||||||
xmlTextWriterSetIndent(mpImpl->mpWriter, 1);
|
xmlTextWriterSetIndent(mpImpl->mpWriter, nIndent);
|
||||||
xmlTextWriterStartDocument(mpImpl->mpWriter, nullptr, "UTF-8", nullptr);
|
xmlTextWriterStartDocument(mpImpl->mpWriter, nullptr, "UTF-8", nullptr);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -73,6 +73,26 @@ void XmlWriter::endDocument()
|
|||||||
mpImpl->mpWriter = nullptr;
|
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)
|
void XmlWriter::startElement(const OString& sName)
|
||||||
{
|
{
|
||||||
xmlChar* xmlName = xmlCharStrdup(sName.getStr());
|
xmlChar* xmlName = xmlCharStrdup(sName.getStr());
|
||||||
@@ -82,6 +102,21 @@ void XmlWriter::startElement(const OString& sName)
|
|||||||
|
|
||||||
void XmlWriter::endElement() { xmlTextWriterEndElement(mpImpl->mpWriter); }
|
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)
|
void XmlWriter::attribute(const OString& name, const OString& value)
|
||||||
{
|
{
|
||||||
xmlChar* xmlName = xmlCharStrdup(name.getStr());
|
xmlChar* xmlName = xmlCharStrdup(name.getStr());
|
||||||
|
Reference in New Issue
Block a user