HTMLToken.cpp
Go to the documentation of this file.00001 #include "HTMLToken.h"
00002
00003 #include <cctype>
00004
00005 string TypeToString(HTMLTokenType type)
00006 {
00007 string result = "";
00008
00009 switch(type)
00010 {
00011 case TAG_START:
00012 result = "TAG_START";
00013 break;
00014
00015 case TAG_END:
00016 result = "TAG_END";
00017 break;
00018
00019 case TEXT:
00020 result = "TEXT";
00021 break;
00022
00023 case END:
00024 result = "END";
00025 break;
00026
00027 default:
00028 result = "ERROR! Contact TA's about TypeToString() in HTMLToken.cpp.";
00029 break;
00030 }
00031
00032 return result;
00033 }
00034
00035 HTMLToken::HTMLToken(const string& tokenValue, HTMLTokenType tokenType)
00036 {
00037 value = tokenValue;
00038 type = tokenType;
00039 }
00040 HTMLToken::HTMLToken(const HTMLToken& toCopy)
00041 {
00042 value = toCopy.value;
00043 type = toCopy.type;
00044 attributes = toCopy.attributes;
00045 }
00046 HTMLToken::~HTMLToken()
00047 {
00048 }
00049
00050 string HTMLToken::GetValue() const
00051 {
00052 return value;
00053 }
00054
00055 HTMLTokenType HTMLToken::GetType() const
00056 {
00057 return type;
00058 }
00059
00060 bool HTMLToken::AttributeExists(const string& attribute)
00061 {
00062 return attributes.find(ToLower(attribute)) != attributes.end();
00063 }
00064 string HTMLToken::GetAttribute(const string& attribute)
00065 {
00066 string lowerAtt = ToLower(attribute);
00067 if( IsTag() == false || attributes.find(lowerAtt) == attributes.end())
00068 {
00069 return string("");
00070 }
00071 else
00072 {
00073 return attributes[lowerAtt];
00074 }
00075 }
00076 void HTMLToken::SetAttribute(const string& attribute, const string& value)
00077 {
00078 if(IsTag())
00079 {
00080 attributes[ToLower(attribute)] = value;
00081 }
00082 }
00083
00084 string HTMLToken::ToLower(const string& str)
00085 {
00086 string result = str;
00087 for(int i = 0; i < (int)result.length(); i++)
00088 {
00089 result[i] = tolower(result[i]);
00090 }
00091 return result;
00092 }
00093
00094 bool HTMLToken::IsTag() const
00095 {
00096 return type == TAG_START || type == TAG_END;
00097 }
00098
00099