lib/xamplr/xampl-generator.rb in xamplr-1.2.0 vs lib/xamplr/xampl-generator.rb in xamplr-1.3.15

- old
+ new

@@ -1,14 +1,15 @@ - require 'fileutils' require 'getoptlong' +require 'set' module XamplGenerator require "xamplr" require "xamplr/xampl-hand-generated" require "xamplr/simpleTemplate/simple-template" require "xamplr/graphml-out.rb" + require "xamplr/yuml-out.rb" class Attribute attr_accessor :tag_name end @@ -42,18 +43,18 @@ "xamplr/templates/element_mixed.template", "xamplr/templates/element_simple.template", "xamplr/templates/package.template", ] - def initialize(options = nil, *predefined_elements) + def initialize(options_in = nil, *predefined_elements) @elements_map = {} @xpp = nil @templates = nil @generated_package_map = nil - if options then - @options = options + if options_in then + @options = options_in else @options = Xampl.make(Options) do |options| options.new_index_attribute("id") options.new_index_attribute("pid").persisted = true; @@ -125,25 +126,25 @@ element_stack = [] current_element = nil while not @xpp.endDocument? do case @xpp.nextEvent - when Xampl_PP::START_ELEMENT - element_stack.push current_element unless nil == current_element - current_element = start_element(current_element) - when Xampl_PP::END_ELEMENT - current_element = element_stack.pop - when Xampl_PP::TEXT, - Xampl_PP::CDATA_SECTION, - Xampl_PP::ENTITY_REF - if current_element then - text = @xpp.text - if (nil != @xpp.text) then - text = text.strip - current_element.found_text_content if 0 < text.size + when Xampl_PP::START_ELEMENT + element_stack.push current_element unless nil == current_element + current_element = start_element(current_element) + when Xampl_PP::END_ELEMENT + current_element = element_stack.pop + when Xampl_PP::TEXT, + Xampl_PP::CDATA_SECTION, + Xampl_PP::ENTITY_REF + if current_element then + text = @xpp.text + if (nil != @xpp.text) then + text = text.strip + current_element.found_text_content if 0 < text.size + end end - end end end end def comprehend_from_files(filenames) @@ -222,10 +223,18 @@ FileUtils.mkdir_p(directory_name) unless File.exist?(directory_name) end ensure_templates + module_names = Set.new + @elements_map.each do |ns, elements| + elements.element_child.each do |element| + module_names << element.package + break + end + end + lookup_element = {} @elements_map.each do |ns, elements| elements.element_child.each do |element| lookup_element[element.nstag] = element end @@ -238,10 +247,16 @@ place = find_place(directory_name, element.package) @templates.element = element @templates.package_name = element.package + if element.class_name == element.package then + puts "ERROR: Class #{ element.package } is in a module with the same name -- this NOT going to work" + elsif module_names.member?(element.class_name) + puts "WARNING: a Class and a Module have the same name (#{ element.package }) -- this is highly unlikely to work" + end + @templates.child_modules(place) end end @elements_map.each do |ns, elements| @@ -290,25 +305,36 @@ @elements_map.each_value do |elements| puts elements.pp_xml end end - def print_elements(filename) + def print_elements(base_filename, print_options=nil) + return unless base_filename + return unless print_options + return if 0 == print_options.size + root = Elements.new @elements_map.each_value do |elements| elements.element_child.each do |element| root.children << element end end - File.open(filename, "w") do |out| - root.pp_xml(out) + print_options.each do | print_option | + case print_option + when :schema then + File.open("#{base_filename}.xml", "w") do |out| + root.pp_xml(out) + end + when :graphml then + graphml_out = GraphMLOut.new(@elements_map) + graphml_out.write_graph_ml(base_filename) + when :yuml then + yuml_out = YUMLOut.new(@elements_map) + yuml_out.write_yuml(base_filename) + end end - - graphml_out = GraphMLOut.new(@elements_map) - graphml_out.write_graph_ml(filename) - end def go(args, &eval_context) options = args[:options] if options then @@ -360,11 +386,12 @@ attr_name.downcase! name.gsub!(/[A-Z]/, "_\\&") name.gsub!(/__+/, "_") class_name = "" - name.each("_") do |chunk| + #name.each("_") do |chunk| + name.split("_").each do |chunk| class_name << chunk.capitalize end class_name.gsub!("_", "") return class_name, attr_name @@ -462,10 +489,19 @@ self.persisted = iattr.persisted break end end end + + if self.persisted then + attribute = Attribute.new + attribute.name = 'scheduled_for_deletion_at' + attribute.tag_name = 'scheduled-for-deletion-at' + attribute.namespace = nil + self.add_attribute(attribute) + end + end end end def XamplGenerator.from_command_line(options=nil) @@ -483,33 +519,33 @@ write_elements = nil directory = File.join(".", "tmp") opts.each do |opt, arg| case opt - when "--help" then - puts "--help, -h :: this help message" - puts "--options, -o :: xml file seting the generation options" - puts "--elements, -e :: xml file providing a hint 'schema' (very optional)" - puts "--gen:options, -O :: write an xml file describing the options used (default gen-options.xml)" - puts "--gen:elements, -E :: write an xml file describing the 'schema' (default gen-elements.xml)" - puts "--directory, -o :: where to write the generated files (default #{directory})" - puts "--version, -o :: what version of xampl is this?" - exit - when "--version" then - puts "version 0.0.0" - exit - when "--directory" - directory = arg - when "--options" - puts "sorry, cannot read options yet" - when "--elements" - puts "sorry, cannot read elements yet" - when "--gen:options" - write_options = (arg and (0 < arg.length)) ? arg : "gen-options.xml" - when "--gen:elements" - write_elements = (arg and (0 < arg.length)) ? arg : "gen-elements.xml" - else - puts " #{opt} #{arg}" + when "--help" then + puts "--help, -h :: this help message" + puts "--options, -o :: xml file seting the generation options" + puts "--elements, -e :: xml file providing a hint 'schema' (very optional)" + puts "--gen:options, -O :: write an xml file describing the options used (default gen-options.xml)" + puts "--gen:elements, -E :: write an xml file describing the 'schema' (default gen-elements.xml)" + puts "--directory, -o :: where to write the generated files (default #{directory})" + puts "--version, -o :: what version of xampl is this?" + exit + when "--version" then + puts "version 0.0.0" + exit + when "--directory" + directory = arg + when "--options" + puts "sorry, cannot read options yet" + when "--elements" + puts "sorry, cannot read elements yet" + when "--gen:options" + write_options = (arg and (0 < arg.length)) ? arg : "gen-options.xml" + when "--gen:elements" + write_elements = (arg and (0 < arg.length)) ? arg : "gen-elements.xml" + else + puts " #{opt} #{arg}" end end puts "write options to: #{write_options}" if write_options puts "write elements to: #{write_elements}" if write_elements