00001 #include "SDXTabBasedStreamWriter.h"
00002
00003 using namespace std;
00004 using namespace SDX::Formats;
00005
00006 void TabBasedStreamWriter::startDocument(){
00007 m_level = 0;
00008 }
00009
00010 void TabBasedStreamWriter::startNode(string nodeName){
00011 *m_oStream << endl;
00012 for(int i = 0; i < m_level; i++)
00013 *m_oStream << '\t';
00014
00015 unsigned int quotePosition = nodeName.find_first_of("\"'");
00016 if(quotePosition == string::npos)
00017 *m_oStream << nodeName;
00018 else {
00019 char quote = nodeName.at(quotePosition);
00020 *m_oStream << quote << nodeName << quote;
00021 }
00022
00023 ++m_level;
00024 }
00025
00026 void TabBasedStreamWriter::writeAttribute(string name, string value){
00027 *m_oStream << " ";
00028
00029
00030 if(!name.empty()){
00031 unsigned int quotePosition = name.find_first_of("\"'");
00032 if(quotePosition == string::npos){
00033 size_t whiteSpace = name.find_first_of(" \t");
00034 if(whiteSpace == string::npos)
00035 *m_oStream << name;
00036 else
00037 *m_oStream << '"' << name << '"';
00038 } else {
00039 char quote = name.at(quotePosition);
00040 if(quote == '"')
00041 quote = '\'';
00042 else if(quote == '\'')
00043 quote = '"';
00044
00045 *m_oStream << quote << name << quote;
00046 }
00047 *m_oStream << "=";
00048 }
00049
00050
00051 unsigned int quotePosition = value.find_first_of("\"'");
00052 if(quotePosition == string::npos){
00053 size_t whiteSpace = value.find_first_of(" \t");
00054 if(whiteSpace == string::npos)
00055 *m_oStream << value;
00056 else
00057 *m_oStream << '"' << value << '"';
00058 } else {
00059 char quote = value.at(quotePosition);
00060 if(quote == '"')
00061 quote = '\'';
00062 else if(quote == '\'')
00063 quote = '"';
00064
00065 *m_oStream << quote << value << quote;
00066 }
00067 }
00068
00069 void TabBasedStreamWriter::endDocument(){
00070 *m_oStream << endl;
00071 }
00072
00073 void TabBasedStreamWriter::endNode(){
00074 --m_level;
00075 }