1 // ANNA - Anna is Not 'N' Anymore
3 // (c) Copyright 2005-2014 Eduardo Ramos Testillano & Francisco Ruiz Rayo
5 // https://bitbucket.org/testillano/anna
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 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.
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 #ifndef anna_xml_Compiler_hpp
38 #define anna_xml_Compiler_hpp
40 #include <anna/config/defines.hpp>
41 #include <anna/core/RuntimeException.hpp>
42 #include <anna/core/mt/Mutex.hpp>
44 // Three-space tabulator for xml string presentation (node indent)
45 #define ANNA_XML_COMPILER_TAB " "
56 Genera el documento XML a correspondiente a un nodo anna::xml::Node.
58 class Compiler : public Mutex {
61 Modo de generacion del documento XML.
65 Visual = 0, /**< Separa cada uno de los componentes con espacios y retornos de carro */
66 Compact = 1, /**< Los componentes no tienen separacion entre si. Se puede usar para optimizar envios de mensajes */
67 ShowNullAttribute = 2, /**< Visualiza el valor nulo de un atributo como ="" */
68 NoNamespaces = 4 /**<Obtiene el documento XML sin ofrecer informaciĆ³n sobre los namespaces */
78 Obtiene el documento XML correspondiente al nodo recibido.
79 \param node Nodo XML del que queremos obtener el documento XML origen.
80 \param flag Combinacion de valores que configura la forma de crear el documento.
81 \return Una cadena con el documento XML correspondiente al nodo recibido.
83 const char* apply(const Node* node, const int flag = Mode::Visual) throw(RuntimeException);
86 Obtiene el documento XML correspondiente al nodo recibido.
87 \param document Documento del que obtener los ajustes de configuraciĆ³n del documento XML.
88 \param node Nodo XML del que queremos obtener el documento XML origen.
89 \param flag Combinacion de valores que configura la forma de crear el documento.
90 \return Una cadena con el documento XML correspondiente al nodo recibido.
92 const char* apply(const Document& document, const Node* node, const int flag = Mode::Visual) throw(RuntimeException);
98 a_buffer = new char [a_maxSize = 8192];
99 a_buffer [a_size = 0] = 0;
101 ~Result() { delete [] a_buffer; }
103 void clear() throw() { a_buffer [a_size = 0] = 0; }
105 void operator += (const char c)
106 throw(RuntimeException) {
107 extend(sizeof(c) * 2);
108 a_buffer [a_size ++] = c;
109 a_buffer [a_size] = 0;
112 void operator += (const char* str)
113 throw(RuntimeException) {
114 const int len = anna_strlen(str);
116 anna_strcpy(a_buffer + a_size, str);
120 void operator += (const std::string& str) throw(RuntimeException) {(*this) += str.c_str(); }
122 const char* c_str() const throw() { return a_buffer; }
129 void extend(const int size) throw(RuntimeException);
134 Compiler(const Compiler&);
136 static void apply(const Node*, Result&, const int level, const int flags) throw(RuntimeException);
137 static void apply(const Node*, Result&, const int flags) throw(RuntimeException);
138 static void open(const Node*, Result&, const int level, const bool quickClose, const bool newline, const int flags)
139 throw(RuntimeException);
140 static void writeFullName(const Node*, Result&, const int flags) throw(RuntimeException);
141 static void writeFullName(const Attribute*, Result&, const int flags) throw(RuntimeException);
142 static void close(const Node*, Result&, const int level, const int flags) throw(RuntimeException);