Method: REXML::IOSource#initialize

Defined in:
lib/extensions/rexml/rexml/source.rb,
lib/extensions/rhoxml/rexml/source.rb

#initialize(arg, block_size = 500, encoding = nil) ⇒ IOSource

block_size has been deprecated



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
# File 'lib/extensions/rexml/rexml/source.rb', line 138

def initialize(arg, block_size=500, encoding=nil)
  @er_source = @source = arg
  @to_utf = false

  # Determining the encoding is a deceptively difficult issue to resolve.
  # First, we check the first two bytes for UTF-16.  Then we
  # assume that the encoding is at least ASCII enough for the '>', and
  # we read until we get one of those.  This gives us the XML declaration,
  # if there is one.  If there isn't one, the file MUST be UTF-8, as per
  # the XML spec.  If there is one, we can determine the encoding from
  # it.
  @buffer = ""
  str = @source.read( 2 ) || ''
  if encoding
    self.encoding = encoding
  elsif str[0,2] == "\xfe\xff"
    @line_break = "\000>"
  elsif str[0,2] == "\xff\xfe"
    @line_break = ">\000"
  elsif str[0,2] == "\xef\xbb"
    str += @source.read(1)
    str = '' if (str[2,1] == "\xBF")
    @line_break = ">"
  else
    @line_break = ">"
  end
  super( @source.eof? ? str : str+@source.readline( @line_break ) )

  if !@to_utf and
      @buffer.respond_to?(:force_encoding) and
      @source.respond_to?(:external_encoding) and
      @source.external_encoding != "UTF-8" #::Encoding::UTF_8
    @force_utf8 = true
  else
    @force_utf8 = false
  end
end