Main Page | Class Hierarchy | Class List | File List | Class Members | File Members

dom_node.cpp

Go to the documentation of this file.
00001 /*
00002 **  ClanLib SDK
00003 **  Copyright (c) 1997-2005 The ClanLib Team
00004 **
00005 **  This software is provided 'as-is', without any express or implied
00006 **  warranty.  In no event will the authors be held liable for any damages
00007 **  arising from the use of this software.
00008 **
00009 **  Permission is granted to anyone to use this software for any purpose,
00010 **  including commercial applications, and to alter it and redistribute it
00011 **  freely, subject to the following restrictions:
00012 **
00013 **  1. The origin of this software must not be misrepresented; you must not
00014 **     claim that you wrote the original software. If you use this software
00015 **     in a product, an acknowledgment in the product documentation would be
00016 **     appreciated but is not required.
00017 **  2. Altered source versions must be plainly marked as such, and must not be
00018 **     misrepresented as being the original software.
00019 **  3. This notice may not be removed or altered from any source distribution.
00020 **
00021 **  Note: Some of the libraries ClanLib link to may have additional
00022 **  requirements or restrictions.
00023 **
00024 **  File Author(s):
00025 **
00026 **    Magnus Norddahl
00027 */
00028 
00029 #include "precomp.h"
00030 #include "dom_node.h"
00031 #include "dom_node_list.h"
00032 #include "dom_named_node_map.h"
00033 #include "dom_element.h"
00034 #include "dom_attr.h"
00035 #include "dom_text.h"
00036 #include "dom_cdata_section.h"
00037 #include "dom_entity_reference.h"
00038 #include "dom_entity.h"
00039 #include "dom_processing_instruction.h"
00040 #include "dom_comment.h"
00041 #include "dom_document.h"
00042 #include "dom_document_type.h"
00043 #include "dom_document_fragment.h"
00044 #include "dom_notation.h"
00045 #include "dom_node_generic.h"
00046 #include "dom_document_generic.h"
00047 
00049 // CL_DomNode construction:
00050 
00051 CL_DomNode::CL_DomNode()
00052 {
00053 }
00054 
00055 CL_DomNode::CL_DomNode(const CL_SharedPtr<CL_DomNode_Generic> &impl) : impl(impl)
00056 {
00057 }
00058 
00059 CL_DomNode::CL_DomNode(const CL_DomNode &copy) : impl(copy.impl)
00060 {
00061 }
00062 
00063 CL_DomNode::CL_DomNode(CL_DomDocument &doc, unsigned short node_type)
00064 : impl(new CL_DomNode_Generic)
00065 {
00066         impl->owner_document = doc.impl;
00067         impl->node_type = node_type;
00068 }
00069 
00070 CL_DomNode::~CL_DomNode()
00071 {
00072 }
00073 
00075 // CL_DomNode attributes:
00076 
00077 std::string CL_DomNode::get_node_name() const
00078 {
00079         if (impl)
00080         {
00081                 switch (impl->node_type)
00082                 {
00083                 case CDATA_SECTION_NODE:
00084                         return "#cdata-section";
00085                 case COMMENT_NODE:
00086                         return "#comment";
00087                 case DOCUMENT_NODE:
00088                         return "#document";
00089                 case DOCUMENT_FRAGMENT_NODE:
00090                         return "#document-fragment";
00091                 case TEXT_NODE:
00092                         return "#text";
00093                 case ATTRIBUTE_NODE:
00094                 case DOCUMENT_TYPE_NODE:
00095                 case ELEMENT_NODE:
00096                 case ENTITY_NODE:
00097                 case ENTITY_REFERENCE_NODE:
00098                 case NOTATION_NODE:
00099                 case PROCESSING_INSTRUCTION_NODE:
00100                 default:
00101                         return impl->node_name;
00102                 }
00103         }
00104         return std::string();
00105 }
00106 
00107 std::string CL_DomNode::get_node_value() const
00108 {
00109         if (impl)
00110         {
00111                 switch (impl->node_type)
00112                 {
00113                 case DOCUMENT_NODE:
00114                 case DOCUMENT_FRAGMENT_NODE:
00115                 case DOCUMENT_TYPE_NODE:
00116                 case ELEMENT_NODE:
00117                 case ENTITY_NODE:
00118                 case ENTITY_REFERENCE_NODE:
00119                 case NOTATION_NODE:
00120                         return std::string();
00121 
00122                 case TEXT_NODE:
00123                 case ATTRIBUTE_NODE:
00124                 case PROCESSING_INSTRUCTION_NODE:
00125                 default:
00126                         return impl->node_value;
00127                 }
00128         }
00129         return std::string();
00130 }
00131 
00132 void CL_DomNode::set_node_value(const std::string &value)
00133 {
00134         if (impl) impl->node_value = value;
00135 }
00136 
00137 unsigned short CL_DomNode::get_node_type() const
00138 {
00139         if (impl) return impl->node_type;
00140         return NULL_NODE;
00141 }
00142 
00143 CL_DomNode CL_DomNode::get_parent_node() const
00144 {
00145         if (impl) return CL_DomNode(impl->parent);
00146         return CL_DomNode();
00147 }
00148 
00149 CL_DomNodeList CL_DomNode::get_child_nodes() const
00150 {
00151         return CL_DomNodeList();
00152 }
00153 
00154 CL_DomNode CL_DomNode::get_first_child() const
00155 {
00156         if (impl) return CL_DomNode(impl->first_child);
00157         return CL_DomNode();
00158 }
00159 
00160 CL_DomNode CL_DomNode::get_last_child() const
00161 {
00162         if (impl) return CL_DomNode(impl->last_child);
00163         return CL_DomNode();
00164 }
00165 
00166 CL_DomNode CL_DomNode::get_previous_sibling() const
00167 {
00168         if (impl) return CL_DomNode(impl->previous_sibling);
00169         return CL_DomNode();
00170 }
00171 
00172 CL_DomNode CL_DomNode::get_next_sibling() const
00173 {
00174         if (impl) return CL_DomNode(impl->next_sibling);
00175         return CL_DomNode();
00176 }
00177 
00178 CL_DomNamedNodeMap CL_DomNode::get_attributes()
00179 {
00180         if (impl && impl->node_type == ELEMENT_NODE)
00181                 return impl->attributes;
00182         return CL_DomNamedNodeMap();
00183 }
00184 
00185 CL_DomDocument CL_DomNode::get_owner_document()
00186 {
00187         if (impl) return CL_DomDocument(impl->owner_document);
00188         return CL_DomDocument();
00189 }
00190 
00191 bool CL_DomNode::is_null() const
00192 {
00193         return get_node_type() == NULL_NODE;
00194 }
00195 
00196 bool CL_DomNode::is_element() const
00197 {
00198         return get_node_type() == ELEMENT_NODE;
00199 }
00200 
00201 bool CL_DomNode::is_attr() const
00202 {
00203         return get_node_type() == ATTRIBUTE_NODE;
00204 }
00205 
00206 bool CL_DomNode::is_text() const
00207 {
00208         return get_node_type() == TEXT_NODE;
00209 }
00210 
00211 bool CL_DomNode::is_cdata_section() const
00212 {
00213         return get_node_type() == CDATA_SECTION_NODE;
00214 }
00215 
00216 bool CL_DomNode::is_entity_reference() const
00217 {
00218         return get_node_type() == ENTITY_REFERENCE_NODE;
00219 }
00220 
00221 bool CL_DomNode::is_entity() const
00222 {
00223         return get_node_type() == ENTITY_NODE;
00224 }
00225 
00226 bool CL_DomNode::is_processing_instruction() const
00227 {
00228         return get_node_type() == PROCESSING_INSTRUCTION_NODE;
00229 }
00230 
00231 bool CL_DomNode::is_comment() const
00232 {
00233         return get_node_type() == COMMENT_NODE;
00234 }
00235 
00236 bool CL_DomNode::is_document() const
00237 {
00238         return get_node_type() == DOCUMENT_NODE;
00239 }
00240 
00241 bool CL_DomNode::is_document_type() const
00242 {
00243         return get_node_type() == DOCUMENT_TYPE_NODE;
00244 }
00245 
00246 bool CL_DomNode::is_document_fragment() const
00247 {
00248         return get_node_type() == DOCUMENT_FRAGMENT_NODE;
00249 }
00250 
00251 bool CL_DomNode::is_notation() const
00252 {
00253         return get_node_type() == NOTATION_NODE;
00254 }
00255 
00257 // CL_DomNode operations:
00258 
00259 CL_DomNode &CL_DomNode::operator =(const CL_DomNode &copy)
00260 {
00261         impl = copy.impl;
00262         return *this;
00263 }
00264 
00265 bool CL_DomNode::operator ==(const CL_DomNode &other) const
00266 {
00267         return (impl == other.impl);
00268 }
00269 
00270 CL_DomNode CL_DomNode::insert_before(CL_DomNode &new_child, CL_DomNode &ref_child)
00271 {
00272         if (impl)
00273         {
00274                 new_child.impl->previous_sibling = ref_child.impl->previous_sibling;
00275                 new_child.impl->next_sibling = ref_child.impl;
00276                 ref_child.impl->previous_sibling = new_child.impl;
00277                 if (new_child.impl->previous_sibling)
00278                         new_child.impl->previous_sibling->next_sibling = new_child.impl;
00279                 if (impl->first_child == ref_child.impl) impl->first_child = new_child.impl;
00280                 new_child.impl->parent = impl;
00281 
00282                 return new_child;
00283         }
00284         return CL_DomNode();
00285 }
00286 
00287 CL_DomNode CL_DomNode::replace_child(CL_DomNode &new_child, CL_DomNode &old_child)
00288 {
00289         if (impl)
00290         {
00291                 new_child.impl->previous_sibling = old_child.impl->previous_sibling;
00292                 new_child.impl->next_sibling = old_child.impl->next_sibling;
00293                 new_child.impl->parent = impl;
00294                 if (impl->first_child == old_child.impl) impl->first_child = new_child.impl;
00295                 if (impl->last_child == old_child.impl) impl->last_child = new_child.impl;
00296                 old_child.impl->previous_sibling = CL_WeakPtr<CL_DomNode_Generic>();
00297                 old_child.impl->next_sibling = CL_SharedPtr<CL_DomNode_Generic>();
00298                 old_child.impl->parent = CL_WeakPtr<CL_DomNode_Generic>();
00299                 return new_child;
00300         }
00301         return CL_DomNode();
00302 }
00303 
00304 CL_DomNode CL_DomNode::remove_child(CL_DomNode &old_child)
00305 {
00306         if (impl)
00307         {
00308                 CL_SharedPtr<CL_DomNode_Generic> prev = old_child.impl->previous_sibling;
00309                 CL_SharedPtr<CL_DomNode_Generic> next = old_child.impl->next_sibling;
00310                 if (next) next->previous_sibling = prev;
00311                 if (prev) prev->next_sibling = next;
00312                 if (impl->first_child == old_child.impl) impl->first_child = next;
00313                 if (impl->last_child == old_child.impl) impl->last_child = prev;
00314                 old_child.impl->previous_sibling = CL_WeakPtr<CL_DomNode_Generic>();
00315                 old_child.impl->next_sibling = CL_SharedPtr<CL_DomNode_Generic>();
00316                 old_child.impl->parent = CL_WeakPtr<CL_DomNode_Generic>();
00317         }
00318         return CL_DomNode();
00319 }
00320 
00321 CL_DomNode CL_DomNode::append_child(CL_DomNode new_child)
00322 {
00323         if (impl)
00324         {
00325                 if (impl->last_child)
00326                 {
00327                         impl->last_child->next_sibling = new_child.impl;
00328                         new_child.impl->previous_sibling = impl->last_child;
00329                         impl->last_child = new_child.impl;
00330                 }
00331                 else
00332                 {
00333                         impl->first_child = new_child.impl;
00334                         impl->last_child = new_child.impl;
00335                 }
00336                 impl->parent = impl;
00337                 return new_child;
00338         }
00339         return CL_DomNode();
00340 }
00341 
00342 bool CL_DomNode::has_child_nodes() const
00343 {
00344         if (impl) return (impl->first_child != 0);
00345         return false;
00346 }
00347 
00348 CL_DomNode CL_DomNode::clone_node(bool deep) const
00349 {
00350         return CL_DomNode();
00351 }
00352 
00353 CL_DomElement CL_DomNode::to_element() const
00354 {
00355         if (is_element()) return CL_DomElement(impl);
00356         return CL_DomElement();
00357 }
00358 
00359 CL_DomAttr CL_DomNode::to_attr() const
00360 {
00361         if (is_attr()) return CL_DomAttr(impl);
00362         return CL_DomAttr();
00363 }
00364 
00365 CL_DomText CL_DomNode::to_text() const
00366 {
00367         if (is_text()) return CL_DomText(impl);
00368         return CL_DomText();
00369 }
00370 
00371 CL_DomCDATASection CL_DomNode::to_cdata_section() const
00372 {
00373         if (is_cdata_section()) return CL_DomCDATASection(impl);
00374         return CL_DomCDATASection();
00375 }
00376 
00377 CL_DomEntityReference CL_DomNode::to_entity_reference() const
00378 {
00379         if (is_entity_reference()) return CL_DomEntityReference(impl);
00380         return CL_DomEntityReference();
00381 }
00382 
00383 CL_DomEntity CL_DomNode::to_entity() const
00384 {
00385         if (is_entity()) return CL_DomEntity(impl);
00386         return CL_DomEntity();
00387 }
00388 
00389 CL_DomProcessingInstruction CL_DomNode::to_processing_instruction() const
00390 {
00391         if (is_processing_instruction()) return CL_DomProcessingInstruction(impl);
00392         return CL_DomProcessingInstruction();
00393 }
00394 
00395 CL_DomComment CL_DomNode::to_comment() const
00396 {
00397         if (is_comment()) return CL_DomComment(impl);
00398         return CL_DomComment();
00399 }
00400 
00401 CL_DomDocument CL_DomNode::to_document() const
00402 {
00403         if (is_document()) return CL_DomDocument(impl);
00404         return CL_DomDocument();
00405 }
00406 
00407 CL_DomDocumentType CL_DomNode::to_document_type() const
00408 {
00409         if (is_document_type()) return CL_DomDocumentType(impl);
00410         return CL_DomDocumentType();
00411 }
00412 
00413 CL_DomDocumentFragment CL_DomNode::to_document_fragment() const
00414 {
00415         if (is_document_fragment()) return CL_DomDocumentFragment(impl);
00416         return CL_DomDocumentFragment();
00417 }
00418 
00419 CL_DomNotation CL_DomNode::to_notation() const
00420 {
00421         if (is_notation()) return CL_DomNotation(impl);
00422         return CL_DomNotation();
00423 }
00424 
00425 CL_DomNode CL_DomNode::named_item(const std::string &name) const
00426 {
00427         CL_DomNode node = get_first_child();
00428         while (node.is_null() == false)
00429         {
00430                 if (node.get_node_name() == name) return node;
00431                 node = node.get_next_sibling();
00432         }
00433         return CL_DomNode();
00434 }
00435 
00437 // CL_DomNode implementation:

Generated on Sat Feb 19 22:51:15 2005 for npcore by  doxygen 1.4.1