Milo  0.1
Milo:EquationManipulationProgram
Public Member Functions | Private Member Functions | Private Attributes | List of all members
XML::Parser Class Reference

#include <xml.h>

Inheritance diagram for XML::Parser:
Inheritance graph
[legend]
Collaboration diagram for XML::Parser:
Collaboration graph
[legend]

Public Member Functions

 Parser (std::istream &in, const std::string &root)
 
virtual ~Parser ()
 
bool check (State state, const std::string &tag=std::string())
 
Parser & next (State state, const std::string &tag=std::string())
 
void finish ()
 
const std::string & getTag () const
 
bool getAttribute (const std::string &name, std::string &value)
 
bool hasAttributes ()
 
bool hasElement ()
 
const std::string & getElement ()
 
void syntaxError (const std::string &msg)
 

Private Member Functions

bool EOL () const
 
void next ()
 
void tokenize (std::istream &in)
 
void tokenize (std::string xml)
 
void parse_attributes (const std::string &nv)
 
void parse (State &state, std::string &tag)
 

Private Attributes

std::vector< std::string > m_tokens
 Storage for XML tokens to be processed.
 
unsigned int m_pos
 Index of current token being processed.
 
std::unordered_map< std::string, std::string > m_attributes
 
FSM fsm
 XML finite state machine.
 
std::string m_tag
 last heder name tag processed
 
std::string m_element
 last tag element processed
 

Detailed Description

Parsing of XML tree from input stream. Inspector member functions getTag, getAttributes, getElement allow you to extract the XML information read in.

Constructor & Destructor Documentation

XML::Parser::Parser ( std::istream &  in,
const std::string &  root 
)

Constructor for class Parser. Initialize character input stream and read in root header tag.

Parameters
inInput containing xml content
rootRoot xml tag
virtual XML::Parser::~Parser ( )
inlinevirtual

Virtual desctructor for class Parser. Class Parser is often inherited to support using this class as a paramter to class's constructor to deserialize.

Member Function Documentation

bool Parser::check ( State  state,
const std::string &  tag = std::string() 
)

Checks that the current XML token matches the state and if not empty, the tag passed to it.

Parameters
stateXML state to be matched.
tagtag to be matched if non empty.
Returns
True if match succeeded.
bool XML::Parser::EOL ( ) const
inlineprivate

Checks if at end of XML processing.

Returns
Return true if last XML token was processed.
void Parser::finish ( )

Finish parsing XML stream by classing all remaining footers. Assume that all remaining tags are footers and close them.

bool Parser::getAttribute ( const std::string &  name,
std::string &  value 
)

Looks for a attribute name and store its value in the argument value. All name/value pairs have been stores in m_attributes data member. If string value in name is found, store value in argument value.

Parameters
nameName of attribute to be searched for.
[out]valueValue of attribute found. @
Returns
Return true if name found in attribute map
const std::string& XML::Parser::getElement ( )
inline

Return the last element in the last header/footer processed. Will be empty if no element was found.

Returns
Return last element processed if any.
const std::string& XML::Parser::getTag ( ) const
inline

Return the last tag processed by the stream. This is the current tag being processed. This is not cleared until a new header or footer is processed.

Returns
Current tag
bool XML::Parser::hasAttributes ( )
inline

Checks for any attributes in the last header processed. Checks for non-empty attributes map which is clear when new header is parsed.

Returns
True if attributes have been processed.
bool XML::Parser::hasElement ( )
inline

Checks for any element in last header/footer tags processed. If an element was found in last header/footer tag it is stored in m_element.

Returns
Return true if m_element is non-empty.
Parser & Parser::next ( State  state,
const std::string &  tag = std::string() 
)

Checks that the next XML token matches the state and if not empty, the tag passed to it. If match fails throw a syntax error exception.

Parameters
stateXML state to be matched.
tagtag to be matched if non empty.
Returns
Reference to this class object for chaining calls.
void Parser::next ( )
private

Parses the current token. Populates the Parser class object with the XML information and advances the FSM checking if the transition was legal.

void Parser::parse ( State &  state,
std::string &  tag 
)
private

Parses the current token and save result in state and tag arguments. Will parse the current token, m_tokens[m_pos], and return the corresponding state and tag if appropiate. Mostly a helper function for Parser::next().

Parameters
[out]stateLoaded with state of the current token.
[out]tagLoaded with tag of current token if appropiate otherwise empty.
void Parser::parse_attributes ( const std::string &  nv)
private

Parses the attributes of a header tag. Expects a string containing name="value" pairs separated by white space. It then populates the m_attributes map with these name value pairs.

Parameters
nvString containing mulitple name="value" pairs seperated by whitespace.
void Parser::syntaxError ( const std::string &  msg)

Throws an exception display message string and XML trace. The exception string contain all the XML that has been parsed so far plus the message string.

Parameters
msgText for message in syntax error.
void Parser::tokenize ( std::istream &  in)
private

Tokenize the entire XML input stream. Reads from the stream tokenizing it. It reads line by line until it finds the character that will end the current XML tag which is then tokenized. Carriage returns are perserved and can be in name, value pairs and element tags.

Parameters
inCharacter input stream containing XML.
void Parser::tokenize ( std::string  xml)
private

Helper function for tokenize(std::istream&). This function is passed substrings which are further processed into tokens.

Parameters
xmlSubstring from XML input stream.

Member Data Documentation

std::unordered_map<std::string, std::string> XML::Parser::m_attributes
private

Map of attributes of last header processed. The name/value pairs of the attributes from the last header processed, i.e. text="foo".


The documentation for this class was generated from the following files: