Remove dynamic exceptions
[anna.git] / source / xml / DTD.cpp
1 // ANNA - Anna is Not Nothingness Anymore                                                         //
2 //                                                                                                //
3 // (c) Copyright 2005-2015 Eduardo Ramos Testillano & Francisco Ruiz Rayo                         //
4 //                                                                                                //
5 // See project site at http://redmine.teslayout.com/projects/anna-suite                           //
6 // See accompanying file LICENSE or copy at http://www.teslayout.com/projects/public/anna.LICENSE //
7
8
9 #include <anna/core/tracing/Logger.hpp>
10 #include <anna/core/tracing/TraceMethod.hpp>
11 #include <anna/config/defines.hpp>
12
13 #include <libxml/parser.h>
14
15 #include <anna/xml/DTD.hpp>
16
17 using namespace std;
18 using namespace anna;
19 using namespace anna::xml;
20
21 DTD::~DTD() {
22   if(a_handle != NULL) {
23     xmlFreeDtd(a_handle);
24     a_handle = NULL;
25   }
26 }
27
28 void DTD::initialize(const char* content)
29 noexcept(false) {
30   LOGMETHOD(TraceMethod tf("anna::xml::DTD", "initialize", ANNA_FILE_LOCATION));
31   Guard guard(a_mutex);
32
33   if(a_handle != NULL) {
34     xmlFreeDtd(a_handle);
35     a_handle = NULL;
36   }
37
38   a_handle = parse(content);
39 }
40
41 void DTD::validate(_xmlValidCtxt* context, _xmlDoc* document) const
42 noexcept(false) {
43   if(a_handle == NULL)
44     throw RuntimeException("'DTD::inicialize' was not called", ANNA_FILE_LOCATION);
45
46   if(context == NULL)
47     throw RuntimeException("Cannot validate with a NULL context", ANNA_FILE_LOCATION);
48
49   if(document == NULL)
50     throw RuntimeException("Cannot validate a NULL XML document", ANNA_FILE_LOCATION);
51
52   if(xmlValidateDtd(context, document, a_handle) == 0) {
53     string msg("XML Document");
54
55     if(document->name) {
56       msg += ": ";
57       msg += document->name;
58     } else if(document->URL) {
59       msg += ": ";
60       msg += (const char*) document->URL;
61     }
62
63     msg += " | Does not comply the DTD especification ";
64     throw RuntimeException(msg, ANNA_FILE_LOCATION);
65   }
66 }
67