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 23 : int lclWriteCallback(void* pContext, const char* sBuffer, int nLen)
27 : {
28 23 : SvStream* pStream = static_cast<SvStream*>(pContext);
29 23 : return (int) pStream->Write(sBuffer, nLen);
30 : }
31 :
32 10 : int lclCloseCallback(void* pContext)
33 : {
34 10 : SvStream* pStream = static_cast<SvStream*>(pContext);
35 10 : pStream->Flush();
36 10 : return 0; // 0 or -1 in case of error
37 : }
38 :
39 : } // anonymous namespace
40 :
41 10 : XmlWriter::XmlWriter(SvStream* pStream) :
42 : mpStream(pStream),
43 10 : mpWriter(NULL)
44 10 : {}
45 :
46 10 : XmlWriter::~XmlWriter()
47 10 : {}
48 :
49 10 : void XmlWriter::startDocument()
50 : {
51 10 : if (mpWriter == NULL && mpStream != NULL)
52 : {
53 10 : xmlOutputBufferPtr xmlOutBuffer = xmlOutputBufferCreateIO(lclWriteCallback, lclCloseCallback, mpStream, NULL);
54 10 : mpWriter = xmlNewTextWriter(xmlOutBuffer);
55 10 : xmlTextWriterSetIndent(mpWriter, 1);
56 10 : xmlTextWriterStartDocument(mpWriter, NULL, "UTF-8", NULL);
57 : }
58 10 : }
59 :
60 10 : void XmlWriter::endDocument()
61 : {
62 10 : xmlTextWriterEndDocument(mpWriter);
63 10 : xmlFreeTextWriter(mpWriter);
64 10 : mpWriter = NULL;
65 10 : }
66 :
67 3 : void XmlWriter::element(const OString& name)
68 : {
69 3 : startElement(name);
70 3 : endElement();
71 3 : }
72 :
73 569 : void XmlWriter::startElement(const OString& name)
74 : {
75 569 : xmlChar* xmlName = xmlCharStrdup(name.getStr());
76 569 : xmlTextWriterStartElement(mpWriter, xmlName);
77 569 : xmlFree(xmlName);
78 569 : }
79 :
80 1214 : void XmlWriter::attribute(const OString& name, const OString & value)
81 : {
82 1214 : xmlChar* xmlName = xmlCharStrdup(name.getStr());
83 1214 : xmlChar* xmlValue = xmlCharStrdup(value.getStr());
84 1214 : xmlTextWriterWriteAttribute(mpWriter, xmlName, xmlValue);
85 1214 : xmlFree(xmlValue);
86 1214 : xmlFree(xmlName);
87 1214 : }
88 :
89 1214 : void XmlWriter::attribute(const OString& name, const OUString& value)
90 : {
91 1214 : attribute(name, OUStringToOString(value, RTL_TEXTENCODING_UTF8).getStr());
92 1214 : }
93 :
94 1096 : void XmlWriter::attribute(const OString& name, const sal_Int32 aNumber)
95 : {
96 1096 : attribute(name, OUString::number(aNumber));
97 1096 : }
98 :
99 12 : void XmlWriter::content(const OUString& aValue)
100 : {
101 12 : content(OUStringToOString(aValue, RTL_TEXTENCODING_UTF8));
102 12 : }
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 569 : void XmlWriter::endElement()
112 : {
113 569 : xmlTextWriterEndElement(mpWriter);
114 569 : }
115 :
116 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|