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_element.h" 00031 #include "dom_node_list.h" 00032 #include "dom_attr.h" 00033 #include "dom_document.h" 00034 #include "dom_node_generic.h" 00035 00037 // CL_DomElement construction: 00038 00039 CL_DomElement::CL_DomElement() 00040 { 00041 } 00042 00043 CL_DomElement::CL_DomElement(CL_DomDocument &doc, const std::string &tag_name) 00044 : CL_DomNode(doc, ELEMENT_NODE) 00045 { 00046 impl->node_name = tag_name; 00047 } 00048 00049 CL_DomElement::CL_DomElement(const CL_SharedPtr<CL_DomNode_Generic> &impl) : CL_DomNode(impl) 00050 { 00051 } 00052 00053 CL_DomElement::~CL_DomElement() 00054 { 00055 } 00056 00058 // CL_DomElement attributes: 00059 00060 std::string CL_DomElement::get_tag_name() const 00061 { 00062 if (impl) return impl->node_name; 00063 return std::string(); 00064 } 00065 00067 // CL_DomElement operations: 00068 00069 bool CL_DomElement::has_attribute(const std::string &name) const 00070 { 00071 if (impl) 00072 { 00073 for(int i = 0; i < (int)impl->attributes.get_length(); ++i) 00074 if (impl->attributes.item(i).to_attr().get_name() == name) 00075 return true; 00076 } 00077 return false; 00078 } 00079 00080 std::string CL_DomElement::get_attribute(const std::string &name) const 00081 { 00082 if (impl) 00083 { 00084 for(int i = 0; i < (int)impl->attributes.get_length(); ++i) 00085 if (impl->attributes.item(i).to_attr().get_name() == name) 00086 return impl->attributes.item(i).to_attr().get_value(); 00087 } 00088 return std::string(); 00089 } 00090 00091 std::string CL_DomElement::get_attribute(const std::string &name, const std::string &default_value) const 00092 { 00093 if (impl) 00094 { 00095 for(int i = 0; i < (int)impl->attributes.get_length(); ++i) 00096 if (impl->attributes.item(i).to_attr().get_name() == name) 00097 return impl->attributes.item(i).to_attr().get_value(); 00098 } 00099 return default_value; 00100 } 00101 00102 void CL_DomElement::set_attribute(const std::string &name, const std::string &value) 00103 { 00104 if (impl) 00105 { 00106 for(int i = 0; i < (int)impl->attributes.get_length(); ++i) 00107 if (impl->attributes.item(i).to_attr().get_name() == name) 00108 { 00109 impl->attributes.item(i).to_attr().set_value(value); 00110 return; 00111 } 00112 00113 CL_DomAttr attr(get_owner_document(), name); 00114 impl->attributes.set_named_item(attr); 00115 attr.impl->node_value = value; 00116 } 00117 } 00118 00119 void CL_DomElement::remove_attribute(const std::string &name) 00120 { 00121 if (impl) 00122 { 00123 impl->attributes.remove_named_item(name); 00124 } 00125 } 00126 00127 CL_DomNodeList CL_DomElement::get_elements_by_tag_name(const std::string &name) 00128 { 00129 return CL_DomNodeList(*this, name); 00130 } 00131 00132 std::string CL_DomElement::get_text() const 00133 { 00134 std::string str; 00135 if (has_child_nodes() == false) 00136 return str; 00137 00138 CL_DomNode cur = get_first_child(); 00139 while (!cur.is_null()) 00140 { 00141 if (cur.is_text()) 00142 str.append(cur.get_node_value()); 00143 cur = cur.get_next_sibling(); 00144 } 00145 return str; 00146 } 00147 00148 void CL_DomElement::normalize() 00149 { 00150 } 00151 00153 // CL_DomElement implementation:
1.4.1