2011-05-22 07:46:23 -04:00
|
|
|
require 'time'
|
2012-01-01 12:57:55 -05:00
|
|
|
require 'base64'
|
2013-11-12 22:31:02 -05:00
|
|
|
require 'bigdecimal'
|
2009-04-22 20:41:28 -04:00
|
|
|
require 'active_support/core_ext/module/delegation'
|
2011-05-07 16:44:33 -04:00
|
|
|
require 'active_support/core_ext/string/inflections'
|
2013-11-12 22:31:02 -05:00
|
|
|
require 'active_support/core_ext/date_time/calculations'
|
2009-04-22 20:41:28 -04:00
|
|
|
|
2009-01-29 11:24:16 -05:00
|
|
|
module ActiveSupport
|
2009-03-09 15:46:06 -04:00
|
|
|
# = XmlMini
|
2009-03-09 16:42:42 -04:00
|
|
|
#
|
|
|
|
# To use the much faster libxml parser:
|
|
|
|
# gem 'libxml-ruby', '=0.9.7'
|
|
|
|
# XmlMini.backend = 'LibXML'
|
2009-01-29 11:24:16 -05:00
|
|
|
module XmlMini
|
|
|
|
extend self
|
2008-11-26 02:37:10 -05:00
|
|
|
|
2010-07-23 14:11:14 -04:00
|
|
|
# This module decorates files deserialized using Hash.from_xml with
|
2010-04-29 06:27:25 -04:00
|
|
|
# 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
|
|
|
|
|
|
|
|
DEFAULT_ENCODINGS = {
|
|
|
|
"binary" => "base64"
|
2010-10-05 22:21:07 -04:00
|
|
|
} unless defined?(DEFAULT_ENCODINGS)
|
2010-04-29 06:27:25 -04:00
|
|
|
|
|
|
|
TYPE_NAMES = {
|
|
|
|
"Symbol" => "symbol",
|
|
|
|
"Fixnum" => "integer",
|
|
|
|
"Bignum" => "integer",
|
|
|
|
"BigDecimal" => "decimal",
|
|
|
|
"Float" => "float",
|
|
|
|
"TrueClass" => "boolean",
|
|
|
|
"FalseClass" => "boolean",
|
|
|
|
"Date" => "date",
|
2012-05-23 03:59:13 -04:00
|
|
|
"DateTime" => "dateTime",
|
|
|
|
"Time" => "dateTime",
|
2010-04-29 06:27:25 -04:00
|
|
|
"Array" => "array",
|
|
|
|
"Hash" => "hash"
|
|
|
|
} unless defined?(TYPE_NAMES)
|
|
|
|
|
|
|
|
FORMATTING = {
|
|
|
|
"symbol" => Proc.new { |symbol| symbol.to_s },
|
|
|
|
"date" => Proc.new { |date| date.to_s(:db) },
|
2012-05-23 03:59:13 -04:00
|
|
|
"dateTime" => Proc.new { |time| time.xmlschema },
|
2012-01-01 12:57:55 -05:00
|
|
|
"binary" => Proc.new { |binary| ::Base64.encode64(binary) },
|
2010-04-29 06:27:25 -04:00
|
|
|
"yaml" => Proc.new { |yaml| yaml.to_yaml }
|
|
|
|
} unless defined?(FORMATTING)
|
|
|
|
|
2011-05-22 01:47:32 -04:00
|
|
|
# TODO use regexp instead of Date.parse
|
2010-04-29 06:27:25 -04:00
|
|
|
unless defined?(PARSING)
|
|
|
|
PARSING = {
|
2013-11-12 22:31:02 -05:00
|
|
|
"symbol" => Proc.new { |symbol| symbol.to_s.to_sym },
|
2010-04-29 06:27:25 -04:00
|
|
|
"date" => Proc.new { |date| ::Date.parse(date) },
|
2011-05-22 07:46:23 -04:00
|
|
|
"datetime" => Proc.new { |time| Time.xmlschema(time).utc rescue ::DateTime.parse(time).utc },
|
2010-04-29 06:27:25 -04:00
|
|
|
"integer" => Proc.new { |integer| integer.to_i },
|
|
|
|
"float" => Proc.new { |float| float.to_f },
|
|
|
|
"decimal" => Proc.new { |number| BigDecimal(number) },
|
2013-11-12 22:31:02 -05:00
|
|
|
"boolean" => Proc.new { |boolean| %w(1 true).include?(boolean.to_s.strip) },
|
2010-04-29 06:27:25 -04:00
|
|
|
"string" => Proc.new { |string| string.to_s },
|
|
|
|
"yaml" => Proc.new { |yaml| YAML::load(yaml) rescue yaml },
|
2012-01-01 12:57:55 -05:00
|
|
|
"base64Binary" => Proc.new { |bin| ::Base64.decode64(bin) },
|
2010-04-29 06:27:25 -04:00
|
|
|
"binary" => Proc.new { |bin, entity| _parse_binary(bin, entity) },
|
|
|
|
"file" => Proc.new { |file, entity| _parse_file(file, entity) }
|
|
|
|
}
|
|
|
|
|
|
|
|
PARSING.update(
|
|
|
|
"double" => PARSING["float"],
|
|
|
|
"dateTime" => PARSING["datetime"]
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2009-03-10 15:08:42 -04:00
|
|
|
delegate :parse, :to => :backend
|
2009-03-09 20:27:39 -04:00
|
|
|
|
2012-11-14 00:58:33 -05:00
|
|
|
def backend
|
|
|
|
current_thread_backend || @backend
|
|
|
|
end
|
|
|
|
|
2009-03-09 15:46:06 -04:00
|
|
|
def backend=(name)
|
2012-11-14 00:58:33 -05:00
|
|
|
backend = name && cast_backend_name_to_module(name)
|
|
|
|
self.current_thread_backend = backend if current_thread_backend
|
|
|
|
@backend = backend
|
2009-03-10 15:08:42 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def with_backend(name)
|
2012-11-14 00:58:33 -05:00
|
|
|
old_backend = current_thread_backend
|
|
|
|
self.current_thread_backend = name && cast_backend_name_to_module(name)
|
2009-03-10 15:08:42 -04:00
|
|
|
yield
|
|
|
|
ensure
|
2012-11-14 00:58:33 -05:00
|
|
|
self.current_thread_backend = old_backend
|
2009-03-09 15:39:20 -04:00
|
|
|
end
|
2010-04-29 06:27:25 -04:00
|
|
|
|
|
|
|
def to_tag(key, value, options)
|
|
|
|
type_name = options.delete(:type)
|
|
|
|
merged_options = options.merge(:root => key, :skip_instruct => true)
|
|
|
|
|
|
|
|
if value.is_a?(::Method) || value.is_a?(::Proc)
|
|
|
|
if value.arity == 1
|
|
|
|
value.call(merged_options)
|
|
|
|
else
|
|
|
|
value.call(merged_options, key.to_s.singularize)
|
|
|
|
end
|
|
|
|
elsif value.respond_to?(:to_xml)
|
|
|
|
value.to_xml(merged_options)
|
|
|
|
else
|
|
|
|
type_name ||= TYPE_NAMES[value.class.name]
|
|
|
|
type_name ||= value.class.name if value && !value.respond_to?(:to_str)
|
|
|
|
type_name = type_name.to_s if type_name
|
2012-05-23 03:59:13 -04:00
|
|
|
type_name = "dateTime" if type_name == "datetime"
|
2010-04-29 06:27:25 -04:00
|
|
|
|
|
|
|
key = rename_key(key.to_s, options)
|
|
|
|
|
|
|
|
attributes = options[:skip_types] || type_name.nil? ? { } : { :type => type_name }
|
|
|
|
attributes[:nil] = true if value.nil?
|
|
|
|
|
|
|
|
encoding = options[:encoding] || DEFAULT_ENCODINGS[type_name]
|
|
|
|
attributes[:encoding] = encoding if encoding
|
|
|
|
|
|
|
|
formatted_value = FORMATTING[type_name] && !value.nil? ?
|
|
|
|
FORMATTING[type_name].call(value) : value
|
|
|
|
|
|
|
|
options[:builder].tag!(key, formatted_value, attributes)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def rename_key(key, options = {})
|
2010-11-03 14:02:42 -04:00
|
|
|
camelize = options[:camelize]
|
2010-04-29 06:27:25 -04:00
|
|
|
dasherize = !options.has_key?(:dasherize) || options[:dasherize]
|
2010-10-31 23:16:55 -04:00
|
|
|
if camelize
|
2010-11-03 14:02:42 -04:00
|
|
|
key = true == camelize ? key.camelize : key.camelize(camelize)
|
2010-10-31 23:16:55 -04:00
|
|
|
end
|
2010-07-17 17:45:19 -04:00
|
|
|
key = _dasherize(key) if dasherize
|
2010-04-29 06:27:25 -04:00
|
|
|
key
|
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
2010-07-17 17:45:19 -04:00
|
|
|
def _dasherize(key)
|
2011-05-08 11:08:56 -04:00
|
|
|
# $2 must be a non-greedy regex for this to work
|
|
|
|
left, middle, right = /\A(_*)(.*?)(_*)\Z/.match(key.strip)[1,3]
|
2011-05-09 11:08:59 -04:00
|
|
|
"#{left}#{middle.tr('_ ', '--')}#{right}"
|
2010-07-17 17:45:19 -04:00
|
|
|
end
|
|
|
|
|
2012-05-23 03:59:13 -04:00
|
|
|
# TODO: Add support for other encodings
|
2010-04-29 06:27:25 -04:00
|
|
|
def _parse_binary(bin, entity) #:nodoc:
|
|
|
|
case entity['encoding']
|
|
|
|
when 'base64'
|
2012-01-01 12:57:55 -05:00
|
|
|
::Base64.decode64(bin)
|
2010-04-29 06:27:25 -04:00
|
|
|
else
|
|
|
|
bin
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def _parse_file(file, entity)
|
2012-01-01 12:57:55 -05:00
|
|
|
f = StringIO.new(::Base64.decode64(file))
|
2010-04-29 06:27:25 -04:00
|
|
|
f.extend(FileLike)
|
|
|
|
f.original_filename = entity['name']
|
|
|
|
f.content_type = entity['content_type']
|
|
|
|
f
|
|
|
|
end
|
2012-11-14 00:58:33 -05:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def current_thread_backend
|
|
|
|
Thread.current[:xml_mini_backend]
|
|
|
|
end
|
|
|
|
|
|
|
|
def current_thread_backend=(name)
|
|
|
|
Thread.current[:xml_mini_backend] = name && cast_backend_name_to_module(name)
|
|
|
|
end
|
|
|
|
|
|
|
|
def cast_backend_name_to_module(name)
|
|
|
|
if name.is_a?(Module)
|
|
|
|
name
|
|
|
|
else
|
|
|
|
require "active_support/xml_mini/#{name.downcase}"
|
|
|
|
ActiveSupport.const_get("XmlMini_#{name}")
|
|
|
|
end
|
|
|
|
end
|
2009-03-09 15:46:06 -04:00
|
|
|
end
|
2008-11-26 02:37:10 -05:00
|
|
|
|
2009-03-09 16:42:42 -04:00
|
|
|
XmlMini.backend = 'REXML'
|
2009-03-09 15:46:06 -04:00
|
|
|
end
|