indexing description: "Eiffel code generator for DOM structuros" project: "Project Goanna " library: "XMLE Tool" date: "$Date$" revision: "$Revision$" author: "Glenn Maughan " copyright: "Copyright (c) 2001 Glenn Maughan and others" license: "Eiffel Forum Freeware License v1 (see forum.txt)." class CODE_GENERATOR inherit DOM_NODE_TYPE export {NONE} all end creation make feature -- Initialisation make (name: STRING) is -- Create a new code generator to produce XMLE classes. require name_exists: name /= Void do -- construct all required names document_name := name create class_name.make_from_string (document_name + Xmle_class_name_extension) class_name.to_upper create class_file_name.make_from_string (class_name + Xmle_class_extension) class_file_name.to_lower create bdom_file_name.make_from_string (class_name + Xmle_dom_storage_extension) bdom_file_name.to_lower end feature -- Generation generate (doc: DOM_DOCUMENT) is -- Create an Eiffel class to represent 'doc' require doc_exists: doc /= Void do document := doc build_xmle_document_wrapper build_xmle_document_class store_document end feature {NONE} -- Implementation Xmle_class_name_extension: STRING is "_XMLE" Xmle_document_type: STRING is "XMLE_DOCUMENT" Xmle_class_extension: STRING is ".e" Xmle_dom_storage_extension: STRING is ".bdom" document_name: STRING -- Generic name of document. class_name: STRING -- Class type of generated XMLE class. class_file_name: STRING -- Name of file to store XMLE wrapper class. bdom_file_name: STRING -- Name of file to store binary representation of document. document_wrapper: XMLE_DOCUMENT_WRAPPER -- The document storage wrapper document: DOM_DOCUMENT -- The document to produce xmle_document_class: EIFFEL_CLASS -- The Eiffel code representation. build_xmle_document_wrapper is -- Construct the wrapper object. do create document_wrapper.make (document) end build_xmle_document_class is -- Generate the code for the XMLE document wrapper class. local dest: IO_MEDIUM do create xmle_document_class.make (class_name) build_indexing_clause build_inheritance_clause build_creation_routine build_bdom_file_name_constant build_id_node_functions create {PLAIN_TEXT_FILE} dest.make_open_write (class_file_name) xmle_document_class.write (dest) dest.close end build_indexing_clause is -- Generate indexing clause do xmle_document_class.add_indexing_clause ("description: %"XMLE document wrapper for " + document_name + " document%"") xmle_document_class.add_indexing_clause ("note: %"Automatically generated by Goanna XMLE%"") end build_inheritance_clause is -- Build inheritance clause do xmle_document_class.add_parent (Xmle_document_type) end build_creation_routine is -- build creation clause and creation routines do xmle_document_class.add_creation_procedure_name ("make") end build_bdom_file_name_constant is -- build the constant holding the name of the binary DOM file local group: EIFFEL_FEATURE_GROUP attr: EIFFEL_ATTRIBUTE do create group.make ("Implementation") xmle_document_class.add_feature_group (group) create attr.make ("bdom_file_name", "STRING") attr.set_value ("%"" + bdom_file_name + "%"") group.add_feature (attr) end build_id_node_functions is -- Create a function for each node with an id, as stored in id_nodes table local group: EIFFEL_FEATURE_GROUP routine: EIFFEL_ROUTINE cursor: DS_HASH_TABLE_CURSOR [DOM_NODE, STRING] pair: DS_PAIR [STRING, STRING] do create group.make ("Access") xmle_document_class.add_feature_group (group) -- iterate through all stored id nodes and create an access routine for each from cursor := document_wrapper.id_nodes.new_cursor cursor.start until cursor.off loop create routine.make ("get_node_" + cursor.key) routine.set_type (class_type_for_node (cursor.item.node_type)) routine.add_body_line ("Result ?= wrapper.get_node_by_id (%"" + cursor.key + "%")") create pair.make ("element_exists", "Result /= Void") routine.add_postcondition (pair) group.add_feature (routine) cursor.forth end end store_document is -- Store the document object structure in the file 'bdom_file_name'. do document_wrapper.store_by_name (bdom_file_name) end class_type_for_node (node_type: INTEGER): STRING is -- Return class type for 'node_type' do inspect node_type when Attribute_node then Result := "DOM_ATTRIBUTE" when Cdata_section_node then Result := "DOM_CDATA_SECTION" when Comment_node then Result := "DOM_COMMENT" when Document_fragment_node then Result := "DOM_DOCUMENT_FRAGMENT" when Document_node then Result := "DOM_DOCUMENT" when Document_type_node then Result := "DOM_DOCUMENT_TYPE" when Element_node then Result := "DOM_ELEMENT" when Entity_node then Result := "DOM_ENTITY" when Entity_reference_node then Result := "DOM_ENTITY_REFERENCE" when Notation_node then Result := "DOM_NOTATION" when Processing_instruction_node then Result := "DOM_PROCESSING_INSTRUCTION" when Text_node then Result := "DOM_TEXT" else Result := "DOM_NODE" end end end -- class CODE_GENERATOR