summaryrefslogtreecommitdiff
path: root/svtools/source/svhtml/HtmlWriter.cxx
blob: cd73bad074dafa5453d9c7c7c2ed99c4f5680102 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/* -*- 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>
#include <tools/stream.hxx>
#include <sal/log.hxx>
#include <svtools/htmlout.hxx>

HtmlWriter::HtmlWriter(SvStream& rStream, std::string_view rNamespace) :
    mrStream(rStream),
    mbPrettyPrint(true)
{
    if (!rNamespace.empty())
    {
        // Convert namespace alias to a prefix.
        maNamespace = OString::Concat(rNamespace) + ":";
    }
}

HtmlWriter::~HtmlWriter()
{
    assert(maElementStack.empty());
}

void HtmlWriter::prettyPrint(bool b)
{
    mbPrettyPrint = b;
}

void HtmlWriter::start(const OString& aElement)
{
    if (mbOpeningTagOpen)
    {
        mrStream.WriteChar('>');
        if (mbPrettyPrint)
            mrStream.WriteChar('\n');
    }
    maElementStack.push_back(aElement);

    if (mbPrettyPrint)
    {
        for(size_t i = 0; i < maElementStack.size() - 1; i++)
        {
            mrStream.WriteOString("  ");
        }
    }

    mrStream.WriteChar('<');
    mrStream.WriteOString(Concat2View(maNamespace + aElement));
    mbOpeningTagOpen = true;
}

void HtmlWriter::single(const OString &aContent)
{
    start(aContent);
    end();
}

void HtmlWriter::flushStack()
{
    while (!maElementStack.empty())
    {
        end();
    }
}

bool HtmlWriter::end(const OString& aElement)
{
    bool bExpected = maElementStack.back() == aElement;
    SAL_WARN_IF(!bExpected, "svtools", "HtmlWriter: end element mismatch - '" << aElement << "' expected '" << maElementStack.back() << "'");
    end();
    return bExpected;
}

void HtmlWriter::end()
{
    if (mbOpeningTagOpen)
    {
        mrStream.WriteOString("/>");
        if (mbPrettyPrint)
            mrStream.WriteOString("\n");
        mbOpeningTagOpen = false;
    }
    else
    {
        if (mbPrettyPrint)
        {
            for(size_t i = 0; i < maElementStack.size() - 1; i++)
            {
                mrStream.WriteOString("  ");
            }
        }
        mrStream.WriteOString("</");
        mrStream.WriteOString(Concat2View(maNamespace + maElementStack.back()));
        mrStream.WriteOString(">");
        if (mbPrettyPrint)
            mrStream.WriteOString("\n");
    }
    maElementStack.pop_back();
}

void HtmlWriter::attribute(std::string_view aAttribute, std::string_view aValue)
{
    attribute(aAttribute, OStringToOUString(aValue, RTL_TEXTENCODING_UTF8));
}

void HtmlWriter::attribute(std::string_view aAttribute, const sal_Int32 aValue)
{
    attribute(aAttribute, OString::number(aValue));
}

void HtmlWriter::attribute(std::string_view aAttribute, const OUString& aValue)
{
    assert(mbOpeningTagOpen);
    if (mbOpeningTagOpen && !aAttribute.empty() && !aValue.isEmpty())
    {
        mrStream.WriteChar(' ');
        mrStream.WriteOString(aAttribute);
        mrStream.WriteOString("=\"");
        HTMLOutFuncs::Out_String(mrStream, aValue);
        mrStream.WriteChar('"');
    }
}

void HtmlWriter::attribute(std::string_view aAttribute)
{
    assert(mbOpeningTagOpen);
    if (mbOpeningTagOpen && !aAttribute.empty())
    {
        mrStream.WriteChar(' ');
        mrStream.WriteOString(aAttribute);
    }
}

void HtmlWriter::characters(std::string_view rChars)
{
    if (mbOpeningTagOpen)
    {
        mrStream.WriteOString(">");
        mbOpeningTagOpen = false;
    }
    mrStream.WriteOString(rChars);
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */