Classes | List of all members
moot::mootExpatParser Class Reference

C++ Wrapper for expat XML parsers.

Inheritance diagram for moot::mootExpatParser:
Inheritance graph
[legend]
Collaboration diagram for moot::mootExpatParser:
Collaboration graph
[legend]

Classes

class  ContextBuffer
 Utility class for expat input contexts. More...
 

Public Member Functions

Constructors etc.
 mootExpatParser (size_t bufsize=8192, const std::string &encoding="")
 
virtual void setEncoding (const std::string &encoding="")
 
virtual void reset (void)
 
virtual ~mootExpatParser (void)
 
Input Selection
virtual void from_mstream (mootio::mistream *mistreamp, bool autoclose=false)
 
virtual void from_mstream (mootio::mistream &mistream, bool autoclose=false)
 
virtual void from_filename (const char *filename)
 
virtual void from_file (FILE *file)
 
virtual void from_fd (int fd)
 
virtual void from_buffer (const void *buf, size_t len)
 
virtual void from_string (const char *s)
 
virtual void from_cxxstream (std::istream &is)
 
virtual void close (void)
 
Parsing
virtual bool parse_check (void)
 
bool parse_filename (const std::string &filename)
 
bool parse_file (FILE *infile=stdin, const std::string &in_name="")
 
bool parse_buffer (const char *buf, size_t buflen)
 
bool parse_all (mootio::mistream *in=__null)
 
bool parse_chunk (int &nbytes, int &is_final, mootio::mistream *in=__null)
 
Utilities
virtual void context_dump (FILE *tofile=__null)
 
virtual std::string context_string (void)
 
virtual void carp (const char *fmt,...)
 
virtual void xpcarp (const char *fmt,...)
 
long int line_number (void)
 
int column_number (void)
 
long byte_offset (void)
 
int byte_count (void)
 
Handlers
virtual void XmlDeclHandler (const XML_Char *version, const XML_Char *encoding, int standalone)
 
virtual void StartElementHandler (const char *el, const char **attr)
 
virtual void EndElementHandler (const char *el)
 
virtual void CharacterDataHandler (const XML_Char *s, int len)
 
virtual void ProcessingInstructionHandler (const XML_Char *s, const XML_Char *target, const XML_Char *data)
 
virtual void CommentHandler (const XML_Char *s)
 
virtual void StartCdataSectionHandler (void)
 
virtual void EndCdataSectionHandler (void)
 
virtual void DefaultHandler (const XML_Char *s, int len)
 

Static Public Member Functions

Expat Callback Wrappers
static void _xp_XmlDeclHandler (mootExpatParser *mp, const XML_Char *version, const XML_Char *encoding, int standalone)
 
static void _xp_StartElementHandler (mootExpatParser *mp, const char *el, const char **attr)
 
static void _xp_EndElementHandler (mootExpatParser *mp, const char *el)
 
static void _xp_CharacterDataHandler (mootExpatParser *mp, const XML_Char *s, int len)
 
static void _xp_CommentHandler (mootExpatParser *mp, const XML_Char *s)
 
static void _xp_DefaultHandler (mootExpatParser *mp, const XML_Char *s, int len)
 

Public Attributes

I/O Data
mootio::mistreamxp_istream
 Current input stream. More...
 
bool xp_istream_created
 whether input stream mis was created locally More...
 
Internal Data
char * xml_buf
 Parse buffer for expat parser. More...
 
size_t xml_buflen
 Allocated size of xml_buf. More...
 
std::string xml_encoding
 Input encoding override (goofy) More...
 
XML_Parser parser
 The underlying expat parser object. More...
 

Constructor & Destructor Documentation

◆ mootExpatParser()

moot::mootExpatParser::mootExpatParser ( size_t  bufsize = 8192,
const std::string &  encoding = "" 
)

Default constructor:

Parameters
bufsizelength of parse buffer for expat
encodingoverride document input encoding (broken?)

◆ ~mootExpatParser()

virtual moot::mootExpatParser::~mootExpatParser ( void  )
virtual

Default destructor

Referenced by setEncoding().

Member Function Documentation

◆ setEncoding()

virtual void moot::mootExpatParser::setEncoding ( const std::string &  encoding = "")
inlinevirtual

Override document encoding. Implicitly reset()s the parser.

References from_mstream(), reset(), and ~mootExpatParser().

◆ reset()

virtual void moot::mootExpatParser::reset ( void  )
virtual

Reset parser state: re-initializes the internal xml buffer, as well as the expat parser, its encoding and handlers.

Reimplemented in moot::TokenReaderExpat.

Referenced by setEncoding().

◆ from_mstream() [1/2]

virtual void moot::mootExpatParser::from_mstream ( mootio::mistream mistreamp,
bool  autoclose = false 
)
virtual

◆ from_mstream() [2/2]

virtual void moot::mootExpatParser::from_mstream ( mootio::mistream mistream,
bool  autoclose = false 
)
inlinevirtual

Select input from a mootio::mistream reference. This is the basic case. Descendant classes may override this method.

References from_file(), from_filename(), and from_mstream().

Referenced by from_mstream().

◆ from_filename()

virtual void moot::mootExpatParser::from_filename ( const char *  filename)
virtual

Select input from a named file. Descendants using named file input may override this method. The filename "-" may be used to specify stdin. Default implementation calls from_mstream().

Reimplemented in moot::TokenReaderExpat.

Referenced by from_mstream().

◆ from_file()

virtual void moot::mootExpatParser::from_file ( FILE *  file)
virtual

Select input from a C stream. Caller is responsible for opening and closing the stream. Descendants using C stream input may override this method. Default implementation calls from_mstream().

Reimplemented in moot::TokenReaderExpat.

Referenced by from_mstream().

◆ from_fd()

virtual void moot::mootExpatParser::from_fd ( int  fd)
inlinevirtual

Select input from a file descriptor. Caller is responsible for opening and closing the stream. Descendants using file descriptor input may override this method. No default implementation.

Reimplemented in moot::TokenReaderExpat.

References mootio::mstream::close(), and from_buffer().

◆ from_buffer()

virtual void moot::mootExpatParser::from_buffer ( const void *  buf,
size_t  len 
)
virtual

Select input from a C memory-buffer. Caller is responsible for allocation and de-allocation. Descendants using C memory-buffer input may override this method. Default implementation calls from_mstream().

Reimplemented in moot::TokenReaderExpat.

Referenced by from_fd(), and from_string().

◆ from_string()

virtual void moot::mootExpatParser::from_string ( const char *  s)
inlinevirtual

Select input from a NUL-terminated C string. Caller is responsible for allocation and de-allocation. Descendants using C string input may override this method. Default implementation calls from_cbuffer(s,len).

References mootio::mstream::close(), context_dump(), from_buffer(), from_cxxstream(), parse_all(), parse_buffer(), parse_check(), parse_chunk(), parse_file(), and parse_filename().

◆ from_cxxstream()

virtual void moot::mootExpatParser::from_cxxstream ( std::istream &  is)
virtual

Select input from a C++ stream. Caller is responsible for allocation and de-allocation. Descendants using C++ stream input may override this method. Default implementation calls from_mstream().

Reimplemented in moot::TokenReaderExpat.

Referenced by from_string().

◆ close()

virtual void moot::mootExpatParser::close ( void  )
virtual

Finish input from currently selected source & perform any required cleanup operations. This method should always be called before selecting a new input source.

Descendants may override this method.

Reimplemented in moot::TokenReaderExpat.

◆ parse_check()

virtual bool moot::mootExpatParser::parse_check ( void  )
virtual

Check whether object is in a sane state for parsing, printing warning messages if this is not the case

Referenced by from_string().

◆ parse_filename()

bool moot::mootExpatParser::parse_filename ( const std::string &  filename)

Parse an entire named file

Referenced by from_string().

◆ parse_file()

bool moot::mootExpatParser::parse_file ( FILE *  infile = stdin,
const std::string &  in_name = "" 
)

Parse a C stream until EOF

Referenced by from_string().

◆ parse_buffer()

bool moot::mootExpatParser::parse_buffer ( const char *  buf,
size_t  buflen 
)

Parse from your very own string buffer.

Warning
This method is not intended to be called incrementally, so the stack is cleared before parsing the buffer. If you want to parse incrementally, use parse_chunk()
Parameters
bufbuffer to parse
buflennumber of bytes to parse from the buffer

Referenced by from_string().

◆ parse_all()

bool moot::mootExpatParser::parse_all ( mootio::mistream in = __null)

Parse the entirety of the stream in, which defaults to the currently selected input stream. Only minimal sanity checks are performed.

Referenced by from_string().

◆ parse_chunk()

bool moot::mootExpatParser::parse_chunk ( int &  nbytes,
int &  is_final,
mootio::mistream in = __null 
)

Read in and parse the next chunk from input source in using an internal buffer. Returns true on success, false on failure.

Parameters
nbytesnumber of bytes read from file will be stored here
is_finalwill be 1 if the file ended, 0 otherwise
ininput stream to read from (default is to use current selection)
Warning
you should have called reset() at least once before calling this method – no sanity checks are performed!

Referenced by from_string().

◆ context_dump()

virtual void moot::mootExpatParser::context_dump ( FILE *  tofile = __null)
virtual

Print current parser context (in real input encoding)

Referenced by from_string().

◆ context_string()

virtual std::string moot::mootExpatParser::context_string ( void  )
inlinevirtual

Get current parser context as a std::string

References carp(), moot::mootExpatParser::ContextBuffer::ContextBuffer(), and xpcarp().

◆ carp()

virtual void moot::mootExpatParser::carp ( const char *  fmt,
  ... 
)
virtual

complain

Referenced by context_string().

◆ xpcarp()

virtual void moot::mootExpatParser::xpcarp ( const char *  fmt,
  ... 
)
virtual

Complain initiated by expat

Referenced by context_string().

◆ line_number()

long int moot::mootExpatParser::line_number ( void  )
inline

Get current line number of expat parser

◆ column_number()

int moot::mootExpatParser::column_number ( void  )
inline

Get current column number of expat parser

◆ byte_offset()

long moot::mootExpatParser::byte_offset ( void  )
inline

Get current byte offset of expat parser

◆ byte_count()

int moot::mootExpatParser::byte_count ( void  )
inline

Get byte-count for current event of expat parser

◆ XmlDeclHandler()

virtual void moot::mootExpatParser::XmlDeclHandler ( const XML_Char *  version,
const XML_Char *  encoding,
int  standalone 
)
inlinevirtual

Handle XML declarations

Reimplemented in moot::TokenReaderExpat.

Referenced by _xp_XmlDeclHandler().

◆ StartElementHandler()

virtual void moot::mootExpatParser::StartElementHandler ( const char *  el,
const char **  attr 
)
inlinevirtual

Handle start elements

Reimplemented in moot::TokenReaderExpat.

Referenced by _xp_StartElementHandler().

◆ EndElementHandler()

virtual void moot::mootExpatParser::EndElementHandler ( const char *  el)
inlinevirtual

Handle end elements

Reimplemented in moot::TokenReaderExpat.

Referenced by _xp_EndElementHandler().

◆ CharacterDataHandler()

virtual void moot::mootExpatParser::CharacterDataHandler ( const XML_Char *  s,
int  len 
)
inlinevirtual

Handle character data

Reimplemented in moot::TokenReaderExpat.

Referenced by _xp_CharacterDataHandler().

◆ ProcessingInstructionHandler()

virtual void moot::mootExpatParser::ProcessingInstructionHandler ( const XML_Char *  s,
const XML_Char *  target,
const XML_Char *  data 
)
inlinevirtual

Handle PIs

◆ CommentHandler()

virtual void moot::mootExpatParser::CommentHandler ( const XML_Char *  s)
inlinevirtual

Handle comments

Reimplemented in moot::TokenReaderExpat.

Referenced by _xp_CommentHandler().

◆ StartCdataSectionHandler()

virtual void moot::mootExpatParser::StartCdataSectionHandler ( void  )
inlinevirtual

Handle start of CDATA sections

◆ EndCdataSectionHandler()

virtual void moot::mootExpatParser::EndCdataSectionHandler ( void  )
inlinevirtual

Handle end of CDATA sections

◆ DefaultHandler()

virtual void moot::mootExpatParser::DefaultHandler ( const XML_Char *  s,
int  len 
)
inlinevirtual

Handle any other document-internal data (no entity expansion!)

Reimplemented in moot::TokenReaderExpat.

Referenced by _xp_DefaultHandler().

◆ _xp_XmlDeclHandler()

static void moot::mootExpatParser::_xp_XmlDeclHandler ( mootExpatParser mp,
const XML_Char *  version,
const XML_Char *  encoding,
int  standalone 
)
inlinestatic

References XmlDeclHandler().

◆ _xp_StartElementHandler()

static void moot::mootExpatParser::_xp_StartElementHandler ( mootExpatParser mp,
const char *  el,
const char **  attr 
)
inlinestatic

References StartElementHandler().

◆ _xp_EndElementHandler()

static void moot::mootExpatParser::_xp_EndElementHandler ( mootExpatParser mp,
const char *  el 
)
inlinestatic

References EndElementHandler().

◆ _xp_CharacterDataHandler()

static void moot::mootExpatParser::_xp_CharacterDataHandler ( mootExpatParser mp,
const XML_Char *  s,
int  len 
)
inlinestatic

◆ _xp_CommentHandler()

static void moot::mootExpatParser::_xp_CommentHandler ( mootExpatParser mp,
const XML_Char *  s 
)
inlinestatic

References CommentHandler().

◆ _xp_DefaultHandler()

static void moot::mootExpatParser::_xp_DefaultHandler ( mootExpatParser mp,
const XML_Char *  s,
int  len 
)
inlinestatic

References DefaultHandler().

Member Data Documentation

◆ xp_istream

mootio::mistream* moot::mootExpatParser::xp_istream

◆ xp_istream_created

bool moot::mootExpatParser::xp_istream_created

◆ xml_buf

char* moot::mootExpatParser::xml_buf

◆ xml_buflen

size_t moot::mootExpatParser::xml_buflen

◆ xml_encoding

std::string moot::mootExpatParser::xml_encoding

◆ parser

XML_Parser moot::mootExpatParser::parser

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