1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
|
require "report/xml_report_util.rb"
ENV['REPORT_DTD'] = "docbook-xml-4.5/docbookx.dtd" unless ENV['REPORT_DTD']
#transfer to absolute path
ENV['REPORT_DTD'] = File.expand_path(ENV['REPORT_DTD']) if File.exist?(ENV['REPORT_DTD'])
# = XMLReport
#
# uses REXML to generate an XML document in DocBook article format
#
# uses Env-Variable _XMLREPORT_DTD_ to specifiy the dtd
#
module Reports
class XMLReport
include REXML
# create new xmlreport
def initialize(title, pubdate=nil, author_firstname = nil, author_surname = nil)
@doc = Document.new
decl = XMLDecl.new
@doc << decl
type = DocType.new('article PUBLIC "-//OASIS//DTD DocBook XML V4.5//EN" "'+ENV['REPORT_DTD']+'"')
@doc << type
@root = Element.new("article")
@doc << @root
article_info = Element.new("articleinfo")
article_info << Reports::XMLReportUtil.text_element("title", title)
author = Element.new("author")
author << Reports::XMLReportUtil.text_element("firstname", author_firstname)
author << Reports::XMLReportUtil.text_element("surname", author_surname)
article_info << author
article_info << Reports::XMLReportUtil.text_element("pubdate", pubdate)
@root << article_info
@resource_path_elements = {}
end
#
# returns the root element of the document
# call-seq:
# get_root_element => REXML::Element
#
def get_root_element
@root
end
# adds a new section to a REXML:Element, returns the section as element
# call-seq:
# add_section(element, title) => REXML::Element
#
def add_section(element, title)
section = Element.new("section")
section << Reports::XMLReportUtil.text_element("title", title)
element << section
return section
end
# adds a new paragraph to a REXML:Element, returns the paragraph as element
# call-seq:
# add_paragraph( element, text ) => REXML::Element
#
def add_paragraph( element, text, literallayout=false )
unless literallayout
para = Reports::XMLReportUtil.text_element("para", text)
element << para
return para
else
literal = Reports::XMLReportUtil.text_element("literallayout", Text.new(text,true))
element << literal
return literal
end
end
# adds a new image to a REXML:Element, returns the figure as element
#
# example: <tt>add_imagefigure( section2, "Nice graph", "/images/graph1.svg", "SVG", "This graph shows..." )</tt>
#
# call-seq:
# add_imagefigure( element, title, path, filetype, caption = nil ) => REXML::Element
#
def add_imagefigure( element, title, path, filetype, caption = nil )
figure = Reports::XMLReportUtil.attribute_element("figure", {"float" => 0})
figure << Reports::XMLReportUtil.text_element("title", title)
media = Element.new("mediaobject")
image = Element.new("imageobject")
imagedata = Reports::XMLReportUtil.attribute_element("imagedata",
{"fileref" => path, "format"=>filetype, "contentwidth" => "6in", "contentdepth"=> "4in"
})#"width" => "6in", "height" => "5in"}) #"contentwidth" => "100%"})
#imagedata = Reports::XMLReportUtil.attribute_element("imagedata",{"width" => "6in", "fileref" => path, "format"=>filetype})
@resource_path_elements[imagedata] = "fileref"
image << imagedata
media << image
media << Reports::XMLReportUtil.text_element("caption", caption) if caption
figure << media
element << figure
return figure
end
def add_image( element, url )
image = Element.new("imageobject")
imagedata = Reports::XMLReportUtil.attribute_element("imagedata",
{"fileref" => url, "format"=>"PNG", "contentwidth" => "2in" }) #PENDING: do not hardcode size
image << imagedata
element << image
return image
end
# adds a table to a REXML:Element, _table_values_ should be a multi-dimensional-array, returns the table as element
#
# call-seq:
# add_table( element, title, table_values, first_row_is_table_header=true ) => REXML::Element
#
def add_table( element, title, table_values, first_row_is_table_header=true, transpose=false, auto_link_urls=true )
raise "table_values is not mulit-dimensional-array" unless table_values && table_values.is_a?(Array) && table_values[0].is_a?(Array)
values = transpose ? table_values.transpose : table_values
table = Reports::XMLReportUtil.attribute_element("table",{"frame" => "none", "colsep" => 1, "rowsep" => 1 })
table << Reports::XMLReportUtil.text_element("title", title)
raise "column count 0" if values.at(0).size < 1
tgroup = Reports::XMLReportUtil.attribute_element("tgroup",{"cols" => values.at(0).size})
table_body_values = values
if first_row_is_table_header
table_head_values = values[0];
table_body_values = values[1..-1];
thead = Element.new("thead")
row = Element.new("row")
table_head_values.each do |v|
entry = Element.new("entry")
if auto_link_urls && v.to_s =~ /^http:\/\//
add_url(entry, v.to_s)
else
entry.text = v.to_s
end
row << entry
end
thead << row
tgroup << thead
end
tbody = Element.new("tbody")
table_body_values.each do |r|
row = Element.new("row")
r.each do |v|
entry = Element.new("entry")
if auto_link_urls && v.to_s =~ /depict/ #PENDING
add_image(entry, v.to_s)
elsif auto_link_urls && v.to_s =~ /^http:\/\//
add_url(entry, v.to_s, v.to_s)
else
entry.text = v.to_s
end
row << entry
end
tbody << row
end
tgroup << tbody
table << tgroup
element << table
return table
end
# adds a list to a REXML:Element, returns the list as element
#
# call-seq:
# add_list( element, list_values ) => REXML::Element
#
def add_list( element, list_values )
list = Element.new("itemizedlist")
list_values.each do |l|
listItem = Element.new("listitem")
add_paragraph(listItem, l.to_s)
list << listItem
end
element << list
return list
end
def add_url (element, url, description=url )
ulink = Element.new("ulink")
ulink.add_attributes({"url" => url})
ulink.text = description
element << ulink
return ulink
end
# writes xml document
def write_to( out = $stdout, resource_path=nil )
#alternativly use base href in html-header
if (resource_path)
@resource_path_elements.each do |k,v|
raise "attribute '"+v+"' not found in element '"+k+"'" unless k.attributes.has_key?(v)
k.add_attribute( v, resource_path.to_s+"/"+k.attributes[v].to_s )
end
end
@doc.write(out,2, true, true)
out.flush
end
# call-seq:
# self.generate_demo_xml_report => Reports::XMLReport
#
def self.generate_demo_xml_report
rep = Reports::XMLReport.new("Demo report", "subtitle" "Fistname", "Surname")
section1 = rep.add_section(rep.get_root_element, "First Section")
rep.add_paragraph(section1, "some text")
rep.add_paragraph(section1, "even more text")
rep.add_imagefigure(section1, "Figure", "http://upload.wikimedia.org/wikipedia/commons/thumb/e/eb/Siegel_der_Albert-Ludwigs-Universit%C3%A4t_Freiburg.svg/354px-Siegel_der_Albert-Ludwigs-Universit%C3%A4t_Freiburg.svg", "SVG", "this is the logo of freiburg university")
section2 = rep.add_section(rep.get_root_element,"Second Section")
rep.add_section(section2,"A Subsection")
rep.add_section(section2,"Another Subsection")
rep.add_url(section2,"www.google.de", "link zu google")
sec3 = rep.add_section(rep.get_root_element,"Third Section")
rep.add_paragraph(sec3, "some \n more text for section 3",true)
rep.add_image(sec3, "http://ambit.uni-plovdiv.bg:8080/ambit2/depict/cdk?search=c1ccccc1")
#vals= [["a", "b", "c"],["a2", "b2", "c2"],["1", "2", "http://3"]]
#rep.add_table(rep.get_root_element, "demo-table", vals)
return rep
end
end
end
#Reports::XMLReport.generate_demo_xml_report.write_to
#puts "\n\n"
#puts REXML::Text.new("hey ho, lets go!\nasdf",false).to_s
|