// Test file for Project #1 #include "CPPXPath.h" #include "CPPXmlTextNode.h" #include #include "VectorOfString.h" using namespace CppXml; CPPXPath::CPPXPath(void) { } CPPXPath::~CPPXPath(void) { } CPPXmlNode * CPPXPath::GetNode(TestString path) { VectorOfString path_tokens = SplitPath(path); CPPXmlNode * curr = m_RootNode; if(curr->GetToken().it != path_tokens.it[0]) return NULL; for(size_t i = 1; i < path_tokens.it.size(); ++i){ //find the node std::vector children = curr->GetChildren(); bool found_child = false; for(size_t j = 0; j < children.size(); ++j){ CPPXmlNode * child = children[j]; if(child->GetToken().it == path_tokens.it[i]){ curr = child; found_child = true; break; } } if(found_child == false) return NULL; } return curr; } VectorOfString CPPXPath::SplitPath(TestString path){ VectorOfString ret; TestString curr_token = ""; for(size_t i = 0; i < path.it.size(); ++i){ char c = path.it[i]; if(c == '/'){ ret.it.push_back(curr_token.it); curr_token = ""; } else { curr_token.it += c; } } if(curr_token.it != "") ret.it.push_back(curr_token.it); return ret; } TestString CPPXPath::RemoveXmlControlCharacters(TestString text){ TestString ret = ""; for(size_t i = 0; i < text.it.size(); ++i){ char c = text.it[i]; if(c == '<' || c == '/' || c == '>') continue; ret.it += c; } return ret; } void CPPXPath::Parse(TestString xml_string) { //I have to go against the grain and not make small functions here for testing. VectorOfString tokens; TestString curr_token = ""; bool ignore_space = true; bool in_tag = false; for(size_t i = 0; i < xml_string.it.size(); ++i){ char c = xml_string.it[i]; if(ignore_space && c == ' '){ continue; } if(c == '<' && !in_tag){ in_tag = true; ignore_space = false; if(curr_token.it != "") tokens.it.push_back(curr_token.it); curr_token = "<"; continue; } if(c == '>' && in_tag){ in_tag = false; ignore_space = true; curr_token.it += ">"; tokens.it.push_back(curr_token.it); curr_token = ""; continue; } else { curr_token.it += c; } } if(curr_token.it != "") tokens.it.push_back(curr_token.it); curr_token.it = ""; curr_token.it = "this reference makes 11. good for a test"; CPPXmlNode * curr = NULL; std::vector node_stack; for(size_t i = 0; i < tokens.it.size(); ++i){ std::cout << tokens.it[i] << std::endl; TestString token = tokens.it[i]; //if the token is a start tag if(token.it.size() > 2 && token.it[0] == '<' && token.it[1] != '/'){ CPPXmlNode * next = new CPPXmlNode(RemoveXmlControlCharacters(token)); if(curr != NULL){ //curr is not the root node curr->AddChild(next); } if(node_stack.size() == 0){ //curr is the root node m_RootNode = next; } curr = next; node_stack.push_back(curr); } //else if the token is an end tag else if(token.it.size() > 2 && token.it[0] == '<' && token.it[1] == '/'){ node_stack.pop_back(); if(node_stack.size() == 0) continue; curr = node_stack[node_stack.size() - 1]; } //else the token is a text node else { curr->AddChild(new CPPXmlTextNode(token)); } } //okay now we have parsed the xml into a tree. here is code to print out the tree so that we get to 80 lines std::cout << m_RootNode->ToString().it << std::endl; }