/
java_pull_parser.rb
183 lines (166 loc) · 4.92 KB
/
java_pull_parser.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
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
171
172
173
174
175
176
177
178
179
180
181
182
183
require 'rexml/parseexception'
module JREXML
begin
XmlPullParser = Java::org.xmlpull.v1.XmlPullParser
rescue
raise LoadError, "JREXML is only for JRuby" if RUBY_PLATFORM !~ /java/
XPP_VERSION = "1.1.4"
begin
require "xpp3-#{XPP_VERSION}.jar"
@triedxpp ||= 0
@triedxpp += 1
retry unless @triedxpp > 1
rescue LoadError
raise LoadError, "Unable to load XmlPullParser java class; " +
"you need to include xpp3-#{XPP_VERSION}.jar on the classpath"
end
end
START_DOCUMENT = XmlPullParser::START_DOCUMENT
END_DOCUMENT = XmlPullParser::END_DOCUMENT
START_TAG = XmlPullParser::START_TAG
END_TAG = XmlPullParser::END_TAG
TEXT = XmlPullParser::TEXT
CDSECT = XmlPullParser::CDSECT
COMMENT = XmlPullParser::COMMENT
ENTITY_REF = XmlPullParser::ENTITY_REF
IGNORABLE_WHITESPACE = XmlPullParser::IGNORABLE_WHITESPACE
PROCESSING_INSTRUCTION = XmlPullParser::PROCESSING_INSTRUCTION
ADJACENT_EVENTS = [TEXT, ENTITY_REF]
module JavaPullParser
def self.factory
@factory ||= proc do
fact = org.xmlpull.v1.XmlPullParserFactory.newInstance
fact.set_namespace_aware false
fact.set_validating false
fact
end.call
end
def stream=(source)
enc = source.encoding if source.respond_to?(:encoding)
@source = JavaPullParser.factory.newPullParser
def @source.buffer; ""; end # In case REXML tries to call #buffer on our source
@source.setInput java.io.ByteArrayInputStream.new(get_bytes(source)), enc
end
# Returns true if there are no more events
def empty?
event_stack.empty?
end
# Returns true if there are more events. Synonymous with !empty?
def has_next?
!empty?
end
# Push an event back on the head of the stream. This method
# has (theoretically) infinite depth.
def unshift(event)
@event_stack ||= []
@event_stack.unshift event
end
def peek(depth = 0)
raise "not implemented"
end
def pull
event = event_stack.shift
unless @first_event_seen
@first_event_seen = true
version = @source.getProperty("http://xmlpull.org/v1/doc/properties.html#xmldecl-version")
if version
standalone = @source.getProperty("http://xmlpull.org/v1/doc/properties.html#xmldecl-standalone")
encoding = @source.getInputEncoding
unshift event
return [:xmldecl, version, encoding, standalone]
end
end
convert_event(event)
end
def all_events
events = []
while event = pull
events << event
end
events
end
private
def convert_event(event)
if ADJACENT_EVENTS.include?(event)
text = ""
loop do
case event
when TEXT
text << @source.text
when ENTITY_REF
val = @source.text
if val
text << val
else
text << "&#{@source.name};"
end
end
event = event_stack.shift
break unless event
if !ADJACENT_EVENTS.include?(event)
unshift event
return [:text, text]
end
end
end
convert_event_without_text_or_entityref(event)
end
def convert_event_without_text_or_entityref(event)
case event
when START_DOCUMENT
[:start_document]
when END_DOCUMENT
@document_ended = true
[:end_document]
when START_TAG
attributes = {}
0.upto(@source.attribute_count - 1) do |i|
attributes[@source.getAttributeName(i)] = @source.getAttributeValue(i)
end
[:start_element, @source.name, attributes]
when END_TAG
[:end_element, @source.name]
when IGNORABLE_WHITESPACE
[:text, @source.text]
when CDSECT
[:cdata, @source.text]
when COMMENT
[:comment, @source.text]
when PROCESSING_INSTRUCTION
pi_info = @source.text.split(/ /, 2)
pi_info[1] = " #{pi_info[1]}" # REXML likes the space there
[:processing_instruction, *pi_info]
when nil
nil
else
[:unknown, debug_event(event)]
end
end
def event_stack
@event_stack ||= []
if @event_stack.empty? && !@document_ended
begin
@event_stack << @source.nextToken
rescue NativeException => e
raise REXML::ParseException, e.message
end
end
@event_stack
end
def get_bytes(src)
src = src.buffer if src.respond_to?(:buffer) # REXML::Source crap
string = if src.respond_to?(:read)
src.read
else
src.to_s
end
string.to_java_bytes
end
def debug_event(event)
"XmlPullParser::#{XmlPullParser::TYPES[event]}" if event
end
def using_jrexml?
true
end
end
end