-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgen_classes_from_xml.rb
67 lines (55 loc) · 1.51 KB
/
gen_classes_from_xml.rb
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
require 'net/http'
require 'rexml/document'
# spec data
url = 'https://raw.github.com/Matroska-Org/foundation-source/master/spectool/specdata.xml'
# get the XML data as a string
xml_data = Net::HTTP.get_response(URI.parse(url)).body
# parse it as XML
doc = REXML::Document.new(xml_data)
puts <<'EOF'
#!/usr/bin/ruby
#
# Distributed under the MIT license
# see the file COPYING for details
# or visit http://www.opensource.org/licenses/mit-license.php
#
# \$Id\$
#
# Classes for the "Matroska EBML doc type". Generated with
# gen_classes_from_xml.rb.
#
# Written by Moritz Bunkus <[email protected]>.
#
# This file is generated automatically by the
# gen_classes_from_xml.rb script and should not be
# modified manually!
require "ebml"
module Matroska
EOF
classnames = {
'master' => 'Ebml::Master',
'uinteger' => 'Ebml::UInteger',
'integer' => 'Ebml::SInteger',
'string' => 'Ebml::String',
'utf-8' => 'Ebml::UnicodeString',
'binary' => 'Ebml::Binary',
'float' => 'Ebml::Float',
'date' => 'Ebml::Date'
}
# extract each element and convert to a class definition
doc.elements.each('table/element') do |ele|
name = ele.attributes['name'].sub('-', '_')
type = ele.attributes['type']
parent = classnames[type]
id = Integer(ele.attributes['id'])
length = case when id<=0xFF then 1 when id<=0xFFFF then 2 when id<=0xFFFFFF then 3 else 4 end
puts <<"EOF"
class #{name} < #{parent}
def #{name}.global_id
@@id ||= Ebml::Id.new(0x#{id.to_s(16)}, #{length})
return @@id
end
end
EOF
end
puts "end"