summaryrefslogtreecommitdiffstats
path: root/src/qhpxmlwriter.cpp
blob: b3ab95a683f4bbde371ada7f1626f2fa9dcd0231 (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
153
154
155
/*
 * Copyright (C) 2008 by Sebastian Pipping.
 * Copyright (C) 2008 Dimitri van Heesch.
 *
 * Permission to use, copy, modify, and distribute this software and its
 * documentation under the terms of the GNU General Public License is hereby
 * granted. No representations are made about the suitability of this software
 * for any purpose. It is provided "as is" without express or implied warranty.
 * See the GNU General Public License for more details.
 *
 * Documents produced by Doxygen are derivative works derived from the
 * input used in their production; they are not affected by this license.
 *
 * Sebastian Pipping <sebastian@pipping.org>
 */

#include "qhpxmlwriter.h"
#include "util.h"
#include "qcstring.h"

QhpXmlWriter::QhpXmlWriter()
    : m_indentLevel(0), m_curLineIndented(false), m_compress(false)
{
}

QhpXmlWriter::~QhpXmlWriter()
{
}

void QhpXmlWriter::setIndentLevel(int level)
{
  m_indentLevel = level;
}

void QhpXmlWriter::setCompressionEnabled(bool enabled)
{
  m_compress = enabled;
}

void QhpXmlWriter::insert(QhpXmlWriter const & source)
{
  m_backend << source.m_backend.str();
}

void QhpXmlWriter::dumpTo(TextStream & file)
{
  file << m_backend.str();
}

void QhpXmlWriter::open(const QCString &elementName,
    const char * const attributes[])
{
  indent();
  openPure(elementName, attributes);
  newLine();
  m_indentLevel++;
}

void QhpXmlWriter::openClose(const QCString &elementName,
    const char * const attributes[])
{
  indent();
  openClosePure(elementName, attributes);
  newLine();
}

void QhpXmlWriter::openCloseContent(const QCString &elementName,
    const QCString &content)
{
  indent();
  openPure(elementName);
  m_backend << convertToXML(content);
  closePure(elementName);
  newLine();
}

void QhpXmlWriter::close(const QCString &elementName)
{
  m_indentLevel--;
  indent();
  closePure(elementName);
  newLine();
}

void QhpXmlWriter::declaration(const QCString &version, const QCString &encoding)
{
  m_backend << "<?xml version=\"" << version << "\" encoding=\"" << encoding << "\"?>";
  newLine();
}

void QhpXmlWriter::indent()
{
  if (m_curLineIndented)
  {
    return;
  }
  for (int i = 0; i < m_indentLevel; i++)
  {
    m_backend << "  ";
  }
  m_curLineIndented = true;
}

void QhpXmlWriter::newLine()
{
  if (!m_compress)
  {
    m_backend << "\n";
    m_curLineIndented = false;
  }
}

void QhpXmlWriter::openPureHelper(const QCString &elementName,
                                  const char * const attributes[], bool close)
{
  m_backend << "<" << elementName;
  if (attributes)
  {
    for (const char * const * walker = attributes;
        walker[0]; walker += 2)
    {
      const char *const key = walker[0];
      const char *const value = walker[1];
      if (!value)
      {
        continue;
      }
      m_backend << " " << key << "=\"" << convertToXML(value) << "\"";
    }
  }

  if (close)
  {
    m_backend << " /";
  }
  m_backend << ">";
}

void QhpXmlWriter::openPure(const QCString &elementName,
                            const char * const attributes[])
{
  openPureHelper(elementName, attributes, false);
}

void QhpXmlWriter::openClosePure(const QCString &elementName,
                                 const char * const attributes[])
{
  openPureHelper(elementName, attributes, true);
}

void QhpXmlWriter::closePure(const QCString &elementName)
{
  m_backend << "</" << elementName << ">";
}