indexing description: "Objects that ..." author: "" date: "$Date$" revision: "$Revision$" class XML_DOCUMENT inherit XML_NODE redefine to_xml_internal end create make feature -- Creation make is -- do make_node (Current, Void, "", "", "!DOCUMENT!") child_nodes.insert_actions.extend (agent on_child_inserted (?)) has_error := False end feature -- Access file_name: STRING xml_declaration: XML_DECLARATION set_xml_declaration (xml_decl: XML_DECLARATION) is -- sets the xml declaration for this document do xml_declaration := xml_decl end document_element : XML_ELEMENT is -- gets the documents root element local c: LINKED_LIST_CURSOR[XML_NODE] do c := child_nodes.cursor from child_nodes.start until Result /= Void or child_nodes.off loop if child_nodes.item.node_type.is_equal ("XML_ELEMENT") then Result ?= child_nodes.item end child_nodes.forth end child_nodes.go_to (c) end has_error: BOOLEAN last_error: STRING feature -- Access from {XML_NODE} node_type : STRING is -- gets the node type do Result := "XML_DOCUMENT" end feature -- Node Creation create_attribute (a_name: STRING) : XML_ATTRIBUTE is -- creates a new xml attribute node do create Result.make_node (Current, Void, "", Void, a_name) end create_xml_declaration (version, encoding: STRING) : XML_DECLARATION is -- creates a new xml declaration node require valid_version: version /= Void and not version.is_empty local attr: XML_ATTRIBUTE do create Result.make_node (Current, Void, "", Void, "?xml?") attr := create_attribute ("version") attr.set_value (version) Result.attributes.extend (attr) if encoding /= Void and not encoding.is_empty then attr := create_attribute ("encoding") attr.set_value (encoding) Result.attributes.extend (attr) end end create_element (a_name: STRING) : XML_ELEMENT is -- creates a new xml element node do create Result.make_node (Current, Void, "", Void, a_name) end create_text (a_text: STRING) : XML_TEXT is -- creates a new xml text node do create Result.make_node (Current, Void, "", Void, "text") Result.set_value (a_text) end create_cdata_section (some_data: STRING) : XML_CDATA_SECTION is -- creates a new CDATA section do create Result.make_node (Current, Void, "", Void, "CDATA") Result.set_value (some_data) end feature -- Duplication / Import import_node (node: XML_NODE; deep: BOOLEAN) : XML_NODE is -- imports the `node' into the current document (recursively if `deep' is true) do Result := node.clone_node (deep) --Result.set_ end clone_node (deep: BOOLEAN) : like Current is -- creates a clone of the current node local c: LINKED_LIST_CURSOR[XML_NODE] do create Result.make if deep then if xml_declaration /= Void then Result.set_xml_declaration (xml_declaration.clone_node (deep)) end c := child_nodes.cursor from child_nodes.start until child_nodes.after loop Result.child_nodes.append (child_nodes.item.clone_node (deep)) child_nodes.forth end child_nodes.go_to (c) end end feature {XML_NODE} -- Implementation to_xml_internal (pretty_print: BOOLEAN; indent_depth: INTEGER) : STRING is -- generates the string for the xml content do create Result.make_empty Result.append (xml_declaration.to_xml_internal (pretty_print, indent_depth)) if pretty_print then Result.append ("%N") end Result.append (document_element.to_xml_internal (pretty_print, indent_depth)) end feature -- Initialization load_from_file (path: STRING) is -- local an_expat_parser_factory: XM_EXPAT_PARSER_FACTORY parser: XM_PARSER doc_builder: DOCUMENT_BUILDER file: KL_TEXT_INPUT_FILE do file_name := path create name.make ("", "!DOCUMENT! (" + file_name + ")", Current) create an_expat_parser_factory if an_expat_parser_factory.is_expat_parser_available then parser := an_expat_parser_factory.new_expat_parser else create {XM_EIFFEL_PARSER} parser.make end parser.set_string_mode_latin1 create doc_builder.make_doc (Current) parser.set_callbacks (doc_builder) create file.make (path) file.open_read -- Parse and display result parser.parse_from_stream (file) if not parser.is_correct then has_error := True last_error := parser.last_error_extended_description end end load_from_string (xstring: STRING) is -- local an_expat_parser_factory: XM_EXPAT_PARSER_FACTORY parser: XM_PARSER doc_builder: DOCUMENT_BUILDER do create name.make ("", "!DOCUMENT!", Current) create an_expat_parser_factory if an_expat_parser_factory.is_expat_parser_available then parser := an_expat_parser_factory.new_expat_parser else create {XM_EIFFEL_PARSER} parser.make end parser.set_string_mode_latin1 create doc_builder.make_doc (Current) parser.set_callbacks (doc_builder) -- Parse and display result parser.parse_from_string (xstring) if not parser.is_correct then has_error := True last_error := parser.last_error_extended_description end end save_to_file (path: STRING; pretty_print: BOOLEAN) is -- saves the current xml document to the given path local file: KL_TEXT_OUTPUT_FILE do file_name := path create file.make (path) file.open_write if file.is_open_write then file.put_string (to_xml (pretty_print)) file.flush file.close else has_error := True last_error := "Unable to open file for writing: " + path end end feature {NONE} -- Implementation on_child_inserted (node: XML_NODE) is -- gets called whenever a child node is inserted into the child_nodes list local decl: XML_DECLARATION do if node.node_type.is_equal ("XML_DECLARATION") and xml_declaration = Void then decl ?= node set_xml_declaration (decl) end end end