Exemple #1
0
void HTMLParser::processHeader(HTMLTokenizer & tokenizer){
  while(tokenizer.HasNextToken()){
    HTMLToken curToken = tokenizer.GetNextToken();
    
    if(isText(curToken)){
      header += curToken.GetValue();
      processText(curToken.GetValue());
    }else if(isHeaderEnd(curToken))
      return;
    else if(isLinkStart(curToken))
      processLink(curToken);
  }
}
Exemple #2
0
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;
    
  }
}
Exemple #3
0
//------------------------------------------------------------------------------------------------
void XMLReader::HandleEnd (HTMLToken t) {
	string val = StringUtil::ToLowerCopy(t.GetValue());

	if (val == "board")
		inBoard = false;
	else if (val == "history")
		inHistory = false;
	else if (val == "move") {
		moves.push_front(move);
		inMove = false;
		moveNum = 0;
	}
}
Exemple #4
0
void GameLoader::LoadBoard(HTMLTokenizer & tokenizer, Board * boardPtr) const
{
	HTMLToken currentToken = tokenizer.GetNextToken();
	while (currentToken.GetValue() != "board")
	{
		CheckForEndToken(currentToken);
		currentToken = tokenizer.GetNextToken();
	}

	currentToken = tokenizer.GetNextToken();
	while (currentToken.GetValue() != "board")
	{
		CheckForEndToken(currentToken);
		if (currentToken.GetValue() == "piece")
		{
			int pieceType = ConvertTypeStrToInt(currentToken.GetAttribute("type"));
			int pieceColor = ConvertColorStrToInt(currentToken.GetAttribute("color"));
			int col = atoi(currentToken.GetAttribute("column").c_str());
			int row = atoi(currentToken.GetAttribute("row").c_str());
			boardPtr->SetPiece(BoardPosition(row, col), pieceType, pieceColor);
		}
		currentToken = tokenizer.GetNextToken();
	}
}
	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;
		}
	}
Exemple #6
0
//------------------------------------------------------------------------------------------------
void XMLReader::HandleStart (HTMLToken t) {
	string val = StringUtil::ToLowerCopy(t.GetValue());

	if (val == "board")
		inBoard = true;
	else if (val == "history")
		inHistory = true;
	else if (val == "move") {
		moveNum = 1;
		move.SetCaptured(NOTYPE);
		inMove = true;
	}


	if (val == "piece" && inMove) {
		switch(moveNum) {
			case 0 : 
				break;
			case 1 : {
				move.SetType(GetTypeByName(t.GetAttribute("type")));
				move.SetColor(GetColorByName(t.GetAttribute("color")));
				Position pstart;
				pstart.SetRow(atoi((t.GetAttribute("row")).c_str()));
				pstart.SetCol(atoi((t.GetAttribute("column")).c_str()));
				move.SetStart(pstart);
				moveNum++;
				break;
			}
			case 2 : {
				Position pend;
				pend.SetRow(atoi((t.GetAttribute("row")).c_str()));
				pend.SetCol(atoi((t.GetAttribute("column")).c_str()));
				move.SetEnd(pend);
				moveNum++;
				break;
			}
			case 3 :
				move.SetCaptured(GetTypeByName(t.GetAttribute("type")));
				moveNum++;
				break;
		}
	}
}
Exemple #7
0
void HTMLParser::processBody(HTMLTokenizer & tokenizer){
  assert(tokenizer.HasNextToken());
  
  while(tokenizer.HasNextToken()){
    HTMLToken curToken = tokenizer.GetNextToken();
    
    if(isText(curToken)){
      string tmpstr = curToken.GetValue();
      processDescription(tmpstr);
      processText(tmpstr);
    }else if(isLinkStart(curToken))
      processLink(curToken);
    else if(isHeaderStart(curToken) && header.empty()){
	processHeader(tokenizer);
    }else if(isScriptStart(curToken)){
      processScript(tokenizer);
    }else if(isBodyEnd(curToken)){
      return;
    }
    
  }
}
Exemple #8
0
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();
		}
	}
}
Exemple #9
0
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);
}
Exemple #10
0
bool HTMLParser::isScriptEnd(HTMLToken & token){
  string lower = token.GetValue();
  StringUtil::ToLower(lower);
  return (token.GetType() == TAG_END && lower == "script");    
}
Exemple #11
0
bool HTMLParser::isLinkStart(HTMLToken & token){
  string lower = token.GetValue();
  StringUtil::ToLower(lower);
  return (token.GetType() == TAG_START && lower == "a");  
}
Exemple #12
0
bool HTMLParser::isText(HTMLToken & token){
  string lower = token.GetValue();
  StringUtil::ToLower(lower);
  return (token.GetType() == TEXT && !isWhiteSpace(lower));
}
Exemple #13
0
bool HTMLParser::isTitleEnd(HTMLToken & token){
  string lower = token.GetValue();
  StringUtil::ToLower(lower);
  return (token.GetType() == TAG_END && lower.compare("title") == 0);
}
Exemple #14
0
bool HTMLParser::isHeadStart(HTMLToken & token){
  string lower = token.GetValue();
  StringUtil::ToLower(lower);
  return (token.GetType() == TAG_START && lower.compare("head") == 0);
}