Class: RightAws::RightAWSParser

Inherits:
Object
  • Object
show all
Defined in:
lib/awsbase/right_awsbase.rb

Overview


Constant Summary collapse

@@xml_lib =

:nodoc:

'rexml'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ RightAWSParser

Returns a new instance of RightAWSParser.



352
353
354
355
356
357
358
359
# File 'lib/awsbase/right_awsbase.rb', line 352

def initialize(params={})
  @xmlpath = ''
  @result  = false
  @text    = ''
  @xml_lib = params[:xml_lib] || @@xml_lib
  @logger  = params[:logger]
  reset
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *params) ⇒ Object

Parser must have a lots of methods (see /usr/lib/ruby/1.8/rexml/parsers/streamparser.rb) We dont need most of them in RightAWSParser and method_missing helps us to skip their definition



415
416
417
418
419
420
421
422
# File 'lib/awsbase/right_awsbase.rb', line 415

def method_missing(method, *params)
    # if the method is one of known - just skip it ...
  return if [:comment, :attlistdecl, :notationdecl, :elementdecl, 
             :entitydecl, :cdata, :xmldecl, :attlistdecl, :instruction, 
             :doctype].include?(method)
    # ... else - call super to raise an exception
  super(method, params)
end

Instance Attribute Details

#resultObject

Returns the value of attribute result.



348
349
350
# File 'lib/awsbase/right_awsbase.rb', line 348

def result
  @result
end

#xml_libObject

Returns the value of attribute xml_lib.



350
351
352
# File 'lib/awsbase/right_awsbase.rb', line 350

def xml_lib
  @xml_lib
end

#xmlpathObject (readonly)

Returns the value of attribute xmlpath.



349
350
351
# File 'lib/awsbase/right_awsbase.rb', line 349

def xmlpath
  @xmlpath
end

Class Method Details

.xml_libObject

xml library name: ‘rexml’ | ‘libxml’



341
342
343
# File 'lib/awsbase/right_awsbase.rb', line 341

def self.xml_lib
  @@xml_lib
end

.xml_lib=(new_lib_name) ⇒ Object



344
345
346
# File 'lib/awsbase/right_awsbase.rb', line 344

def self.xml_lib=(new_lib_name)
  @@xml_lib = new_lib_name
end

Instance Method Details

#parse(xml_text, params = {}) ⇒ Object

Parser method. Params:

xml_text         - xml message text(String) or Net:HTTPxxx instance (response)
params[:xml_lib] - library name: 'rexml' | 'libxml'


378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
# File 'lib/awsbase/right_awsbase.rb', line 378

def parse(xml_text, params={})
    # Get response body
  xml_text = xml_text.body unless xml_text.is_a?(String)
  @xml_lib = params[:xml_lib] || @xml_lib
    # load xml library
  if @xml_lib=='libxml' && !defined?(XML::SaxParser)
    begin
      require 'xml/libxml' 
    rescue LoadError => e
      @xml_lib = 'rexml'
      if @logger
        @logger.error e.inspect
        @logger.error e.backtrace
        @logger.info "Can not load 'libxml' library. 'Rexml' is used for parsing."
      end
    end
  end
    # Parse the xml text
  case @xml_lib
  when 'libxml'  
    xml = XML::SaxParser.new
    xml.string = xml_text
    xml.on_start_element{|name, attr_hash| self.tag_start(name, attr_hash)}
    xml.on_characters{   |text|            self.text(text)}
    xml.on_end_element{  |name|            self.tag_end(name)}
    xml.parse
  else
    if @logger && @xml_lib!='rexml'
      @logger.info "RightAWSParser#parse: Unknown xml library ('#{@xml_lib}') is selected. The default 'rexml' is used."
    end
    REXML::Document.parse_stream(xml_text, self)
  end
end

#resetObject

the functions to be overriden by children (if nessesery)



424
# File 'lib/awsbase/right_awsbase.rb', line 424

def reset                     ; end

#tag_end(name) ⇒ Object



365
366
367
368
369
# File 'lib/awsbase/right_awsbase.rb', line 365

def tag_end(name)
  @xmlpath[/^(.*?)\/?#{name}$/]
  @xmlpath = $1
  tagend(name)
end

#tag_start(name, attributes) ⇒ Object



360
361
362
363
364
# File 'lib/awsbase/right_awsbase.rb', line 360

def tag_start(name, attributes)
  @text = ''
  tagstart(name, attributes)
  @xmlpath += @xmlpath.empty? ? name : "/#{name}"
end

#tagend(name) ⇒ Object



426
# File 'lib/awsbase/right_awsbase.rb', line 426

def tagend(name)              ; end

#tagstart(name, attributes) ⇒ Object



425
# File 'lib/awsbase/right_awsbase.rb', line 425

def tagstart(name, attributes); end

#tagtext(text) ⇒ Object



427
# File 'lib/awsbase/right_awsbase.rb', line 427

def tagtext(text)             ; end

#text(text) ⇒ Object



370
371
372
373
# File 'lib/awsbase/right_awsbase.rb', line 370

def text(text)
  @text = text
  tagtext(text)
end