summaryrefslogtreecommitdiff
path: root/svtools/source/svhtml
diff options
context:
space:
mode:
authorTomaž Vajngerl <tomaz.vajngerl@collabora.com>2014-03-15 23:09:09 +0100
committerTomaž Vajngerl <tomaz.vajngerl@collabora.com>2014-04-06 22:02:10 +0200
commit96c548a7c6700cbc5206e5ea80c1eb87d349f3a3 (patch)
tree5f2efc5323017cecbfa0d98a8be015a90ae27b9f /svtools/source/svhtml
parent82a3eb625a846207839de7abc65ee14d3045c143 (diff)
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
Diffstat (limited to 'svtools/source/svhtml')
-rw-r--r--svtools/source/svhtml/HtmlWriter.cxx101
1 files changed, 101 insertions, 0 deletions
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: */