Added the parse_xml() routines

This commit is contained in:
Davis King 2012-11-18 00:29:51 -05:00
parent a641960521
commit 0b7e3020de
3 changed files with 289 additions and 46 deletions

View File

@ -6,7 +6,9 @@
#include "xml_parser_kernel_abstract.h"
#include <sstream>
#include <string>
#include <fstream>
#include <iostream>
#include "xml_parser_kernel_interfaces.h"
#include "../algs.h"
@ -1356,6 +1358,159 @@ namespace dlib
}
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
class xml_parse_error : public error
{
public:
xml_parse_error(
const std::string& a
): error(a) {}
};
namespace impl
{
class default_xml_error_handler : public error_handler
{
std::string filename;
public:
default_xml_error_handler (
) {}
default_xml_error_handler (
const std::string& filename_
) :filename(filename_) {}
virtual void error (
const unsigned long
)
{
// just ignore non-fatal errors
}
virtual void fatal_error (
const unsigned long line_number
)
{
std::ostringstream sout;
if (filename.size() != 0)
sout << "There is a fatal error on line " << line_number << " in the XML file '"<<filename<<"'.";
else
sout << "There is a fatal error on line " << line_number << " in the XML being processed.";
throw xml_parse_error(sout.str());
}
};
}
inline void parse_xml (
std::istream& in,
document_handler& dh,
error_handler& eh
)
{
xml_parser parser;
parser.add_document_handler(dh);
parser.add_error_handler(eh);
parser.parse(in);
}
inline void parse_xml (
std::istream& in,
error_handler& eh,
document_handler& dh
)
{
xml_parser parser;
parser.add_document_handler(dh);
parser.add_error_handler(eh);
parser.parse(in);
}
inline void parse_xml (
std::istream& in,
error_handler& eh
)
{
xml_parser parser;
parser.add_error_handler(eh);
parser.parse(in);
}
inline void parse_xml (
std::istream& in,
document_handler& dh
)
{
xml_parser parser;
parser.add_document_handler(dh);
impl::default_xml_error_handler eh;
parser.add_error_handler(eh);
parser.parse(in);
}
// ----------------------------------------------------------------------------------------
inline void parse_xml (
const std::string& filename,
document_handler& dh,
error_handler& eh
)
{
std::ifstream in(filename.c_str());
if (!in)
throw xml_parse_error("Unable to open file '" + filename + "'.");
xml_parser parser;
parser.add_document_handler(dh);
parser.add_error_handler(eh);
parser.parse(in);
}
inline void parse_xml (
const std::string& filename,
error_handler& eh,
document_handler& dh
)
{
std::ifstream in(filename.c_str());
if (!in)
throw xml_parse_error("Unable to open file '" + filename + "'.");
xml_parser parser;
parser.add_document_handler(dh);
parser.add_error_handler(eh);
parser.parse(in);
}
inline void parse_xml (
const std::string& filename,
error_handler& eh
)
{
std::ifstream in(filename.c_str());
if (!in)
throw xml_parse_error("Unable to open file '" + filename + "'.");
xml_parser parser;
parser.add_error_handler(eh);
parser.parse(in);
}
inline void parse_xml (
const std::string& filename,
document_handler& dh
)
{
std::ifstream in(filename.c_str());
if (!in)
throw xml_parse_error("Unable to open file '" + filename + "'.");
xml_parser parser;
parser.add_document_handler(dh);
impl::default_xml_error_handler eh(filename);
parser.add_error_handler(eh);
parser.parse(in);
}
// ----------------------------------------------------------------------------------------

View File

@ -149,6 +149,128 @@ namespace dlib
provides a global swap function
!*/
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
class xml_parse_error : public error
{
/*!
WHAT THIS OBJECT REPRESENTS
This is the exception object thrown by the parse_xml() routines defined
below.
!*/
};
// ----------------------------------------------------------------------------------------
void parse_xml (
std::istream& in,
document_handler& dh,
error_handler& eh
);
/*!
ensures
- makes an xml_parser and tells it to parse the given input stream using the
supplied document_handler and error_handler.
!*/
void parse_xml (
std::istream& in,
error_handler& eh,
document_handler& dh
)
/*!
ensures
- makes an xml_parser and tells it to parse the given input stream using the
supplied document_handler and error_handler.
!*/
void parse_xml (
std::istream& in,
error_handler& eh
);
/*!
ensures
- makes an xml_parser and tells it to parse the given input stream using the
supplied error_handler.
!*/
void parse_xml (
std::istream& in,
document_handler& dh
);
/*!
ensures
- makes an xml_parser and tells it to parse the given input stream using the
supplied document_handler.
- Uses a default error handler that will throw an xml_parse_error exception
if a fatal parsing error is encountered.
throws
- xml_parse_error
Thrown if a fatal parsing error is encountered.
!*/
// ----------------------------------------------------------------------------------------
void parse_xml (
const std::string& filename,
document_handler& dh,
error_handler& eh
);
/*!
ensures
- makes an xml_parser and tells it to parse the given input file using the
supplied error_handler and document_handler.
throws
- xml_parse_error
Thrown if there is a problem opening the input file.
!*/
void parse_xml (
const std::string& filename,
error_handler& eh,
document_handler& dh
);
/*!
ensures
- makes an xml_parser and tells it to parse the given input file using the
supplied error_handler and document_handler.
throws
- xml_parse_error
Thrown if there is a problem opening the input file.
!*/
void parse_xml (
const std::string& filename,
error_handler& eh
);
/*!
ensures
- makes an xml_parser and tells it to parse the given input file using the
supplied error_handler.
throws
- xml_parse_error
Thrown if there is a problem opening the input file.
!*/
void parse_xml (
const std::string& filename,
document_handler& dh
);
/*!
ensures
- makes an xml_parser and tells it to parse the given input file using the
supplied document_handler.
- Uses a default error handler that will throw an xml_parse_error exception
if a fatal parsing error is encountered.
throws
- xml_parse_error
Thrown if a fatal parsing error is encountered or if there is a problem
opening the input file.
!*/
// ----------------------------------------------------------------------------------------
}
#endif // DLIB_XML_PARSER_KERNEl_ABSTRACT_

View File

@ -91,57 +91,23 @@ public:
// ----------------------------------------------------------------------------------------
class xml_error_handler : public error_handler
{
/*
This class handles error events that occur during parsing.
Just like the document_handler class above it just prints the events to the screen.
*/
public:
virtual void error (
const unsigned long line_number
)
{
cout << "There is a non-fatal error on line " << line_number << " in the file we are parsing." << endl;
}
virtual void fatal_error (
const unsigned long line_number
)
{
cout << "There is a fatal error on line " << line_number << " so parsing will now halt" << endl;
}
};
// ----------------------------------------------------------------------------------------
int main(int argc, char** argv)
{
// Check if the user entered an argument to this application.
if (argc != 2)
try
{
cout << "Please enter an xml file to parse on the command line" << endl;
return 1;
}
// Check if the user entered an argument to this application.
if (argc != 2)
{
cout << "Please enter an xml file to parse on the command line" << endl;
return 1;
}
// Try to open the file given on the command line
ifstream fin(argv[1]);
if (!fin)
doc_handler dh;
parse_xml(argv[1], dh);
}
catch (std::exception& e)
{
cout << "unable to open file: " << argv[1] << endl;
return 1;
cout << e.what() << endl;
}
// now make the xml parser and our document and error handlers
xml_parser parser;
doc_handler dh;
xml_error_handler eh;
// now associate the handlers with the parser and tell it to parse
parser.add_document_handler(dh);
parser.add_error_handler(eh);
parser.parse(fin);
}