beardley 1.3.0 → 1.4.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5fb39c7d9e87727cb784ba5a0aa657a958d2dea5
4
- data.tar.gz: 2afddcefe455d1d62763c475115f3636be3916c9
3
+ metadata.gz: 2f58f515d166e39603ec3f4728ec495dfc01d7fe
4
+ data.tar.gz: a4a30e2b36d687576a5b5b6cc14d20d8bda182ef
5
5
  SHA512:
6
- metadata.gz: a64f8932195186124df96d11742b8cc257212368d20d2c67f89ef3eaca05f21bd2dd15d2299023e1009143d279a6ff86892ba1f05b32e46b2fd8286cd84020ad
7
- data.tar.gz: a2a7af806fac157475365bb2e16c83d10e7f195fbba48cf6c62778334cb1b3fbca5b019ef4acce62ec30a2f89f7154f043712c6c8aa6e2941e193d0b0c5ef06a
6
+ metadata.gz: f6b12e641c0392c07aab5cae431909070cce792cfaf564e0d8690a9e07609267a5d705d096a2fa5c588a8aa796f2ec5e1d68265352fcff3eb850c5f1868532bb
7
+ data.tar.gz: f3a7b78287225a60b9a24ca638841a5ae0c4b56e0400e5f71902d4847b9dbef51f9e5397947f57f2aa9401eb6d332007016508d79e46aa9f08e05de4b1d9755c
data/lib/beardley.rb CHANGED
@@ -21,31 +21,29 @@
21
21
  # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
22
  # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
23
 
24
- require "beardley/version"
25
- require "pathname"
26
- require "digest"
27
- require "rjb"
28
- require "rjb-loader"
29
-
24
+ require 'beardley/version'
25
+ require 'pathname'
26
+ require 'digest'
27
+ require 'rjb'
28
+ require 'rjb-loader' if ENV['DISABLED_JAVA'].to_i.zero?
30
29
 
31
30
  module Beardley
32
-
33
31
  class << self
34
32
  attr_accessor :config, :exporters
35
33
 
36
34
  # Changes the level of verbosity
37
35
  def with_warnings(flag = nil)
38
- old_verbose, $VERBOSE = $VERBOSE, flag
36
+ old_verbose = $VERBOSE
37
+ $VERBOSE = flag
39
38
  yield
40
39
  ensure
41
40
  $VERBOSE = old_verbose
42
41
  end
43
-
44
42
  end
45
43
 
46
44
  # Default report params
47
45
  self.config = {
48
- report_params: {}
46
+ report_params: {}
49
47
  }
50
48
  self.exporters = {
51
49
  odt: 'net.sf.jasperreports.engine.export.oasis.JROdtExporter',
@@ -54,21 +52,22 @@ module Beardley
54
52
  docx: 'net.sf.jasperreports.engine.export.ooxml.JRDocxExporter',
55
53
  xlsx: 'net.sf.jasperreports.engine.export.ooxml.JRXlsxExporter'
56
54
  }
57
-
58
- RjbLoader.before_load do |config|
59
- Dir[Pathname.new(__FILE__).dirname.join("..", "vendor", "java", "*.jar")].each do |path|
60
- config.classpath << File::PATH_SEPARATOR + File.expand_path(path)
55
+
56
+ if ENV['DISABLED_JAVA'].to_i.zero?
57
+ RjbLoader.before_load do |config|
58
+ Dir[Pathname.new(__FILE__).dirname.join('..', 'vendor', 'java', '*.jar')].each do |path|
59
+ config.classpath << File::PATH_SEPARATOR + File.expand_path(path)
60
+ end
61
61
  end
62
- end
63
62
 
64
- RjbLoader.after_load do |config|
65
- _Locale = Rjb::import('java.util.Locale')
66
- Beardley.config[:report_params]["REPORT_LOCALE"] = _Locale.new('en', 'US')
67
- Beardley.config[:report_params]["XML_LOCALE"] = _Locale.new('en', 'US')
68
- Beardley.config[:report_params]["XML_DATE_PATTERN"] = "yyyy-MM-dd'T'HH:mm:ss"
69
- Beardley.config[:report_params]["XML_NUMBER_PATTERN"] = '###0.00'
63
+ RjbLoader.after_load do |_config|
64
+ _Locale = Rjb.import('java.util.Locale')
65
+ Beardley.config[:report_params]['REPORT_LOCALE'] = _Locale.new('en', 'US')
66
+ Beardley.config[:report_params]['XML_LOCALE'] = _Locale.new('en', 'US')
67
+ Beardley.config[:report_params]['XML_DATE_PATTERN'] = "yyyy-MM-dd'T'HH:mm:ss"
68
+ Beardley.config[:report_params]['XML_NUMBER_PATTERN'] = '###0.00'
69
+ end
70
70
  end
71
71
 
72
72
  autoload :Report, 'beardley/report'
73
-
74
73
  end
@@ -22,33 +22,33 @@
22
22
  # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
23
 
24
24
  module Beardley
25
-
26
25
  # This class permit to produce reports in PDF, ODT and DOCX using Jasper
27
26
  class Report
28
27
  attr_reader :source_file, :object_file
29
-
28
+
30
29
  # Constructor for a report generator
31
30
  def initialize(report, options = {})
32
31
  @parameters = options.delete(:report) || {}
33
- @source_file, @object_file = nil, nil
32
+ @source_file = nil
33
+ @object_file = nil
34
34
  if report.is_a?(Pathname)
35
- @source_file = report if report.extname.downcase == ".jrxml" or report.extname.downcase == ".xml"
36
- @object_file = report if report.extname.downcase == ".jasper"
35
+ @source_file = report if report.extname.casecmp('.jrxml').zero? || report.extname.casecmp('.xml').zero?
36
+ @object_file = report if report.extname.casecmp('.jasper').zero?
37
37
  elsif report.is_a?(String)
38
38
  hash = Digest::SHA256.hexdigest(report)
39
- @source_file = Pathname.new(options[:tmp_dir] || "/tmp").join("report-" + hash + '.jrxml')
39
+ @source_file = Pathname.new(options[:tmp_dir] || '/tmp').join('report-' + hash + '.jrxml')
40
40
  File.open(@source_file, 'wb') do |f|
41
41
  f.write(report)
42
42
  end
43
43
  end
44
44
  if locale = options.delete(:locale)
45
- locale_parts = locale.split("-")[0..2]
46
- _Locale = Rjb::import('java.util.Locale')
47
- @parameters["REPORT_LOCALE"] = _Locale.new(*locale_parts)
45
+ locale_parts = locale.split('-')[0..2]
46
+ _Locale = Rjb.import('java.util.Locale')
47
+ @parameters['REPORT_LOCALE'] = _Locale.new(*locale_parts)
48
48
  end
49
- @object_file ||= @source_file.dirname.join(@source_file.basename.to_s + ".jasper")
49
+ @object_file ||= @source_file.dirname.join(@source_file.basename.to_s + '.jasper')
50
50
  unless @object_file.is_a?(Pathname)
51
- raise ArgumentError, "An object must be given at least"
51
+ raise ArgumentError, 'An object must be given at least'
52
52
  end
53
53
  end
54
54
 
@@ -56,34 +56,34 @@ module Beardley
56
56
  def to_pdf(*args)
57
57
  options = extract_options!(args)
58
58
  datasource = args[0]
59
- _JasperPrint = Rjb::import('net.sf.jasperreports.engine.JasperPrint')
60
- _JasperExportManager = Rjb::import('net.sf.jasperreports.engine.JasperExportManager')
61
- return _JasperExportManager._invoke('exportReportToPdf', 'Lnet.sf.jasperreports.engine.JasperPrint;', prepare(datasource))
59
+ _JasperPrint = Rjb.import('net.sf.jasperreports.engine.JasperPrint')
60
+ _JasperExportManager = Rjb.import('net.sf.jasperreports.engine.JasperExportManager')
61
+ _JasperExportManager._invoke('exportReportToPdf', 'Lnet.sf.jasperreports.engine.JasperPrint;', prepare(datasource))
62
62
  end
63
63
 
64
64
  # Export report to ODT with given datasource
65
65
  def to_odt(*args)
66
- return to(:odt, *args)
66
+ to(:odt, *args)
67
67
  end
68
68
 
69
69
  # Export report to ODS with given datasource
70
70
  def to_ods(*args)
71
- return to(:ods, *args)
71
+ to(:ods, *args)
72
72
  end
73
73
 
74
74
  # Export report to CSV with given datasource
75
75
  def to_csv(*args)
76
- return to(:csv, *args)
76
+ to(:csv, *args)
77
77
  end
78
78
 
79
79
  # Export report to DOCX with given datasource
80
80
  def to_docx(*args)
81
- return to(:docx, *args)
81
+ to(:docx, *args)
82
82
  end
83
83
 
84
84
  # Export report to XLSX with given datasource
85
85
  def to_xlsx(*args)
86
- return to(:xlsx, *args)
86
+ to(:xlsx, *args)
87
87
  end
88
88
 
89
89
  # Generic method to export to some format like ODT and DOCX as file in the given place
@@ -92,21 +92,27 @@ module Beardley
92
92
  datasource = args[0]
93
93
  path = options[:path] || File.join(Dir.tmpdir, "beardley_#{Time.now.to_i.to_s(36)}_#{rand(100_000_000_000).to_s(36)}.#{format}")
94
94
  if format == :pdf
95
- _JasperPrint = Rjb::import('net.sf.jasperreports.engine.JasperPrint')
96
- _JasperExportManager = Rjb::import('net.sf.jasperreports.engine.JasperExportManager')
97
- _JasperExportManager._invoke('exportReportToPdfFile', 'Lnet.sf.jasperreports.engine.JasperPrint;Ljava.lang.String;', prepare(datasource), Rjb::import('java.lang.String').new(path.to_s))
95
+ _JasperPrint = Rjb.import('net.sf.jasperreports.engine.JasperPrint')
96
+ _JasperExportManager = Rjb.import('net.sf.jasperreports.engine.JasperExportManager')
97
+ _JasperExportManager._invoke('exportReportToPdfFile', 'Lnet.sf.jasperreports.engine.JasperPrint;Ljava.lang.String;', prepare(datasource), Rjb.import('java.lang.String').new(path.to_s))
98
98
  elsif Beardley.exporters[format]
99
- exporter = Beardley.with_warnings { Rjb::import(Beardley.exporters[format]) }.new
100
- _JRExporterParameter = Rjb::import('net.sf.jasperreports.engine.JRExporterParameter')
99
+ exporter = Beardley.with_warnings { Rjb.import(Beardley.exporters[format]) }.new
100
+ _JRExporterParameter = Rjb.import('net.sf.jasperreports.engine.JRExporterParameter')
101
101
  exporter.setParameter(_JRExporterParameter.JASPER_PRINT, prepare(datasource))
102
102
  exporter.setParameter(_JRExporterParameter.OUTPUT_FILE_NAME, path.to_s)
103
103
  exporter.exportReport
104
104
  else
105
105
  raise "Invalid export format: #{format.inspect}"
106
106
  end
107
- return path
107
+ path
108
108
  end
109
109
 
110
+ def compile(xml_path, compiled_path)
111
+ if File.exist?(xml_path)
112
+ _JasperCompileManager = Rjb.import('net.sf.jasperreports.engine.JasperCompileManager')
113
+ _JasperCompileManager.compileReportToFile(xml_path, compiled_path)
114
+ end
115
+ end
110
116
 
111
117
  # XLS and RTF are not suitable anymore
112
118
  # NOTE: JasperReports can not produce DOC files
@@ -118,64 +124,63 @@ module Beardley
118
124
  options = extract_options!(args)
119
125
  datasource = args[0]
120
126
  file = Tempfile.new("to_#{format}")
121
- exporter = Beardley.with_warnings { Rjb::import(Beardley.exporters[format]) }.new
122
- _JRExporterParameter = Rjb::import('net.sf.jasperreports.engine.JRExporterParameter')
127
+ exporter = Beardley.with_warnings { Rjb.import(Beardley.exporters[format]) }.new
128
+ _JRExporterParameter = Rjb.import('net.sf.jasperreports.engine.JRExporterParameter')
123
129
  exporter.setParameter(_JRExporterParameter.JASPER_PRINT, prepare(datasource))
124
130
  exporter.setParameter(_JRExporterParameter.OUTPUT_FILE_NAME, file.path.to_s)
125
131
  exporter.exportReport
126
132
  file.rewind
127
133
  report = file.read
128
134
  file.close(true)
129
- return report
135
+ report
130
136
  end
131
137
 
132
138
  # Extract options from a list of arguments
133
139
  def extract_options!(args)
134
- return (args[-1].is_a?(Hash) ? args.delete_at(-1) : {})
140
+ (args[-1].is_a?(Hash) ? args.delete_at(-1) : {})
135
141
  end
136
142
 
137
143
  # Create object file if not exist and load datasource
138
144
  def prepare(datasource = nil)
139
145
  # Compile it, if needed
140
146
  if @source_file && ((!@object_file.exist? && @source_file.exist?) || (@source_file.exist? && @source_file.mtime > @object_file.mtime))
141
- _JasperCompileManager = Rjb::import('net.sf.jasperreports.engine.JasperCompileManager')
147
+ _JasperCompileManager = Rjb.import('net.sf.jasperreports.engine.JasperCompileManager')
142
148
  _JasperCompileManager.compileReportToFile(@source_file.to_s, @object_file.to_s)
143
149
  end
144
150
  load_datasource(datasource)
145
151
  end
146
152
 
147
-
148
153
  # Build the default parameters Hash for printing
149
154
  def prepare_params
150
- _HashMap = Rjb::import('java.util.HashMap')
151
- _JavaString = Rjb::import('java.lang.String')
155
+ _HashMap = Rjb.import('java.util.HashMap')
156
+ _JavaString = Rjb.import('java.lang.String')
152
157
 
153
158
  # Converting default report params to java HashMap
154
159
  params = _HashMap.new
155
- Beardley.config[:report_params].each do |k,v|
160
+ Beardley.config[:report_params].each do |k, v|
156
161
  params.put(k, v)
157
162
  end
158
-
163
+
159
164
  # Convert the ruby parameters' hash to a java HashMap, but keeps it as
160
165
  # default when they already represent a JRB entity.
161
166
  # Pay attention that, for now, all other parameters are converted to string!
162
167
  @parameters.each do |key, value|
163
168
  params.put(_JavaString.new(key.to_s), parameter_value_of(value))
164
169
  end
165
-
166
- return params
170
+
171
+ params
167
172
  end
168
173
 
169
174
  # Load parseable XML datasource with java component
170
175
  def load_datasource(datasource = nil)
171
176
  jasper_params = prepare_params
172
177
 
173
- # Parse and load XML as datasource
178
+ # Parse and load XML as datasource
174
179
  if datasource
175
- _InputSource = Rjb::import('org.xml.sax.InputSource')
176
- _StringReader = Rjb::import('java.io.StringReader')
177
- _JRXmlUtils = Rjb::import('net.sf.jasperreports.engine.util.JRXmlUtils')
178
- _JRXPathQueryExecuterFactory = Beardley.with_warnings { Rjb::import('net.sf.jasperreports.engine.query.JRXPathQueryExecuterFactory') }
180
+ _InputSource = Rjb.import('org.xml.sax.InputSource')
181
+ _StringReader = Rjb.import('java.io.StringReader')
182
+ _JRXmlUtils = Rjb.import('net.sf.jasperreports.engine.util.JRXmlUtils')
183
+ _JRXPathQueryExecuterFactory = Beardley.with_warnings { Rjb.import('net.sf.jasperreports.engine.query.JRXPathQueryExecuterFactory') }
179
184
  input_source = _InputSource.new
180
185
  input_source.setCharacterStream(_StringReader.new(datasource.to_s))
181
186
  data_document = Beardley.with_warnings do
@@ -185,21 +190,19 @@ module Beardley
185
190
  end
186
191
 
187
192
  # Build JasperPrint
188
- return fill_report(jasper_params, datasource)
193
+ fill_report(jasper_params, datasource)
189
194
  end
190
195
 
191
-
192
196
  # Fill the report with valid method depending on datasource
193
197
  def fill_report(params, datasource = nil)
194
- _JasperFillManager = Rjb::import('net.sf.jasperreports.engine.JasperFillManager')
198
+ _JasperFillManager = Rjb.import('net.sf.jasperreports.engine.JasperFillManager')
195
199
  if datasource
196
200
  return _JasperFillManager.fillReport(@object_file.to_s, params)
197
201
  else
198
- _JREmptyDataSource = Rjb::import('net.sf.jasperreports.engine.JREmptyDataSource')
202
+ _JREmptyDataSource = Rjb.import('net.sf.jasperreports.engine.JREmptyDataSource')
199
203
  return _JasperFillManager.fillReport(@object_file.to_s, params, _JREmptyDataSource.new)
200
204
  end
201
205
  end
202
-
203
206
 
204
207
  # Returns the value without conversion when it's converted to Java Types.
205
208
  # When isn't a Rjb class, returns a Java String of it.
@@ -209,10 +212,8 @@ module Beardley
209
212
  if param.class.parent == Rjb
210
213
  param
211
214
  else
212
- Rjb::import('java.lang.String').new(param.to_s)
215
+ Rjb.import('java.lang.String').new(param.to_s)
213
216
  end
214
217
  end
215
-
216
218
  end
217
-
218
219
  end
@@ -1,10 +1,10 @@
1
1
  module Beardley
2
2
  module VERSION
3
3
  MAJOR = 1
4
- MINOR = 3
5
- PATCH = 0
4
+ MINOR = 4
5
+ PATCH = 1
6
6
  PRE = nil
7
-
8
- STRING = [MAJOR, MINOR, PATCH, PRE].compact.join(".")
7
+
8
+ STRING = [MAJOR, MINOR, PATCH, PRE].compact.join('.')
9
9
  end
10
10
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: beardley
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.0
4
+ version: 1.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brice Texier
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-07-14 00:00:00.000000000 Z
11
+ date: 2017-06-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rjb
@@ -94,6 +94,20 @@ dependencies:
94
94
  - - ">"
95
95
  - !ruby/object:Gem::Version
96
96
  version: '1'
97
+ - !ruby/object:Gem::Dependency
98
+ name: term-ansicolor
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">"
102
+ - !ruby/object:Gem::Version
103
+ version: '1'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">"
109
+ - !ruby/object:Gem::Version
110
+ version: '1'
97
111
  description: Generate reports using JasperReports reporting tool
98
112
  email: burisu@oneiros.fr
99
113
  executables: []
@@ -133,9 +147,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
133
147
  version: '0'
134
148
  requirements: []
135
149
  rubyforge_project:
136
- rubygems_version: 2.4.5
150
+ rubygems_version: 2.5.2
137
151
  signing_key:
138
152
  specification_version: 4
139
153
  summary: JasperReports integration
140
154
  test_files: []
141
- has_rdoc: