indexing description: "Objects that ..." author: "" date: "$Date$" revision: "$Revision$" deferred class XML_NODE feature {XML_NODE} -- Creation make_node (doc: XML_DOCUMENT; parent_node: XML_NODE; a_namespace, a_prefix, a_local_part: STRING) is -- Creates a new instance of this xml node type do owner_document := doc parent := parent_node namespace := a_namespace create name.make (a_prefix, a_local_part, Current) create child_nodes.make (Current) create attributes.make (Current) end feature -- Access owner_document: XML_DOCUMENT parent: XML_NODE namespace: STRING name: XML_QUALIFIED_NAME node_type: STRING is -- gets the type of this xml node deferred end attributes: XML_ATTRIBUTE_LIST child_nodes: XML_NODE_LIST -- child nodes of this xml node value: STRING set_value (val: STRING) is -- sets the value of this xml node do value := val end inner_text : STRING is -- gets the inner text of this xml node do create Result.make_empty from child_nodes.start until child_nodes.after loop Result.append (child_nodes.item.inner_text) child_nodes.forth end end set_inner_text (text: STRING) is -- sets the inner text of this xml node do end select_nodes (xpath: STRING) : XML_NODE_LIST is -- selects the nodes as specified in the xpath string local x: XPATH do create x.make_from_path (xpath) Result := x.select_nodes (Current) end select_single_node (xpath: STRING) : XML_NODE is -- selects the first node of the node list specified by the xpath string local x: XPATH do create x.make_from_path (xpath) Result := x.select_first_node (Current) end first_child : XML_NODE is -- gets the first child node of the current node do if child_nodes.count > 0 then Result := child_nodes.first end end last_child : XML_NODE is -- gets the last child node of the current node do if child_nodes.count > 0 then Result := child_nodes.last end end next_sibling : XML_NODE is -- gets the next sibling node of the current node local index: INTEGER do index := parent.child_nodes.index_of (Current, 1) check index /= 0 end if index < parent.child_nodes.count then Result := parent.child_nodes.i_th (index + 1) end end previous_sibling : XML_NODE is -- gets the next sibling node of the current node local index: INTEGER do index := parent.child_nodes.index_of (Current, 1) check index /= 0 end if index > 1 then Result := parent.child_nodes.i_th (index - 1) end end to_xml (pretty_print: BOOLEAN) : STRING is -- generates the serialized string representation of this xml node do Result := to_xml_internal (pretty_print, 0) end remove_from_tree is -- removes the current node from the tree require has_parent: parent /= Void do parent.child_nodes.start parent.child_nodes.prune (Current) set_parent (Void) end feature -- Cloning clone_node (deep: BOOLEAN) : like Current is -- creates a clone of the current node deferred ensure result_has_no_doc: Result.owner_document = Void end feature {XML_NODE, XML_BASE_LIST} set_parent (a_parent: XML_NODE) is -- sets the parent node of the current node to a_parent local list: LIST[XML_NODE] do parent := a_parent list := attributes list.do_all (agent set_parent_on_node (?, a_parent)) child_nodes.do_all (agent set_parent_on_node (?, a_parent)) end set_parent_on_node (a_node: XML_NODE; a_parent: XML_NODE) is -- sets the parent of to do a_node.set_parent (a_parent) end set_owner_document (a_document: XML_DOCUMENT) is -- sets the owner document of this node to require cant_be_in_document: owner_document = Void local list: LIST[XML_NODE] do owner_document := a_document list := attributes list.do_all (agent set_parent_on_node (?, a_document)) child_nodes.do_all (agent set_parent_on_node (?, a_document)) end set_owner_document_on_node (a_node: XML_NODE; a_document: XML_DOCUMENT) is -- sets the owner document of to do a_node.set_owner_document (a_document) end feature {XML_NODE} -- Implementation replace_child (child, new_child: XML_NODE) is -- replaces child with new_child in the -- child_nodes list, if child exists in the list. -- if child is Void or not contained within the child -- list, new_child will simply be added to the list -- (feature preserves child_nodes cursor) require new_child_not_void: new_child /= Void local c: CURSOR do if child = Void or else not child_nodes.has (child) then child_nodes.extend (new_child) else c := child_nodes.cursor child_nodes.start child_nodes.search (child) check not child_nodes.exhausted end child_nodes.replace (new_child) child_nodes.go_to (c) end ensure child_nodes.has (new_child) end to_xml_internal (pretty_print: BOOLEAN; indent_depth: INTEGER) : STRING is -- generates the string for the xml content do Result := "" end get_indent_string (indent_depth: INTEGER) : STRING is -- local i: INTEGER do create Result.make_empty from i := 0 until i >= indent_depth loop Result.append_character ('%T') i := i + 1 end end feature -- Debug debug_out : STRING is -- debug output do Result := debug_out_impl ("") end feature {XML_NODE} -- Debug debug_out_impl (pre: STRING) : STRING is -- do create Result.make_empty Result.append (pre + node_type + ": " + name.name + "%N") from attributes.start until attributes.off loop Result.append (attributes.item.debug_out_impl (pre + " ")) attributes.forth end from child_nodes.start until child_nodes.off loop Result.append (child_nodes.item.debug_out_impl (pre + " ")) child_nodes.forth end end end