Loading...
Searching...
No Matches
XML_Parser.h
/************************************************************************/
/* */
/* Centre for Speech Technology Research */
/* University of Edinburgh, UK */
/* Copyright (c) 1996,1997 */
/* All Rights Reserved. */
/* */
/* Permission is hereby granted, free of charge, to use and distribute */
/* this software and its documentation without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of this work, and to */
/* permit persons to whom this work is furnished to do so, subject to */
/* the following conditions: */
/* 1. The code must retain the above copyright notice, this list of */
/* conditions and the following disclaimer. */
/* 2. Any modifications must be clearly marked as such. */
/* 3. Original authors' names are not deleted. */
/* 4. The authors' names are not used to endorse or promote products */
/* derived from this software without specific prior written */
/* permission. */
/* */
/* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */
/* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */
/* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */
/* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */
/* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */
/* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */
/* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */
/* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */
/* THIS SOFTWARE. */
/* */
/*************************************************************************/
#ifndef __XML_PARSER_H__
#define __XML_PARSER_H__
#if !defined(CHAR_SIZE)
# define CHAR_SIZE 8
#endif
#if (CHAR_SIZE!=8)
# error EST can only handle 8 bit characters
#endif
#include "EST_String.h"
#include "EST_Regex.h"
#include "EST_TKVL.h"
#include "EST_THash.h"
#include "EST_TDeque.h"
#include "EST_TList.h"
#include "rxp/rxp.h"
// We only use types and functions from rxp.h, so we can throw away
// some of the macros which cause problems.
#undef get
/**@name XML Parser
* Recursive descent parsing skeleton with hooks for processing.
* A C++ wrapper around the rxp parser.
*
* @author Richard Caley <rjc@cstr.ed.ac.uk>
* @version $Id: XML_Parser.h,v 1.3 2004/05/04 00:00:17 awb Exp $
*/
//@{
class XML_Parser;
class XML_Parser_Class;
/// Nice name for list of attribute-value pairs.
typedef EST_TStringHash<EST_String> XML_Attribute_List;
/** A Class of parsers, All parsers share callbacks and a
* list of known public IDs.
*/
class XML_Parser_Class {
private:
/** Map PUBLIC and SYSTEM IDs to places on the local system.
*/
EST_TKVL<EST_Regex, EST_String> known_ids;
protected:
/** Do any necessary remappings and open a stream which reads the given
* entity.
*/
static InputSource open_entity(Entity ent, void *arg);
/**@name The callbacks.
*
* These methods can be overridden in a subclass to create a class
* of parsers to do whatever you want.
*/
//@{
/** Called when starting a document.
*/
virtual void document_open(XML_Parser_Class &c,
XML_Parser &p,
void *data);
/** Called at the end of a document.
*/
virtual void document_close(XML_Parser_Class &c,
XML_Parser &p,
void *data);
/** Called when an element starts.
*/
virtual void element_open(XML_Parser_Class &c,
XML_Parser &p,
void *data,
const char *name,
XML_Attribute_List &attributes);
/** Called when an element ends.
*/
virtual void element_close(XML_Parser_Class &c,
XML_Parser &p,
void *data,
const char *name);
/** Called for empty elements.
*
* Defaults to element_open(...) followed by element_closed(...).
*/
virtual void element(XML_Parser_Class &c,
XML_Parser &p,
void *data,
const char *name,
XML_Attribute_List &attributes);
/** Called for parsed character data sequences.
*/
virtual void pcdata(XML_Parser_Class &c,
XML_Parser &p,
void *data,
const char *chars);
/** Called for unparsed character data sequences.
*/
virtual void cdata(XML_Parser_Class &c,
XML_Parser &p,
void *data,
const char *chars);
/** Called for processing directives.
*/
virtual void processing(XML_Parser_Class &c,
XML_Parser &p,
void *data,
const char *instruction);
/** Called when there is an error in parsing.
*/
virtual void error(XML_Parser_Class &c,
XML_Parser &p,
void *data);
//@}
/** This can be called from any of the callbacks to present "message"
* as an error through the error callback, thus getting filename and
* line information into the message.
*/
void error(XML_Parser_Class &c,
XML_Parser &p,
void *data,
EST_String message);
/// Get the error message for the last error.
const char *get_error(XML_Parser &p);
public:
/** Create an object representing the class of parsers.
*/
XML_Parser_Class();
virtual ~XML_Parser_Class() { }
/** Add a mapping from entity ID (SYSTEM or PUBLIC) to filename.
*
* The string can contain escapes like \2 which are replaced by
* the text matching the Nth bracketed part of the regular expression.
*/
void register_id(EST_Regex id_pattern, EST_String directory);
/** Fill in the list with the known entity ID mappings.
*/
void registered_ids(EST_TList<EST_String> &list);
/**@name Creating a parser
*
* Each of these methods creates a one-shot parser which will run over the
* indicated text.
*/
//@{
/// Create a parser for the RXP InputSource.
XML_Parser *make_parser(InputSource source, void *data);
/// Create a parser for the RXP InputSource.
XML_Parser *make_parser(InputSource source, Entity initial_entity, void *data);
/// Create a parser for a stdio input stream.
XML_Parser *make_parser(FILE *input, void *data);
/** Create a parser for a stdio input stream, giving a description for
* use in errors.
*/
XML_Parser *make_parser(FILE *input, const EST_String desc, void *data);
// Create a parser for the named file.
XML_Parser *make_parser(const EST_String filename, void *data);
//@}
/** Utility which tries to open an entity called ID at places
* specified in the mapping of this parser class.
*/
InputSource try_and_open(Entity ent);
/** XML_Parser defines the behaviour of an individual one-shot
* parser.
*/
friend class XML_Parser;
};
/** An actual parser. Each such instance parses just one stream which is
* given when the parser is created.
*
* The behaviour of the parser is given by the class to which it belongs.
*/
class XML_Parser {
private:
/// Last error message from the parser.
EST_String p_error_message;
/// Set true when context is being remembered.
bool p_track_context;
/// Set true when contents is being remembered. (not yet implemented)
bool p_track_contents;
protected:
/** The class to which this parser belongs. Defines the behaviour of
* the parser.
*/
XML_Parser_Class *pclass;
/// The piece of markup being processed.
XBit current_bit;
/// Where we are reading from.
InputSource source;
/** The entity we started from. May need to be freed at the end of the
* parse.
*/
Entity initial_entity;
/// Arbitrary data which can be used by callbacks.
void *data;
/// The RXP parser object.
Parser p;
/// If context is being tracked, this is a stack of element names.
EST_TDeque<EST_String> p_context;
/// Creator used by XML_Parser_Class::make_parser()
XML_Parser(XML_Parser_Class &parent,
InputSource source,
Entity initial_entity,
void *data);
/// Open. Asks the parser class to do the work.
InputSource open(Entity ent);
/// Get the error message for the last error.
const char *get_error();
public:
/// Destructor, may close input if required.
~XML_Parser();
/** Request that parser keep track of the currently open elements.
*
* These are recorded on a atsck. Use context() to access the information.
*/
void track_context(bool flag);
/** Keep track of the content of open elements.
*
* Not yet implemented.
*/
void track_contents(bool flag);
/** Get the name of the nth enclosing element.
*
* context(0) is the element we are directly inside.
*/
EST_String context(int n);
/// Run the parser.
void go();
friend class XML_Parser_Class;
};
//@}
#endif
EST_Regex
Definition EST_Regex.h:55
EST_String
Definition EST_String.h:70
EST_TDeque
Definition EST_TDeque.h:48
EST_TKVL
Definition EST_TKVL.h:72
EST_TList
Definition EST_TList.h:109
EST_TStringHash
Definition EST_THash.h:281
XML_Parser_Class
Definition XML_Parser.h:78
XML_Parser_Class::XML_Parser
friend class XML_Parser
Definition XML_Parser.h:231
XML_Parser_Class::get_error
const char * get_error(XML_Parser &p)
Get the error message for the last error.
Definition XML_Parser.cc:225
XML_Parser_Class::element
virtual void element(XML_Parser_Class &c, XML_Parser &p, void *data, const char *name, XML_Attribute_List &attributes)
Definition XML_Parser.cc:186
XML_Parser_Class::make_parser
XML_Parser * make_parser(InputSource source, void *data)
Create a parser for the RXP InputSource.
Definition XML_Parser.cc:72
XML_Parser_Class::register_id
void register_id(EST_Regex id_pattern, EST_String directory)
Definition XML_Parser.cc:48
XML_Parser_Class::error
virtual void error(XML_Parser_Class &c, XML_Parser &p, void *data)
Definition XML_Parser.cc:220
XML_Parser_Class::try_and_open
InputSource try_and_open(Entity ent)
Definition XML_Parser.cc:125
XML_Parser_Class::processing
virtual void processing(XML_Parser_Class &c, XML_Parser &p, void *data, const char *instruction)
Definition XML_Parser.cc:214
XML_Parser_Class::document_close
virtual void document_close(XML_Parser_Class &c, XML_Parser &p, void *data)
Definition XML_Parser.cc:174
XML_Parser_Class::pcdata
virtual void pcdata(XML_Parser_Class &c, XML_Parser &p, void *data, const char *chars)
Definition XML_Parser.cc:202
XML_Parser_Class::document_open
virtual void document_open(XML_Parser_Class &c, XML_Parser &p, void *data)
Definition XML_Parser.cc:169
XML_Parser_Class::element_open
virtual void element_open(XML_Parser_Class &c, XML_Parser &p, void *data, const char *name, XML_Attribute_List &attributes)
Definition XML_Parser.cc:179
XML_Parser_Class::cdata
virtual void cdata(XML_Parser_Class &c, XML_Parser &p, void *data, const char *chars)
Definition XML_Parser.cc:208
XML_Parser_Class::registered_ids
void registered_ids(EST_TList< EST_String > &list)
Definition XML_Parser.cc:53
XML_Parser_Class::element_close
virtual void element_close(XML_Parser_Class &c, XML_Parser &p, void *data, const char *name)
Definition XML_Parser.cc:196
XML_Parser_Class::XML_Parser_Class
XML_Parser_Class()
Definition XML_Parser.cc:44
XML_Parser_Class::open_entity
static InputSource open_entity(Entity ent, void *arg)
Definition XML_Parser.cc:160
XML_Parser
Definition XML_Parser.h:240
XML_Parser::p
Parser p
The RXP parser object.
Definition XML_Parser.h:273
XML_Parser::current_bit
XBit current_bit
The piece of markup being processed.
Definition XML_Parser.h:259
XML_Parser::get_error
const char * get_error()
Get the error message for the last error.
Definition XML_Parser.cc:395
XML_Parser::XML_Parser
XML_Parser(XML_Parser_Class &parent, InputSource source, Entity initial_entity, void *data)
Creator used by XML_Parser_Class::make_parser().
Definition XML_Parser.cc:246
XML_Parser::data
void * data
Arbitrary data which can be used by callbacks.
Definition XML_Parser.h:270
XML_Parser::~XML_Parser
~XML_Parser()
Destructor, may close input if required.
Definition XML_Parser.cc:261
XML_Parser::open
InputSource open(Entity ent)
Open. Asks the parser class to do the work.
Definition XML_Parser.cc:269
XML_Parser::track_contents
void track_contents(bool flag)
Definition XML_Parser.cc:388
XML_Parser::go
void go()
Run the parser.
Definition XML_Parser.cc:274
XML_Parser::context
EST_String context(int n)
Definition XML_Parser.cc:450
XML_Parser::p_context
EST_TDeque< EST_String > p_context
If context is being tracked, this is a stack of element names.
Definition XML_Parser.h:276
XML_Parser::track_context
void track_context(bool flag)
Definition XML_Parser.cc:383
XML_Parser::pclass
XML_Parser_Class * pclass
Definition XML_Parser.h:256
XML_Parser::source
InputSource source
Where we are reading from.
Definition XML_Parser.h:262
XML_Parser::initial_entity
Entity initial_entity
Definition XML_Parser.h:267