#include "YahooNode.h"
istream & operator >>(istream & is, YahooNode &n)
{
const string topicBegin("<Topic r:id=\"Top/Computers/");
const string dirEnd("\">");
const string topicEnd("</Topic>");
const string urlBegin("<link r:resource=\"");
const string urlEnd("\"/>");
string line;
string::size_type start;
string::size_type end;
n.title = "";
while(getline (is, line)) {
start = line.find(topicBegin);
if (start != string::npos){
start += topicBegin.size();
end = line.find(dirEnd);
n.title = line.substr(start, end-start);
while ((start = n.title.find("/")) != string::npos){
n.title.replace(start,1, "-");
}
}
start = line.find(urlBegin);
if (start != string::npos) {
start += urlBegin.size();
end = line.find(urlEnd);
string url = line.substr(start, end-start);
n.urls.push_back(url);
}
start = line.find(topicEnd);
if (start != string::npos)
break;
};
return is;
}
bool YahooNode::childOf(const YahooNode &n)
{
string::size_type pos;
pos = title.find(n.title); if (pos == string::npos) return false;
else return true;
}
void YahooNode::addChild(YahooNode *n)
{
n->nextSibling = 0;
if (firstChild == 0){
firstChild = n;
}
else {
YahooNode *c;
for (c = firstChild; c->nextSibling != 0; c = c->nextSibling)
{};
c->nextSibling = n;
}
}
void YahooNode::printAsHTML(map<string, URL> & m)
{
string filename = title + ".html";
ofstream fout(filename.c_str());
if (fout == 0) {
cerr << "Could not open " << filename << endl;
return;
}
fout << "<html><head><title>PS8:" << title << "</title></head><body>" << endl
<< "<center><h1>" << title << "</h1></center>" << endl;
fout << "<hr><ul>" << endl;
for (YahooNode * c = firstChild; c != 0; c= c->nextSibling){
string fname = c->title + ".html";
fout << "<li><a href=\"" << fname << "\"><b>" << c->title << "</b></a>" << endl;
};
fout << "</ul><hr>" << endl;
fout << "<ul>" << endl;
vector<string>::iterator i;
for (i = urls.begin(); i!= urls.end(); ++i){
string & r = *i;
URL u = m[r];
fout << "<li>" << u << endl;
}
fout << "</ul>" << endl;
fout << "</html>" << endl;
if (firstChild != 0)
firstChild->printAsHTML(m);
if (nextSibling != 0)
nextSibling->printAsHTML(m);
}