Computing 4 Assignment 5 - David Lordan
 All Classes Files Functions Variables Enumerations Enumerator
Enumerations | Functions | Variables
main.cpp File Reference
#include <cstdlib>
#include <string>
#include <fstream>
#include <iostream>
#include <vector>
#include <algorithm>
#include "Element.h"

Go to the source code of this file.

Enumerations

enum  ParserState {
  UNKNOWN, STARTING_DOCUMENT, DIRECTIVE, ELEMENT_OPENING_TAG,
  ELEMENT_CONTENT, ELEMENT_NAME_AND_CONTENT, ELEMENT_CLOSING_TAG, SELF_CLOSING_TAG,
  STARTING_COMMENT, IN_COMMENT, ENDING_COMMENT, ONE_LINE_COMMENT,
  ERROR
}
 

Functions

void ShowState (ParserState ps)
 
ParserState parse (string currentLine, ParserState currentState, string &content, string &tagName)
 
string trim (string str)
 
void showStack (vector< Element * > elementVect)
 
void proccesLine (string currentLine, ParserState &currentState, int lineNumber, string content, string tagName)
 
int openfile (string strPath)
 
void clearVectorContents (vector< Element * > &vect)
 
void displayTree (Element *currentElement)
 
void deleteTree (Element *currentElement)
 
void askDisplay ()
 
bool askOutputFormat ()
 
void addSpaces (Element *currentElement, ofstream &os)
 
void writeHTML (Element *currentElement, ofstream &os)
 
void writeJSON (Element *currentElement, ofstream &os)
 
int main (int argc, char **argv)
 

Variables

vector< Element * > elementVect
 
ElementcurrentElement = new Element
 
ElementemptyRoot = currentElement
 
bool showProcessing = false
 

Enumeration Type Documentation

Enumeration for each of the possible states of XML document. These will be used extensively in the program to determine the context of a particular line of text. These states are determined by the parse function below. This was made by Prof. Jesse Heines of UML for the 91.204 Computing IV course and is taken from that course's lecture notes.

Enumerator
UNKNOWN 
STARTING_DOCUMENT 
DIRECTIVE 
ELEMENT_OPENING_TAG 
ELEMENT_CONTENT 
ELEMENT_NAME_AND_CONTENT 
ELEMENT_CLOSING_TAG 
SELF_CLOSING_TAG 
STARTING_COMMENT 
IN_COMMENT 
ENDING_COMMENT 
ONE_LINE_COMMENT 
ERROR 

Definition at line 68 of file main.cpp.

Function Documentation

void addSpaces ( Element currentElement,
ofstream &  os 
)

Function to add spaces to an output file corresponding with the element's level. This is simply to make the JSON file more readable and does not affect the file's validity.

Parameters
currentElement- The current element who's level is be checked.
os- The output stream of a JSON file. Passed by reference.

Definition at line 738 of file main.cpp.

void askDisplay ( )

Function to as the user if they wish to have the tree building processed displayed for them. If so, the boolean 'showProcessing' is set to true.

Definition at line 664 of file main.cpp.

bool askOutputFormat ( )

Function to ask the user for an output format. Currently the only optional output format is as an HTML file. I considered making the JSON output optional, but since that is the purpose of the current assignment, the JSON file will be created no matter what.

Returns
- A boolean value that will be checked before creating the HTML file of the tree.

Definition at line 700 of file main.cpp.

void clearVectorContents ( vector< Element * > &  vect)

As each object within the pointer had memory allocated on the heap, they must each be deleted "manually".

Parameters
vect- The vector of Element objects, each of which is to be deleted.

Definition at line 570 of file main.cpp.

void deleteTree ( Element currentElement)

Recursively traverses the tree, delete elements that whose memory was allocated dynamically.

Parameters
currentElement- The element that is to be deleted after all it's children are deleted.

Definition at line 645 of file main.cpp.

void displayTree ( Element currentElement)

Function that is called recursively to output the element tree, which is already stored in it's entirety in memory.

Parameters
currentElement- The current element whose information is to be displayed.

Definition at line 583 of file main.cpp.

int main ( int  argc,
char **  argv 
)

Standard C++ main function.

Parameters
argc- Number of command line arguments.
argv- Array of pointers to command line arguments.
Returns
- Returns error code if the program does not end properly.

If the 'openFile' function exits successfully, the element tree is displayed, converted to a JSON structure and exported, exported as an HTML file, then deleted recursively.

Definition at line 945 of file main.cpp.

int openfile ( string  strPath)

This function opens and reads the passed text file, line by line. As each line is taken from the input file, leading white space is removed, the current state is determined by calling the 'parse' function, and the line is processed. The 'processLine' function will also display relevant information about the current line. As each line is processed, an element tree is created.

Parameters
strPath- The name of the file to be read.
Returns
Status flag - If the current state after reading the file is not 'ERROR', returns EXIT_SUCCESS.

Definition at line 501 of file main.cpp.

ParserState parse ( string  currentLine,
ParserState  currentState,
string &  content,
string &  tagName 
)

This function parses a line read from the XML file, extracts the appropriate content and returns the determined state. The function assumes that the 'trim' function has already been applied to the line of text, removing any leading white space and/or tabs. Further assumptions are as follows: (1) The XML file is well-formed. (2) There is at most one complete element per line. (3) All element opening tags start on new lines. (4) If there is an element closing tag, it is the last thing to appear on that line. (5) If there is no element closing tag on the same line as an element opening tag, the line has no content. (6) Attributes and their values are ignored. Much of this logic of this function is based on lecture notes of Prof. Jesse Heines of UML for the 91.204 Computing IV course.

Parameters
currentLine- Most recent line read from XML file by 'openFile' function
currentState- The current state of the parser as determined by the last line parsed.
content- Used to store extracted content, if applicable. Passed by ref.
tagName- Used to store extracted tag name, if applicable. Passed by ref.
Returns
- The new parser state as determined by the algorithm in the function.

Definition at line 137 of file main.cpp.

void proccesLine ( string  currentLine,
ParserState currentState,
int  lineNumber,
string  content,
string  tagName 
)

Function to process each line, building the element tree. If the user has chosen so, the tree building itself will be displayed.

Parameters
currentLine- The current line of an XML file which is to be processed.
currentState- The current parser state of the XML file. Used to give context to a line.
lineNumber- The line number on which the current line was found.
content- The content within an element that was extracted from the 'parse' function.
tagName- The tag name that was extracted from an XML opening tag by the 'parse' function.

Definition at line 280 of file main.cpp.

void showStack ( vector< Element * >  elementVect)

This function iterates through the elementVect, which is used as a stack, displaying the current stack content. This is called whenever the stack content is changed.

Parameters
elementVect- The vector which stores element pointers, used to implement a stack.

Definition at line 251 of file main.cpp.

void ShowState ( ParserState  ps)

This function is used during debugging to display the parser state. This function was created by Prof. Jesse Heines of UML for the 91.204 Computing IV course and is taken from that course's lecture notes.

Parameters
psthe parser state

Definition at line 82 of file main.cpp.

string trim ( string  str)

Trim leading and trailing white space (spaces and tabs) from the string passed as an argument and return the trimmed string. NOTE: This function was written by Prof. Jesse Heines of UMass Lowell for the Fall 2014 91.204-Computing IV class.

Parameters
strstring to trim
Returns
trimmed string

Definition at line 234 of file main.cpp.

void writeHTML ( Element currentElement,
ofstream &  os 
)

This is an experimental function to output the element tree as an HTML file. This was suggested as an extra challenge for the assignment by Prof. Heines and is not part of the assignment's requirements. This is virtually identical to the algorithm in the 'displayTree' function.

Parameters
currentElement- The current element whose data is to be written to the HTML file.
os- ofstream object that is sent recursively to the function. Passed by reference.

Definition at line 752 of file main.cpp.

void writeJSON ( Element currentElement,
ofstream &  os 
)

Function to output the element tree as a JSON file. This a very similar algorithm to the 'displayTree' and 'writeHTML' functions, though with changes that allow for proper JSON syntax. The function is called recursively, traversing the entire element tree and creating a matching JSON structure.

Parameters
currentElement- The element whose children and attributes are to be written to the JSON file.
os- The output stream to the JSON file. Passed by reference.

Definition at line 837 of file main.cpp.

Variable Documentation

Element* currentElement = new Element

This is an element which is used as a placeholder for the current element being processed in the tree.

Definition at line 49 of file main.cpp.

vector<Element*> elementVect

Vector which acts as a stack, storing pointers to elements. This is used as the element tree is being built to display the building process.

Definition at line 43 of file main.cpp.

Element* emptyRoot = currentElement

A place holder "empty" element that is used to determine if the current element has been changd. This is used in the tree building and display process. Important note: This is NOT the root element of the XML file or the tree.

Definition at line 56 of file main.cpp.

bool showProcessing = false

A boolean value that stores whether or not the user wishes to see the tree being built.

Definition at line 59 of file main.cpp.