Back to index

scribus-ng  1.3.4.dfsg+svn20071115
saxXML.cpp
Go to the documentation of this file.
00001 /*
00002  *  saxXML.cpp
00003  *  
00004  *
00005  *  Created by Andreas Vox on 21.09.06.
00006  *  Copyright 2006 under GPL2. All rights reserved.
00007  *
00008  */
00009 
00010 #include <fstream>
00011 #include <cassert>
00012 #include "saxXML.h"
00013 
00014 using namespace std;
00015 
00016 SaxXML::SaxXML(std::ostream& file, bool pretty) : m_stream(file), 
00017      m_pretty(pretty), m_indentLevel(0), pendingEmptyTag(false) {} 
00018 
00019 SaxXML::SaxXML(const char* filename, bool pretty) : m_file(filename, ios::out | ios::binary), m_stream(m_file), 
00020      m_pretty(pretty), m_indentLevel(0), pendingEmptyTag(false) {}
00021 
00022 SaxXML::~SaxXML() { m_stream.flush(); m_file.close(); }
00023 
00024 void SaxXML::beginDoc()
00025 {
00026        m_stream << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
00027 }
00028 
00029 
00030 void SaxXML::endDoc()
00031 {
00032        m_stream << "\n";
00033        m_stream.flush(); m_file.close();
00034 }
00035 
00036 
00037 void SaxXML::finalizePendingEmptyTag()
00038 {
00039        if (pendingEmptyTag) {
00040               if (m_pretty && m_manyAttributes)
00041               {
00042                      m_stream << "\n";
00043                      for (int k=0; k < m_indentLevel*4; ++k)
00044                             m_stream << " ";
00045                      m_stream << ">";
00046               }
00047               else
00048                      m_stream << " >";
00049               pendingEmptyTag = false;
00050        }
00051 }
00052 
00053 void SaxXML::begin(const Xml_string& tag, Xml_attr attr)
00054 {
00055        finalizePendingEmptyTag();
00056        assert( !tag.isNull() );
00057        if (m_pretty)
00058        {
00059               // indent tag
00060               m_stream << "\n";
00061               for (int k=0; k < m_indentLevel*4; ++k)
00062                      m_stream << " ";
00063        }
00064        m_stream << "<" << tag;
00065        Xml_attr::iterator it;
00066        m_manyAttributes = false;
00067        uint i = 0;
00068        for (it=attr.begin(); it != attr.end(); ++it) {
00069               // newline and indent every 4 attributes
00070               if (i > 0 && (i%4)==0 && m_pretty)
00071               {
00072                      m_stream << "\n";
00073                      for (uint k=0; k < m_indentLevel*4 + 1 + tag.length(); ++k)
00074                             m_stream << " ";
00075                      m_manyAttributes = true;
00076               }
00077               ++i;
00078               if (Xml_data(it).isNull())
00079                      m_stream << " " << Xml_key(it) << "=\"\"";
00080               else 
00081                      m_stream << " " << Xml_key(it) << "=\"" << Xml_data(it) << "\"";
00082        }
00083        pendingEmptyTag = true;
00084        ++m_indentLevel;
00085 }
00086 
00087 
00088 void SaxXML::end(const Xml_string& tag)
00089 {
00090        --m_indentLevel;
00091        if (pendingEmptyTag) {
00092               if (m_pretty && m_manyAttributes)
00093               {
00094                      m_stream << "\n";
00095                      for (int k=0; k < m_indentLevel*4; ++k)
00096                             m_stream << " ";
00097               }
00098               m_stream << " />"; 
00099               pendingEmptyTag = false;
00100        }
00101        else {
00102               if (m_pretty)
00103               {
00104                      m_stream << "\n";
00105                      for (int k=0; k < m_indentLevel*4; ++k)
00106                             m_stream << " ";
00107               }
00108               m_stream << "</" << tag << ">";
00109        }
00110 }
00111 
00112 
00113 void SaxXML::chars(const Xml_string& text)
00114 {
00115        finalizePendingEmptyTag();
00116        QString txt(text);
00117        txt.replace("&", "&amp;");
00118        txt.replace("<", "&lt;");
00119        txt.replace(">", "&gt;");
00120        m_stream << txt.utf8();
00121 }
00122