00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #ifndef _terimber_sxml_hpp_
00029 #define _terimber_sxml_hpp_
00030
00031 #include "xml/sxml.h"
00032 #include "base/except.h"
00033
00034 BEGIN_TERIMBER_NAMESPACE
00035 #pragma pack(4)
00036
00038
00039 xml_forceinline
00040 const attributeDecl*
00041 xml_node::cast_to_attribute() const
00042 {
00043 assert(_decl->get_type() == ATTRIBUTE_NODE);
00044 return static_cast< const attributeDecl* >(_decl);
00045 }
00046
00048
00049
00050
00051 xml_forceinline
00052 const xml_value_node*
00053 xml_value_node::cast_to_node_value(const xml_tree_node* x)
00054 {
00055 switch (x->_decl->get_type())
00056 {
00057 case TEXT_NODE:
00058 case PROCESSING_INSTRUCTION_NODE:
00059 case CDATA_SECTION_NODE:
00060 case COMMENT_NODE:
00061 case ATTRIBUTE_NODE:
00062 return static_cast< const xml_value_node* >(x);
00063 default:
00064 assert(false);
00065 return 0;
00066 }
00067 }
00068
00069
00070
00071
00072 xml_forceinline
00073 xml_value_node*
00074 xml_value_node::cast_to_node_value(xml_tree_node* x)
00075 {
00076 switch (x->_decl->get_type())
00077 {
00078 case TEXT_NODE:
00079 case PROCESSING_INSTRUCTION_NODE:
00080 case CDATA_SECTION_NODE:
00081 case COMMENT_NODE:
00082 case ATTRIBUTE_NODE:
00083 return static_cast< xml_value_node* >(x);
00084 default:
00085 assert(false);
00086 return 0;
00087 }
00088 }
00089
00091
00092
00093
00094 xml_forceinline
00095 xml_tree_node*
00096 xml_container::add_node(xml_tree_node* node)
00097 {
00098
00099 node->_right = 0;
00100 node->_left = _last_child;
00101
00102 return _last_child = (_first_child ? _last_child->_right : _first_child) = node;
00103 }
00104
00105 xml_forceinline
00106 xml_tree_node*
00107 xml_container::append_node(xml_tree_node* after, xml_tree_node* node)
00108 {
00109
00110 node->_right = after->_right;
00111 node->_left = after;
00112 return after->_right = (after->_right ? after->_right->_left : _last_child) = node;
00113 }
00114
00115
00116
00117
00118 xml_forceinline
00119 xml_tree_node*
00120 xml_container::insert_node(xml_tree_node* before, xml_tree_node* node)
00121 {
00122
00123 node->_left = before->_left;
00124 node->_right = before;
00125 return before->_left = (before->_left ? before->_left->_right : _first_child) = node;
00126 }
00127
00128 xml_forceinline
00129 xml_tree_node*
00130 xml_container::remove_node(xml_tree_node* node)
00131 {
00132 (node->_left ? node->_left->_right : _first_child) = node->_right;
00133 (node->_right ? node->_right->_left : _last_child) = node->_left;
00134
00135
00136
00137
00138
00139 return node->_right ? node->_right : (node->_left ? node->_left : node->_parent);
00140 }
00141
00142
00143
00144
00145 xml_forceinline
00146 bool
00147 xml_container::has_children() const
00148 {
00149 return _first_child != 0;
00150 }
00151
00152 xml_forceinline
00153 bool
00154 xml_container::is_container() const
00155 {
00156 switch (_decl->get_type())
00157 {
00158 case ELEMENT_NODE:
00159 case DOCUMENT_NODE:
00160 return true;
00161 default:
00162 return false;
00163 }
00164 }
00165
00166
00167
00168
00169 xml_forceinline
00170 const xml_container*
00171 xml_container::cast_to_container(const xml_tree_node* x)
00172 {
00173 #ifdef _DEBUG
00174 switch (x->_decl->get_type())
00175 {
00176 case ELEMENT_NODE:
00177 case DOCUMENT_NODE:
00178 #endif
00179 return static_cast< const xml_container* >(x);
00180 #ifdef _DEBUG
00181 default:
00182 assert(false);
00183 return 0;
00184 }
00185 #endif
00186 }
00187
00188
00189
00190
00191 xml_forceinline
00192 xml_container*
00193 xml_container::cast_to_container(xml_tree_node* x)
00194 {
00195 #ifdef _DEBUG
00196 switch (x->_decl->get_type())
00197 {
00198 case ELEMENT_NODE:
00199 case DOCUMENT_NODE:
00200 #endif
00201 return static_cast< xml_container* >(x);
00202 #ifdef _DEBUG
00203 default:
00204 assert(false);
00205 return 0;
00206 }
00207 #endif
00208 }
00209
00211 xml_forceinline
00212 bool
00213 xml_element::has_attributes() const
00214 {
00215 return _first_attr != 0;
00216 }
00217
00218
00219
00220
00221
00222 xml_forceinline
00223 const elementDecl*
00224 xml_element::cast_decl() const
00225 {
00226 return static_cast< const elementDecl* >(_decl);
00227 }
00228
00229
00230
00231
00232 xml_forceinline
00233 void
00234 xml_element::clear()
00235 {
00236 xml_container::clear();
00237 _first_attr = 0;
00238 _last_attr = 0;
00239 }
00240
00241
00242
00243
00244 xml_forceinline
00245 const xml_element*
00246 xml_element::cast_to_element(const xml_tree_node* x)
00247 {
00248 #ifdef _DEBUG
00249 switch (x->_decl->get_type())
00250 {
00251 case ELEMENT_NODE:
00252 #endif
00253 return static_cast< const xml_element* >(x);
00254 #ifdef _DEBUG
00255 default:
00256 assert(false);
00257 return 0;
00258 }
00259 #endif
00260 }
00261
00262 xml_forceinline
00263 xml_element*
00264 xml_element::cast_to_element(xml_tree_node* x)
00265 {
00266 #ifdef _DEBUG
00267 switch (x->_decl->get_type())
00268 {
00269 case ELEMENT_NODE:
00270 #endif
00271 return static_cast< xml_element* >(x);
00272 #ifdef _DEBUG
00273 default:
00274 assert(false);
00275 return 0;
00276 }
00277 #endif
00278 }
00279
00280 xml_forceinline
00281 xml_tree_node*
00282 xml_element::add_attribute(xml_tree_node* node)
00283 {
00284
00285 node->_right = 0;
00286 node->_left = _last_attr;
00287
00288 return _last_attr = (_first_attr ? _last_attr->_right : _first_attr) = node;
00289 }
00290
00291
00292
00293
00294 xml_forceinline
00295 xml_tree_node*
00296 xml_element::append_attribute(xml_tree_node* after, xml_tree_node* node)
00297 {
00298
00299 node->_right = after->_right;
00300 node->_left = after;
00301 return after->_right = (after->_right ? after->_right->_left : _last_attr) = node;
00302 }
00303
00304
00305
00306
00307 xml_forceinline
00308 xml_tree_node*
00309 xml_element::insert_attribute(xml_tree_node* before, xml_tree_node* node)
00310 {
00311
00312 node->_left = before->_left;
00313 node->_right = before;
00314 return before->_left = (before->_left ? before->_left->_right : _first_attr) = node;
00315 }
00316
00317 xml_forceinline
00318 xml_tree_node*
00319 xml_element::remove_attribute(xml_tree_node* node)
00320 {
00321 (node->_left ? node->_left->_right : _first_attr) = node->_right;
00322 (node->_right ? node->_right->_left : _last_attr) = node->_left;
00323
00324
00325
00326
00327
00328 return node->_right ? node->_right : (node->_left ? node->_left : node->_parent);
00329 }
00330
00332 xml_forceinline
00333 byte_allocator&
00334 xml_document::get_data_allocator()
00335 {
00336 return _data_allocator;
00337 }
00338
00339 xml_forceinline
00340 byte_allocator&
00341 xml_document::get_model_allocator()
00342 {
00343 return _model_allocator;
00344 }
00345
00346 xml_forceinline
00347 byte_allocator&
00348 xml_document::get_tmp_allocator()
00349 {
00350 return _tmp_allocator;
00351 }
00352
00353 xml_forceinline
00354 mem_pool_t&
00355 xml_document::get_small_manager()
00356 {
00357 return _small_manager;
00358 }
00359
00360 xml_forceinline
00361 mem_pool_t&
00362 xml_document::get_big_manager()
00363 {
00364 return _big_manager;
00365 }
00366
00367 xml_forceinline
00368 const string_t&
00369 xml_document::get_doc_name() const
00370 {
00371 return _doc_name;
00372 }
00373
00374 xml_forceinline
00375 bool
00376 xml_document::is_on_fly() const
00377 {
00378 return _on_fly;
00379 }
00380
00381 xml_forceinline
00382 xml_element&
00383 xml_document::get_root_element()
00384 {
00385 return _root;
00386 }
00387
00388 xml_forceinline
00389 const xml_element&
00390 xml_document::get_root_element() const
00391 {
00392 return _root;
00393 }
00394
00395
00396
00397
00398 xml_forceinline
00399 xml_value_node*
00400 xml_document::add_comment(const char* value, xml_tree_node* sibling, bool after)
00401 {
00402 xml_container* parent = _container_stack.top();
00403 string_t text(value, &_data_allocator);
00404 xml_value_node* retVal = new(check_pointer(_data_allocator.allocate(sizeof(xml_value_node)))) xml_value_node(&_comment_decl, parent);
00405 retVal->_value.strVal = text;
00406 sibling ? (after ? parent->append_node(sibling, retVal) : parent->insert_node(sibling, retVal)) : parent->add_node(retVal);
00407 return retVal;
00408 }
00409
00410
00411
00412
00413 xml_forceinline
00414 xml_value_node*
00415 xml_document::add_cdata(const char* value, xml_tree_node* sibling, bool after)
00416 {
00417 xml_container* parent = _container_stack.top();
00418 string_t text(value, &_data_allocator);
00419 xml_value_node* retVal = new(check_pointer(_data_allocator.allocate(sizeof(xml_value_node)))) xml_value_node(&_cdata_decl, parent);
00420 retVal->_value.strVal = text;
00421 sibling ? (after ? parent->append_node(sibling, retVal) : parent->insert_node(sibling, retVal)) : parent->add_node(retVal);
00422 return retVal;
00423 }
00424
00425
00426
00427
00428 xml_forceinline
00429 xml_value_node*
00430 xml_document::add_pi(const char* name, const char* value, xml_tree_node* sibling, bool after)
00431 {
00432
00433 if (!name || !*name)
00434 exception::_throw("Processing instruction must contain the name");
00435
00436 if (!str_template::strnocasecmp(name, str_xml, os_minus_one))
00437 exception::_throw("Invalid Processing instruction name");
00438
00439 xml_container* parent = _container_stack.top();
00440 string_t text(value, &_data_allocator);
00441 xml_value_node* retVal = new(check_pointer(_data_allocator.allocate(sizeof(xml_value_node)))) xml_value_node(&add_pi_decl(name), parent);
00442 retVal->_value.strVal = text;
00443 sibling ? (after ? parent->append_node(sibling, retVal) : parent->insert_node(sibling, retVal)) : parent->add_node(retVal);
00444 return retVal;
00445 }
00446
00447
00448
00449
00450 xml_forceinline
00451 xml_value_node*
00452 xml_document::add_text(const char* value, xml_tree_node* sibling, bool after)
00453 {
00454 xml_container* parent = _container_stack.top();
00455 string_t text(value, &_data_allocator);
00456 xml_value_node* retVal = new(check_pointer(_data_allocator.allocate(sizeof(xml_value_node)))) xml_value_node(&_text_decl, parent);
00457 retVal->_value.strVal = text;
00458 sibling ? (after ? parent->append_node(sibling, retVal) : parent->insert_node(sibling, retVal)) : parent->add_node(retVal);
00459 return retVal;
00460 }
00461
00462
00463
00464
00465 xml_forceinline
00466 void
00467 xml_document::add_entity_desc(const entityDecl& decl)
00468 {
00469 xml_container* parent = _container_stack.top();
00470 xml_tree_node* retVal = new(check_pointer(_data_allocator.allocate(sizeof(xml_tree_node)))) xml_tree_node(&decl, parent);
00471 parent->add_node(retVal);
00472 }
00473
00474
00475
00476
00477 xml_forceinline
00478 void
00479 xml_document::add_notation_desc(const notationDecl& decl)
00480 {
00481 xml_container* parent = _container_stack.top();
00482 xml_tree_node* retVal = new(check_pointer(_data_allocator.allocate(sizeof(xml_tree_node)))) xml_tree_node(&decl, parent);
00483 parent->add_node(retVal);
00484 }
00485
00486
00487
00488
00489 xml_forceinline
00490 void
00491 xml_document::add_element_desc(const elementDecl& decl)
00492 {
00493 xml_container* parent = _container_stack.top();
00494 xml_tree_node* retVal = new(check_pointer(_data_allocator.allocate(sizeof(xml_tree_node)))) xml_tree_node(&decl, parent);
00495 parent->add_node(retVal);
00496 }
00497
00498 #pragma pack()
00499 END_TERIMBER_NAMESPACE
00500
00501 #endif // _terimber_sxml_hpp_