Updated license
[anna.git] / include / anna / xml / Compiler.hpp
1 // ANNA - Anna is Not Nothingness 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 #ifndef anna_xml_Compiler_hpp
38 #define anna_xml_Compiler_hpp
39
40 #include <anna/config/defines.hpp>
41 #include <anna/core/RuntimeException.hpp>
42 #include <anna/core/mt/Mutex.hpp>
43
44 // Three-space tabulator for xml string presentation (node indent)
45 #define ANNA_XML_COMPILER_TAB  "   "
46
47 namespace anna {
48
49 namespace xml {
50
51 class Node;
52 class Document;
53 class Attribute;
54
55 /**
56    Genera el documento XML a correspondiente a un nodo anna::xml::Node.
57 */
58 class Compiler : public Mutex {
59 public:
60   /**
61      Modo de generacion del documento XML.
62   */
63   struct Mode {
64     enum _v {
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 */
69     };
70   };
71
72   /**
73      Constructor.
74   */
75   Compiler();
76
77   /**
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.
82   */
83   const char* apply(const Node* node, const int flag = Mode::Visual) throw(RuntimeException);
84
85   /**
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.
91   */
92   const char* apply(const Document& document, const Node* node, const int flag = Mode::Visual) throw(RuntimeException);
93
94 private:
95   class Result {
96   public:
97     Result() {
98       a_buffer = new char [a_maxSize = 8192];
99       a_buffer [a_size = 0] = 0;
100     }
101     ~Result() { delete [] a_buffer; }
102
103     void clear() throw() { a_buffer [a_size = 0] = 0; }
104
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;
110     }
111
112     void operator += (const char* str)
113     throw(RuntimeException) {
114       const int len = anna_strlen(str);
115       extend(len + 1);
116       anna_strcpy(a_buffer + a_size, str);
117       a_size += len;
118     }
119
120     void operator += (const std::string& str) throw(RuntimeException) {(*this) += str.c_str(); }
121
122     const char* c_str() const throw() { return a_buffer; }
123
124   private:
125     char* a_buffer;
126     int a_size;
127     int a_maxSize;
128
129     void extend(const int size) throw(RuntimeException);
130   };
131
132   Result a_result;
133
134   Compiler(const Compiler&);
135
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);
143 };
144
145 }
146 }
147
148 #endif