void GameLoader::CheckForEndToken(const HTMLToken & tokenToCheck) const { if (tokenToCheck.GetType() == HTMLTokenType::END) { throw CS240Exception("Invalid file"); } }
//------------------------------------------------------------------------------------------------ void XMLReader::ParseXML(string & xml) { HTMLTokenizer tokenizer(xml); while (tokenizer.HasNextToken()) { HTMLToken cur = tokenizer.GetNextToken(); switch (cur.GetType()) { case TAG_START : //cout << "TAG_START:" << cur.GetValue() << endl; HandleStart(cur); break; case TAG_END : //cout << "TAG_END:" << cur.GetValue() << endl; HandleEnd(cur); break; case TEXT : //cout << "TEXT:" << cur.GetValue() << endl; //HandleText(&cur); break; case END : //cout << "END:" << cur.GetValue() << endl; //HandleEnd(cur); break; } } }
void HTMLParser::processTitle(HTMLTokenizer & tokenizer){ assert(tokenizer.HasNextToken()); while(tokenizer.HasNextToken()){ HTMLToken titleToken = tokenizer.GetNextToken(); if(titleToken.GetType() == TEXT){ title += titleToken.GetValue(); processText(titleToken.GetValue()); } if(isTitleEnd(titleToken)) return; } }
void HTMLParser::MarkEndFlags(HTMLToken & current_tok) { //Marks flags for when we see the closing tag. if(current_tok.GetType() == TAG_END) { string tag_value = current_tok.GetValue(); StringUtil::ToLower(tag_value); if(tag_value == "html") inHTML = false; if(tag_value == "head") inHead = false; if(tag_value == "title") inTitle = false; if(tag_value == "body") inBody = false; if(tag_value == "script") inScript = false; if(tag_value[0] == 'h' && isdigit(tag_value[1])) inH = false; } }
void GameLoader::LoadMoveHistory(HTMLTokenizer & tokenizer, MoveHistory * gameHistory) const { HTMLToken currentToken = tokenizer.GetNextToken(); while (currentToken.GetValue() != "history") { CheckForEndToken(currentToken); currentToken = tokenizer.GetNextToken(); } currentToken = tokenizer.GetNextToken(); while (currentToken.GetValue() != "history") { CheckForEndToken(currentToken); if (currentToken.GetValue() == "move" && currentToken.GetType() == HTMLTokenType::TAG_START) { while (currentToken.GetValue() != "piece") { CheckForEndToken(currentToken); currentToken = tokenizer.GetNextToken(); } HTMLToken origin = currentToken; currentToken = tokenizer.GetNextToken(); while (currentToken.GetValue() != "piece") { CheckForEndToken(currentToken); currentToken = tokenizer.GetNextToken(); } HTMLToken destination = currentToken; currentToken = tokenizer.GetNextToken(); int originPieceType = ConvertTypeStrToInt(origin.GetAttribute("type")); int originPieceColor = ConvertColorStrToInt(origin.GetAttribute("color")); int originCol = atoi(origin.GetAttribute("column").c_str()); int originRow = atoi(origin.GetAttribute("row").c_str()); int destinationCol = atoi(destination.GetAttribute("column").c_str()); int destinationRow = atoi(destination.GetAttribute("row").c_str()); int capturedPieceType = -1; int capturedCol = -1; int capturedRow = -1; while (currentToken.GetValue() != "piece" && currentToken.GetValue() != "move") { CheckForEndToken(currentToken); currentToken = tokenizer.GetNextToken(); } if (currentToken.GetValue() == "piece") { capturedPieceType = ConvertTypeStrToInt(currentToken.GetAttribute("type")); capturedCol = atoi(currentToken.GetAttribute("column").c_str()); capturedRow = atoi(currentToken.GetAttribute("row").c_str()); } gameHistory->AddMove(Move(originPieceType, originPieceColor, BoardPosition(originRow, originCol), BoardPosition(destinationRow, destinationCol), capturedPieceType, BoardPosition(capturedRow, capturedCol))); } else { currentToken = tokenizer.GetNextToken(); } } }
bool HTMLParser::isHeaderEnd(HTMLToken & token){ string lower = token.GetValue(); StringUtil::ToLower(lower); boost::regex re("[hH][1-9]"); return (boost::regex_match(lower, re) && token.GetType() == TAG_END); }
bool HTMLParser::isScriptEnd(HTMLToken & token){ string lower = token.GetValue(); StringUtil::ToLower(lower); return (token.GetType() == TAG_END && lower == "script"); }
bool HTMLParser::isLinkStart(HTMLToken & token){ string lower = token.GetValue(); StringUtil::ToLower(lower); return (token.GetType() == TAG_START && lower == "a"); }
bool HTMLParser::isText(HTMLToken & token){ string lower = token.GetValue(); StringUtil::ToLower(lower); return (token.GetType() == TEXT && !isWhiteSpace(lower)); }
bool HTMLParser::isTitleEnd(HTMLToken & token){ string lower = token.GetValue(); StringUtil::ToLower(lower); return (token.GetType() == TAG_END && lower.compare("title") == 0); }
bool HTMLParser::isHeadStart(HTMLToken & token){ string lower = token.GetValue(); StringUtil::ToLower(lower); return (token.GetType() == TAG_START && lower.compare("head") == 0); }