Module: JREXML::JavaPullParser

Defined in:
lib/jrexml/java_pull_parser.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.bufferObject

In case REXML tries to call #buffer on our source


46
# File 'lib/jrexml/java_pull_parser.rb', line 46

def @source.buffer; ""; end

.factoryObject


34
35
36
37
38
39
40
41
# File 'lib/jrexml/java_pull_parser.rb', line 34

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

Instance Method Details

#all_eventsObject


86
87
88
89
90
91
92
# File 'lib/jrexml/java_pull_parser.rb', line 86

def all_events
  events = []
  while event = pull
    events << event
  end
  events
end

#empty?Boolean

Returns true if there are no more events

Returns:

  • (Boolean)

51
52
53
# File 'lib/jrexml/java_pull_parser.rb', line 51

def empty?
  event_stack.empty?
end

#has_next?Boolean

Returns true if there are more events. Synonymous with !empty?

Returns:

  • (Boolean)

56
57
58
# File 'lib/jrexml/java_pull_parser.rb', line 56

def has_next?
  !empty?
end

#peek(depth = 0) ⇒ Object


67
68
69
# File 'lib/jrexml/java_pull_parser.rb', line 67

def peek(depth = 0)
  raise "not implemented"
end

#pullObject


71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/jrexml/java_pull_parser.rb', line 71

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

#stream=(source) ⇒ Object


43
44
45
46
47
48
# File 'lib/jrexml/java_pull_parser.rb', line 43

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

#unshift(event) ⇒ Object

Push an event back on the head of the stream. This method has (theoretically) infinite depth.


62
63
64
65
# File 'lib/jrexml/java_pull_parser.rb', line 62

def unshift(event)
  @event_stack ||= []
  @event_stack.unshift event
end