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 00031 00032 #ifndef header_dom_node 00033 #define header_dom_node 00034 00035 #if _MSC_VER > 1000 00036 #pragma once 00037 #endif 00038 00039 #include "sharedptr.h" 00040 #include <string> 00041 00042 class CL_DomElement; 00043 class CL_DomAttr; 00044 class CL_DomText; 00045 class CL_DomCDATASection; 00046 class CL_DomEntityReference; 00047 class CL_DomEntity; 00048 class CL_DomProcessingInstruction; 00049 class CL_DomComment; 00050 class CL_DomDocument; 00051 class CL_DomDocumentType; 00052 class CL_DomDocumentFragment; 00053 class CL_DomNotation; 00054 class CL_DomNodeList; 00055 class CL_DomNamedNodeMap; 00056 class CL_DomNode_Generic; 00057 00058 //: DOM Node class. 00059 //- !group=Core/XML! 00060 //- !header=core.h! 00061 //- <p>The Node interface is the primary datatype for the entire Document Object Model. 00062 //- It represents a single node in the document tree. While all objects implementing 00063 //- the Node interface expose methods for dealing with children, not all objects 00064 //- implementing the Node interface may have children. For example, Text nodes may 00065 //- not have children, and adding children to such nodes results in a CL_DOMException 00066 //- being thrown.</p> 00067 //- <p>The attributes 'node_name', 'node_value' and 'attributes' are included as a 00068 //- mechanism to get at node information without casting down to the specific derived 00069 //- interface. In cases where there is no obvious mapping of these attributes for a 00070 //- specific node_type (e.g., node_value for an Element or attributes for a Comment), 00071 //- this returns null. Note that the specialized interfaces may contain additional 00072 //- and more convenient mechanisms to get and set the relevant information.</p> 00073 class CL_DomNode 00074 { 00076 public: 00077 CL_DomNode(); 00078 00079 CL_DomNode(const CL_DomNode ©); 00080 00081 ~CL_DomNode(); 00082 00084 public: 00085 //: An integer indicating which type of node this is. 00086 enum NodeType 00087 { 00088 NULL_NODE = 0, 00089 ELEMENT_NODE = 1, 00090 ATTRIBUTE_NODE = 2, 00091 TEXT_NODE = 3, 00092 CDATA_SECTION_NODE = 4, 00093 ENTITY_REFERENCE_NODE = 5, 00094 ENTITY_NODE = 6, 00095 PROCESSING_INSTRUCTION_NODE = 7, 00096 COMMENT_NODE = 8, 00097 DOCUMENT_NODE = 9, 00098 DOCUMENT_TYPE_NODE = 10, 00099 DOCUMENT_FRAGMENT_NODE = 11, 00100 NOTATION_NODE = 12 00101 }; 00102 00103 //: Returns the node name. 00104 //- <p>The return value vary according to the node type as follows:</p> 00105 //- <ul> 00106 //- <li>Element: name of tag</li> 00107 //- <li>Attr: name of attribute</li> 00108 //- <li>Text: "#text"</li> 00109 //- <li>CDataSection: "#cdata-section"</li> 00110 //- <li>EntityReference: name of entity referenced</li> 00111 //- <li>Entity: entity name</li> 00112 //- <li>ProcessingInstruction: target</li> 00113 //- <li>Comment: "#comment"</li> 00114 //- <li>Document: "#document"</li> 00115 //- <li>DocumentType: document type name</li> 00116 //- <li>DocumentFragment: "#document-fragment"</li> 00117 //- <li>Notation: notation name</li> 00118 //- </ul> 00119 std::string get_node_name() const; 00120 00121 //: Returns the node value. 00122 //- <p>The return value vary according to the node type as follows:</p> 00123 //- <ul> 00124 //- <li>Element: null</li> 00125 //- <li>Attr: value of attribute</li> 00126 //- <li>Text: content of text node</li> 00127 //- <li>CDataSection: content of CDATA Section</li> 00128 //- <li>EntityReference: null</li> 00129 //- <li>Entity: null</li> 00130 //- <li>ProcessingInstruction: entire content excluding the target</li> 00131 //- <li>Comment: content of the comment</li> 00132 //- <li>Document: null</li> 00133 //- <li>DocumentType: null</li> 00134 //- <li>DocumentFragment: null</li> 00135 //- <li>Notation: null</li> 00136 //- </ul> 00137 std::string get_node_value() const; 00138 00139 //: Sets the node value. 00140 void set_node_value(const std::string &value); 00141 00142 //: Returns the node type (one of those in the NodeType enum). 00143 unsigned short get_node_type() const; 00144 00145 //: Returns the parent of this node. 00146 //- <p>All nodes, except Document, DocumentFragment, and Attr may have a parent. 00147 //- However, if a node has just been created and not yet added to the tree, or if 00148 //- it has been removed from the tree, this is null.</p> 00149 CL_DomNode get_parent_node() const; 00150 00151 //: Returns a NodeList that contains all children of this node. 00152 //- <p>If there are no children, this is a NodeList containing no nodes. The content 00153 //- of the returned NodeList is "live" in the sense that, for instance, changes to 00154 //- the children of the node object that it was created from are immediately reflected 00155 //- in the nodes returned by the NodeList accessors; it is not a static snapshot of 00156 //- the content of the node. This is true for every NodeList, including the ones 00157 //- returned by the getElementsByTagName method.</p> 00158 CL_DomNodeList get_child_nodes() const; 00159 00160 //: The first child of this node. 00161 //- <p>If there is no such node, this returns a null node.</p> 00162 CL_DomNode get_first_child() const; 00163 00164 //: The last child of this node. 00165 //- <p>If there is no such node, this returns a null node.</p> 00166 CL_DomNode get_last_child() const; 00167 00168 //: The node immediately preceding this node. 00169 //- <p>If there is no such node, this returns a null node.</p> 00170 CL_DomNode get_previous_sibling() const; 00171 00172 //: The node immediately following this node. 00173 //- <p>If there is no such node, this returns a null node.</p> 00174 CL_DomNode get_next_sibling() const; 00175 00176 //: A NamedNodeMap containing the attributes of this node (if it is an Element) or null otherwise. 00177 CL_DomNamedNodeMap get_attributes(); 00178 00179 //: The Document object associated with this node. 00180 //- <p>This is also the Document object used to create new nodes. When this node is a Document this is null.</p> 00181 CL_DomDocument get_owner_document(); 00182 00183 //: Returns true if this is a null node. 00184 bool is_null() const; 00185 00186 //: Returns true if this is an element node. 00187 bool is_element() const; 00188 00189 //: Returns true if this is an attribute node. 00190 bool is_attr() const; 00191 00192 //: Returns true if this is a text node. 00193 bool is_text() const; 00194 00195 //: Returns true if this is a CDATA section node. 00196 bool is_cdata_section() const; 00197 00198 //: Returns true if this is an entity reference node. 00199 bool is_entity_reference() const; 00200 00201 //: Returns true if this is an entity node. 00202 bool is_entity() const; 00203 00204 //: Returns true if this is a processing instruction node. 00205 bool is_processing_instruction() const; 00206 00207 //: Returns true if this is a comment node. 00208 bool is_comment() const; 00209 00210 //: Returns true if this is a document node. 00211 bool is_document() const; 00212 00213 //: Returns true if this is a document type node. 00214 bool is_document_type() const; 00215 00216 //: Returns true if this is a document fragment node. 00217 bool is_document_fragment() const; 00218 00219 //: Returns true if this is a notation node. 00220 bool is_notation() const; 00221 00222 //: Returns true if this node has any children. 00223 bool has_child_nodes() const; 00224 00226 public: 00227 //: Copy assignment operator. 00228 //- <p>All objects in the DOM are handles to the underlying implementation. Therefore this doesn't 00229 //- actually copy contents between two CL_DomNode's, but instead change the two CL_DomNode's to point 00230 //- at the same node in the DOM.</p> 00231 CL_DomNode &operator =(const CL_DomNode ©); 00232 00233 //: Compare operator. 00234 bool operator ==(const CL_DomNode &other) const; 00235 00236 //: Inserts the node new_child before the existing child node ref_child. 00237 //- <p>If refChild is a null node, inserts new_child at the end of the list of children.</p> 00238 //- <p>If newChild is a DocumentFragment object, all of its children are inserted, in the same order, 00239 //- before ref_child. If the new_child is already in the tree, it is first removed.</p> 00240 //param new_child: The node to insert. 00241 //param ref_child: The reference node, i.e., the node before which the new node must be inserted. 00242 //retval: The node being inserted. 00243 CL_DomNode insert_before(CL_DomNode &new_child, CL_DomNode &ref_child); 00244 00245 //: Replaces the child node old_child with new_child in the list of children. 00246 //- <p>If the new_child is already in the tree, it is first removed.</p> 00247 //param new_child: The new node to put in the child list. 00248 //param old_child: The node being replaced in the list. 00249 //retval: The node replaced. 00250 CL_DomNode replace_child(CL_DomNode &new_child, CL_DomNode &old_child); 00251 00252 //: Removes the child node indicated by old_child from the list of children, and returns it. 00253 CL_DomNode remove_child(CL_DomNode &old_child); 00254 00255 //: Adds the node new_child to the end of the list of children of this node. 00256 //- <p>If the new_child is already in the tree, it is first removed.</p> 00257 CL_DomNode append_child(CL_DomNode new_child); 00258 00259 //: Returns a duplicate of this node, i.e., serves as a generic copy constructor for nodes. 00260 //- <p>The duplicate node has no parent.</p> 00261 //- <p>Cloning an Element copies all attributes and their values, including those generated 00262 //- by the XML processor to represent defaulted attributes, but this method does not copy 00263 //- any text it contains unless it is a deep clone, since the text is contained in a child 00264 //- Text node. Cloning any other type of node simply returns a copy of this node.</p> 00265 //param deep: If true, recursively clone the subtree under the specified node; if false, clone only the node itself (and its attributes, if it is an Element). 00266 //retval: The duplicate node. 00267 CL_DomNode clone_node(bool deep) const; 00268 00269 //: Returns the Element interface to this node. 00270 //- <p>If the node is not an Element node, then it returns a null node.</p> 00271 CL_DomElement to_element() const; 00272 00273 //: Returns the Attribute interface to this node. 00274 //- <p>If the node is not an Attribute node, then it returns a null node.</p> 00275 CL_DomAttr to_attr() const; 00276 00277 //: Returns the Text interface to this node. 00278 //- <p>If the node is not a Text node, then it returns a null node.</p> 00279 CL_DomText to_text() const; 00280 00281 //: Returns the CDATA Section interface to this node. 00282 //- <p>If the node is not a CDATA Section node, then it returns a null node.</p> 00283 CL_DomCDATASection to_cdata_section() const; 00284 00285 //: Returns the Entity Reference interface to this node. 00286 //- <p>If the node is not an Entity Reference node, then it returns a null node.</p> 00287 CL_DomEntityReference to_entity_reference() const; 00288 00289 //: Returns the Entity interface to this node. 00290 //- <p>If the node is not an Entity node, then it returns a null node.</p> 00291 CL_DomEntity to_entity() const; 00292 00293 //: Returns the Processing Instruction interface to this node. 00294 //- <p>If the node is not a Processing Instrucion node, then it returns a null node.</p> 00295 CL_DomProcessingInstruction to_processing_instruction() const; 00296 00297 //: Returns the Comment interface to this node. 00298 //- <p>If the node is not a Comment node, then it returns a null node.</p> 00299 CL_DomComment to_comment() const; 00300 00301 //: Returns the Document interface to this node. 00302 //- <p>If the node is not a Document node, then it returns a null node.</p> 00303 CL_DomDocument to_document() const; 00304 00305 //: Returns the Document Type interface to this node. 00306 //- <p>If the node is not a Document Type node, then it returns a null node.</p> 00307 CL_DomDocumentType to_document_type() const; 00308 00309 //: Returns the Document Fragment interface to this node. 00310 //- <p>If the node is not a DocumentFragment node, then it returns a null node.</p> 00311 CL_DomDocumentFragment to_document_fragment() const; 00312 00313 //: Returns the Notation interface to this node. 00314 //- <p>If the node is not a Notation node, then it returns a null node.</p> 00315 CL_DomNotation to_notation() const; 00316 00317 //: Returns the first child node with the specified node name. 00318 //- <p>Returns a null node if no child is found.</p> 00319 CL_DomNode named_item(const std::string &name) const; 00320 00322 protected: 00323 CL_DomNode(CL_DomDocument &doc, unsigned short node_type); 00324 00325 CL_DomNode(const CL_SharedPtr<CL_DomNode_Generic> &impl); 00326 00327 CL_SharedPtr<CL_DomNode_Generic> impl; 00328 00329 friend class CL_DomDocument; 00330 }; 00331 00332 #endif
1.4.1