Orb/Doxygen/src/qhpxmlwriter.cpp
changeset 0 42188c7ea2d9
equal deleted inserted replaced
-1:000000000000 0:42188c7ea2d9
       
     1 /*
       
     2  * Copyright (C) 2008 by Sebastian Pipping.
       
     3  * Copyright (C) 2008 Dimitri van Heesch.
       
     4  *
       
     5  * Permission to use, copy, modify, and distribute this software and its
       
     6  * documentation under the terms of the GNU General Public License is hereby 
       
     7  * granted. No representations are made about the suitability of this software 
       
     8  * for any purpose. It is provided "as is" without express or implied warranty.
       
     9  * See the GNU General Public License for more details.
       
    10  *
       
    11  * Documents produced by Doxygen are derivative works derived from the
       
    12  * input used in their production; they are not affected by this license.
       
    13  *
       
    14  * Sebastian Pipping <sebastian@pipping.org>
       
    15  */
       
    16 
       
    17 #include "qhpxmlwriter.h"
       
    18 #include "util.h"
       
    19 
       
    20 #include <qfile.h>
       
    21 
       
    22 QhpXmlWriter::QhpXmlWriter() 
       
    23     : m_out(&m_backend), m_indentLevel(0), 
       
    24       m_curLineIndented(false), m_compress(false)
       
    25 {
       
    26 }
       
    27 
       
    28 QhpXmlWriter::~QhpXmlWriter()
       
    29 {
       
    30 }
       
    31 
       
    32 void QhpXmlWriter::setIndentLevel(int level)
       
    33 {
       
    34   m_indentLevel = level;
       
    35 }
       
    36 
       
    37 void QhpXmlWriter::setCompressionEnabled(bool enabled)
       
    38 {
       
    39   m_compress = enabled;
       
    40 }
       
    41 
       
    42 void QhpXmlWriter::insert(QhpXmlWriter const & source)
       
    43 {
       
    44   m_out << source.m_backend.data();
       
    45 }
       
    46 
       
    47 void QhpXmlWriter::dumpTo(QFile & file)
       
    48 {
       
    49   file.writeBlock(m_backend.data(), m_backend.length());
       
    50 }
       
    51 
       
    52 void QhpXmlWriter::open(char const * elementName,
       
    53     char const * const * attributes)
       
    54 {
       
    55   indent();
       
    56   openPure(elementName, attributes);
       
    57   newLine();
       
    58   m_indentLevel++;
       
    59 }
       
    60 
       
    61 void QhpXmlWriter::openClose(char const * elementName,
       
    62     char const * const * attributes)
       
    63 {
       
    64   indent();
       
    65   openClosePure(elementName, attributes);
       
    66   newLine();
       
    67 }
       
    68 
       
    69 void QhpXmlWriter::openCloseContent(char const * elementName,
       
    70     char const * content)
       
    71 {
       
    72   indent();
       
    73   openPure(elementName);
       
    74   m_out << convertToXML(content);
       
    75   closePure(elementName);
       
    76   newLine();
       
    77 }
       
    78 
       
    79 void QhpXmlWriter::close(char const * elementName)
       
    80 {
       
    81   m_indentLevel--;
       
    82   indent();
       
    83   closePure(elementName);
       
    84   newLine();
       
    85 }
       
    86 
       
    87 void QhpXmlWriter::indent()
       
    88 {
       
    89   if (m_curLineIndented)
       
    90   {
       
    91     return;
       
    92   }
       
    93   for (int i = 0; i < m_indentLevel; i++) 
       
    94   {
       
    95     m_out << "  ";
       
    96   }
       
    97   m_curLineIndented = true;
       
    98 }
       
    99 
       
   100 void QhpXmlWriter::newLine()
       
   101 {
       
   102   if (!m_compress)
       
   103   {
       
   104     m_out << "\n";
       
   105     m_curLineIndented = false;
       
   106   }
       
   107 }
       
   108 
       
   109 void QhpXmlWriter::openPureHelper(char const * elementName,
       
   110                                   char const * const * attributes, bool close)
       
   111 {
       
   112   m_out << "<" << elementName;
       
   113   if (attributes)
       
   114   {
       
   115     for (char const * const * walker = attributes;
       
   116         walker[0]; walker += 2)
       
   117     {
       
   118       char const * const key = walker[0];
       
   119       char const * const value = walker[1];
       
   120       if (!value)
       
   121       {
       
   122         continue;  
       
   123       }
       
   124       m_out << " " << key << "=\"" << convertToXML(value) << "\"";
       
   125     }
       
   126   }
       
   127 
       
   128   if (close)
       
   129   {
       
   130     m_out << " /";
       
   131   }
       
   132   m_out << ">";
       
   133 }
       
   134 
       
   135 void QhpXmlWriter::openPure(char const * elementName,
       
   136                             char const * const * attributes)
       
   137 {
       
   138   openPureHelper(elementName, attributes, false);
       
   139 }
       
   140 
       
   141 void QhpXmlWriter::openClosePure(char const * elementName,
       
   142                                  char const * const * attributes)
       
   143 {
       
   144   openPureHelper(elementName, attributes, true);
       
   145 }
       
   146 
       
   147 void QhpXmlWriter::closePure(char const * elementName)
       
   148 {
       
   149   m_out << "</" << elementName << ">";
       
   150 }
       
   151