1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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/.
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
) :
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
)
37 void HtmlWriter::start(const OString
& aElement
)
41 mrStream
.WriteChar('>');
43 mrStream
.WriteChar('\n');
45 maElementStack
.push_back(aElement
);
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
)
66 void HtmlWriter::flushStack()
68 while (!maElementStack
.empty())
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() << "'");
82 void HtmlWriter::end()
86 mrStream
.WriteOString("/>");
88 mrStream
.WriteOString("\n");
89 mbOpeningTagOpen
= false;
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(">");
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: */