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
This commit is contained in:
41
include/svtools/HtmlWriter.hxx
Normal file
41
include/svtools/HtmlWriter.hxx
Normal file
@@ -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: */
|
@@ -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 \
|
||||
|
101
svtools/source/svhtml/HtmlWriter.cxx
Normal file
101
svtools/source/svhtml/HtmlWriter.cxx
Normal file
@@ -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: */
|
Reference in New Issue
Block a user