diff options
-rw-r--r-- | include/svtools/HtmlWriter.hxx | 41 | ||||
-rw-r--r-- | svtools/Library_svt.mk | 1 | ||||
-rw-r--r-- | svtools/source/svhtml/HtmlWriter.cxx | 101 |
3 files changed, 143 insertions, 0 deletions
diff --git a/include/svtools/HtmlWriter.hxx b/include/svtools/HtmlWriter.hxx new file mode 100644 index 000000000000..7bcb2387947e --- /dev/null +++ b/include/svtools/HtmlWriter.hxx @@ -0,0 +1,41 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + */ + +#ifndef INCLUDED_HTML_HXX +#define INCLUDED_HTML_HXX + +#include <rtl/string.hxx> +#include <tools/stream.hxx> +#include <vector> +#include <svtools/svtdllapi.h> + +class SVT_DLLPUBLIC HtmlWriter +{ +private: + std::vector<OString> mElementStack; + SvStream& mStream; + + bool mElementOpen; + bool mContentWritten; + +public: + HtmlWriter(SvStream& rStream); + virtual ~HtmlWriter(); + + void start(OString aElement); + void end(); + void write(OString aContent); + void attribute(OString aAttribute, OString aValue); + void endAttribute(); +}; + +#endif + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/svtools/Library_svt.mk b/svtools/Library_svt.mk index 47628080b2c3..192f14a31c81 100644 --- a/svtools/Library_svt.mk +++ b/svtools/Library_svt.mk @@ -197,6 +197,7 @@ $(eval $(call gb_Library_add_exception_objects,svt,\ svtools/source/svhtml/htmlkywd \ svtools/source/svhtml/htmlout \ svtools/source/svhtml/htmlsupp \ + svtools/source/svhtml/HtmlWriter \ svtools/source/svhtml/parhtml \ svtools/source/svrtf/parrtf \ svtools/source/svrtf/rtfkeywd \ diff --git a/svtools/source/svhtml/HtmlWriter.cxx b/svtools/source/svhtml/HtmlWriter.cxx new file mode 100644 index 000000000000..36a3a4feb19b --- /dev/null +++ b/svtools/source/svhtml/HtmlWriter.cxx @@ -0,0 +1,101 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + */ + +#include <svtools/HtmlWriter.hxx> + +HtmlWriter::HtmlWriter(SvStream& rStream) : + mStream(rStream), + mElementOpen(false), + mContentWritten(false) +{} + +HtmlWriter::~HtmlWriter() +{} + +void HtmlWriter::start(OString aElement) +{ + if (mElementOpen) + { + mStream.WriteChar('>'); + if (!mContentWritten) + mStream.WriteChar('\n'); + mContentWritten = false; + } + mElementStack.push_back(aElement); + + for(size_t i = 0; i < mElementStack.size() - 1; i++) + mStream.WriteCharPtr(" "); + + mStream.WriteChar('<'); + mStream.WriteOString(aElement); + mElementOpen = true; +} + +void HtmlWriter::endAttribute() +{ + if (mElementOpen) + { + mStream.WriteCharPtr(" />"); + mStream.WriteCharPtr("\n"); + mElementOpen = false; + } +} + +void HtmlWriter::end() +{ + if (mElementOpen) + { + mStream.WriteCharPtr(" />"); + mStream.WriteCharPtr("\n"); + } + else + { + if (!mContentWritten) + { + for(size_t i = 0; i < mElementStack.size() - 1; i++) + { + mStream.WriteCharPtr(" "); + } + } + mStream.WriteCharPtr("</"); + mStream.WriteOString(mElementStack.back()); + mStream.WriteCharPtr(">\n"); + } + mElementStack.pop_back(); + mElementOpen = false; + mContentWritten = false; +} + +void HtmlWriter::write(OString aContent) +{ + if (mElementOpen) + { + mStream.WriteChar('>'); + mElementOpen = false; + } + mContentWritten = true; + mStream.WriteOString(aContent); +} + +void HtmlWriter::attribute(OString aAttribute, OString aValue) +{ + if (mElementOpen && !aAttribute.isEmpty() && !aValue.isEmpty()) + { + mStream.WriteChar(' '); + mStream.WriteOString(aAttribute); + mStream.WriteChar('='); + mStream.WriteChar('"'); + mStream.WriteOString(aValue); + mStream.WriteChar('"'); + } +} + + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |