Method: MultiSAX::SAX#parse

Defined in:
lib/multisax.rb

#parse(source, listener) ⇒ Object

The main parsing method. Listener can be Class.newMultiSAX::Callbacks.new. Returns the listener after SAX is applied. If you have not called open(), this will call it using default value (all libraries).

From 0.0.1, source can be IO as well as String.
SAX's listeners are usually modified destructively.
So instances shouldn't be provided.


275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
# File 'lib/multisax.rb', line 275

def parse(source,listener)
  if !@parser && !open
    raise "Failed to open SAX library. REXML, which is a standard Ruby module, might be also corrupted."
  end
  saxhelper=@saxhelper.new.__init__(listener)
  if source.is_a?(String)
    case @parser
      when :ox           then Ox.sax_parse(saxhelper,StringIO.new(source),:convert_special=>true)
      when :oxhtml       then Ox.sax_parse(saxhelper,StringIO.new(source),:convert_special=>true,:smart=>true)
      when :libxml       then parser=LibXML::XML::SaxParser.string(source);parser.callbacks=saxhelper;parser.parse
      when :nokogiri     then parser=Nokogiri::XML::SAX::Parser.new(saxhelper);parser.parse(source)
      when :nokogirihtml then parser=Nokogiri::HTML::SAX::Parser.new(saxhelper);parser.parse(source)
      when :xmlparser    then saxhelper.parse(source)
      when :oga          then parser=Oga::XML::SaxParser.new(saxhelper,source);parser.parse
      when :ogahtml      then parser=Oga::HTML::SaxParser.new(saxhelper,source);parser.parse
      when :xerces       then parser=XercesR::SAXParser.new;parser.setDocumentHandler(saxhelper);parser.parsebuf(source)
      when :rexmlstream  then REXML::Parsers::StreamParser.new(source,saxhelper).parse
      when :rexmlsax2    then parser=REXML::Parsers::SAX2Parser.new(source);parser.listen(saxhelper);parser.parse
    end
  else
    case @parser
      when :ox           then Ox.sax_parse(saxhelper,source,:convert_special=>true)
      when :oxhtml       then Ox.sax_parse(saxhelper,source,:convert_special=>true,:smart=>true)
      when :libxml       then parser=LibXML::XML::SaxParser.io(source);parser.callbacks=saxhelper;parser.parse
      when :nokogiri     then parser=Nokogiri::XML::SAX::Parser.new(saxhelper);parser.parse(source)
      when :nokogirihtml then parser=Nokogiri::HTML::SAX::Parser.new(saxhelper);parser.parse(source.read) # fixme: nokogirihtml IO doesn't allow errors.
      when :xmlparser    then saxhelper.parse(source)
      when :oga          then parser=Oga::XML::SaxParser.new(saxhelper,source);parser.parse
      when :ogahtml      then parser=Oga::HTML::SaxParser.new(saxhelper,source);parser.parse
      when :xerces       then parser=XercesR::SAXParser.new;parser.setDocumentHandler(saxhelper);parser.parsebuf(source.read)
      when :rexmlstream  then REXML::Parsers::StreamParser.new(source,saxhelper).parse
      when :rexmlsax2    then parser=REXML::Parsers::SAX2Parser.new(source);parser.listen(saxhelper);parser.parse
    end
  end
  listener
end