Class: RightAws::RightAWSParser

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

Overview

:nodoc:

Direct Known Subclasses

AcfInterface::AcfDistributionConfigParser, AcfInterface::AcfDistributionListParser, AcfInterface::AcfDistributionParser, Ec2::QEc2AllocateAddressParser, Ec2::QEc2AttachAndDetachVolumeParser, Ec2::QEc2BundleInstanceParser, Ec2::QEc2ConfirmProductInstanceParser, Ec2::QEc2CreateKeyPairParser, Ec2::QEc2CreateSnapshotParser, Ec2::QEc2CreateVolumeParser, Ec2::QEc2DescribeAddressesParser, Ec2::QEc2DescribeAvailabilityZonesParser, Ec2::QEc2DescribeBundleTasksParser, Ec2::QEc2DescribeImageAttributeParser, Ec2::QEc2DescribeImagesParser, Ec2::QEc2DescribeInstancesParser, Ec2::QEc2DescribeKeyPairParser, Ec2::QEc2DescribeRegionsParser, Ec2::QEc2DescribeSecurityGroupsParser, Ec2::QEc2DescribeSnapshotsParser, Ec2::QEc2DescribeVolumesParser, Ec2::QEc2GetConsoleOutputParser, Ec2::QEc2RegisterImageParser, Ec2::QEc2TerminateInstancesParser, Ec2::RightBoolResponseParser, RightErrorResponseParser, RightHttp2xxParser, SdbInterface::QSdbGetAttributesParser, SdbInterface::QSdbListDomainParser, SdbInterface::QSdbQueryParser, SdbInterface::QSdbQueryWithAttributesParser, SdbInterface::QSdbSelectParser, SdbInterface::QSdbSimpleParser, SqsGen2Interface::SqsCreateQueueParser, SqsGen2Interface::SqsGetQueueAttributesParser, SqsGen2Interface::SqsListQueuesParser, SqsGen2Interface::SqsReceiveMessageParser, SqsGen2Interface::SqsSendMessagesParser, SqsGen2Interface::SqsStatusParser, SqsInterface::SqsCreateQueueParser, SqsInterface::SqsGetQueueAttributesParser, SqsInterface::SqsGetVisibilityTimeoutParser, SqsInterface::SqsListGrantsParser, SqsInterface::SqsListQueuesParser, SqsInterface::SqsReceiveMessagesParser, SqsInterface::SqsSendMessagesParser, SqsInterface::SqsStatusParser

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.



666
667
668
669
670
671
672
673
# File 'lib/helene/rightscale/awsbase/right_awsbase.rb', line 666

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



739
740
741
742
743
744
745
746
# File 'lib/helene/rightscale/awsbase/right_awsbase.rb', line 739

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.



662
663
664
# File 'lib/helene/rightscale/awsbase/right_awsbase.rb', line 662

def result
  @result
end

#xml_libObject

Returns the value of attribute xml_lib.



664
665
666
# File 'lib/helene/rightscale/awsbase/right_awsbase.rb', line 664

def xml_lib
  @xml_lib
end

#xmlpathObject (readonly)

Returns the value of attribute xmlpath.



663
664
665
# File 'lib/helene/rightscale/awsbase/right_awsbase.rb', line 663

def xmlpath
  @xmlpath
end

Class Method Details

.xml_libObject



655
656
657
# File 'lib/helene/rightscale/awsbase/right_awsbase.rb', line 655

def self.xml_lib
  @@xml_lib
end

.xml_lib=(new_lib_name) ⇒ Object



658
659
660
# File 'lib/helene/rightscale/awsbase/right_awsbase.rb', line 658

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'


693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
# File 'lib/helene/rightscale/awsbase/right_awsbase.rb', line 693

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)



748
# File 'lib/helene/rightscale/awsbase/right_awsbase.rb', line 748

def reset                     ; end

#tag_end(name) ⇒ Object



679
680
681
682
683
684
# File 'lib/helene/rightscale/awsbase/right_awsbase.rb', line 679

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

#tag_start(name, attributes) ⇒ Object



674
675
676
677
678
# File 'lib/helene/rightscale/awsbase/right_awsbase.rb', line 674

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

#tagend(name) ⇒ Object



750
# File 'lib/helene/rightscale/awsbase/right_awsbase.rb', line 750

def tagend(name)              ; end

#tagstart(name, attributes) ⇒ Object



749
# File 'lib/helene/rightscale/awsbase/right_awsbase.rb', line 749

def tagstart(name, attributes); end

#tagtext(text) ⇒ Object



751
# File 'lib/helene/rightscale/awsbase/right_awsbase.rb', line 751

def tagtext(text)             ; end

#text(text) ⇒ Object



685
686
687
688
# File 'lib/helene/rightscale/awsbase/right_awsbase.rb', line 685

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