From 96c548a7c6700cbc5206e5ea80c1eb87d349f3a3 Mon Sep 17 00:00:00 2001 From: Tomaž Vajngerl Date: Sat, 15 Mar 2014 23:09:09 +0100 Subject: svtools: HtmlWriter - for writing HTML structure to a stream HtmlWriter is used to write the structure of a HTML document to a stream. The goal is to abstract the messy construction of strings when writing attributes of a html element and other HTML specifics needed when structuring a HTML document. Change-Id: Ibdf42914e43ef02f16a43e4230575ed7340e68d8 --- svtools/source/svhtml/HtmlWriter.cxx | 101 +++++++++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 svtools/source/svhtml/HtmlWriter.cxx (limited to 'svtools/source/svhtml') 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 + +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("\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: */ -- cgit