/
hqmf.rake
170 lines (115 loc) · 5.58 KB
/
hqmf.rake
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
require 'pathname'
require 'fileutils'
require 'json'
require 'cqm-parsers'
namespace :hqmf do
desc 'Parse all xml files to JSON and save them to ./tmp'
task :parse_all, [:path, :version] do |t, args|
raise "You must specify the HQMF XML file path to convert" unless args.path
FileUtils.mkdir_p File.join(".","tmp",'json','all')
path = File.expand_path(args.path)
version = args.version || HQMF::Parser::HQMF_VERSION_1
Dir.glob(File.join(path,'**','*.xml')) do |measure_def|
puts "####################################"
puts "### processing: #{measure_def}..."
puts "####################################"
if (version == HQMF::Parser::HQMF_VERSION_1)
doc = HQMF::Parser::V1Parser.new.parse(File.open(file).read, version)
else
doc = HQMF::Parser::V2Parser.new.parse(File.open(file).read, version)
end
filename = Pathname.new(measure_def).basename
File.open(File.join(".","tmp",'json','all',"#{filename}.json"), 'w') {|f| f.write(doc.to_json.to_json(max_nesting: 100).gsub(/",/,"\",\n")) }
puts "\n"
end
end
desc 'Parse specified xml file to JSON and save it to ./tmp'
task :parse, [:file,:version] do |t, args|
FileUtils.mkdir_p File.join(".","tmp",'json')
raise "You must specify the HQMF XML file to convert" unless args.file
version = args.version || HQMF::Parser::HQMF_VERSION_1
file = File.expand_path(args.file)
filename = Pathname.new(file).basename
if (version == HQMF::Parser::HQMF_VERSION_1)
doc = HQMF::Parser::V1Parser.new.parse(File.open(file).read, version)
else
doc = HQMF::Parser::V2Parser.new.parse(File.open(file).read, version)
end
outfile = File.join(".","tmp",'json',"#{filename}.json")
File.open(outfile, 'w') {|f| f.write(JSON.pretty_generate(doc.to_json, max_nesting: 100)) }
puts "wrote result to: #{outfile}"
end
desc 'Parse specified xml file to V1 JSON and save it to ./tmp'
task :parse_v1, [:file] do |t, args|
FileUtils.mkdir_p File.join(".","tmp",'json')
raise "You must specify the HQMF XML file to convert" unless args.file
file = File.expand_path(args.file)
filename = Pathname.new(file).basename
doc = HQMF1::Document.new(File.open(file).read).to_json
outfile = File.join(".","tmp",'json',"#{filename}_v1.json")
File.open(outfile, 'w') {|f| f.write(JSON.pretty_generate(doc.to_json, max_nesting: 100)) }
puts "wrote result to: #{outfile}"
end
desc 'Convert V1 JSON to V2 JSON and save it to ./tmp'
task :convert, [:file] do |t, args|
FileUtils.mkdir_p File.join(".","tmp",'json')
raise "You must specify the V1 JSON file to convert" unless args.file
file = File.expand_path(args.file)
filename = Pathname.new(file).basename
converted = HQMF::DocumentConverter.convert(JSON.parse(File.open(file).read,:symbolize_names => true))
outfile = File.join(".","tmp",'json',"#{filename}_v2.json")
File.open(outfile, 'w') {|f| f.write(JSON.pretty_generate(converted.to_json, max_nesting: 100)) }
puts "wrote result to: #{outfile}"
end
desc 'Convert specified HQMF V1 xml file to HQMF V2 and save it to ./tmp'
task :upgrade, [:file] do |t, args|
FileUtils.mkdir_p File.join(".","tmp",'xml')
raise "You must specify the HQMF XML file to convert" unless args.file
version = HQMF::Parser::HQMF_VERSION_1
file = File.expand_path(args.file)
filename = Pathname.new(file).basename
doc = HQMF::Parser::V1Parser.new.parse(File.open(file).read, version)
hqmf_xml = HQMF2::Generator::ModelProcessor.to_hqmf(doc)
xml = Nokogiri.XML(hqmf_xml) do |config|
config.default_xml.noblanks
end
outfile = File.join(".","tmp",'xml',"#{filename}")
File.open(outfile, 'w') {|f| f.write(xml.to_xml(:indent => 2)) }
puts "wrote result to: #{outfile}"
end
desc 'Convert specified HQMF V1 xml file to HQMF V2 and save it to ./tmp'
task :upgrade_all, [:dir] do |t, args|
outdir = File.join(".","tmp",'xml','upgrade')
FileUtils.mkdir_p outdir
raise "You must specify the HQMF XML directory to convert" unless args.dir
Dir.glob(File.join(args.dir,'**','*.xml')) do |file|
version = HQMF::Parser::HQMF_VERSION_1
file = File.expand_path(file)
filename = "#{Pathname.new(file).basename('.*')}_R2.xml"
doc = HQMF::Parser::V1Parser.new.parse(File.open(file).read, version)
hqmf_xml = HQMF2::Generator::ModelProcessor.to_hqmf(doc)
xml = Nokogiri.XML(hqmf_xml) do |config|
config.default_xml.noblanks
end
outfile = File.join(outdir,"#{filename}")
File.open(outfile, 'w') {|f| f.write(xml.to_xml(:indent => 2)) }
puts "wrote result to: #{outfile}"
end
end
desc 'Roundtrip specified HQMF V2 xml file through the model and back to HQMF V2 and save it to ./tmp'
task :roundtrip, [:file] do |t, args|
FileUtils.mkdir_p File.join(".","tmp",'xml')
raise "You must specify the HQMF XML file to roundtrip" unless args.file
version = HQMF::Parser::HQMF_VERSION_2
file = File.expand_path(args.file)
filename = Pathname.new(file).basename
doc = HQMF::Parser::V1Parser.new.parse(File.open(file).read, version)
hqmf_xml = HQMF2::Generator::ModelProcessor.to_hqmf(doc)
xml = Nokogiri.XML(hqmf_xml) do |config|
config.default_xml.noblanks
end
outfile = File.join(".","tmp",'xml',"#{filename}")
File.open(outfile, 'w') {|f| f.write(xml.to_xml(:indent => 2)) }
puts "wrote result to: #{outfile}"
end
end