00001 #include "xmlwriter.h"
00002 #include <stdarg.h>
00003 #include "exception.h"
00004
00005 xmlwriter::xmlwriter(string sTmp)
00006 {
00007 sXmlFile = sTmp;
00008 fp = NULL;
00009 iLevel = 0;
00010 fp = fopen(sXmlFile.c_str(),"w");
00011 if(fp == NULL)
00012 {
00013 std::cout<<"Unable to open output file";
00014 return;
00015 }
00016 else
00017 {
00018 fprintf(fp,"<?xml version=\"1.0\" encoding=\"UTF-8\" \?>");
00019 }
00020 }
00021
00022 xmlwriter::~xmlwriter()
00023 {
00024 if(fp != NULL)
00025 fclose(fp);
00026 if (vectAttrData.size() != 0)
00027 throw new Exception("Not all xml tags were closed");
00028
00029 }
00030
00031
00032 void xmlwriter::CreateTag(string sTag)
00033 {
00034 NewLine();
00035 fprintf(fp,"<%s",sTag.c_str());
00036
00037 unsigned i=0;
00038 while(i<vectAttrData.size())
00039 {
00040 string sTmp = vectAttrData[i];
00041 fprintf(fp," %s=", sTmp.c_str());
00042 sTmp = vectAttrData[i+1];
00043 fprintf(fp,"\"%s\"", sTmp.c_str());
00044 i+=2;
00045 }
00046 vectAttrData.clear();
00047 fprintf(fp,">");
00048 sTagStack.push(sTag);
00049 iLevel++;
00050
00051 }
00052
00053
00054 void xmlwriter::CreateEmptyElement(string sTag)
00055 {
00056 NewLine();
00057 fprintf(fp,"<%s",sTag.c_str());
00058
00059 unsigned i=0;
00060 while(i<vectAttrData.size())
00061 {
00062 string sTmp = vectAttrData[i];
00063 fprintf(fp," %s=", sTmp.c_str());
00064 sTmp = vectAttrData[i+1];
00065 fprintf(fp,"\"%s\"", sTmp.c_str());
00066 i+=2;
00067 }
00068 vectAttrData.clear();
00069 fprintf(fp,"/>");
00070
00071 }
00072
00073
00074 void xmlwriter::CloseTag()
00075 {
00076 iLevel--;
00077 NewLine();
00078 fprintf(fp,"</%s>",sTagStack.top().c_str());
00079 sTagStack.pop();
00080 return;
00081 }
00082
00083 void xmlwriter::CloseTag(std::string sTag)
00084 {
00085 if (sTag != sTagStack.top().c_str())
00086 {
00087 throw new Exception("xmlwriter::CloseTag %s tag closed before %s",sTag.c_str(),sTagStack.top().c_str());
00088 }
00089 CloseTag();
00090 }
00091
00092
00093 void xmlwriter::CloseAlltags()
00094 {
00095 while(sTagStack.size() != 0)
00096 {
00097 iLevel--;
00098
00099 NewLine();
00100 fprintf(fp,"</%s>",sTagStack.top().c_str());
00101 sTagStack.pop();
00102 }
00103 return;
00104 }
00105 void xmlwriter::CreateChild(string sTag,string sValue)
00106 {
00107 NewLine();
00108
00109
00110 fprintf(fp,"<%s",sTag.c_str());
00111
00112 unsigned i=0;
00113 while(i<vectAttrData.size())
00114 {
00115 string sTmp = vectAttrData[i];
00116 fprintf(fp," %s=", sTmp.c_str());
00117 sTmp = vectAttrData[i+1];
00118 fprintf(fp,"\"%s\"", sTmp.c_str());
00119 i+=2;
00120 }
00121 vectAttrData.clear();
00122
00123 fprintf(fp,">%s</%s>",sValue.c_str(),sTag.c_str());
00124 }
00125
00126 void xmlwriter::AddAttributes(string sKey, string sVal)
00127 {
00128 vectAttrData.push_back(sKey);
00129 vectAttrData.push_back(sVal);
00130 }
00131
00132
00133 void xmlwriter::AddComment(string sComment)
00134 {
00135 NewLine();
00136 fprintf(fp,"<!--%s-->",sComment.c_str());
00137 }
00138
00139
00140 void xmlwriter::AddString(string str)
00141 {
00142 NewLine();
00143 fprintf(fp,"%s",str.c_str());
00144 }
00145
00146 void xmlwriter::NewLine()
00147 {
00148
00149 fprintf(fp,"\n");
00150 for(int iTmp =0;iTmp<iLevel;iTmp++)
00151 fprintf(fp," ");
00152
00153 }
00154
00155
00156
00157