1 // ANNA - Anna is Not Nothingness Anymore
3 // (c) Copyright 2005-2014 Eduardo Ramos Testillano & Francisco Ruiz Rayo
5 // http://redmine.teslayout.com/projects/anna-suite
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions
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
17 // * Neither the name of the copyright holder 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.
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.
33 // Authors: eduardo.ramos.testillano@gmail.com
34 // cisco.tierra@gmail.com
37 #include <anna/core/tracing/Logger.hpp>
38 #include <anna/core/tracing/TraceMethod.hpp>
39 #include <anna/config/defines.hpp>
41 #include <libxml/parser.h>
43 #include <anna/xml/Document.hpp>
44 #include <anna/xml/Parser.hpp>
48 using namespace anna::xml;
50 Document::Document() :
51 anna::DataBlock(true),
55 a_contentIsCString(false),
60 Document::~Document() {
61 if(a_handle != NULL) {
71 void Document::initialize(const char* content)
72 throw(RuntimeException) {
73 LOGMETHOD(TraceMethod tf("anna::xml::Document", "initialize (char*)", ANNA_FILE_LOCATION));
75 a_handle = do_initialize(content);
76 setEncoding((char*) a_handle->encoding);
77 setVersion((char*) a_handle->version);
80 void Document::initialize(const DataBlock& content)
81 throw(RuntimeException) {
82 LOGMETHOD(TraceMethod tf("anna::xml::Document", "initialize (DataBlock)", ANNA_FILE_LOCATION));
84 a_handle = do_initialize(content);
85 setEncoding((char*) a_handle->encoding);
86 setVersion((char*) a_handle->version);
89 const xml::Node* Document::parse()
90 throw(RuntimeException) {
92 a_parser = new Parser;
94 return a_parser->apply(*this);
97 const xml::Node* Document::parse(const xml::DTD& dtd)
98 throw(RuntimeException) {
100 a_parser = new Parser;
102 return a_parser->apply(*this, dtd);
105 void Document::clear()
107 if(a_handle != NULL) {
108 xmlFreeDoc(a_handle);
112 if(a_encoding != NULL)
115 if(a_version != NULL)
119 a_contentIsCString = false;
122 const char* Document::getEncoding() const
124 return (a_encoding == NULL) ? NULL : ((a_encoding->empty() == true) ? NULL : a_encoding->c_str());
127 const char* Document::getVersion() const
129 return (a_version == NULL) ? NULL : ((a_version->empty() == true) ? NULL : a_version->c_str());
132 const char* Document::getContentAsCString() const
133 throw(RuntimeException) {
134 const char* result = NULL;
136 if(a_contentIsCString == false) {
137 Document* _this = const_cast <Document*>(this);
139 if(a_asCString == NULL)
140 _this->a_asCString = new DataBlock(true);
142 *(_this->a_asCString) = *this;
143 *(_this->a_asCString) += '\0';
144 result = a_asCString->getData();
151 void Document::setEncoding(const char* encoding)
153 if(a_encoding == NULL) {
155 a_encoding = new string(encoding);
158 *a_encoding = encoding;
164 void Document::setVersion(const char* version)
166 if(a_version == NULL) {
168 a_version = new string(version);
171 *a_version = version;