ANNA Suite  2020b
Multipurpose development suite for Telco applications
Node.hpp
Go to the documentation of this file.
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_Node_hpp
10 #define anna_xml_Node_hpp
11 
12 #include <vector>
13 
14 #include <anna/core/Allocator.hpp>
16 #include <anna/core/functions.hpp>
19 
20 #include <anna/xml/Namespace.hpp>
21 
22 namespace anna {
23 
24 namespace xml {
25 
26 class Parser;
27 class Attribute;
28 class Text;
29 class XPath;
30 class Decompressor;
31 
56 class Node {
57  struct NamespaceByName {
58  static const std::string& value(const Namespace* ns) { return ns->getName(); }
59  };
60 
61 public:
62  typedef std::vector <Node*> Children;
63  typedef Children::iterator child_iterator;
64  typedef Children::const_iterator const_child_iterator;
66  typedef std::vector <Attribute*> Attributes;
67  typedef Attributes::iterator attribute_iterator;
68  typedef Attributes::const_iterator const_attribute_iterator;
73 
78  Node(const char* name);
79 
83  virtual ~Node();
84 
89  const char* getName() const { return a_name.c_str(); }
90 
95  const std::string& getNameAsString() const { return a_name; }
96 
101  const Node* getParent() const { return a_parent; }
102 
107  Node* getParent() { return const_cast <Node*>(a_parent); }
108 
117  const Attribute* getAttribute(const char* name, const bool exceptionWhenNotFound = true) const noexcept(false);
118 
123  const Text* getText() const { return a_text; }
124 
129  const Namespace* getNamespace() const { return a_namespace; }
130 
154  child_iterator child_begin() { return a_children.begin(); }
155 
179  const_child_iterator child_begin() const { return a_children.begin(); }
180 
185  const_attribute_iterator attribute_begin() const { return a_attributes.begin(); }
186 
191  const_namespace_iterator namespace_begin() const { return a_root->a_namespaces->begin(); }
192 
193 
199  child_iterator child_end() { return a_children.end(); }
200 
206  const_child_iterator child_end() const { return a_children.end(); }
207 
212  int child_size() const { return a_children.size(); }
213 
218  const_attribute_iterator attribute_end() const { return a_attributes.end(); }
219 
224  const_namespace_iterator namespace_end() const { return a_root->a_namespaces->end(); }
225 
230  int namespace_size() const { return a_root->a_namespaces->size(); }
231 
243  const Node* find(const char* childName, const bool exceptionWhenNotFound = true) const
244  noexcept(false);
245 
253  xml::Attribute* createAttribute(const char* name, const char* value, const Namespace* _namespace = NULL) ;
254 
262  xml::Attribute* createAttribute(const char* name, const std::string& value, const Namespace* _namespace = NULL) {
263  return createAttribute(name, value.c_str(), _namespace);
264  }
265 
273  xml::Attribute* createAttribute(const char* name, const int value, const Namespace* _namespace = NULL) {
274  return createAttribute(name, anna::functions::asString(value), _namespace);
275  }
276 
285  xml::Attribute* createAttribute(const char* name, const S64* value, const Namespace* _namespace = NULL) {
286  return createAttribute(name, anna::functions::asString(*value), _namespace);
287  }
288 
297  xml::Attribute* createAttribute(const char* name, const U64* value, const Namespace* _namespace = NULL) {
298  return createAttribute(name, anna::functions::asString(*value), _namespace);
299  }
300 
306  xml::Text* createText(const char* text) noexcept(false);
307 
313  xml::Text* createText(const std::string& text) noexcept(false) { return createText(text.c_str()); }
314 
319  Node* createChild(const char* name) ;
320 
329  const Namespace* createNamespace(const char* name, const char* reference) noexcept(false) {
330  std::string _name(name);
331  return createNamespace(_name, reference);
332  }
333 
342  const Namespace* createNamespace(const std::string& name, const char* reference) noexcept(false);
343 
348  void setNamespace(const Namespace* _namespace) { a_namespace = _namespace; }
349 
354  Namespace* namespace_find(const char* name, const bool exceptionWhenNotFound = true) noexcept(false) {
355  const std::string _name(name);
356  return namespace_find(_name, exceptionWhenNotFound);
357  }
358 
363  Namespace* namespace_find(const std::string& name, const bool exceptionWhenNotFound = true) noexcept(false);
364 
369  const Namespace* namespace_find(const char* name, const bool exceptionWhenNotFound = true) const noexcept(false) {
370  return const_cast <Node*>(this)->namespace_find(name, exceptionWhenNotFound);
371  }
372 
377  const Namespace* namespace_find(const std::string& name, const bool exceptionWhenNotFound = true) const noexcept(false) {
378  return const_cast <Node*>(this)->namespace_find(name, exceptionWhenNotFound);
379  }
380 
384  void clear() ;
385 
390  std::string asString() const ;
391 
399  static Node* node(child_iterator& ii) { return *ii; }
400 
406  static const Node* node(const_child_iterator& ii) { return *ii; }
407 
413  static const Attribute* attribute(const_attribute_iterator& ii) { return *ii; }
414 
420  static const Namespace* xnamespace(const_namespace_iterator& ii) { return namespace_container::data(ii);; }
421 
422 protected:
428  static Attribute* attribute(attribute_iterator& ii) { return *ii; }
429 
430 private:
431  std::string a_name;
432  const Node* a_parent;
433  Node* a_root;
434  Children a_children;
435  Attributes a_attributes;
436  Text* a_text;
437  const Namespace* a_namespace;
438 
439  // Esta instancia sólo la creará el nodo root.
440  namespace_container* a_namespaces;
441 
442  typedef Recycler <Node> node_pool;
444  typedef Recycler <Text> text_pool;
445 
447 
448  node_pool* a_node_pool;
449  attribute_pool* a_attribute_pool;
450  text_pool* a_text_pool;
451  namespace_pool* a_namespace_pool;
452 
453  /* Para evitar que se pueda crear desde el exterior */
454  Node();
455 
456  void setRoot(Node* root) { a_root = root; }
457  void setName(const char* name) { a_name = name; }
458 
459  static const Attribute* find(const char* attrName, const_attribute_iterator, const_attribute_iterator) ;
460 
461  // Allow sort in compiler
462  attribute_iterator attribute_begin() { return a_attributes.begin(); }
463  attribute_iterator attribute_end() { return a_attributes.end(); }
464 
465  friend class Parser;
466  friend class Allocator<Node>;
467  friend class XPath;
468  friend class Decompressor;
469  friend class Compiler;
470 };
471 
472 }
473 }
474 
475 #endif
container::iterator iterator
Definition: SortedVector.hpp:33
Definition: Compiler.hpp:28
std::vector< Attribute * > Attributes
Definition: Node.hpp:66
std::vector< Node * > Children
Definition: Node.hpp:62
xml::Attribute * createAttribute(const char *name, const char *value, const Namespace *_namespace=NULL)
const_child_iterator child_begin() const
Definition: Node.hpp:179
static const Node * node(const_child_iterator &ii)
Definition: Node.hpp:406
xml::Text * createText(const std::string &text) noexcept(false)
Definition: Node.hpp:313
const_attribute_iterator attribute_begin() const
Definition: Node.hpp:185
void setNamespace(const Namespace *_namespace)
Definition: Node.hpp:348
const Namespace * namespace_find(const char *name, const bool exceptionWhenNotFound=true) const noexcept(false)
Definition: Node.hpp:369
Node * createChild(const char *name)
xml::Attribute * createAttribute(const char *name, const std::string &value, const Namespace *_namespace=NULL)
Definition: Node.hpp:262
xml::Attribute Attribute
Definition: Attribute.hpp:21
xml::Attribute * createAttribute(const char *name, const S64 *value, const Namespace *_namespace=NULL)
Definition: Node.hpp:285
namespace_container::const_iterator const_namespace_iterator
Definition: Node.hpp:72
const Node * getParent() const
Definition: Node.hpp:101
const Text * getText() const
Definition: Node.hpp:123
Definition: Node.hpp:56
Definition: Text.hpp:34
SortedVector< Namespace, NamespaceByName, std::string > namespace_container
Definition: Node.hpp:70
Definition: Decompressor.hpp:30
static T * data(iterator ii)
Definition: SortedVector.hpp:132
Children::const_iterator const_child_iterator
Definition: Node.hpp:64
Attributes::iterator attribute_iterator
Definition: Node.hpp:67
child_iterator child_begin()
Definition: Node.hpp:154
int64_t S64
Definition: defines.hpp:84
Definition: SortedVector.hpp:30
std::string asString() const
static std::string asString(const int number)
static const Attribute * attribute(const_attribute_iterator &ii)
Definition: Node.hpp:413
Attributes::const_iterator const_attribute_iterator
Definition: Node.hpp:68
const_attribute_iterator attribute_end() const
Definition: Node.hpp:218
static const Namespace * xnamespace(const_namespace_iterator &ii)
Definition: Node.hpp:420
Definition: Namespace.hpp:30
xml::Text * createText(const char *text) noexcept(false)
Definition: Parser.hpp:63
xml::Attribute * createAttribute(const char *name, const U64 *value, const Namespace *_namespace=NULL)
Definition: Node.hpp:297
Definition: Attribute.hpp:35
Node * getParent()
Definition: Node.hpp:107
Definition: Allocator.hpp:19
const std::string & getNameAsString() const
Definition: Node.hpp:95
int namespace_size() const
Definition: Node.hpp:230
const Namespace * namespace_find(const std::string &name, const bool exceptionWhenNotFound=true) const noexcept(false)
Definition: Node.hpp:377
const_namespace_iterator namespace_end() const
Definition: Node.hpp:224
Definition: Recycler.hpp:30
child_iterator child_end()
Definition: Node.hpp:199
const Attribute * getAttribute(const char *name, const bool exceptionWhenNotFound=true) const noexcept(false)
xml::Attribute * createAttribute(const char *name, const int value, const Namespace *_namespace=NULL)
Definition: Node.hpp:273
container::const_iterator const_iterator
Definition: SortedVector.hpp:34
const char * getName() const
Definition: Node.hpp:89
Definition: app.hpp:12
uint64_t U64
Definition: defines.hpp:81
virtual ~Node()
int child_size() const
Definition: Node.hpp:212
const_child_iterator child_end() const
Definition: Node.hpp:206
const std::string & getName() const
Definition: Namespace.hpp:36
const Node * find(const char *childName, const bool exceptionWhenNotFound=true) const noexcept(false)
const Namespace * getNamespace() const
Definition: Node.hpp:129
Namespace * namespace_find(const char *name, const bool exceptionWhenNotFound=true) noexcept(false)
Definition: Node.hpp:354
const_namespace_iterator namespace_begin() const
Definition: Node.hpp:191
static Attribute * attribute(attribute_iterator &ii)
Definition: Node.hpp:428
const Namespace * createNamespace(const char *name, const char *reference) noexcept(false)
Definition: Node.hpp:329
Children::iterator child_iterator
Definition: Node.hpp:63
Definition: XPath.hpp:33
static Node * node(child_iterator &ii)
Definition: Node.hpp:399
namespace_container::iterator namespace_iterator
Definition: Node.hpp:71