Class: RightAws::RightAWSParser

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

Overview

:nodoc:

Constant Summary collapse

DEFAULT_XML_LIBRARY =

default parsing library

'rexml'
@@supported_xml_libs =

a list of supported parsers

[DEFAULT_XML_LIBRARY, 'libxml']
@@xml_lib =

xml library name: ‘rexml’ | ‘libxml’

DEFAULT_XML_LIBRARY

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ RightAWSParser

Returns a new instance of RightAWSParser.



581
582
583
584
585
586
587
588
# File 'lib/awsbase/right_awsbase.rb', line 581

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



653
654
655
656
657
658
659
660
# File 'lib/awsbase/right_awsbase.rb', line 653

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.



577
578
579
# File 'lib/awsbase/right_awsbase.rb', line 577

def result
  @result
end

#xml_libObject

Returns the value of attribute xml_lib.



579
580
581
# File 'lib/awsbase/right_awsbase.rb', line 579

def xml_lib
  @xml_lib
end

#xmlpathObject (readonly)

Returns the value of attribute xmlpath.



578
579
580
# File 'lib/awsbase/right_awsbase.rb', line 578

def xmlpath
  @xmlpath
end

Class Method Details

.xml_libObject



570
571
572
# File 'lib/awsbase/right_awsbase.rb', line 570

def self.xml_lib
  @@xml_lib
end

.xml_lib=(new_lib_name) ⇒ Object



573
574
575
# File 'lib/awsbase/right_awsbase.rb', line 573

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'


607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
# File 'lib/awsbase/right_awsbase.rb', line 607

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
    # check that we had no problems with this library otherwise use default 
  @xml_lib = DEFAULT_XML_LIBRARY unless @@supported_xml_libs.include?(@xml_lib)       
    # load xml library
  if @xml_lib=='libxml' && !defined?(XML::SaxParser)
    begin
      require 'xml/libxml'
      # is it new ? - Setup SaxParserCallback 
      if XML::Parser::VERSION >= '0.5.1.0'
        RightSaxParserCallback.include_callback 
      end           
    rescue LoadError => e
      @@supported_xml_libs.delete(@xml_lib) 
      @xml_lib = DEFAULT_XML_LIBRARY           
      if @logger
        @logger.error e.inspect
        @logger.error e.backtrace
        @logger.info "Can not load 'libxml' library. '#{DEFAULT_XML_LIBRARY}' is used for parsing." 
      end
    end
  end
    # Parse the xml text
  case @xml_lib
  when 'libxml'  
    xml        = XML::SaxParser.new 
    xml.string = xml_text 
    # check libxml-ruby version 
    if XML::Parser::VERSION >= '0.5.1.0'
      xml.callbacks = RightSaxParserCallback.new(self) 
    else 
      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)} 
    end 
    xml.parse
  else
    REXML::Document.parse_stream(xml_text, self)
  end
end

#resetObject

the functions to be overriden by children (if nessesery)



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

def reset                     ; end

#tag_end(name) ⇒ Object



594
595
596
597
598
# File 'lib/awsbase/right_awsbase.rb', line 594

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

#tag_start(name, attributes) ⇒ Object



589
590
591
592
593
# File 'lib/awsbase/right_awsbase.rb', line 589

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

#tagend(name) ⇒ Object



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

def tagend(name)              ; end

#tagstart(name, attributes) ⇒ Object



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

def tagstart(name, attributes); end

#tagtext(text) ⇒ Object



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

def tagtext(text)             ; end

#text(text) ⇒ Object



599
600
601
602
# File 'lib/awsbase/right_awsbase.rb', line 599

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