~ubuntu-branches/ubuntu/precise/libxml++2.6/precise

« back to all changes in this revision

Viewing changes to libxml++/validators/validator.cc

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Holbach
  • Date: 2005-03-13 15:46:33 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20050313154633-iubyqdtu6y3p8915
Tags: 2.10.0-0ubuntu2
added doxygen to the build-depends

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* xml++.cc
 
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.
 
6
 */
 
7
 
 
8
#include "libxml++/validators/validator.h"
 
9
 
 
10
#include <libxml/parser.h>
 
11
 
 
12
#include <cstdarg> //For va_list.
 
13
 
 
14
namespace xmlpp {
 
15
 
 
16
Validator::Validator()
 
17
: valid_(0), exception_(0)
 
18
{
 
19
}
 
20
 
 
21
Validator::~Validator()
 
22
{
 
23
  release_underlying();
 
24
}
 
25
 
 
26
void Validator::initialize_valid()
 
27
{
 
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;
 
32
 
 
33
  //Allow the callback_validity_*() methods to retrieve the C++ instance:
 
34
  valid_->userData = this;
 
35
 
 
36
  //Clear these temporary buffers too:
 
37
  validate_error_.erase();
 
38
  validate_warning_.erase();
 
39
}
 
40
 
 
41
void Validator::release_underlying()
 
42
{
 
43
  if(valid_)
 
44
  {
 
45
    valid_->userData = 0; //Not really necessary.
 
46
 
 
47
    xmlFreeValidCtxt(valid_);
 
48
    valid_ = 0;
 
49
  }
 
50
}
 
51
 
 
52
void Validator::on_validity_error(const Glib::ustring& message)
 
53
{
 
54
  //Throw an exception later when the whole message has been received:
 
55
  validate_error_ += message;
 
56
}
 
57
 
 
58
void Validator::on_validity_warning(const Glib::ustring& message)
 
59
{
 
60
  //Throw an exception later when the whole message has been received:
 
61
  validate_warning_ += message;
 
62
}
 
63
 
 
64
void Validator::check_for_validity_messages()
 
65
{
 
66
  if(!validate_error_.empty())
 
67
  {
 
68
    if(!exception_)
 
69
      exception_ = new validity_error("Validity error:\n" + validate_error_);
 
70
 
 
71
    validate_error_.erase();
 
72
  }
 
73
 
 
74
  if(!validate_warning_.empty())
 
75
  {
 
76
    if(!exception_)
 
77
      exception_ = new validity_error("Validity warning:\n" + validate_warning_);
 
78
 
 
79
    validate_warning_.erase();
 
80
  }
 
81
}
 
82
 
 
83
void Validator::callback_validity_error(void* valid_, const char* msg, ...)
 
84
{
 
85
  Validator* validator = static_cast<Validator*>(valid_);
 
86
 
 
87
  if(validator)
 
88
  {
 
89
    //Convert the ... to a string:
 
90
    va_list arg;
 
91
    char buff[1024]; //TODO: Larger/Shared
 
92
 
 
93
    va_start(arg, msg);
 
94
    vsnprintf(buff, sizeof(buff)/sizeof(buff[0]), msg, arg);
 
95
    va_end(arg);
 
96
 
 
97
    try
 
98
    {
 
99
      validator->on_validity_error(Glib::ustring(buff));
 
100
    }
 
101
    catch(const exception& e)
 
102
    {
 
103
      validator->handleException(e);
 
104
    }
 
105
  }
 
106
}
 
107
 
 
108
void Validator::callback_validity_warning(void* valid_, const char* msg, ...)
 
109
{
 
110
  Validator* validator = static_cast<Validator*>(valid_);
 
111
 
 
112
  if(validator)
 
113
  {
 
114
    //Convert the ... to a string:
 
115
    va_list arg;
 
116
    char buff[1024]; //TODO: Larger/Shared
 
117
 
 
118
    va_start(arg, msg);
 
119
    vsnprintf(buff, sizeof(buff)/sizeof(buff[0]), msg, arg);
 
120
    va_end(arg);
 
121
 
 
122
    try
 
123
    {
 
124
      validator->on_validity_warning(Glib::ustring(buff));
 
125
    }
 
126
    catch(const exception& e)
 
127
    {
 
128
      validator->handleException(e);
 
129
    }
 
130
  }
 
131
}
 
132
 
 
133
void Validator::handleException(const exception& e)
 
134
{
 
135
  exception_ = e.Clone();
 
136
 
 
137
  release_underlying();
 
138
}
 
139
 
 
140
void Validator::check_for_exception()
 
141
{
 
142
  check_for_validity_messages();
 
143
 
 
144
  if(exception_)
 
145
  {
 
146
    exception* tmp = exception_;
 
147
    exception_ = 0;
 
148
    tmp->Raise();
 
149
  }
 
150
}
 
151
 
 
152
} // namespace xmlpp
 
153
 
 
154