Ensures normalization on waitfe/fc-xml operations
[anna.git] / source / xml / DocumentFile.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 <sys/types.h>
10 #include <sys/stat.h>
11 #include <fcntl.h>
12 #include <unistd.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 <anna/io/functions.hpp>
20
21 #include <libxml/HTMLparser.h>
22
23 #include <anna/xml/DocumentFile.hpp>
24
25 using namespace std;
26 using namespace anna;
27 using namespace anna::xml;
28
29 _xmlDoc* DocumentFile::do_initialize(const char* filename)
30 throw(RuntimeException) {
31   LOGMETHOD(TraceMethod tf("anna::xml::DocumentFile", "do_initialize", ANNA_FILE_LOCATION));
32   _xmlDoc* result;
33   a_filename = filename;
34   LOGDEBUG(
35     string msg("xml::DocumentFile::do_initialize | Filename: ");
36     msg += filename;
37     Logger::debug(msg, ANNA_FILE_LOCATION);
38   );
39
40   if(io::functions::exists(filename) == false) {
41     string msg("xml::DocumentFile::do_initialize | File: ");
42     msg += filename;
43     msg += " | Not found";
44     throw RuntimeException(msg, ANNA_FILE_LOCATION);
45   }
46
47   result = xmlParseFile(filename);
48
49   if(result == NULL)
50     throw RuntimeException(functions::asString("Error analyzing XML document: %s", filename), ANNA_FILE_LOCATION);
51
52   return result;
53 }
54
55 _xmlDoc* DocumentFile::do_initialize(const anna::DataBlock&)
56 throw(RuntimeException) {
57   throw RuntimeException("xml::DocumentFile::do_initialize | Not implemented", ANNA_FILE_LOCATION);
58   return NULL;
59 }
60
61
62 const anna::DataBlock& DocumentFile::getContent() const
63 throw(RuntimeException) {
64   const anna::DataBlock& result = Document::getContent();
65
66   if(result.isEmpty() == false)
67     return result;
68
69   int stream;
70
71   if((stream = open(a_filename.c_str(), O_RDONLY)) == -1)
72     throw RuntimeException(a_filename, errno, ANNA_FILE_LOCATION);
73
74   const int size = lseek(stream, 0, SEEK_END);
75   lseek(stream, 0, SEEK_SET);
76   char* buffer = new char [size];
77   ssize_t r = read(stream, buffer, size);
78   close(stream);
79   const_cast <DocumentFile*>(this)->setContent(buffer, size);
80   delete buffer;
81   return result;
82 }
83
84