83 cout <<
"ParserState = ";
85 case UNKNOWN: cout <<
"UNKNOWN";
109 case ERROR: cout <<
"ERROR";
111 default: cout <<
"UNKNOWN";
141 if (currentLine.find(
'>') == string::npos) {
150 if (currentLine[1] ==
'!' && currentLine[2] ==
'-' && currentLine[3] ==
'-') {
161 else if (currentLine[0] !=
'<'
162 && currentLine[currentLine.find(
'>') - 1] ==
'-'
163 && currentLine[currentLine.find(
'>') - 2] ==
'-') {
171 if (currentLine[0] ==
'<') {
175 if (currentLine[1] ==
'?') {
176 content = currentLine.substr(2, currentLine.rfind(
'?') - 2);
182 }
else if (currentLine[1] ==
'!' && currentLine [2] ==
'-' && currentLine[3] ==
'-') {
183 content = currentLine.substr(4, currentLine.rfind(
'-') - 5);
188 }
else if (currentLine[1] ==
'/') {
189 tagName = currentLine.substr(2, currentLine.find(
'>') - 2);
198 tagName = currentLine.substr(1, currentLine.find(
'>') - 1);
199 tagName = tagName.substr(0, tagName.find(
' '));
202 if (currentLine[currentLine.rfind(
'<') + 1] ==
'/') {
203 content = currentLine.substr(currentLine.find(
'>') + 1, currentLine.find(
'/'));
204 content = content.substr(0, content.find(
'/') - 1);
207 else if (currentLine[currentLine.rfind(
'>') - 1] ==
'/') {
208 tagName = tagName.substr(0, tagName.find(
'/'));
209 content = (
"{EMPTY}");
235 string::iterator it = str.begin();
236 while (*it ==
' ' || *it ==
'\t') {
237 str.erase(str.begin());
240 while (*it ==
' ' || *it ==
'\t') {
241 str.erase(str.end() - 1);
252 cout <<
"*** The stack now contains: ";
254 if (!elementVect.empty()) {
255 for (vector<Element*>::iterator it = elementVect.begin(); it != elementVect.end(); ++it) {
256 cout << (*it)->getTagName();
260 if (it == elementVect.end() - 1)
267 cout <<
"{EMPTY}" << endl;
285 switch (currentState) {
290 cout << lineNumber <<
": " << currentLine << endl;
291 cout <<
"*** Comment started";
298 cout << lineNumber <<
": " << currentLine << endl;
299 cout <<
"*** Comment line";
307 cout << lineNumber <<
": " << currentLine << endl;
308 cout <<
"*** Ending comment";
315 cout << lineNumber <<
": " << content << endl;
316 cout <<
"*** One line comment";
324 cout << lineNumber <<
" : " << currentLine << endl;
325 cout <<
"*** Directive = " << content << endl;
336 Element* ElementPtr =
new Element(tagName, lineNumber,
"Empty", currentLine);
339 currentElement->
addChild(ElementPtr);
349 cout << lineNumber <<
" : " << currentLine << endl;
350 cout <<
"*** Element Opened = " << tagName << endl;
352 if (currentElement != emptyRoot) {
353 cout <<
"*** Creating new child: " << ElementPtr->
getTagName()
354 <<
", for parent: " << currentElement->
getTagName() << endl;
356 cout <<
"*** No parent found, creating root element: " << ElementPtr->
getTagName() << endl;
360 cout <<
"*** Attributes found: " << endl;
371 currentElement = ElementPtr;
379 Element* ElementPtr =
new Element(tagName, lineNumber, content, currentLine);
380 currentElement->
addChild(ElementPtr);
384 cout << lineNumber <<
" : " << currentLine << endl;
385 cout <<
"*** Complete element found:" << endl;
386 cout <<
"*** Element Name = " << tagName << endl;
387 cout <<
"*** Element Content = " << content << endl;
388 cout <<
"*** Creating new child: " << ElementPtr->
getTagName()
389 <<
", for parent: " << currentElement->
getTagName() << endl;
392 cout <<
"*** Attributes found: " << endl;
395 cout <<
"*** Stack unchanged" << endl;
411 cout << lineNumber <<
" : " << currentLine << endl;
415 cout <<
"ERROR!!! - Closing tag on line " << lineNumber
416 <<
" does not match last opened tag, '"
419 cout <<
"XML is not well formed. Ending program." << endl;
420 currentState =
ERROR;
427 currentElement = currentElement->
getParent();
432 cout <<
"*** Element closed = " << tagName << endl;
444 Element* ElementPtr =
new Element(tagName, lineNumber,
"Empty", currentLine);
445 currentElement->
addChild(ElementPtr);
449 cout << lineNumber <<
" : " << currentLine << endl;
450 cout <<
"*** Self-closing element found: " << endl;
451 cout <<
"*** Element Name = " << tagName << endl;
452 cout <<
"*** Element Content = " << content << endl;
453 cout <<
"*** Creating new child: " << ElementPtr->
getTagName()
454 <<
", for parent: " << currentElement->
getTagName() << endl;
457 cout <<
"*** Attributes found: " << endl;
460 cout <<
"*** Stack unchanged." << endl;
470 cout << currentLine << endl;
471 cout <<
"Parser state unknown!" << endl;
472 cout <<
"Last line checked was line number " << lineNumber <<
"." << endl;
473 cout <<
"Entire line: " << currentLine << endl;
474 cout <<
"XML is not well formed. Ending program." << endl;
480 cout << currentLine << endl;
481 cout <<
"ERROR!!!" << endl;
482 cout <<
"XML is not well formed. Ending program." << endl;
488 cout << endl << endl;
510 infile.open(strPath);
519 getline(infile, currentLine);
527 cout <<
"Building tree for file: " << strPath <<
"..." << endl << endl;
533 while (!currentLine.empty() && currentState !=
ERROR && currentState !=
UNKNOWN) {
537 currentLine =
trim(currentLine);
542 currentState =
parse(currentLine, currentState, content, tagName);
546 proccesLine(currentLine, currentState, lineNumber, content, tagName);
550 getline(infile, currentLine);
572 for (vector<Element*>::iterator it = vect.begin(); it != vect.end(); ++it) {
586 if (currentElement != emptyRoot) {
588 for (
int i = 1; i < currentElement->
getLevel(); i++) {
591 cout <<
"Element \"" << currentElement->
getTagName()
592 <<
"\" was found at line " << currentElement->
getLineNo()
593 <<
", level " << currentElement->
getLevel();
595 cout <<
", with no content";
597 cout <<
", containing \"" << currentElement->
getStrContent() <<
'"';
606 cout <<
", along with " << currentElement->
vecAttribute.size();
609 cout <<
" attribute: " << endl;
611 cout <<
" attributes: " << endl;
622 for (
int i = 0; i < 40; i++) {
625 cout << endl << endl <<
"Now showing the element tree:" << endl << endl;
632 for (vector<Element*>::iterator it = currentElement->
vecChildren.begin();
649 for (vector<Element*>::iterator it = currentElement->
vecChildren.begin();
667 while (input != 1 && input != 2 && cin) {
668 cout <<
"Would you like to see the tree as it is being built?" << endl;
669 cout <<
"Enter 1 for yes or 2 for no:" << endl;
685 cout <<
"Invalid entry, please try again." << endl;
687 cin.ignore(256,
'\n');
703 while (input != 1 && input != 2 && cin) {
704 cout <<
"\nWould you like to also create an HTML file of the element tree?" << endl;
705 cout <<
"Enter 1 for yes or 2 for no:" << endl;
721 cout <<
"Invalid entry, please try again." << endl;
723 cin.ignore(256,
'\n');
739 for (
int i = 0; i < currentElement->
getLevel(); i++) {
754 if (currentElement == emptyRoot) {
755 cout <<
"Now outputting the element tree as an HTML file..." << endl;
757 os <<
"<!DOCTYPE html>\n<html lang=\"en\">\n\n<head>\n <title></title>\n"
758 " <meta charset=\"utf-8\">\n</head>\n<body>\n";
761 if (currentElement != emptyRoot) {
764 for (
int i = 1; i < currentElement->
getLevel(); i++) {
767 os <<
"Element \"" << currentElement->
getTagName()
768 <<
"\" was found at line " << currentElement->
getLineNo()
769 <<
", level " << currentElement->
getLevel();
771 os <<
", with no content";
773 os <<
", containing \"" << currentElement->
getStrContent() <<
'"';
782 os <<
", along with " << currentElement->
vecAttribute.size();
785 os <<
" attribute: ";
787 os <<
" attributes: ";
789 for (vector<Attribute*>::iterator it = currentElement->
vecAttribute.begin();
793 for (
int j = 1; j < currentElement->
getLevel(); j++) {
797 os <<
"Attribute name: " << (*it)->getAttName();
798 os <<
", Attribute value: " << (*it)->getAttValue();
807 os <<
"</p>" << endl;
814 for (vector<Element*>::iterator it = currentElement->
vecChildren.begin();
822 if (currentElement == emptyRoot) {
825 cout <<
"HTML output complete." << endl;
840 if (currentElement == emptyRoot) {
841 cout <<
"Now outputting the element tree as a JSON file..." << endl;
847 os <<
"\"" << currentElement->
getTagName() <<
"\":{\n";
849 for (vector<Attribute*>::iterator it = currentElement->
vecAttribute.begin();
852 os <<
"\"" << (*it)->getAttName() <<
"\":";
853 os <<
"\"" << (*it)->getAttValue() <<
"\",\n";
860 os <<
"\"" << currentElement->
getTagName() <<
"\":";
866 if (currentElement->
getStrContent().find(
'\"') != string::npos) {
868 for (
int i = 0; i < content.size(); i++) {
869 if (content[i] ==
'\"') {
870 content = content.substr(0, i) +
'\\' + content.substr(i, content.size());
889 os <<
"\"" << currentElement->
getTagName() <<
"\"";
893 for (vector<Attribute*>::iterator it = currentElement->
vecAttribute.begin();
897 os <<
"\"" << (*it)->getAttName() <<
"\":";
898 os <<
"\"" << (*it)->getAttValue() <<
"\"";
909 os <<
":\"Empty\",\n";
914 for (vector<Element*>::iterator it = currentElement->
vecChildren.begin();
932 if (currentElement == emptyRoot) {
934 cout <<
"JSON output complete." << endl;
945 int main(
int argc,
char** argv) {
955 if (
openfile(
"Assignment5_musicFile.xml") == EXIT_SUCCESS) {
961 jsonStream.open(
"JSON_OUTPUT.json");
968 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 addSpaces(Element *currentElement, ofstream &os)
void deleteTree(Element *currentElement)
void writeJSON(Element *currentElement, ofstream &os)
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)
void setStrContent(string content)