Updated license
[anna.git] / source / xml / Compiler.cpp
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 #include <anna/core/mt/Guard.hpp>
38
39 #include <anna/xml/Compiler.hpp>
40 #include <anna/xml/Node.hpp>
41 #include <anna/xml/Attribute.hpp>
42 #include <anna/xml/Text.hpp>
43 #include <anna/xml/internal/sccs.hpp>
44 #include <anna/xml/Document.hpp>
45 #include <anna/xml/Namespace.hpp>
46
47 using namespace anna;
48 using namespace anna::xml;
49
50 Compiler::Compiler() {
51   sccs::activate();
52 }
53
54 const char* Compiler::apply(const Node* node, const int flags)
55 throw(RuntimeException) {
56   Guard guard(this, "xml::Compiler::apply");
57   a_result.clear();
58
59   if(flags & Mode::Compact)
60     apply(node, a_result, flags);
61   else
62     apply(node, a_result, 0, flags);
63
64   return a_result.c_str();
65 }
66
67 const char* Compiler::apply(const Document& document, const Node* node, const int flags)
68 throw(RuntimeException) {
69   Guard guard(this, "xml::Compiler::apply");
70   a_result.clear();
71   const char* version = document.getVersion();
72   a_result += "<?xml";
73
74   if(version != NULL) {
75     a_result += " version='";
76     a_result += version;
77     a_result += "'";
78   }
79
80   const char* encoding = document.getEncoding();
81
82   if(encoding != NULL) {
83     a_result += " encoding='";
84     a_result += encoding;
85     a_result += "'";
86   }
87
88   a_result += "?>\n";
89
90   if(flags & Mode::Compact)
91     apply(node, a_result, flags);
92   else
93     apply(node, a_result, 0, flags);
94
95   return a_result.c_str();
96 }
97
98 //------------------------------------------------------------------------------------
99 // (1) Si tiene nodos hijos => ignora el texto, porque no se como representar
100 // nodos + texto.
101 //------------------------------------------------------------------------------------
102 void Compiler::apply(const Node* node, Result& result, const int level, const int flags)
103 throw(RuntimeException) {
104   const bool hasText(node->getText() != NULL);
105   Node::const_child_iterator ii = node->child_begin();
106   Node::const_child_iterator maxii = node->child_end();
107   const bool hasChildren(ii != maxii);
108
109   if(hasChildren == false && hasText == false)
110     open(node, result, level, true, true, flags);
111   else if(hasChildren == false && hasText == true) {
112     open(node, result, level, false, false, flags);
113     result += node->getText()->getValue();
114     close(node, result, 0, flags);
115   } else if(hasChildren == true) {                    // (1)
116     open(node, result, level, false, true, flags);
117
118     for(; ii != maxii; ii ++)
119       apply(Node::node(ii), result, level + 1, flags);
120
121     close(node, result, level, flags);
122   }
123 }
124
125 //------------------------------------------------------------------------------------
126 // (1) Si tiene nodos hijos => ignora el texto, porque no se como representar
127 // nodos + texto.
128 //------------------------------------------------------------------------------------
129 void Compiler::apply(const Node* node, Result& result, const int flags)
130 throw(RuntimeException) {
131   static const int level = 0;
132   const bool hasText(node->getText() != NULL);
133   Node::const_child_iterator ii = node->child_begin();
134   Node::const_child_iterator maxii = node->child_end();
135   const bool hasChildren(ii != maxii);
136
137   if(hasChildren == false && hasText == false)
138     open(node, result, level, true, false, flags);
139   else if(hasChildren == false && hasText == true) {
140     open(node, result, level, false, false, flags);
141     result += node->getText()->getValue();
142     result += "</";
143     writeFullName(node, result, flags);
144     result += ">";
145   } else if(hasChildren == true) {                    // (1)
146     open(node, result, level, false, false, flags);
147
148     for(; ii != maxii; ii ++)
149       apply(Node::node(ii), result, flags);
150
151     result += "</";
152     writeFullName(node, result, flags);
153     result += ">";
154   }
155 }
156
157 /*static*/
158 void Compiler::open(const Node* node, Result& result, const int level, const bool quickClose, const bool newline, const int flags)
159 throw(RuntimeException) {
160   const Attribute* attribute;
161   const Namespace* ns;
162
163   for(int i = 0; i < level; i ++)
164     result += ANNA_XML_COMPILER_TAB;
165
166   result += '<';
167   writeFullName(node, result, flags);
168
169   // Si es el nodo ROOT => Tenemos que representar la deficiĆ³n de los namespaces
170   if(node->getParent() == NULL && node->namespace_size() != 0) {
171     for(xml::Node::const_namespace_iterator ii = node->namespace_begin(), maxii = node->namespace_end(); ii != maxii; ii ++) {
172       ns = xml::Node::xnamespace(ii);
173       result += " xmlns:";
174       result += ns->getName();
175       result += "=\"";
176       result += ns->getReference();
177       result += "\"";
178     }
179   }
180
181   for(Node::const_attribute_iterator ii = node->attribute_begin(), maxii = node->attribute_end(); ii != maxii; ii ++) {
182     attribute = Node::attribute(ii);
183     result += ' ';
184     writeFullName(attribute, result, flags);
185     const std::string& value = attribute->getValue();
186
187     if(value.length() > 0) {
188       result += "=\"";
189       result += attribute->getValue();
190       result += "\"";
191     } else if(flags & Mode::ShowNullAttribute)
192       result += "=\"\"";
193   }
194
195   if(quickClose == true)
196     result += '/';
197
198   result += '>';
199
200   if(newline == true)
201     result += '\n';
202 }
203
204 /*static*/
205 void Compiler::writeFullName(const Node* node, Result& result, const int flags)
206 throw(RuntimeException) {
207   const Namespace* ns;
208
209   if((flags & Mode::NoNamespaces) == 0 && (ns = node->getNamespace()) != NULL) {
210     result += ns->getName();
211     result += ':';
212   }
213
214   result += node->getName();
215 }
216
217 /*static*/
218 void Compiler::writeFullName(const Attribute* attr, Result& result, const int flags)
219 throw(RuntimeException) {
220   const Namespace* ns;
221
222   if((flags & Mode::NoNamespaces) == 0 && (ns = attr->getNamespace()) != NULL) {
223     result += ns->getName();
224     result += ':';
225   }
226
227   result += attr->getName();
228 }
229
230 /*static*/
231 void Compiler::close(const Node* node, Result& result, const int level, const int flags)
232 throw(RuntimeException) {
233   for(int i = 0; i < level; i ++)
234     result += ANNA_XML_COMPILER_TAB;
235
236   result += "</";
237   writeFullName(node, result, flags);
238   result += ">\n";
239 }
240
241 void Compiler::Result::extend(const int nbytes)
242 throw(RuntimeException) {
243   if((a_size + nbytes) >= a_maxSize) {
244     int newSize = ((a_size + nbytes) << 1) - ((a_size + nbytes)  >> 1);
245     char* newBuffer = new char [newSize];
246
247     if(newBuffer == NULL)
248       throw RuntimeException("Insufficient memory", ANNA_FILE_LOCATION);
249
250     if(a_size > 0)
251       anna_memcpy(newBuffer, a_buffer, a_size);
252
253     delete [] a_buffer;
254     a_buffer = newBuffer;
255     a_maxSize = newSize;
256   }
257 }
258