First commit
[anna.git] / source / xml / Document.cpp
1 // ANNA - Anna is Not 'N' Anymore
2 //
3 // (c) Copyright 2005-2014 Eduardo Ramos Testillano & Francisco Ruiz Rayo
4 //
5 // https://bitbucket.org/testillano/anna
6 //
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions
9 // are met:
10 //
11 //     * Redistributions of source code must retain the above copyright
12 // notice, this list of conditions and the following disclaimer.
13 //     * Redistributions in binary form must reproduce the above
14 // copyright notice, this list of conditions and the following disclaimer
15 // in the documentation and/or other materials provided with the
16 // distribution.
17 //     * Neither the name of Google Inc. nor the names of its
18 // contributors may be used to endorse or promote products derived from
19 // this software without specific prior written permission.
20 //
21 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 //
33 // Authors: eduardo.ramos.testillano@gmail.com
34 //          cisco.tierra@gmail.com
35
36
37 #include <anna/core/tracing/Logger.hpp>
38 #include <anna/core/tracing/TraceMethod.hpp>
39 #include <anna/config/defines.hpp>
40
41 #include <libxml/parser.h>
42
43 #include <anna/xml/Document.hpp>
44 #include <anna/xml/Parser.hpp>
45
46 using namespace std;
47 using namespace anna;
48 using namespace anna::xml;
49
50 Document::Document() :
51   anna::DataBlock(true),
52   a_handle(NULL),
53   a_encoding(NULL),
54   a_version(NULL),
55   a_contentIsCString(false),
56   a_asCString(NULL),
57   a_parser(NULL)
58 {;}
59
60 Document::~Document() {
61   if(a_handle != NULL) {
62     xmlFreeDoc(a_handle);
63     a_handle = NULL;
64   }
65
66   delete a_parser;
67   delete a_encoding;
68   delete a_version;
69 }
70
71 void Document::initialize(const char* content)
72 throw(RuntimeException) {
73   LOGMETHOD(TraceMethod tf("anna::xml::Document", "initialize (char*)", ANNA_FILE_LOCATION));
74   clear();
75   a_handle = do_initialize(content);
76   setEncoding((char*) a_handle->encoding);
77   setVersion((char*) a_handle->version);
78 }
79
80 void Document::initialize(const DataBlock& content)
81 throw(RuntimeException) {
82   LOGMETHOD(TraceMethod tf("anna::xml::Document", "initialize (DataBlock)", ANNA_FILE_LOCATION));
83   clear();
84   a_handle = do_initialize(content);
85   setEncoding((char*) a_handle->encoding);
86   setVersion((char*) a_handle->version);
87 }
88
89 const xml::Node* Document::parse()
90 throw(RuntimeException) {
91   if(a_parser == NULL)
92     a_parser = new Parser;
93
94   return a_parser->apply(*this);
95 }
96
97 const xml::Node* Document::parse(const xml::DTD& dtd)
98 throw(RuntimeException) {
99   if(a_parser == NULL)
100     a_parser = new Parser;
101
102   return a_parser->apply(*this, dtd);
103 }
104
105 void Document::clear()
106 throw() {
107   if(a_handle != NULL) {
108     xmlFreeDoc(a_handle);
109     a_handle = NULL;
110   }
111
112   if(a_encoding != NULL)
113     a_encoding->clear();
114
115   if(a_version != NULL)
116     a_version->clear();
117
118   DataBlock::clear();
119   a_contentIsCString = false;
120 }
121
122 const char* Document::getEncoding() const
123 throw() {
124   return (a_encoding == NULL) ? NULL : ((a_encoding->empty() == true) ? NULL : a_encoding->c_str());
125 }
126
127 const char* Document::getVersion() const
128 throw() {
129   return (a_version == NULL) ? NULL : ((a_version->empty() == true) ? NULL : a_version->c_str());
130 }
131
132 const char* Document::getContentAsCString() const
133 throw(RuntimeException) {
134   const char* result = NULL;
135
136   if(a_contentIsCString == false) {
137     Document* _this = const_cast <Document*>(this);
138
139     if(a_asCString == NULL)
140       _this->a_asCString = new DataBlock(true);
141
142     *(_this->a_asCString) = *this;
143     *(_this->a_asCString) += '\0';
144     result = a_asCString->getData();
145   } else
146     result = getData();
147
148   return result;
149 }
150
151 void Document::setEncoding(const char* encoding)
152 throw() {
153   if(a_encoding == NULL) {
154     if(encoding != NULL)
155       a_encoding = new string(encoding);
156   } else {
157     if(encoding != NULL)
158       *a_encoding = encoding;
159     else
160       a_encoding->clear();
161   }
162 }
163
164 void Document::setVersion(const char* version)
165 throw() {
166   if(a_version == NULL) {
167     if(version != NULL)
168       a_version = new string(version);
169   } else {
170     if(version != NULL)
171       *a_version = version;
172     else
173       a_version->clear();
174   }
175 }