qhpxmlwriter.cpp 3.31 KB
Newer Older
Dimitri van Heesch's avatar
Dimitri van Heesch committed
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
/*
 * 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 <qfile.h>

QhpXmlWriter::QhpXmlWriter() 
    : m_out(&m_backend), 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_out << source.m_backend.data();
}

void QhpXmlWriter::dumpTo(QFile & file)
{
  file.writeBlock(m_backend.data(), m_backend.length());
}

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

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

void QhpXmlWriter::openCloseContent(char const * elementName,
    char const * content)
{
  indent();
  openPure(elementName);
  m_out << convertToXML(content);
  closePure(elementName);
  newLine();
}

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

87 88 89 90 91 92
void QhpXmlWriter::declaration(char const * version, char const * encoding)
{
  m_out << "<?xml version=\"" << version << "\" encoding=\"" << encoding << "\"?>";
  newLine();
}

Dimitri van Heesch's avatar
Dimitri van Heesch committed
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 156 157
void QhpXmlWriter::indent()
{
  if (m_curLineIndented)
  {
    return;
  }
  for (int i = 0; i < m_indentLevel; i++) 
  {
    m_out << "  ";
  }
  m_curLineIndented = true;
}

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

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

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

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

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

void QhpXmlWriter::closePure(char const * elementName)
{
  m_out << "</" << elementName << ">";
}