Normalize xml processing
[anna.git] / include / anna / xml / Compiler.hpp
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 #ifndef anna_xml_Compiler_hpp
10 #define anna_xml_Compiler_hpp
11
12 #include <anna/config/defines.hpp>
13 #include <anna/core/RuntimeException.hpp>
14 #include <anna/core/mt/Mutex.hpp>
15
16
17 namespace anna {
18
19 namespace xml {
20
21 class Node;
22 class Document;
23 class Attribute;
24
25 /**
26    Genera el documento XML a correspondiente a un nodo anna::xml::Node.
27 */
28 class Compiler : public Mutex {
29 public:
30   /**
31      Modo de generacion del documento XML.
32   */
33   struct Mode {
34     enum _v {
35       Visual = 0, /**< Separa cada uno de los componentes con espacios y retornos de carro */
36       Compact = 1, /**< Los componentes no tienen separacion entre si. Se puede usar para optimizar envios de mensajes */
37       ShowNullAttribute = 2, /**< Visualiza el valor nulo de un atributo como ="" */
38       NoNamespaces = 4, /**<Obtiene el documento XML sin ofrecer informaciĆ³n sobre los namespaces */
39       Sort = 8, /** Sorts attribute names */
40     };
41   };
42
43   /**
44      Constructor.
45   */
46   Compiler();
47
48   /**
49      Obtiene el documento XML correspondiente al nodo recibido.
50      \param node Nodo XML del que queremos obtener el documento XML origen.
51      \param flag Combinacion de valores que configura la forma de crear el documento.
52      \return Una cadena con el documento XML correspondiente al nodo recibido.
53   */
54   const char* apply(const Node* node, const int flag = Mode::Visual) throw(RuntimeException);
55
56   /**
57      Obtiene el documento XML correspondiente al nodo recibido.
58      \param document Documento del que obtener los ajustes de configuraciĆ³n del documento XML.
59      \param node Nodo XML del que queremos obtener el documento XML origen.
60      \param flag Combinacion de valores que configura la forma de crear el documento.
61      \return Una cadena con el documento XML correspondiente al nodo recibido.
62   */
63   const char* apply(const Document& document, const Node* node, const int flag = Mode::Visual) throw(RuntimeException);
64
65 private:
66   class Result {
67   public:
68     Result() {
69       a_buffer = new char [a_maxSize = 8192];
70       a_buffer [a_size = 0] = 0;
71     }
72     ~Result() { delete [] a_buffer; }
73
74     void clear() throw() { a_buffer [a_size = 0] = 0; }
75
76     void operator += (const char c)
77     throw(RuntimeException) {
78       extend(sizeof(c) * 2);
79       a_buffer [a_size ++] = c;
80       a_buffer [a_size] = 0;
81     }
82
83     void operator += (const char* str)
84     throw(RuntimeException) {
85       const int len = anna_strlen(str);
86       extend(len + 1);
87       anna_strcpy(a_buffer + a_size, str);
88       a_size += len;
89     }
90
91     void operator += (const std::string& str) throw(RuntimeException) {(*this) += str.c_str(); }
92
93     const char* c_str() const throw() { return a_buffer; }
94
95   private:
96     char* a_buffer;
97     int a_size;
98     int a_maxSize;
99
100     void extend(const int size) throw(RuntimeException);
101   };
102
103   Result a_result;
104
105   Compiler(const Compiler&);
106
107   static void apply(const Node*, Result&, const int level, const int flags) throw(RuntimeException);
108   static void apply(const Node*, Result&, const int flags) throw(RuntimeException);
109   static void open(const Node*, Result&, const int level, const bool quickClose, const bool newline, const int flags)
110   throw(RuntimeException);
111   static void writeFullName(const Node*, Result&, const int flags) throw(RuntimeException);
112   static void writeFullName(const Attribute*, Result&, const int flags) throw(RuntimeException);
113   static void close(const Node*, Result&, const int level, const int flags) throw(RuntimeException);
114 };
115
116 }
117 }
118
119 #endif