Remove dynamic exceptions
[anna.git] / source / xml / Document.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/Document.hpp>
16 #include <anna/xml/Parser.hpp>
17
18 using namespace std;
19 using namespace anna;
20 using namespace anna::xml;
21
22 Document::Document() :
23   anna::DataBlock(true),
24   a_handle(NULL),
25   a_encoding(NULL),
26   a_version(NULL),
27   a_contentIsCString(false),
28   a_asCString(NULL),
29   a_parser(NULL)
30 {;}
31
32 Document::~Document() {
33   if(a_handle != NULL) {
34     xmlFreeDoc(a_handle);
35     a_handle = NULL;
36   }
37
38   delete a_parser;
39   delete a_encoding;
40   delete a_version;
41 }
42
43 void Document::initialize(const char* content)
44 noexcept(false) {
45   LOGMETHOD(TraceMethod tf("anna::xml::Document", "initialize (char*)", ANNA_FILE_LOCATION));
46   clear();
47   a_handle = do_initialize(content);
48   setEncoding((char*) a_handle->encoding);
49   setVersion((char*) a_handle->version);
50 }
51
52 void Document::initialize(const DataBlock& content)
53 noexcept(false) {
54   LOGMETHOD(TraceMethod tf("anna::xml::Document", "initialize (DataBlock)", ANNA_FILE_LOCATION));
55   clear();
56   a_handle = do_initialize(content);
57   setEncoding((char*) a_handle->encoding);
58   setVersion((char*) a_handle->version);
59 }
60
61 const xml::Node* Document::parse()
62 noexcept(false) {
63   if(a_parser == NULL)
64     a_parser = new Parser;
65
66   return a_parser->apply(*this);
67 }
68
69 const xml::Node* Document::parse(const xml::DTD& dtd)
70 noexcept(false) {
71   if(a_parser == NULL)
72     a_parser = new Parser;
73
74   return a_parser->apply(*this, dtd);
75 }
76
77 void Document::clear()
78 {
79   if(a_handle != NULL) {
80     xmlFreeDoc(a_handle);
81     a_handle = NULL;
82   }
83
84   if(a_encoding != NULL)
85     a_encoding->clear();
86
87   if(a_version != NULL)
88     a_version->clear();
89
90   DataBlock::clear();
91   a_contentIsCString = false;
92 }
93
94 const char* Document::getEncoding() const
95 {
96   return (a_encoding == NULL) ? NULL : ((a_encoding->empty() == true) ? NULL : a_encoding->c_str());
97 }
98
99 const char* Document::getVersion() const
100 {
101   return (a_version == NULL) ? NULL : ((a_version->empty() == true) ? NULL : a_version->c_str());
102 }
103
104 const char* Document::getContentAsCString() const
105 noexcept(false) {
106   const char* result = NULL;
107
108   if(a_contentIsCString == false) {
109     Document* _this = const_cast <Document*>(this);
110
111     if(a_asCString == NULL)
112       _this->a_asCString = new DataBlock(true);
113
114     *(_this->a_asCString) = *this;
115     *(_this->a_asCString) += '\0';
116     result = a_asCString->getData();
117   } else
118     result = getData();
119
120   return result;
121 }
122
123 void Document::setEncoding(const char* encoding)
124 {
125   if(a_encoding == NULL) {
126     if(encoding != NULL)
127       a_encoding = new string(encoding);
128   } else {
129     if(encoding != NULL)
130       *a_encoding = encoding;
131     else
132       a_encoding->clear();
133   }
134 }
135
136 void Document::setVersion(const char* version)
137 {
138   if(a_version == NULL) {
139     if(version != NULL)
140       a_version = new string(version);
141   } else {
142     if(version != NULL)
143       *a_version = version;
144     else
145       a_version->clear();
146   }
147 }