lib/reports/Report.rb in taskjuggler-0.0.8 vs lib/reports/Report.rb in taskjuggler-0.0.9

- old
+ new

@@ -1,11 +1,12 @@ #!/usr/bin/env ruby -w # encoding: UTF-8 # # = Report.rb -- The TaskJuggler III Project Management Software # -# Copyright (c) 2006, 2007, 2008, 2009, 2010 by Chris Schlaeger <cs@kde.org> +# Copyright (c) 2006, 2007, 2008, 2009, 2010, 2011 +# by Chris Schlaeger <chris@linux.com> # # This program is free software; you can redistribute it and/or modify # it under the terms of version 2 of the GNU General Public License as # published by the Free Software Foundation. # @@ -44,15 +45,24 @@ end # The generate function is where the action happens in this class. The # report defined by all the class attributes and report elements is # generated according the the requested output format(s). - def generate + # _requestedFormats_ can be a list of formats that should be generated (e. + # g. :html, :csv, etc.). + def generate(requestedFormats = nil) generateIntermediateFormat - # Then generate the actual output format. - get('formats').each do |format| + # We either generate the requested formats or the list of formats that + # was specified in the report definition. + (requestedFormats || get('formats')).each do |format| + if @name.empty? + error('empty_report_file_name', + "Report #{@id} has output formats requested, but the " + + "file name is empty.") + end + case format when :html generateHTML copyAuxiliaryFiles when :csv @@ -130,10 +140,19 @@ get(attribute) end # Generate an HTML version of the report. def generateHTML + return nil unless @content + + unless @content.respond_to?('to_html') + warning('html_not_supported', + "HTML format is not supported for report #{@id} of " + + "type #{@typeSpec}.") + return nil + end + html = HTMLDocument.new(:strict) head = html.generateHead("TaskJuggler Report - #{@name}", 'description' => 'TaskJuggler Report', 'keywords' => 'taskjuggler, project, management') if a('selfcontained') @@ -143,14 +162,13 @@ if auxSrcDir.nil? || !File.exists?(cssFileName) dataDirError(cssFileName, AppConfig.dataSearchDirs('data/css')) end cssFile = IO.read(cssFileName) if cssFile.empty? - raise TjException.new, <<"EOT" -Cannot read '#{cssFileName}'. Make sure the file is not empty and you have -read access permission. -EOT + error('css_file_error', + "Cannot read '#{cssFileName}'. Make sure the file is not " + + "empty and you have read access permission.") end head << XMLElement.new('meta', 'http-equiv' => 'Content-Style-Type', 'content' => 'text/css; charset=utf-8') head << (style = XMLElement.new('style', 'type' => 'text/css')) style << XMLBlob.new("\n" + cssFile) @@ -179,11 +197,11 @@ # Make sure we have some margins around the report. body << (frame = XMLElement.new('div', 'class' => 'tj_page')) - frame << @content.to_html if @content + frame << @content.to_html # The footer with some administrative information. frame << (div = XMLElement.new('div', 'class' => 'copyright')) div << XMLText.new(@project['copyright'] + " - ") if @project['copyright'] div << XMLText.new("Project: #{@project['name']} " + @@ -207,13 +225,20 @@ html.write(fileName) end # Generate a CSV version of the report. def generateCSV + # The CSV format can only handle the first element of a report. return nil unless @content - # CSV format can only handle the first element. + unless @content.respond_to?('to_csv') + warning('csv_not_supported', + "CSV format is not supported for report #{@id} of " + + "type #{@typeSpec}.") + return nil + end + return nil unless (csv = @content.to_csv) # Use the CSVFile class to write the Array of Arrays to a colon # separated file. Write to $stdout if the filename was set to '.'. begin @@ -243,9 +268,16 @@ end end # Generate Niku report def generateNiku + unless @content.respond_to?('to_niku') + warning('niku_not_supported', + "niku format is not supported for report #{@id} of " + + "type #{@typeSpec}.") + return nil + end + begin f = @name == '.' ? $stdout : File.new(((@name[0] == '/' ? '' : @project.outputDir) + @name + '.xml').untaint, 'w') f.puts "#{@content.to_niku}"