00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #ifndef MXML_NODE_H
00012 #define MXML_NODE_H
00013
00014 #include <string>
00015 #include <iostream>
00016 #include <list>
00017 #include <cassert>
00018
00019 #include <mxml_error.h>
00020 #include <mxml_attribute.h>
00021
00022 namespace MXML
00023 {
00024
00025 typedef std::list<Attribute *> AttribList;
00026 typedef AttribList::iterator AttribListIter;
00027
00028
00029 template <class __Node>
00030 class __iterator
00031 {
00032 protected:
00033 __Node *m_base;
00034 __Node *m_node;
00035 friend class Node;
00036
00037
00038 void copy( __iterator &src ) { m_base = src.m_base; m_node = src.m_node; }
00039
00040 virtual __iterator &__next() {
00041 assert ( m_node != 0 );
00042 m_node = m_node->next();
00043 return *this;
00044 }
00045 virtual __iterator &__prev();
00046 public:
00047 __iterator( __Node *nd=0 ) { m_base = m_node = nd; }
00048 inline __iterator &operator=( __iterator src ) {
00049 copy( src );
00050 return *this;
00051 }
00052 inline __Node &operator*() const { return *m_node; }
00053 inline __Node *operator->() const { return &(operator*()); }
00054
00055 virtual inline __iterator &operator++(){ return __next(); }
00056 virtual inline __iterator &operator--() { return __prev(); }
00057
00058 virtual inline __iterator operator++(int) {
00059 __iterator tmp = *this;
00060 operator++();
00061 return tmp;
00062 }
00063
00064 virtual inline __iterator operator--(int) {
00065 __iterator tmp = *this;
00066 operator++();
00067 return tmp;
00068 }
00069
00070 inline bool operator==(const __iterator &conf) const
00071 { return m_node == conf.m_node; }
00072 inline bool operator!=( const __iterator &conf)
00073 { return m_node != conf.m_node; }
00074 inline __iterator &operator+=(const int count);
00075 inline __iterator &operator-=(const int count);
00076 inline __iterator operator+(const int count);
00077 inline __iterator operator-(const int count);
00078 inline __iterator operator[](const int count) { return operator+(count); }
00079
00080 };
00081
00082 template <class __Node>
00083 class __deep_iterator:public __iterator< __Node>
00084 {
00085 protected:
00086 friend class Node;
00087
00088 virtual inline __iterator<__Node> &__next();
00089 virtual inline __iterator<__Node> &__prev();
00090 public:
00091 __deep_iterator( __Node *nd=0 ):__iterator< __Node>( nd ) {};
00092 };
00093
00094 template <class __Node>
00095 class __find_iterator:public __deep_iterator< __Node>
00096 {
00097 std::string m_name;
00098 std::string m_attr;
00099 std::string m_valattr;
00100 std::string m_data;
00101 int m_maxmatch;
00102
00103 protected:
00104 friend class Node;
00105 __find_iterator( __Node *nd, std::string name, std::string attr,
00106 std::string valatt, std::string data);
00107 virtual inline __iterator<__Node> &__next();
00108 virtual inline __iterator<__Node> &__prev();
00109 virtual inline __iterator<__Node> &__find();
00110 public:
00111 __find_iterator( __Node *nd=0 );
00112
00113 };
00114
00115 template <class __Node>
00116 class __path_iterator:public __iterator< __Node>
00117 {
00118 std::string m_path;
00119 virtual inline __Node *subfind( __Node *parent, std::string::size_type begin );
00120
00121 protected:
00122 friend class Node;
00123 __path_iterator( __Node *nd, std::string path );
00124 virtual inline __iterator<__Node> &__next();
00125 virtual inline __iterator<__Node> &__prev();
00126 virtual inline __iterator<__Node> &__find();
00127 public:
00128 __path_iterator( __Node *nd=0 );
00129 };
00130
00135 class Node: public Element
00136 {
00137
00138 public:
00139
00144 enum type
00145 {
00148 typeTag=0,
00153 typeXMLDecl,
00158 typeComment,
00163 typePI,
00168 typeDirective,
00175 typeData,
00181 typeDocument,
00183 typeFakeClosing
00184 };
00185
00186 private:
00187 type m_type;
00188 std::string m_name;
00189 std::string m_data;
00190 AttribList m_attrib;
00191 AttribList::iterator m_lastFound;
00192
00193 Node *m_parent;
00194 Node *m_child;
00195 Node *m_last_child;
00196 Node *m_next;
00197 Node *m_prev;
00198
00199 protected:
00200 void nodeIndent( std::ostream &out, const int depth, const int style ) const;
00201
00202 void readData( std::istream in, const int iStyle );
00203
00204 public:
00217 Node( std::istream &in, const int style = 0, const int line=1, const int pos=0 )
00218 throw( MalformedError );
00219
00220
00221
00222
00223
00224
00225
00226
00227 Node( const type tp, const std::string &name = "", const std::string &data = "" ):
00228 Element()
00229 {
00230 m_type = tp;
00231 m_name = name;
00232 m_data = data;
00233 m_lastFound = m_attrib.end();
00234
00235 m_child = m_last_child = m_prev = m_next = m_parent = 0;
00236 }
00237
00241 Node( Node & );
00242
00270 ~Node();
00271
00273 const type nodeType() const { return m_type; }
00274
00278 const std::string name() const { return m_name; }
00282 const std::string data() const { return m_data; }
00283
00289 void name( const std::string &new_name ) { m_name = new_name; }
00290
00296 void data( const std::string &new_value ) { m_data = new_value; }
00297
00300 void addAttribute( Attribute *attrib )
00301 {
00302 m_attrib.push_back( attrib );
00303 }
00304
00312 const std::string getAttribute( const std::string name ) const
00313 throw( NotFoundError );
00314
00322 void setAttribute( const std::string name, const std::string value )
00323 throw( NotFoundError );
00324
00341 bool hasAttribute( const std::string name ) const;
00342
00346 void unlink();
00347
00363 Node * unlinkComplete();
00364
00375 void Node::removeChild( Node *child ) throw( NotFoundError );
00376
00377
00378 Node *parent() const { return m_parent; }
00379 Node *child() const { return m_child; }
00380 Node *next() const { return m_next; }
00381 Node *prev() const { return m_prev; }
00382 Node *lastChild() const { return m_last_child; }
00383
00384
00385 void addBelow( Node * );
00386 void insertBelow( Node * );
00387 void insertBefore( Node * );
00388 void insertAfter( Node * );
00389
00394 int Node::depth() const;
00395
00415 std::string Node::path() const;
00416
00417
00418
00419
00420
00421
00422 Node *clone();
00423
00431 virtual void write( std::ostream &out, const int style ) const;
00432
00433
00434 typedef __iterator<Node> iterator;
00435 typedef __iterator<const Node> const_iterator;
00436 typedef __deep_iterator<Node> deep_iterator;
00437 typedef __deep_iterator<const Node> const_deep_iterator;
00438 typedef __find_iterator<Node> find_iterator;
00439 typedef __find_iterator<const Node> const_find_iterator;
00440 typedef __path_iterator<Node> path_iterator;
00441 typedef __path_iterator<const Node> const_path_iterator;
00442
00443 iterator begin() { return m_child; }
00444 iterator end() { return static_cast<Node *>(0); }
00445 const_iterator const_begin() const { return static_cast<const Node *>(m_child); }
00446 const_iterator const_end() { return static_cast<const Node *>(0); }
00447 deep_iterator deep_begin() { return m_child; }
00448
00490 find_iterator find( std::string name, std::string attrib="", std::string valatt="", std::string data="" );
00491
00496 path_iterator find_path( std::string path );
00497 };
00498
00499 #include <mxml_iterator.h>
00500
00501 }
00502
00503
00504 #endif
00505