2
* libxml++ and this file are copyright (C) 2000 by Ari Johnson
3
* (C) 2002-2004 by the libxml dev team and
4
* are covered by the GNU Lesser General Public License, which should be
5
* included with libxml++ as the file COPYING.
8
#include "libxml++/validators/validator.h"
10
#include <libxml/parser.h>
12
#include <cstdarg> //For va_list.
16
Validator::Validator()
17
: valid_(0), exception_(0)
21
Validator::~Validator()
26
void Validator::initialize_valid()
28
//Tell the validity valid about the callbacks:
29
//(These are only called if validation is on - see above)
30
valid_->error = &callback_validity_error;
31
valid_->warning = &callback_validity_warning;
33
//Allow the callback_validity_*() methods to retrieve the C++ instance:
34
valid_->userData = this;
36
//Clear these temporary buffers too:
37
validate_error_.erase();
38
validate_warning_.erase();
41
void Validator::release_underlying()
45
valid_->userData = 0; //Not really necessary.
47
xmlFreeValidCtxt(valid_);
52
void Validator::on_validity_error(const Glib::ustring& message)
54
//Throw an exception later when the whole message has been received:
55
validate_error_ += message;
58
void Validator::on_validity_warning(const Glib::ustring& message)
60
//Throw an exception later when the whole message has been received:
61
validate_warning_ += message;
64
void Validator::check_for_validity_messages()
66
if(!validate_error_.empty())
69
exception_ = new validity_error("Validity error:\n" + validate_error_);
71
validate_error_.erase();
74
if(!validate_warning_.empty())
77
exception_ = new validity_error("Validity warning:\n" + validate_warning_);
79
validate_warning_.erase();
83
void Validator::callback_validity_error(void* valid_, const char* msg, ...)
85
Validator* validator = static_cast<Validator*>(valid_);
89
//Convert the ... to a string:
91
char buff[1024]; //TODO: Larger/Shared
94
vsnprintf(buff, sizeof(buff)/sizeof(buff[0]), msg, arg);
99
validator->on_validity_error(Glib::ustring(buff));
101
catch(const exception& e)
103
validator->handleException(e);
108
void Validator::callback_validity_warning(void* valid_, const char* msg, ...)
110
Validator* validator = static_cast<Validator*>(valid_);
114
//Convert the ... to a string:
116
char buff[1024]; //TODO: Larger/Shared
119
vsnprintf(buff, sizeof(buff)/sizeof(buff[0]), msg, arg);
124
validator->on_validity_warning(Glib::ustring(buff));
126
catch(const exception& e)
128
validator->handleException(e);
133
void Validator::handleException(const exception& e)
135
exception_ = e.Clone();
137
release_underlying();
140
void Validator::check_for_exception()
142
check_for_validity_messages();
146
exception* tmp = exception_;