Remove dynamic exceptions
[anna.git] / source / xml / DTDMemory.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 <fcntl.h>
10 #include <unistd.h>
11 #include <sys/types.h>
12 #include <sys/stat.h>
13
14 #include <anna/core/tracing/Logger.hpp>
15 #include <anna/core/tracing/TraceMethod.hpp>
16 #include <anna/config/defines.hpp>
17 #include <anna/core/functions.hpp>
18
19 #include <libxml/parser.h>
20
21 #include <anna/xml/DTDMemory.hpp>
22
23 using namespace std;
24 using namespace anna;
25 using namespace anna::xml;
26
27 DTDMemory::DTDMemory(const char *dtd) {
28   a_filename = "/tmp/anna.xml.";
29   a_filename += functions::asString((int) getpid());
30   a_filename += ".dtd";
31   if (dtd) initialize(dtd);
32 }
33
34 //---------------------------------------------------------------------------------------------
35 // Aunque la libXML ofrece funciones para analizar una DTD a partir de un buffer de memoria
36 // hemos sido incapaces de hacerla funcionar correctamente. As� que nos vemos obligados
37 // a volcar el buffer en un archivo .. y una vez all� analizarlo con la funci�n que analiza
38 // la DTD a partir de un archivo.
39 //---------------------------------------------------------------------------------------------
40 _xmlDtd* DTDMemory::parse(const char* content) const
41 noexcept(false) {
42   LOGMETHOD(TraceMethod tf("anna::xml::DTDMemory", "parse", ANNA_FILE_LOCATION));
43   _xmlDtd* result;
44   int stream;
45   LOGDEBUG(
46     Logger::write(Logger::Debug, "DTD", content, ANNA_FILE_LOCATION);
47     Logger::write(Logger::Debug, "Temporary file", a_filename.c_str(), ANNA_FILE_LOCATION)
48   );
49
50   if((stream = open(a_filename.c_str(), O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR)) == -1)
51     throw RuntimeException(a_filename, errno, ANNA_FILE_LOCATION);
52
53   if(write(stream, content, anna_strlen(content)) == -1) {
54     const int xerrno = errno;
55     close(stream);
56     unlink(a_filename.c_str());
57     throw RuntimeException(a_filename, xerrno, ANNA_FILE_LOCATION);
58   }
59
60   close(stream);
61
62   if((result = xmlParseDTD(NULL, BAD_CAST(a_filename.c_str()))) == NULL) {
63     unlink(a_filename.c_str());
64     throw RuntimeException("DTD is wrong", ANNA_FILE_LOCATION);
65   }
66
67   unlink(a_filename.c_str());
68   return result;
69 }
70