File: multi_xml.rb

package info (click to toggle)
ruby-multi-xml 0.6.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 216 kB
  • sloc: ruby: 1,231; makefile: 2
file content (305 lines) | stat: -rw-r--r-- 10,019 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
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
require 'base64'
require 'bigdecimal'
require 'date'
require 'stringio'
require 'time'
require 'yaml'

module MultiXml # rubocop:disable ModuleLength
  class ParseError < StandardError; end
  class NoParserError < StandardError; end
  class DisallowedTypeError < StandardError
    def initialize(type)
      super "Disallowed type attribute: #{type.inspect}"
    end
  end

  unless defined?(REQUIREMENT_MAP)
    REQUIREMENT_MAP = [
      ['ox', :ox],
      ['libxml', :libxml],
      ['nokogiri', :nokogiri],
      ['rexml/document', :rexml],
      ['oga', :oga],
    ].freeze
  end

  CONTENT_ROOT = '__content__'.freeze unless defined?(CONTENT_ROOT)

  unless defined?(PARSING)
    float_proc = proc { |float| float.to_f }
    datetime_proc = proc { |time| Time.parse(time).utc rescue DateTime.parse(time).utc } # rubocop:disable RescueModifier

    PARSING = {
      'symbol'       => proc { |symbol| symbol.to_sym },
      'date'         => proc { |date| Date.parse(date) },
      'datetime'     => datetime_proc,
      'dateTime'     => datetime_proc,
      'integer'      => proc { |integer| integer.to_i },
      'float'        => float_proc,
      'double'       => float_proc,
      'decimal'      => proc { |number| BigDecimal(number) },
      'boolean'      => proc { |boolean| !%w(0 false).include?(boolean.strip) },
      'string'       => proc { |string| string.to_s },
      'yaml'         => proc { |yaml| YAML.load(yaml) rescue yaml }, # rubocop:disable RescueModifier
      'base64Binary' => proc { |binary| ::Base64.decode64(binary) },
      'binary'       => proc { |binary, entity| parse_binary(binary, entity) },
      'file'         => proc { |file, entity| parse_file(file, entity) },
    }.freeze
  end

  unless defined?(TYPE_NAMES)
    TYPE_NAMES = {
      'Symbol'     => 'symbol',
      'Integer'    => 'integer',
      'BigDecimal' => 'decimal',
      'Float'      => 'float',
      'TrueClass'  => 'boolean',
      'FalseClass' => 'boolean',
      'Date'       => 'date',
      'DateTime'   => 'datetime',
      'Time'       => 'datetime',
      'Array'      => 'array',
      'Hash'       => 'hash',
    }.freeze
  end

  DISALLOWED_XML_TYPES = %w(symbol yaml).freeze

  DEFAULT_OPTIONS = {
    :typecast_xml_value => true,
    :disallowed_types => DISALLOWED_XML_TYPES,
    :symbolize_keys => false,
  }.freeze

  class << self
    # Get the current parser class.
    def parser
      return @parser if defined?(@parser)
      self.parser = default_parser
      @parser
    end

    # The default parser based on what you currently
    # have loaded and installed. First checks to see
    # if any parsers are already loaded, then checks
    # to see which are installed if none are loaded.
    def default_parser
      return :ox if defined?(::Ox)
      return :libxml if defined?(::LibXML)
      return :nokogiri if defined?(::Nokogiri)
      return :oga if defined?(::Oga)

      REQUIREMENT_MAP.each do |library, parser|
        begin
          require library
          return parser
        rescue LoadError
          next
        end
      end
      raise(NoParserError.new("No XML parser detected. If you're using Rubinius and Bundler, try adding an XML parser to your Gemfile (e.g. libxml-ruby, nokogiri, or rubysl-rexml). For more information, see https://github.com/sferik/multi_xml/issues/42."))
    end

    # Set the XML parser utilizing a symbol, string, or class.
    # Supported by default are:
    #
    # * <tt>:libxml</tt>
    # * <tt>:nokogiri</tt>
    # * <tt>:ox</tt>
    # * <tt>:rexml</tt>
    # * <tt>:oga</tt>
    def parser=(new_parser)
      case new_parser
      when String, Symbol
        require "multi_xml/parsers/#{new_parser.to_s.downcase}"
        @parser = MultiXml::Parsers.const_get(new_parser.to_s.split('_').collect(&:capitalize).join('').to_s)
      when Class, Module
        @parser = new_parser
      else
        raise('Did not recognize your parser specification. Please specify either a symbol or a class.')
      end
    end

    # Parse an XML string or IO into Ruby.
    #
    # <b>Options</b>
    #
    # <tt>:symbolize_keys</tt> :: If true, will use symbols instead of strings for the keys.
    #
    # <tt>:disallowed_types</tt> :: Types to disallow from being typecasted. Defaults to `['yaml', 'symbol']`. Use `[]` to allow all types.
    #
    # <tt>:typecast_xml_value</tt> :: If true, won't typecast values for parsed document
    def parse(xml, options = {}) # rubocop:disable AbcSize, CyclomaticComplexity, MethodLength, PerceivedComplexity
      xml ||= ''

      options = DEFAULT_OPTIONS.merge(options)

      xml = xml.strip if xml.respond_to?(:strip)
      begin
        xml = StringIO.new(xml) unless xml.respond_to?(:read)

        char = xml.getc
        return {} if char.nil?
        xml.ungetc(char)

        hash = undasherize_keys(parser.parse(xml) || {})
        hash = options[:typecast_xml_value] ? typecast_xml_value(hash, options[:disallowed_types]) : hash
      rescue DisallowedTypeError
        raise
      rescue parser.parse_error => error
        raise(ParseError, error.message, error.backtrace) # rubocop:disable RaiseArgs
      end
      hash = symbolize_keys(hash) if options[:symbolize_keys]
      hash
    end

    # This module decorates files with the <tt>original_filename</tt>
    # and <tt>content_type</tt> methods.
    module FileLike #:nodoc:
      attr_writer :original_filename, :content_type

      def original_filename
        @original_filename || 'untitled'
      end

      def content_type
        @content_type || 'application/octet-stream'
      end
    end

  private

    # TODO: Add support for other encodings
    def parse_binary(binary, entity) #:nodoc:
      case entity['encoding']
      when 'base64'
        Base64.decode64(binary)
      else
        binary
      end
    end

    def parse_file(file, entity)
      f = StringIO.new(Base64.decode64(file))
      f.extend(FileLike)
      f.original_filename = entity['name']
      f.content_type = entity['content_type']
      f
    end

    def symbolize_keys(params)
      case params
      when Hash
        params.inject({}) do |result, (key, value)|
          result.merge(key.to_sym => symbolize_keys(value))
        end
      when Array
        params.collect { |value| symbolize_keys(value) }
      else
        params
      end
    end

    def undasherize_keys(params)
      case params
      when Hash
        params.inject({}) do |hash, (key, value)|
          hash[key.to_s.tr('-'.freeze, '_'.freeze)] = undasherize_keys(value)
          hash
        end
      when Array
        params.collect { |value| undasherize_keys(value) }
      else
        params
      end
    end

    def typecast_xml_value(value, disallowed_types = nil) # rubocop:disable AbcSize, CyclomaticComplexity, MethodLength, PerceivedComplexity
      disallowed_types ||= DISALLOWED_XML_TYPES

      case value
      when Hash
        if value.include?('type') && !value['type'].is_a?(Hash) && disallowed_types.include?(value['type'])
          raise(DisallowedTypeError.new(value['type']))
        end

        if value['type'] == 'array'

          # this commented-out suggestion helps to avoid the multiple attribute
          # problem, but it breaks when there is only one item in the array.
          #
          # from: https://github.com/jnunemaker/httparty/issues/102
          #
          # _, entries = value.detect { |k, v| k != 'type' && v.is_a?(Array) }

          # This attempt fails to consider the order that the detect method
          # retrieves the entries.
          # _, entries = value.detect {|key, _| key != 'type'}

          # This approach ignores attribute entries that are not convertable
          # to an Array which allows attributes to be ignored.
          _, entries = value.detect { |k, v| k != 'type' && (v.is_a?(Array) || v.is_a?(Hash)) }

          case entries
          when NilClass
            []
          when String
            [] if entries.strip.empty?
          when Array
            entries.collect { |entry| typecast_xml_value(entry, disallowed_types) }
          when Hash
            [typecast_xml_value(entries, disallowed_types)]
          else
            raise("can't typecast #{entries.class.name}: #{entries.inspect}")
          end

        elsif value.key?(CONTENT_ROOT)
          content = value[CONTENT_ROOT]
          block = PARSING[value['type']]
          if block
            if block.arity == 1
              value.delete('type') if PARSING[value['type']]
              if value.keys.size > 1
                value[CONTENT_ROOT] = block.call(content)
                value
              else
                block.call(content)
              end
            else
              block.call(content, value)
            end
          else
            value.keys.size > 1 ? value : content
          end
        elsif value['type'] == 'string' && value['nil'] != 'true'
          ''
        # blank or nil parsed values are represented by nil
        elsif value.empty? || value['nil'] == 'true'
          nil
        # If the type is the only element which makes it then
        # this still makes the value nil, except if type is
        # a XML node(where type['value'] is a Hash)
        elsif value['type'] && value.size == 1 && !value['type'].is_a?(Hash)
          nil
        else
          xml_value = value.inject({}) do |hash, (k, v)|
            hash[k] = typecast_xml_value(v, disallowed_types)
            hash
          end

          # Turn {:files => {:file => #<StringIO>} into {:files => #<StringIO>} so it is compatible with
          # how multipart uploaded files from HTML appear
          xml_value['file'].is_a?(StringIO) ? xml_value['file'] : xml_value
        end
      when Array
        value.map! { |i| typecast_xml_value(i, disallowed_types) }
        value.length > 1 ? value : value.first
      when String
        value
      else
        raise("can't typecast #{value.class.name}: #{value.inspect}")
      end
    end
  end
end