76 cout <<
"ParserState = ";
78 case UNKNOWN: cout <<
"UNKNOWN";
102 case ERROR: cout <<
"ERROR";
104 default: cout <<
"UNKNOWN";
134 if (currentLine.find(
'>') == string::npos) {
143 if (currentLine[1] ==
'!' && currentLine[2] ==
'-' && currentLine[3] ==
'-') {
154 else if (currentLine[0] !=
'<'
155 && currentLine[currentLine.find(
'>') - 1] ==
'-'
156 && currentLine[currentLine.find(
'>') - 2] ==
'-') {
164 if (currentLine[0] ==
'<') {
168 if (currentLine[1] ==
'?') {
169 content = currentLine.substr(2, currentLine.rfind(
'?') - 2);
175 }
else if (currentLine[1] ==
'!' && currentLine [2] ==
'-' && currentLine[3] ==
'-') {
176 content = currentLine.substr(4, currentLine.rfind(
'-') - 5);
181 }
else if (currentLine[1] ==
'/') {
182 tagName = currentLine.substr(2, currentLine.find(
'>') - 2);
191 tagName = currentLine.substr(1, currentLine.find(
'>') - 1);
192 tagName = tagName.substr(0, tagName.find(
' '));
195 if (currentLine[currentLine.rfind(
'<') + 1] ==
'/') {
196 content = currentLine.substr(currentLine.find(
'>') + 1, currentLine.find(
'/'));
197 content = content.substr(0, content.find(
'/') - 1);
200 else if (currentLine[currentLine.rfind(
'>') - 1] ==
'/') {
201 content = (
"{EMPTY}");
227 string::iterator it = str.begin();
228 while (*it ==
' ' || *it ==
'\t') {
229 str.erase(str.begin());
232 while (*it ==
' ' || *it ==
'\t') {
233 str.erase(str.end() - 1);
244 cout <<
"*** The stack now contains: ";
246 if (!elementVect.empty()) {
247 for (vector<Element*>::iterator it = elementVect.begin(); it != elementVect.end(); ++it) {
248 cout << (*it)->getTagName();
252 if (it == elementVect.end() - 1)
259 cout <<
"{EMPTY}" << endl;
277 switch (currentState) {
282 cout << lineNumber <<
": " << currentLine << endl;
283 cout <<
"*** Comment started";
290 cout << lineNumber <<
": " << currentLine << endl;
291 cout <<
"*** Comment line";
299 cout << lineNumber <<
": " << currentLine << endl;
300 cout <<
"*** Ending comment";
307 cout << lineNumber <<
": " << content << endl;
308 cout <<
"*** One line comment";
316 cout << lineNumber <<
" : " << currentLine << endl;
317 cout <<
"*** Directive = " << content << endl;
328 Element* ElementPtr =
new Element(tagName, lineNumber,
"Empty", currentLine);
331 currentElement->
addChild(ElementPtr);
341 cout << lineNumber <<
" : " << currentLine << endl;
342 cout <<
"*** Element Opened = " << tagName << endl;
344 if (currentElement != emptyRoot) {
345 cout <<
"*** Creating new child: " << ElementPtr->
getTagName()
346 <<
", for parent: " << currentElement->
getTagName() << endl;
348 cout <<
"*** No parent found, creating root element: " << ElementPtr->
getTagName() << endl;
352 cout <<
"*** Attributes found: " << endl;
363 currentElement = ElementPtr;
371 Element* ElementPtr =
new Element(tagName, lineNumber, content, currentLine);
372 currentElement->
addChild(ElementPtr);
376 cout << lineNumber <<
" : " << currentLine << endl;
377 cout <<
"*** Complete element found:" << endl;
378 cout <<
"*** Element Name = " << tagName << endl;
379 cout <<
"*** Element Content = " << content << endl;
380 cout <<
"*** Creating new child: " << ElementPtr->
getTagName()
381 <<
", for parent: " << currentElement->
getTagName() << endl;
384 cout <<
"*** Attributes found: " << endl;
387 cout <<
"*** Stack unchanged" << endl;
403 cout << lineNumber <<
" : " << currentLine << endl;
407 cout <<
"ERROR!!! - Closing tag on line " << lineNumber
408 <<
" does not match last opened tag, '"
411 cout <<
"XML is not well formed. Ending program." << endl;
412 currentState =
ERROR;
419 currentElement = currentElement->
getParent();
424 cout <<
"*** Element closed = " << tagName << endl;
436 Element* ElementPtr =
new Element(tagName, lineNumber,
"Empty", currentLine);
437 currentElement->
addChild(ElementPtr);
441 cout << lineNumber <<
" : " << currentLine << endl;
442 cout <<
"*** Self-closing element found: " << endl;
443 cout <<
"*** Element Name = " << tagName << endl;
444 cout <<
"*** Element Content = " << content << endl;
445 cout <<
"*** Creating new child: " << ElementPtr->
getTagName()
446 <<
", for parent: " << currentElement->
getTagName() << endl;
449 cout <<
"*** Attributes found: " << endl;
452 cout <<
"*** Stack unchanged." << endl;
462 cout << currentLine << endl;
463 cout <<
"Parser state unknown!" << endl;
464 cout <<
"XML is not well formed. Ending program." << endl;
470 cout << currentLine << endl;
471 cout <<
"ERROR!!!" << endl;
472 cout <<
"XML is not well formed. Ending program." << endl;
478 cout << endl << endl;
500 infile.open(strPath);
509 getline(infile, currentLine);
517 cout <<
"Building tree for file: " << strPath <<
"..." << endl << endl;
523 while (!currentLine.empty() && currentState !=
ERROR && currentState !=
UNKNOWN) {
527 currentLine =
trim(currentLine);
532 currentState =
parse(currentLine, currentState, content, tagName);
536 proccesLine(currentLine, currentState, lineNumber, content, tagName);
540 getline(infile, currentLine);
562 for (vector<Element*>::iterator it = vect.begin(); it != vect.end(); ++it) {
576 if (currentElement != emptyRoot) {
578 for (
int i = 1; i < currentElement->
getLevel(); i++) {
581 cout <<
"Element \"" << currentElement->
getTagName()
582 <<
"\" was found at line " << currentElement->
getLineNo()
583 <<
", level " << currentElement->
getLevel();
585 cout <<
", with no content";
587 cout <<
", containing \"" << currentElement->
getStrContent() <<
'"';
596 cout <<
", along with " << currentElement->
vecAttribute.size();
599 cout <<
" attribute: " << endl;
601 cout <<
" attributes: " << endl;
612 for (
int i = 0; i < 40; i++) {
615 cout << endl << endl <<
"Now showing the element tree:" << endl << endl;
622 for (vector<Element*>::iterator it = currentElement->
vecChildren.begin();
639 for (vector<Element*>::iterator it = currentElement->
vecChildren.begin();
656 while (input != 1 && input != 2 && cin) {
657 cout <<
"Would you like to see the tree as it is being built?" << endl;
658 cout <<
"Enter 1 for yes or 2 for no:" << endl;
674 cout <<
"Invalid entry, please try again." << endl;
676 cin.ignore(256,
'\n');
693 if (currentElement == emptyRoot) {
696 os <<
"<!DOCTYPE html>\n<html lang=\"en\">\n\n<head>\n <title></title>\n"
697 " <meta charset=\"utf-8\">\n</head>\n<body>\n";
700 if (currentElement != emptyRoot) {
703 for (
int i = 1; i < currentElement->
getLevel(); i++) {
706 os <<
"Element \"" << currentElement->
getTagName()
707 <<
"\" was found at line " << currentElement->
getLineNo()
708 <<
", level " << currentElement->
getLevel();
710 os <<
", with no content";
712 os <<
", containing \"" << currentElement->
getStrContent() <<
'"';
721 os <<
", along with " << currentElement->
vecAttribute.size();
724 os <<
" attribute: ";
726 os <<
" attributes: ";
728 for (
int i = 0; i < currentElement->
vecAttribute.size(); i++) {
731 for (
int j = 1; j < currentElement->
getLevel(); j++) {
735 os <<
"Attribute name: " << currentElement->
vecAttribute[i]->getAttName();
736 os <<
", Attribute value: " << currentElement->
vecAttribute[i]->getAttValue();
745 os <<
"</p>" << endl;
752 for (vector<Element*>::iterator it = currentElement->
vecChildren.begin();
760 if (currentElement == emptyRoot) {
772 int main(
int argc,
char** argv) {
778 if (
openfile(
"Assignment4_musicFile.xml") == EXIT_SUCCESS) {
784 htmlStream.open(
"HTML_OUTPUT.html");
int openfile(string strPath)
int main(int argc, char **argv)
void writeHTML(Element *currentElement, ofstream &os)
ParserState parse(string currentLine, ParserState currentState, string &content, string &tagName)
string getStrContent() const
vector< Attribute * > vecAttribute
void setLevel(int newLevel)
void setParent(Element *Parent)
void deleteTree(Element *currentElement)
vector< Element * > vecChildren
void ShowState(ParserState ps)
string getTagName() const
void displayTree(Element *currentElement)
vector< Element * > elementVect
void showStack(vector< Element * > elementVect)
void clearVectorContents(vector< Element * > &vect)
void proccesLine(string currentLine, ParserState ¤tState, int lineNumber, string content, string tagName)
void addChild(Element *newChild)