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.
13 #include <memory> //For auto_ptr.
17 Validator::Validator()
18 : valid_(0), exception_(0)
22 Validator::~Validator()
27 void Validator::initialize_valid()
29 //Tell the validity valid about the callbacks:
30 //(These are only called if validation is on - see above)
31 valid_->error = &callback_validity_error;
32 valid_->warning = &callback_validity_warning;
34 //Allow the callback_validity_*() methods to retrieve the C++ instance:
35 valid_->userData = this;
37 //Clear these temporary buffers too:
38 validate_error_.erase();
39 validate_warning_.erase();
42 void Validator::release_underlying()
46 valid_->userData = 0; //Not really necessary.
48 xmlFreeValidCtxt(valid_);
53 void Validator::on_validity_error(const Glib::ustring& message)
55 //Throw an exception later when the whole message has been received:
56 validate_error_ += message;
59 void Validator::on_validity_warning(const Glib::ustring& message)
61 //Throw an exception later when the whole message has been received:
62 validate_warning_ += message;
65 void Validator::check_for_validity_messages()
67 if(!validate_error_.empty())
70 exception_ = new validity_error("Validity error:\n" + validate_error_);
72 validate_error_.erase();
75 if(!validate_warning_.empty())
78 exception_ = new validity_error("Validity warning:\n" + validate_warning_);
80 validate_warning_.erase();
84 void Validator::callback_validity_error(void* valid_, const char* msg, ...)
86 Validator* validator = static_cast<Validator*>(valid_);
90 //Convert the ... to a string:
92 char buff[1024]; //TODO: Larger/Shared
95 vsnprintf(buff, sizeof(buff)/sizeof(buff[0]), msg, arg);
100 validator->on_validity_error(Glib::ustring(buff));
102 catch(const exception& e)
104 validator->handleException(e);
109 void Validator::callback_validity_warning(void* valid_, const char* msg, ...)
111 Validator* validator = static_cast<Validator*>(valid_);
115 //Convert the ... to a string:
117 char buff[1024]; //TODO: Larger/Shared
120 vsnprintf(buff, sizeof(buff)/sizeof(buff[0]), msg, arg);
125 validator->on_validity_warning(Glib::ustring(buff));
127 catch(const exception& e)
129 validator->handleException(e);
134 void Validator::handleException(const exception& e)
136 exception_ = e.Clone();
138 release_underlying();
141 void Validator::check_for_exception()
143 check_for_validity_messages();
147 std::auto_ptr<exception> tmp(exception_);