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>
13 HtmlWriter::HtmlWriter(SvStream
& rStream
) :
16 mbContentWritten(false),
18 maEncoding(RTL_TEXTENCODING_UTF8
)
21 HtmlWriter::~HtmlWriter()
24 void HtmlWriter::prettyPrint(bool bChoice
)
26 mbPrettyPrint
= bChoice
;
29 void HtmlWriter::start(const OString
& aElement
)
33 mrStream
.WriteChar('>');
34 if (!mbContentWritten
&& mbPrettyPrint
)
35 mrStream
.WriteChar('\n');
36 mbContentWritten
= false;
38 maElementStack
.push_back(aElement
);
42 for(size_t i
= 0; i
< maElementStack
.size() - 1; i
++)
44 mrStream
.WriteCharPtr(" ");
48 mrStream
.WriteChar('<');
49 mrStream
.WriteOString(aElement
);
53 void HtmlWriter::single(const OString
&aContent
)
59 void HtmlWriter::endAttribute()
63 mrStream
.WriteCharPtr("/>");
65 mrStream
.WriteCharPtr("\n");
66 mbElementOpen
= false;
70 void HtmlWriter::flushStack()
72 while (!maElementStack
.empty())
78 void HtmlWriter::flushStack(const OString
& aElement
)
80 OString sCurrentElement
;
83 sCurrentElement
= maElementStack
.back();
85 } while (!maElementStack
.empty() && aElement
!= sCurrentElement
);
88 void HtmlWriter::end()
92 mrStream
.WriteCharPtr("/>");
94 mrStream
.WriteCharPtr("\n");
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(">");
109 mrStream
.WriteCharPtr("\n");
111 maElementStack
.pop_back();
112 mbElementOpen
= false;
113 mbContentWritten
= false;
116 void HtmlWriter::write(const OString
&aContent
)
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: */