libSBML C++ API  5.18.0
XMLInputStream Class Reference

Detailed Description

An interface to an XML input stream.

This class of objects is defined by libSBML only and has no direct equivalent in terms of SBML components. This class is not prescribed by the SBML specifications, although it is used to implement features defined in SBML.

SBML content is serialized using XML; the resulting data can be stored and read to/from a file or data stream. Low-level XML parsers such as Xerces provide facilities to read XML data. To permit the use of different XML parsers (Xerces, Expat or libxml2), libSBML implements an abstraction layer. XMLInputStream and XMLOutputStream are two parts of that abstraction layer.

XMLInputStream is an interface to a file or text string containing XML. It wraps the content to be read, as well as the low-level XML parser to be used and an XMLErrorLog to record errors and other issues (if any arise). Internally, the content will be in the form of either a pointer to a file name or a character string; XMLInputStream knows the form of the content and acts appropriately. Other libSBML object classes use XMLInputStream as their interface for all read operations on the XML data. XMLInputStream provides the functionality to extract data in the form of XMLToken objects. It logs any errors encountered while reading. It also keeps track of whether a read operation has failed irrecoverably or determines whether it is safe to continue reading.

SBMLNamespaces objects can be associated with an XMLInputStream; this facilitates logging errors related to reading XML attributes and elements that may only be relevant to particular Level and Version combinations of SBML.

Note
The convenience of the XMLInputStream and XMLOutputStream abstraction may be useful for developers interested in creating parsers for other XML formats besides SBML. It can provide developers with a layer above more basic XML parsers, as well as some useful programmatic elements such as XMLToken, XMLError, etc.
See also
XMLOutputStream

Public Member Functions

bool containsChild (const std::string &childName, const std::string &container)
 Predicate returning true if a child token of the specified type occurs within a given container element. More...
 
unsigned int determineNumberChildren (const std::string elementName="")
 Returns the number of child tokens of the given element in this stream. More...
 
unsigned int determineNumSpecificChildren (const std::string &childName, const std::string &container)
 Returns the number of child tokens of the specified type within a given container element. More...
 
const std::string & getEncoding ()
 Returns the encoding of the XML stream. More...
 
XMLErrorLoggetErrorLog ()
 Returns the XMLErrorLog used to log parsing problems. More...
 
SBMLNamespacesgetSBMLNamespaces ()
 Returns the SBMLNamespaces object attached to this XMLInputStream. More...
 
const std::string & getVersion ()
 Returns the version of the XML stream. More...
 
bool isEOF () const
 Returns true if end of file (stream) has been reached. More...
 
bool isError () const
 Returns true if a fatal error occurred while reading from this stream. More...
 
bool isGood () const
 Returns true if the stream is in a good state. More...
 
XMLToken next ()
 Returns the next token on this XML input stream. More...
 
const XMLTokenpeek ()
 Returns the next token without consuming it. More...
 
int setErrorLog (XMLErrorLog *log)
 Sets the XMLErrorLog this stream will use to log errors. More...
 
void setSBMLNamespaces (SBMLNamespaces *sbmlns)
 Sets the SBML namespaces associated with this XML input stream. More...
 
void skipPastEnd (const XMLToken &element)
 Consume zero or more tokens up to and including the corresponding end element or EOF. More...
 
void skipText ()
 Consume zero or more tokens up to but not including the next XML element or EOF. More...
 
std::string toString ()
 Prints a string representation of the underlying token stream. More...
 
 XMLInputStream (const char *content, bool isFile=true, const std::string library="", XMLErrorLog *errorLog=NULL)
 Creates a new XMLInputStream. More...
 
virtual ~XMLInputStream ()
 Destroys this XMLInputStream. More...
 

Constructor & Destructor Documentation

XMLInputStream::XMLInputStream ( const char *  content,
bool  isFile = true,
const std::string  library = "",
XMLErrorLog errorLog = NULL 
)

Creates a new XMLInputStream.

Parameters
contentthe source of the stream.
isFilea boolean flag to indicate whether content is a file name. If true, content is assumed to be the file from which the XML content is to be read. If false, content is taken to be a string that is the content to be read.
librarythe name of the parser library to use.
errorLogthe XMLErrorLog object to use.
virtual XMLInputStream::~XMLInputStream ( )
virtual

Destroys this XMLInputStream.

Member Function Documentation

bool XMLInputStream::containsChild ( const std::string &  childName,
const std::string &  container 
)

Predicate returning true if a child token of the specified type occurs within a given container element.

This method allows information from the input stream to be determined without the need to actually read and consume the tokens in the stream. It returns true if the childName element occurs at any point within the element specified by container.

Parameters
childNamea string representing the name of the child element whose presence is to be determined.
containera string representing the name of the element for which the presence of the child element is to be determined.
Returns
boolean true if a child of type childName occurs within the container element, false otherwise.
Note
This method assumes the stream has been read up to and including the element container.
unsigned int XMLInputStream::determineNumberChildren ( const std::string  elementName = "")

Returns the number of child tokens of the given element in this stream.

This method allows information from the input stream to be determined without the need to actually read and consume the tokens in the stream. It returns the number of child elements of the element represented by the elementName, i.e., the number of child elements encountered before the closing tag for the elementName supplied.

If no elementName is supplied or it is an empty string, then as a special case, this method assumes the element is a MathML apply element followed by a function name.

Parameters
elementNamea string representing the name of the element for which the number of children are to be determined.
Returns
an unsigned int giving the number of children of the elementName specified.
Note
This method assumes the stream has been read up to and including the element elementName.
unsigned int XMLInputStream::determineNumSpecificChildren ( const std::string &  childName,
const std::string &  container 
)

Returns the number of child tokens of the specified type within a given container element.

This method allows information from the input stream to be determined without the need to actually read and consume the tokens in the stream. It returns the number of child elements of the childName element within the element specified by container. In other words, it counts the number of childName elements encountered before the closing tag for the container supplied.

Parameters
childNamea string representing the name of the child element whose number is to be determined.
containera string representing the name of the element for which the number of children are to be determined.
Returns
an unsigned int giving the number of children of type childName within the container element.
Note
This method assumes the stream has been read up to and including the element container.
const std::string& XMLInputStream::getEncoding ( )

Returns the encoding of the XML stream.

The encoding is indicated by the xml declaration at the
beginning of an XML document or data stream. The form of this declaration is
<?xml version="1.0" encoding="UTF-8"?>
Note that the SBML specifications require the use of UTF-8 encoding, so for SBML documents, the value returned by this method will always be the string "UTF-8".
Returns
the encoding of the XML stream.
See also
getVersion()
XMLErrorLog* XMLInputStream::getErrorLog ( )

Returns the XMLErrorLog used to log parsing problems.

Returns
the XMLErrorLog used to log XML parse errors and other validation errors (and messages).
SBMLNamespaces* XMLInputStream::getSBMLNamespaces ( )

Returns the SBMLNamespaces object attached to this XMLInputStream.

Returns
the SBMLNamespaces object or NULL if none has been set.
const std::string& XMLInputStream::getVersion ( )

Returns the version of the XML stream.

The version is indicated by the xml declaration at the
beginning of an XML document or data stream. The form of this declaration is
 <?xml version="1.0" encoding="UTF-8"?>
Note that the SBML specifications require the use of version 1.0, so for SBML documents, the value returned by this method will always be the string "1.0".
Returns
the version of the XML stream.
See also
getEncoding()
bool XMLInputStream::isEOF ( ) const

Returns true if end of file (stream) has been reached.

Returns
true if end of file (stream) has been reached, false otherwise.
bool XMLInputStream::isError ( ) const

Returns true if a fatal error occurred while reading from this stream.

Returns
true if a fatal error occurred while reading from this stream.
bool XMLInputStream::isGood ( ) const

Returns true if the stream is in a good state.

The definition of "good state" is that isEOF() and isError() both return false.

Returns
true if the stream is in a good state, false otherwise.
XMLToken XMLInputStream::next ( )

Returns the next token on this XML input stream.

The token is consumed in the process.

Returns
the next XMLToken, or an EOF token (i.e., XMLToken.isEOF() == true).
See also
peek()
const XMLToken& XMLInputStream::peek ( )

Returns the next token without consuming it.

A subsequent call to either peek() or next() will return the same token.

Returns
the next XMLToken or EOF (XMLToken.isEOF() == true).
See also
next()
int XMLInputStream::setErrorLog ( XMLErrorLog log)

Sets the XMLErrorLog this stream will use to log errors.

Returns
integer value indicating success/failure of the function. The value is drawn from the enumeration OperationReturnValues_t. The possible values returned by this function are:
void XMLInputStream::setSBMLNamespaces ( SBMLNamespaces sbmlns)

Sets the SBML namespaces associated with this XML input stream.

This allows this stream to reference the available SBML namespaces being read.

Parameters
sbmlnsthe list of namespaces to use.
void XMLInputStream::skipPastEnd ( const XMLToken element)

Consume zero or more tokens up to and including the corresponding end element or EOF.

Parameters
elementthe element whose end will be sought in the input stream.
void XMLInputStream::skipText ( )

Consume zero or more tokens up to but not including the next XML element or EOF.

std::string XMLInputStream::toString ( )

Prints a string representation of the underlying token stream.

Returns
a string representing the underlying XML token stream.
Note
This method is intended for debugging purposes.