Remove dynamic exceptions
[anna.git] / source / xml / DTD.cc.new
1 #include <anna/core/tracing/Logger.hpp>
2 #include <anna/core/tracing/TraceMethod.hpp>
3 #include <anna/config/defines.hpp>
4
5 #include <libxml/parser.h>
6
7 #include <anna/xml/DTD.hpp>
8
9 using namespace std;
10 using namespace anna;
11 using namespace anna::xml;
12
13 DTD::~DTD ()
14 {
15    if (a_handle != NULL) {
16       xmlFreeDtd (a_handle);
17       a_handle = NULL;
18    }
19 }
20
21 void DTD::initialize (const char* content)
22    noexcept(false)
23 {
24    LOGMETHOD (TraceMethod tf ("anna::xml::DTD", "initialize", ANNA_FILE_LOCATION));
25
26    Guard guard (a_mutex);
27
28    if (a_handle != NULL) {
29       xmlFreeDtd (a_handle);
30       a_handle = NULL;
31    }
32       
33    a_handle = parse (content);
34 }
35
36 void DTD::validate (_xmlValidCtxt* context, _xmlDoc* document) const 
37    noexcept(false)
38 {
39    if (a_handle == NULL)
40       throw RuntimeException ("'DTD::inicialize' was not called", ANNA_FILE_LOCATION);
41
42    if (context == NULL)
43       throw RuntimeException ("Cannot validate with a NULL context", ANNA_FILE_LOCATION);
44
45    if (document == NULL)
46       throw RuntimeException ("Cannot validate a NULL XML document", ANNA_FILE_LOCATION);
47
48    if (xmlValidateDtd (context, document, a_handle) == 0) {
49       string msg ("XML Document");
50       
51       if (document->name) {
52          msg += ": ";
53          msg += document->name;
54       }
55       else if (document->URL) {
56          msg += ": ";
57          msg += (const char*) document->URL;
58       }
59          
60       msg += " | Does not comply the DTD especification ";
61       throw RuntimeException (msg, ANNA_FILE_LOCATION);
62    }
63 }
64