Bump version to 5.0-14
[LibreOffice.git] / svtools / source / svhtml / HtmlWriter.cxx
blobf075d40f7804c7fe944b4751f8506df9f5b83486
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 */
11 #include <svtools/HtmlWriter.hxx>
13 HtmlWriter::HtmlWriter(SvStream& rStream) :
14 mrStream(rStream),
15 mbElementOpen(false),
16 mbContentWritten(false),
17 mbPrettyPrint(true),
18 maEncoding(RTL_TEXTENCODING_UTF8)
21 HtmlWriter::~HtmlWriter()
24 void HtmlWriter::prettyPrint(bool bChoice)
26 mbPrettyPrint = bChoice;
29 void HtmlWriter::start(const OString& aElement)
31 if (mbElementOpen)
33 mrStream.WriteChar('>');
34 if (!mbContentWritten && mbPrettyPrint)
35 mrStream.WriteChar('\n');
36 mbContentWritten = false;
38 maElementStack.push_back(aElement);
40 if (mbPrettyPrint)
42 for(size_t i = 0; i < maElementStack.size() - 1; i++)
44 mrStream.WriteCharPtr(" ");
48 mrStream.WriteChar('<');
49 mrStream.WriteOString(aElement);
50 mbElementOpen = true;
53 void HtmlWriter::single(const OString &aContent)
55 start(aContent);
56 end();
59 void HtmlWriter::endAttribute()
61 if (mbElementOpen)
63 mrStream.WriteCharPtr("/>");
64 if (mbPrettyPrint)
65 mrStream.WriteCharPtr("\n");
66 mbElementOpen = false;
70 void HtmlWriter::flushStack()
72 while (!maElementStack.empty())
74 end();
78 void HtmlWriter::flushStack(const OString& aElement)
80 OString sCurrentElement;
83 sCurrentElement = maElementStack.back();
84 end();
85 } while (!maElementStack.empty() && aElement != sCurrentElement);
88 void HtmlWriter::end()
90 if (mbElementOpen)
92 mrStream.WriteCharPtr("/>");
93 if (mbPrettyPrint)
94 mrStream.WriteCharPtr("\n");
96 else
98 if (!mbContentWritten && mbPrettyPrint)
100 for(size_t i = 0; i < maElementStack.size() - 1; i++)
102 mrStream.WriteCharPtr(" ");
105 mrStream.WriteCharPtr("</");
106 mrStream.WriteOString(maElementStack.back());
107 mrStream.WriteCharPtr(">");
108 if (mbPrettyPrint)
109 mrStream.WriteCharPtr("\n");
111 maElementStack.pop_back();
112 mbElementOpen = false;
113 mbContentWritten = false;
116 void HtmlWriter::write(const OString &aContent)
118 if (mbElementOpen)
120 mrStream.WriteChar('>');
121 mbElementOpen = false;
123 mbContentWritten = true;
124 mrStream.WriteOString(aContent);
127 void HtmlWriter::attribute(const OString &aAttribute, const OString& aValue)
129 if (mbElementOpen && !aAttribute.isEmpty() && !aValue.isEmpty())
131 mrStream.WriteChar(' ');
132 mrStream.WriteOString(aAttribute);
133 mrStream.WriteChar('=');
134 mrStream.WriteChar('"');
135 mrStream.WriteOString(aValue);
136 mrStream.WriteChar('"');
140 void HtmlWriter::attribute(const OString& aAttribute, const sal_Int32 aValue)
142 attribute(aAttribute, OString::number(aValue));
145 void HtmlWriter::attribute(const OString& aAttribute, const char* pValue)
147 attribute(aAttribute, OString(pValue));
150 void HtmlWriter::attribute(const OString& aAttribute, const OUString& aValue)
152 attribute(aAttribute, OUStringToOString(aValue, maEncoding));
155 void HtmlWriter::attribute(const OString& aAttribute)
157 if (mbElementOpen && !aAttribute.isEmpty())
159 mrStream.WriteChar(' ');
160 mrStream.WriteOString(aAttribute);
165 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */