23 #include "XmlParser.h"
26 XmlParser::XmlParser(
string mode)
28 logger = Logger::getLogger(
"XmlParser");
32 XmlParser::~XmlParser() {
36 Document XmlParser::getDocument(
string xml)
39 ifstream infile(xml.c_str());
44 while(getline(infile, temp))
46 if(temp.find(
"<?")==string::npos && temp.find(
"?>")==string::npos)
47 xml.append(temp+
"\n");
52 StringUtil::trim(xml);
53 if(xml.find(
"<")==0 && xml.find(
">")!=string::npos)
55 if(xml.find(
"<?")!=string::npos && xml.find(
"?>")!=string::npos)
57 xml = xml.substr(xml.find(
"?>")+2);
59 readXML(xml,
"",&root);
68 doc.setRootElement(root);
72 void XmlParser::readXML(
string xml,
string parent,
Element *par)
76 StringUtil::trim(xml);
77 int cdt = xml.find(
"<![CDATA[");
80 int ecdt = xml.find(
"]]>");
81 if(ecdt==(
int)string::npos)
83 string errmsg = (
"Incomplete CDATA tag\n");
90 par->setText(xml.substr(cdt+9,ecdt-cdt-9));
94 int cmt = xml.find(
"<!--");
95 if(cmt!=(
int)string::npos)
97 int ecmt = xml.find(
"-->");
98 if(ecmt==(
int)string::npos)
100 string errmsg = (
"Incomplete Comment tag\n");
106 string stx = xml.substr(0,cmt);
107 string enx = xml.substr(ecmt+3);
111 int st = xml.find(
"<")+1;
113 int ed1 = xml.find(
"/>");
114 int ed2 = xml.find(
">");
115 if((ed2<ed1 || ed1==-1) && ed2!=-1)
124 string tag = xml.substr(st,ed-st);
125 int ss = tag.find_first_not_of(
" ");
126 int se = tag.find_last_not_of(
" ")+1;
127 tag = tag.substr(ss,se-ss);
132 if(tag.find_first_of(
" ")!=string::npos)
134 ta = tag.substr(0,tag.find_first_of(
" "));
135 tag = tag.substr(tag.find_first_of(
" ")+1);
136 while(tag.find_first_of(
"=")!=string::npos && tag.find_first_of(
"\"")!=string::npos)
138 string atname = tag.substr(0,tag.find_first_of(
"="));
139 int as = atname.find_first_not_of(
" ");
140 int ae = atname.find_last_not_of(
" ")+1;
141 atname = atname.substr(as,ae-as);
142 int ds = tag.find_first_of(
"\"")+1;
143 tag = tag.substr(ds);
144 string atvalue = tag.substr(0,tag.find_first_of(
"\""));
145 tag = tag.substr(tag.find_first_of(
"\"")+1);
148 element.addAttribute(atname,atvalue);
152 par->addAttribute(atname,atvalue);
159 int initcheck = xml.find_first_of(
"<");
160 unsigned int someTag = (xml.substr(initcheck+1)).find(
"<");
161 int pndTag=0,endTag=0;
162 if(xml.find(
"</"+ta)!=string::npos)
163 pndTag = xml.find(
"</"+ta);
164 else if(xml.find(
"/>")!=string::npos && xml.find(
"/>")<someTag)
165 endTag = xml.find(
"/>");
166 if(xml.find(
"< ")!=string::npos)
168 string errmsg = (
"Invalid Start Tag - at position: " + CastUtil::lexical_cast<
string>((int)xml.find(
"< ")+1) +
"\n");
172 else if(xml.find(
"<\t")!=string::npos)
174 string errmsg = (
"Invalid Start Tag - at position: " + CastUtil::lexical_cast<
string>((int)xml.find(
"<\t")+1) +
"\n");
178 else if(xml.find(
"</ ")!=string::npos)
180 string errmsg = (
"Invalid End Tag - at position: " + CastUtil::lexical_cast<
string>((int)xml.find(
"</ ")+1) +
"\n");
184 else if(xml.find(
"</\t")!=string::npos)
186 string errmsg = (
"Invalid End Tag - at position: " + CastUtil::lexical_cast<
string>((int)xml.find(
"</\t")+1) +
"\n");
190 else if(xml.find(
"< /")!=string::npos)
192 string errmsg = (
"Invalid End Tag - at position: " + CastUtil::lexical_cast<
string>((int)xml.find(
"< /")+1) +
"\n");
196 else if(xml.find(
"/ >")!=string::npos)
198 string errmsg = (
"Invalid End Tag - at position: " + CastUtil::lexical_cast<
string>((int)xml.find(
"/ >")+1) +
"\n");
202 else if(xml.find(
"<\t/")!=string::npos)
204 string errmsg = (
"Invalid End Tag - at position: " + CastUtil::lexical_cast<
string>((int)xml.find(
"<\t/")+1) +
"\n");
208 else if(xml.find(
"<"+ta)==string::npos && xml.find(
"</"+ta)!=string::npos)
210 string errmsg = (
"No Start Tag - for : " + ta +
"\n");
214 else if(xml.find(
"<"+ta)!=string::npos && pndTag==0 && endTag==0)
216 string errmsg = (
"No End Tag - for : " + ta +
"\n");
220 if(xml.find(
"<"+ta)!=string::npos && (xml.find(
"</"+ta)!=string::npos || xml.find(
"/>")!=string::npos))
230 element.setTagName(ta);
231 par->addElement(element);
237 xml = xml.substr(xml.find(
"/>")+2);
239 else if(xml.find(
"</"+ta)!=string::npos)
242 string tagx =
"</"+ta+
">";
243 int end = xml.find(
"</"+ta+
">");
244 string txml = xml.substr(ed+1,end-ed-1);
249 element.setTagName(ta);
250 if(txml.find(
"<")!=string::npos)
252 readXML(txml,ta,&element);
255 element.setText(txml);
256 par->addElement(element);
261 if(txml.find(
"<")!=string::npos)
263 readXML(txml,ta,par);
268 xml = xml.substr(end+tagx.length());
271 if(xml.find(
"<")!=string::npos && (xml.find(
"</")!=string::npos || xml.find(
"/>")!=string::npos))
275 readXML(xml,parent,par);
277 else if(xml.find(
"<")!=string::npos && (xml.find(
"</")==string::npos || xml.find(
"/>")==string::npos))
279 string errmsg = (
"Invalid Start Tag\n");
283 else if(xml.find(
"<")==string::npos && (xml.find(
"</")!=string::npos || xml.find(
"/>")!=string::npos))
285 string errmsg = (
"Invalid End Tag\n");