tdf#154285 Check upper bound of arguments in SbRtl_Minute function
[LibreOffice.git] / svtools / source / svhtml / HtmlWriter.cxx
blobcd73bad074dafa5453d9c7c7c2ed99c4f5680102
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>
12 #include <tools/stream.hxx>
13 #include <sal/log.hxx>
14 #include <svtools/htmlout.hxx>
16 HtmlWriter::HtmlWriter(SvStream& rStream, std::string_view rNamespace) :
17 mrStream(rStream),
18 mbPrettyPrint(true)
20 if (!rNamespace.empty())
22 // Convert namespace alias to a prefix.
23 maNamespace = OString::Concat(rNamespace) + ":";
27 HtmlWriter::~HtmlWriter()
29 assert(maElementStack.empty());
32 void HtmlWriter::prettyPrint(bool b)
34 mbPrettyPrint = b;
37 void HtmlWriter::start(const OString& aElement)
39 if (mbOpeningTagOpen)
41 mrStream.WriteChar('>');
42 if (mbPrettyPrint)
43 mrStream.WriteChar('\n');
45 maElementStack.push_back(aElement);
47 if (mbPrettyPrint)
49 for(size_t i = 0; i < maElementStack.size() - 1; i++)
51 mrStream.WriteOString(" ");
55 mrStream.WriteChar('<');
56 mrStream.WriteOString(Concat2View(maNamespace + aElement));
57 mbOpeningTagOpen = true;
60 void HtmlWriter::single(const OString &aContent)
62 start(aContent);
63 end();
66 void HtmlWriter::flushStack()
68 while (!maElementStack.empty())
70 end();
74 bool HtmlWriter::end(const OString& aElement)
76 bool bExpected = maElementStack.back() == aElement;
77 SAL_WARN_IF(!bExpected, "svtools", "HtmlWriter: end element mismatch - '" << aElement << "' expected '" << maElementStack.back() << "'");
78 end();
79 return bExpected;
82 void HtmlWriter::end()
84 if (mbOpeningTagOpen)
86 mrStream.WriteOString("/>");
87 if (mbPrettyPrint)
88 mrStream.WriteOString("\n");
89 mbOpeningTagOpen = false;
91 else
93 if (mbPrettyPrint)
95 for(size_t i = 0; i < maElementStack.size() - 1; i++)
97 mrStream.WriteOString(" ");
100 mrStream.WriteOString("</");
101 mrStream.WriteOString(Concat2View(maNamespace + maElementStack.back()));
102 mrStream.WriteOString(">");
103 if (mbPrettyPrint)
104 mrStream.WriteOString("\n");
106 maElementStack.pop_back();
109 void HtmlWriter::attribute(std::string_view aAttribute, std::string_view aValue)
111 attribute(aAttribute, OStringToOUString(aValue, RTL_TEXTENCODING_UTF8));
114 void HtmlWriter::attribute(std::string_view aAttribute, const sal_Int32 aValue)
116 attribute(aAttribute, OString::number(aValue));
119 void HtmlWriter::attribute(std::string_view aAttribute, const OUString& aValue)
121 assert(mbOpeningTagOpen);
122 if (mbOpeningTagOpen && !aAttribute.empty() && !aValue.isEmpty())
124 mrStream.WriteChar(' ');
125 mrStream.WriteOString(aAttribute);
126 mrStream.WriteOString("=\"");
127 HTMLOutFuncs::Out_String(mrStream, aValue);
128 mrStream.WriteChar('"');
132 void HtmlWriter::attribute(std::string_view aAttribute)
134 assert(mbOpeningTagOpen);
135 if (mbOpeningTagOpen && !aAttribute.empty())
137 mrStream.WriteChar(' ');
138 mrStream.WriteOString(aAttribute);
142 void HtmlWriter::characters(std::string_view rChars)
144 if (mbOpeningTagOpen)
146 mrStream.WriteOString(">");
147 mbOpeningTagOpen = false;
149 mrStream.WriteOString(rChars);
152 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */