Class: RightDevelop::Parsers::SaxParser

Inherits:
Object
  • Object
show all
Extended by:
XmlPostParser
Defined in:
lib/right_develop/parsers/sax_parser.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from XmlPostParser

remove_nesting

Constructor Details

#initializeSaxParser



47
48
49
50
51
52
53
54
# File 'lib/right_develop/parsers/sax_parser.rb', line 47

def initialize
  unless AVAILABLE
    raise NotImplementedError, "#{self.name} is unavailable on this system because libxml-ruby and/or active_support are not installed"
  end

  @tag  = {}
  @path = []
end

Class Method Details

.parse(text, opts = {}) ⇒ Array or Hash

Parses XML into a ruby hash



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/right_develop/parsers/sax_parser.rb', line 25

def self.parse(text, opts = {})
  unless AVAILABLE
    raise NotImplementedError, "#{self.name} is unavailable on this system because libxml-ruby and/or active_support are not installed"
  end

  # Parse the xml text
  # http://libxml.rubyforge.org/rdoc/
  xml           = ::XML::SaxParser::string(text)
  xml.callbacks = new
  xml.parse

  if opts[:post_parser]
    if opts[:post_parser].kind_of?(Proc)
      return opts[:post_parser].call(xml.callbacks.result)
    else
      raise ArgumentError.new(":post_parser parameter must be a lambda/proc")
    end
  else
    return xml.callbacks.result
  end
end

Instance Method Details

#on_cdata_block(cdata) ⇒ Object



135
136
# File 'lib/right_develop/parsers/sax_parser.rb', line 135

def on_cdata_block(cdata)
end

#on_characters(chars) ⇒ Object



88
89
90
91
92
93
# File 'lib/right_develop/parsers/sax_parser.rb', line 88

def on_characters(chars)
  # Ignore lines that contains white spaces only
  return if chars[/\A\s*\z/m]
  # Put Text
  (@tag['@@text'] ||= '') << chars
end

#on_comment(msg) ⇒ Object



95
96
97
98
# File 'lib/right_develop/parsers/sax_parser.rb', line 95

def on_comment(msg)
  # Put Comments
  (@tag['@@comment'] ||= '') << msg
end

#on_end_documentObject



153
154
# File 'lib/right_develop/parsers/sax_parser.rb', line 153

def on_end_document
end

#on_end_element_ns(name, prefix, uri) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/right_develop/parsers/sax_parser.rb', line 100

def on_end_element_ns(name, prefix, uri)
  # Special handling of empty text fields
  if @tag.is_a?(Hash) && @tag.empty? && @tag['@@text'].nil?
    @tag['@@text'] = nil
  end

  # Finalize tag's text
  if @tag.keys.count == 0
    # Set tag value to nil then the tag is blank
    name.pluralize == name ? @tag = [] : {}
  elsif @tag.keys == ['@@text']
    # Set tag value to string if it has no any other data
    @tag = @tag['@@text']
  end
  # Make sure we saved the changes
  if @path.last[name].is_a?(Array)
    # If it is an Array then update the very last item
    @path.last[name][-1] = @tag
  else
    # Otherwise just replace the tag
    @path.last[name] = @tag
  end
  # Pop parent tag
  @tag = @path.pop
end

#on_error(msg) ⇒ Object

Callbacks



62
63
64
# File 'lib/right_develop/parsers/sax_parser.rb', line 62

def on_error(msg)
  raise msg
end

#on_external_subset(name, external_id, system_id) ⇒ Object



150
151
# File 'lib/right_develop/parsers/sax_parser.rb', line 150

def on_external_subset (name, external_id, system_id)
end

#on_has_external_subsetObject



147
148
# File 'lib/right_develop/parsers/sax_parser.rb', line 147

def on_has_external_subset ()
end

#on_has_internal_subsetObject



138
139
# File 'lib/right_develop/parsers/sax_parser.rb', line 138

def on_has_internal_subset()
end

#on_internal_subset(name, external_id, system_id) ⇒ Object



141
142
# File 'lib/right_develop/parsers/sax_parser.rb', line 141

def on_internal_subset (name, external_id, system_id)
end

#on_is_standaloneObject



144
145
# File 'lib/right_develop/parsers/sax_parser.rb', line 144

def on_is_standalone ()
end

#on_processing_instruction(target, data) ⇒ Object



132
133
# File 'lib/right_develop/parsers/sax_parser.rb', line 132

def on_processing_instruction(target, data)
end

#on_reference(name) ⇒ Object



129
130
# File 'lib/right_develop/parsers/sax_parser.rb', line 129

def on_reference (name)
end

#on_start_documentObject



126
127
# File 'lib/right_develop/parsers/sax_parser.rb', line 126

def on_start_document
end

#on_start_element_ns(name, attr_hash, prefix, uri, namespaces) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/right_develop/parsers/sax_parser.rb', line 66

def on_start_element_ns(name, attr_hash, prefix, uri, namespaces)
  # Push parent tag
  @path << @tag
  # Create a new tag
  if @tag[name]
    @tag[name] = [ @tag[name] ] unless @tag[name].is_a?(Array)
    @tag[name] << {}
    @tag = @tag[name].last
  else
    @tag[name] = {}
    @tag = @tag[name]
  end
  # Put attributes
  attr_hash.each do |key, value|
    @tag["#{key}"] = value
  end
  # Put name spaces
  namespaces.each do |key, value|
    @tag["@xmlns#{key ? ':'+key.to_s : ''}"] = value
  end
end

#resultObject



56
57
58
# File 'lib/right_develop/parsers/sax_parser.rb', line 56

def result
  @tag
end