00001
00002
00003
00004
00005
00006
00007
00008
00010
00011
00012 #include "demlparser.h"
00013 using namespace deml;
00014
00015
00016 DEMLparser::DEMLparser(fstream* stream)
00017 {
00018 fin = stream;
00019 detree = new tree<string>();
00020 iter = detree->begin();
00021 }
00022
00023 tree<string>* DEMLparser::parse(void)
00024 {
00025 if(fin->bad())
00026 return null;
00027
00028 TagStream ts(fin);
00029 vector<string> tokens;
00030
00031
00032 ts.nextData();
00033
00034
00035 tokens = ts.nextTag();
00036 detree->replace(iter, tokens[0]);
00037 processAttributes(tokens);
00038 tokens.clear();
00039 tokens = ts.nextData();
00040 processWhitespace(tokens);
00041
00042 while(!fin->eof())
00043 {
00044 vector<string> tag = ts.nextTag();
00045 vector<string> ws = ts.nextData();
00046
00047 if(isClosingTag(tag))
00048 {
00049 iter = iter.step_out();
00050 processWhitespace(ws);
00051 continue;
00052 }
00053
00054 detree->append_child(iter, tag[0]);
00055 processAttributes(tag);
00056 iter = iter.step_in();
00057 iter = iter.end();
00058 processWhitespace(ws);
00059 }
00060
00061 return detree;
00062 }
00063
00064 bool DEMLparser::isSelfContained(vector<string> v)
00065 {
00066 short end = v.size() - 1;
00067 string s = v[end];
00068 char c = s[0];
00069 if(c == '/')
00070 return true;
00071
00072 return false;
00073 }
00074
00075 bool DEMLparser::isClosingTag(vector<string> v)
00076 {
00077 string str = v[0];
00078 char c = str[0];
00079 if(c == '/')
00080 return true;
00081
00082 return false;
00083 }
00084
00085 void DEMLparser::processAttributes(vector<string> v)
00086 {
00087 for(unsigned short i = 1; i < v.size(); i++)
00088 {
00089 string str = v[i];
00090 unsigned short equalsPos = str.find('=');
00091 unsigned short quoteStart = str.find('\'');
00092 unsigned short doubleQuoteStart = str.find('\"');
00093 unsigned short valueStart, valueEnd;
00094
00095 if(str[0] == '/')
00096 {
00097 continue;
00098 }
00099
00100 if(equalsPos > str.size())
00101 {
00102 detree->append_child(iter, str);
00103 return;
00104 }
00105
00106 if(quoteStart < doubleQuoteStart)
00107 {
00108 valueStart = quoteStart;
00109 valueEnd = str.rfind('\'');
00110 }
00111 else
00112 {
00113 valueStart = doubleQuoteStart;
00114 valueEnd = str.rfind('\"');
00115 }
00116
00117 valueStart++;
00118 valueEnd -= valueStart;
00119
00120 string attr = str.substr(0, equalsPos);
00121 string value = str.substr(valueStart, valueEnd);
00122
00123 tree<string>::iterator vIter = detree->append_child(iter, attr);
00124 detree->append_child(vIter, value);
00125 }
00126 }
00127
00128 void DEMLparser::processWhitespace(vector<string> tokens)
00129 {
00130 for(unsigned short i = 0; i < tokens.size(); i++)
00131 detree->append_child(iter, tokens[i]);
00132 }