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
|
# File 'lib/morpheus.rb', line 4
def self.transform(data, options = {})
unless data.respond_to?(:root)
raise_err "Input data must be a Nokogiri document or similar", options
end
options[:src_type] ||= data.root.name
options[:src_format] ||= "xml"
options[:tgt_type] ||= options[:src_type]
options[:tgt_format] ||= options[:src_format]
[:src_type, :src_format, :tgt_type, :tgt_format].each do |k|
options[k] = options[k].to_s
end
if options[:src_type] == options[:tgt_type] and
options[:src_format] == options[:tgt_format]
return data end
transformation_seq = find_transform_seqs(
options[:dir] || "#{RAILS_ROOT}/app/transforms",
options[:src_type],
options[:src_format],
options[:tgt_type],
options[:tgt_format]
)
unless transformation_seq
raise_err "No transformation sequence found", options
end
transformation_seq.each do |path|
context = options[:context] || Object.new
xslt_context = context_to_xml(context).doc
xslt_str = File.read(path)
if path.end_with?(".haml")
xslt_str = Haml::Engine.new(xslt_str).render(context)
end
xslt = Nokogiri::XSLT(xslt_str)
xslt_env = Nokogiri::XML::Document.new
xslt_env.root = Nokogiri::XML::Node.new("env", xslt_env)
xslt_env.root << xslt_context.root
xslt_env.root << data.root
data = Nokogiri::XML(xslt.transform(xslt_env).to_s)
raise_err "XSLT processing using #{path} failed", options unless data.root
end
return data
end
|