-
Notifications
You must be signed in to change notification settings - Fork 0
/
xmlparser.rb
57 lines (53 loc) · 1.11 KB
/
xmlparser.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
class XMLParser
@@stream ||= {}
@@tags ||= {}
def XMLParser.stream
@@stream
end
def XMLParser.tags
@@tags
end
class Tag
attr_accessor :element, :attributes
def initialize(element, attributes)
@element, @attributes = element, attributes
XMLParser.tags[@attributes['id']] = self
end
end
class Stream
@@context ||= []
def initialize(name)
@name, @buffer = name, String.new
XMLParser.stream[@name] = self
end
def push
@@context.push self
end
def pop
@@context.delete self
end
def write(string)
@buffer += string
end
def clear
@buffer = String.new
end
def to_s
@buffer
end
def Stream.current
@@context.last || XMLParser.stream['main']
end
end
CALLBACKS['streamWindow'] = proc { |hash|
s = (@@stream[hash['id']] || Stream.new(hash['id']))
s.push
}
CALLBACKS['pushStream'] = CALLBACKS['streamWindow']
CALLBACKS['popStream'] = proc { |hash|
@@stream[hash['id']].pop
}
CALLBACKS['clearStream'] = proc { |hash|
@@stream[hash['id']].clear
}
end