Line data Source code
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 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #include <libxml/xmlstring.h>
21 : #include <test/xmlwriter.hxx>
22 :
23 : namespace
24 : {
25 :
26 38 : int lclWriteCallback(void* pContext, const char* sBuffer, int nLen)
27 : {
28 38 : SvStream* pStream = static_cast<SvStream*>(pContext);
29 38 : return (int) pStream->Write(sBuffer, nLen);
30 : }
31 :
32 18 : int lclCloseCallback(void* pContext)
33 : {
34 18 : SvStream* pStream = static_cast<SvStream*>(pContext);
35 18 : pStream->Flush();
36 18 : return 0; // 0 or -1 in case of error
37 : }
38 :
39 : } // anonymous namespace
40 :
41 18 : XmlWriter::XmlWriter(SvStream* pStream) :
42 : mpStream(pStream),
43 18 : mpWriter(NULL)
44 18 : {}
45 :
46 18 : XmlWriter::~XmlWriter()
47 18 : {}
48 :
49 18 : void XmlWriter::startDocument()
50 : {
51 18 : if (mpWriter == NULL && mpStream != NULL)
52 : {
53 18 : xmlOutputBufferPtr xmlOutBuffer = xmlOutputBufferCreateIO(lclWriteCallback, lclCloseCallback, mpStream, NULL);
54 18 : mpWriter = xmlNewTextWriter(xmlOutBuffer);
55 18 : xmlTextWriterSetIndent(mpWriter, 1);
56 18 : xmlTextWriterStartDocument(mpWriter, NULL, "UTF-8", NULL);
57 : }
58 18 : }
59 :
60 18 : void XmlWriter::endDocument()
61 : {
62 18 : xmlTextWriterEndDocument(mpWriter);
63 18 : xmlFreeTextWriter(mpWriter);
64 18 : mpWriter = NULL;
65 18 : }
66 :
67 0 : void XmlWriter::element(const OString& name)
68 : {
69 0 : startElement(name);
70 0 : endElement();
71 0 : }
72 :
73 490 : void XmlWriter::startElement(const OString& name)
74 : {
75 490 : xmlChar* xmlName = xmlCharStrdup(name.getStr());
76 490 : xmlTextWriterStartElement(mpWriter, xmlName);
77 490 : xmlFree(xmlName);
78 490 : }
79 :
80 1256 : void XmlWriter::attribute(const OString& name, const OString & value)
81 : {
82 1256 : xmlChar* xmlName = xmlCharStrdup(name.getStr());
83 1256 : xmlChar* xmlValue = xmlCharStrdup(value.getStr());
84 1256 : xmlTextWriterWriteAttribute(mpWriter, xmlName, xmlValue);
85 1256 : xmlFree(xmlValue);
86 1256 : xmlFree(xmlName);
87 1256 : }
88 :
89 1256 : void XmlWriter::attribute(const OString& name, const OUString& value)
90 : {
91 1256 : attribute(name, OUStringToOString(value, RTL_TEXTENCODING_UTF8).getStr());
92 1256 : }
93 :
94 1068 : void XmlWriter::attribute(const OString& name, const sal_Int32 aNumber)
95 : {
96 1068 : attribute(name, OUString::number(aNumber));
97 1068 : }
98 :
99 24 : void XmlWriter::content(const OUString& aValue)
100 : {
101 24 : content(OUStringToOString(aValue, RTL_TEXTENCODING_UTF8));
102 24 : }
103 :
104 24 : void XmlWriter::content(const OString& aValue)
105 : {
106 24 : xmlChar* xmlValue = xmlCharStrdup(aValue.getStr());
107 24 : xmlTextWriterWriteString(mpWriter, xmlValue);
108 24 : xmlFree(xmlValue);
109 24 : }
110 :
111 490 : void XmlWriter::endElement()
112 : {
113 490 : xmlTextWriterEndElement(mpWriter);
114 490 : }
115 :
116 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|