-
Notifications
You must be signed in to change notification settings - Fork 127
/
xml.rb
executable file
·92 lines (89 loc) · 2.52 KB
/
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
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
# This class converts an entity to a storable XML format.
class Treat::Workers::Formatters::Serializers::XML
# Reauire the Nokogiri XML parser.
require 'nokogiri'
# Serialize an entity tree in XML format.
#
# Options:
# - (String) :file => a file to write to.
def self.serialize(entity, options = {})
options[:file] ||= (entity.id.to_s + '.xml')
if options[:indent].nil?
options = options.merge({:indent => 0})
end
indent = options[:indent]
if options[:indent] == 0
enc = entity.to_s.encoding.to_s.downcase
string = "<?xml version=\"1.0\" " +
"encoding=\"#{enc}\" ?>\n<treat>\n"
else
string = ''
end
spaces = ''
options[:indent].times { spaces << ' ' }
attributes = " id='#{entity.id}'"
if !entity.features.nil? && entity.features.size != 0
attributes << ' '
entity.features.each_pair do |feature, value|
if value.is_a? Treat::Entities::Entity
attributes << "#{feature}='#{value.id}' "
else
value = value.inspect if value.is_a?(Symbol)
attributes << "#{feature}='#{escape(value)}' "
end
end
############ To be refactored
unless entity.edges.empty?
attributes << "edges='"
a = []
entity.edges.each do |edge|
a << ("{target: #{edge.target}, "+
"type: #{edge.type}, " +
"directed: #{edge.directed}, " +
"direction: #{edge.direction}}" )
end
# Structs.
attributes << a.join(',') + "'"
end
############ End of ugly code
end
tag = entity.class.to_s.split('::')[-1].downcase
string += "#{spaces}<#{tag}#{attributes}>"
unless entity.is_a?(Treat::Entities::Token)
string += "\n"
end
if entity.has_children?
options[:indent] += 1
entity.children.each do |child|
string =
string +
serialize(child, options)
end
options[:indent] -= 1
else
string = string + "#{escape(entity.value)}"
end
unless entity.is_a?(Treat::Entities::Token)
string += "#{spaces}"
end
string += "</#{tag}>\n"
if indent == 0
string += "\n</treat>"
if options[:file]
File.open(options[:file], 'w') do |f|
f.write(string)
end
end
end
options[:file]
end
def self.escape(input)
result = input.to_s.dup
result.gsub!("&", "&")
result.gsub!("<", "<")
result.gsub!(">", ">")
result.gsub!("'", "'")
result.gsub!("\"", """)
result
end
end