Changed LICENSE. Now referenced to web site and file on project root directory
[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() {
28   a_filename = "/tmp/anna.xml.";
29   a_filename += functions::asString((int) getpid());
30   a_filename += ".dtd";
31 }
32
33 //---------------------------------------------------------------------------------------------
34 // Aunque la libXML ofrece funciones para analizar una DTD a partir de un buffer de memoria
35 // hemos sido incapaces de hacerla funcionar correctamente. Así que nos vemos obligados
36 // a volcar el buffer en un archivo .. y una vez allí analizarlo con la función que analiza
37 // la DTD a partir de un archivo.
38 //---------------------------------------------------------------------------------------------
39 _xmlDtd* DTDMemory::parse(const char* content) const
40 throw(RuntimeException) {
41   LOGMETHOD(TraceMethod tf("anna::xml::DTDMemory", "parse", ANNA_FILE_LOCATION));
42   _xmlDtd* result;
43   int stream;
44   LOGDEBUG(
45     Logger::write(Logger::Debug, "DTD", content, ANNA_FILE_LOCATION);
46     Logger::write(Logger::Debug, "Temporary file", a_filename.c_str(), ANNA_FILE_LOCATION)
47   );
48
49   if((stream = open(a_filename.c_str(), O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR)) == -1)
50     throw RuntimeException(a_filename, errno, ANNA_FILE_LOCATION);
51
52   if(write(stream, content, anna_strlen(content)) == -1) {
53     const int xerrno = errno;
54     close(stream);
55     unlink(a_filename.c_str());
56     throw RuntimeException(a_filename, xerrno, ANNA_FILE_LOCATION);
57   }
58
59   close(stream);
60
61   if((result = xmlParseDTD(NULL, BAD_CAST(a_filename.c_str()))) == NULL) {
62     unlink(a_filename.c_str());
63     throw RuntimeException("DTD is wrong", ANNA_FILE_LOCATION);
64   }
65
66   unlink(a_filename.c_str());
67   return result;
68 }
69