bump product version to 5.0.4.1
[LibreOffice.git] / test / source / xmlwriter.cxx
blobde3b901902ec0547c9d6706683966007344c8dac
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/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <libxml/xmlstring.h>
21 #include <test/xmlwriter.hxx>
23 namespace
26 int lclWriteCallback(void* pContext, const char* sBuffer, int nLen)
28 SvStream* pStream = static_cast<SvStream*>(pContext);
29 return (int) pStream->Write(sBuffer, nLen);
32 int lclCloseCallback(void* pContext)
34 SvStream* pStream = static_cast<SvStream*>(pContext);
35 pStream->Flush();
36 return 0; // 0 or -1 in case of error
39 } // anonymous namespace
41 XmlWriter::XmlWriter(SvStream* pStream) :
42 mpStream(pStream),
43 mpWriter(NULL)
46 XmlWriter::~XmlWriter()
49 void XmlWriter::startDocument()
51 if (mpWriter == NULL && mpStream != NULL)
53 xmlOutputBufferPtr xmlOutBuffer = xmlOutputBufferCreateIO(lclWriteCallback, lclCloseCallback, mpStream, NULL);
54 mpWriter = xmlNewTextWriter(xmlOutBuffer);
55 xmlTextWriterSetIndent(mpWriter, 1);
56 xmlTextWriterStartDocument(mpWriter, NULL, "UTF-8", NULL);
60 void XmlWriter::endDocument()
62 xmlTextWriterEndDocument(mpWriter);
63 xmlFreeTextWriter(mpWriter);
64 mpWriter = NULL;
67 void XmlWriter::element(const OString& name)
69 startElement(name);
70 endElement();
73 void XmlWriter::startElement(const OString& name)
75 xmlChar* xmlName = xmlCharStrdup(name.getStr());
76 xmlTextWriterStartElement(mpWriter, xmlName);
77 xmlFree(xmlName);
80 void XmlWriter::attribute(const OString& name, const OString & value)
82 xmlChar* xmlName = xmlCharStrdup(name.getStr());
83 xmlChar* xmlValue = xmlCharStrdup(value.getStr());
84 xmlTextWriterWriteAttribute(mpWriter, xmlName, xmlValue);
85 xmlFree(xmlValue);
86 xmlFree(xmlName);
89 void XmlWriter::attribute(const OString& name, const OUString& value)
91 attribute(name, OUStringToOString(value, RTL_TEXTENCODING_UTF8).getStr());
94 void XmlWriter::attribute(const OString& name, const sal_Int32 aNumber)
96 attribute(name, OUString::number(aNumber));
99 void XmlWriter::content(const OUString& aValue)
101 content(OUStringToOString(aValue, RTL_TEXTENCODING_UTF8));
104 void XmlWriter::content(const OString& aValue)
106 xmlChar* xmlValue = xmlCharStrdup(aValue.getStr());
107 xmlTextWriterWriteString(mpWriter, xmlValue);
108 xmlFree(xmlValue);
111 void XmlWriter::endElement()
113 xmlTextWriterEndElement(mpWriter);
116 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */