activesupport 4.2.0 → 5.2.0
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of activesupport might be problematic. Click here for more details.
- checksums.yaml +5 -5
- data/CHANGELOG.md +366 -232
- data/MIT-LICENSE +2 -2
- data/README.rdoc +4 -5
- data/lib/active_support.rb +17 -7
- data/lib/active_support/all.rb +5 -3
- data/lib/active_support/array_inquirer.rb +48 -0
- data/lib/active_support/backtrace_cleaner.rb +7 -5
- data/lib/active_support/benchmarkable.rb +6 -4
- data/lib/active_support/builder.rb +3 -1
- data/lib/active_support/cache.rb +271 -177
- data/lib/active_support/cache/file_store.rb +41 -35
- data/lib/active_support/cache/mem_cache_store.rb +97 -88
- data/lib/active_support/cache/memory_store.rb +27 -30
- data/lib/active_support/cache/null_store.rb +7 -8
- data/lib/active_support/cache/redis_cache_store.rb +454 -0
- data/lib/active_support/cache/strategy/local_cache.rb +67 -34
- data/lib/active_support/cache/strategy/local_cache_middleware.rb +10 -9
- data/lib/active_support/callbacks.rb +654 -560
- data/lib/active_support/concern.rb +5 -3
- data/lib/active_support/concurrency/load_interlock_aware_monitor.rb +17 -0
- data/lib/active_support/concurrency/share_lock.rb +227 -0
- data/lib/active_support/configurable.rb +8 -5
- data/lib/active_support/core_ext.rb +3 -1
- data/lib/active_support/core_ext/array.rb +9 -6
- data/lib/active_support/core_ext/array/access.rb +29 -1
- data/lib/active_support/core_ext/array/conversions.rb +22 -18
- data/lib/active_support/core_ext/array/extract_options.rb +2 -0
- data/lib/active_support/core_ext/array/grouping.rb +11 -18
- data/lib/active_support/core_ext/array/inquiry.rb +19 -0
- data/lib/active_support/core_ext/array/prepend_and_append.rb +5 -3
- data/lib/active_support/core_ext/array/wrap.rb +7 -4
- data/lib/active_support/core_ext/benchmark.rb +3 -1
- data/lib/active_support/core_ext/big_decimal.rb +3 -1
- data/lib/active_support/core_ext/big_decimal/conversions.rb +10 -12
- data/lib/active_support/core_ext/class.rb +4 -3
- data/lib/active_support/core_ext/class/attribute.rb +41 -22
- data/lib/active_support/core_ext/class/attribute_accessors.rb +3 -1
- data/lib/active_support/core_ext/class/subclasses.rb +20 -8
- data/lib/active_support/core_ext/date.rb +6 -4
- data/lib/active_support/core_ext/date/acts_like.rb +3 -1
- data/lib/active_support/core_ext/date/blank.rb +14 -0
- data/lib/active_support/core_ext/date/calculations.rb +11 -9
- data/lib/active_support/core_ext/date/conversions.rb +31 -23
- data/lib/active_support/core_ext/date/zones.rb +4 -2
- data/lib/active_support/core_ext/date_and_time/calculations.rb +179 -56
- data/lib/active_support/core_ext/date_and_time/compatibility.rb +16 -0
- data/lib/active_support/core_ext/date_and_time/zones.rb +12 -12
- data/lib/active_support/core_ext/date_time.rb +7 -4
- data/lib/active_support/core_ext/date_time/acts_like.rb +4 -2
- data/lib/active_support/core_ext/date_time/blank.rb +14 -0
- data/lib/active_support/core_ext/date_time/calculations.rb +58 -20
- data/lib/active_support/core_ext/date_time/compatibility.rb +18 -0
- data/lib/active_support/core_ext/date_time/conversions.rb +16 -12
- data/lib/active_support/core_ext/digest/uuid.rb +7 -5
- data/lib/active_support/core_ext/enumerable.rb +107 -28
- data/lib/active_support/core_ext/file.rb +3 -1
- data/lib/active_support/core_ext/file/atomic.rb +38 -31
- data/lib/active_support/core_ext/hash.rb +11 -9
- data/lib/active_support/core_ext/hash/compact.rb +24 -15
- data/lib/active_support/core_ext/hash/conversions.rb +63 -43
- data/lib/active_support/core_ext/hash/deep_merge.rb +9 -13
- data/lib/active_support/core_ext/hash/except.rb +11 -8
- data/lib/active_support/core_ext/hash/indifferent_access.rb +4 -3
- data/lib/active_support/core_ext/hash/keys.rb +33 -27
- data/lib/active_support/core_ext/hash/reverse_merge.rb +5 -2
- data/lib/active_support/core_ext/hash/slice.rb +8 -8
- data/lib/active_support/core_ext/hash/transform_values.rb +16 -7
- data/lib/active_support/core_ext/integer.rb +5 -3
- data/lib/active_support/core_ext/integer/inflections.rb +3 -1
- data/lib/active_support/core_ext/integer/multiple.rb +2 -0
- data/lib/active_support/core_ext/integer/time.rb +11 -33
- data/lib/active_support/core_ext/kernel.rb +6 -5
- data/lib/active_support/core_ext/kernel/agnostics.rb +2 -0
- data/lib/active_support/core_ext/kernel/concern.rb +5 -1
- data/lib/active_support/core_ext/kernel/reporting.rb +4 -83
- data/lib/active_support/core_ext/kernel/singleton_class.rb +2 -0
- data/lib/active_support/core_ext/load_error.rb +3 -22
- data/lib/active_support/core_ext/marshal.rb +13 -10
- data/lib/active_support/core_ext/module.rb +14 -11
- data/lib/active_support/core_ext/module/aliasing.rb +6 -44
- data/lib/active_support/core_ext/module/anonymous.rb +12 -1
- data/lib/active_support/core_ext/module/attr_internal.rb +8 -9
- data/lib/active_support/core_ext/module/attribute_accessors.rb +43 -40
- data/lib/active_support/core_ext/module/attribute_accessors_per_thread.rb +150 -0
- data/lib/active_support/core_ext/module/concerning.rb +11 -12
- data/lib/active_support/core_ext/module/delegation.rb +121 -39
- data/lib/active_support/core_ext/module/deprecation.rb +4 -2
- data/lib/active_support/core_ext/module/introspection.rb +9 -9
- data/lib/active_support/core_ext/module/reachable.rb +5 -2
- data/lib/active_support/core_ext/module/redefine_method.rb +49 -0
- data/lib/active_support/core_ext/module/remove_method.rb +8 -3
- data/lib/active_support/core_ext/name_error.rb +22 -2
- data/lib/active_support/core_ext/numeric.rb +6 -3
- data/lib/active_support/core_ext/numeric/bytes.rb +22 -0
- data/lib/active_support/core_ext/numeric/conversions.rb +79 -74
- data/lib/active_support/core_ext/numeric/inquiry.rb +28 -0
- data/lib/active_support/core_ext/numeric/time.rb +35 -38
- data/lib/active_support/core_ext/object.rb +14 -13
- data/lib/active_support/core_ext/object/acts_like.rb +12 -1
- data/lib/active_support/core_ext/object/blank.rb +29 -4
- data/lib/active_support/core_ext/object/conversions.rb +6 -4
- data/lib/active_support/core_ext/object/deep_dup.rb +13 -4
- data/lib/active_support/core_ext/object/duplicable.rb +98 -45
- data/lib/active_support/core_ext/object/inclusion.rb +5 -3
- data/lib/active_support/core_ext/object/instance_variables.rb +3 -1
- data/lib/active_support/core_ext/object/json.rb +49 -19
- data/lib/active_support/core_ext/object/to_param.rb +3 -1
- data/lib/active_support/core_ext/object/to_query.rb +6 -4
- data/lib/active_support/core_ext/object/try.rb +70 -22
- data/lib/active_support/core_ext/object/with_options.rb +16 -3
- data/lib/active_support/core_ext/range.rb +7 -4
- data/lib/active_support/core_ext/range/conversions.rb +27 -7
- data/lib/active_support/core_ext/range/each.rb +19 -17
- data/lib/active_support/core_ext/range/include_range.rb +21 -19
- data/lib/active_support/core_ext/range/include_time_with_zone.rb +23 -0
- data/lib/active_support/core_ext/range/overlaps.rb +2 -0
- data/lib/active_support/core_ext/regexp.rb +6 -0
- data/lib/active_support/core_ext/securerandom.rb +25 -0
- data/lib/active_support/core_ext/string.rb +15 -13
- data/lib/active_support/core_ext/string/access.rb +9 -7
- data/lib/active_support/core_ext/string/behavior.rb +3 -1
- data/lib/active_support/core_ext/string/conversions.rb +8 -5
- data/lib/active_support/core_ext/string/exclude.rb +2 -0
- data/lib/active_support/core_ext/string/filters.rb +10 -8
- data/lib/active_support/core_ext/string/indent.rb +6 -4
- data/lib/active_support/core_ext/string/inflections.rb +61 -24
- data/lib/active_support/core_ext/string/inquiry.rb +3 -1
- data/lib/active_support/core_ext/string/multibyte.rb +15 -7
- data/lib/active_support/core_ext/string/output_safety.rb +35 -35
- data/lib/active_support/core_ext/string/starts_ends_with.rb +2 -0
- data/lib/active_support/core_ext/string/strip.rb +4 -5
- data/lib/active_support/core_ext/string/zones.rb +4 -2
- data/lib/active_support/core_ext/time.rb +7 -5
- data/lib/active_support/core_ext/time/acts_like.rb +3 -1
- data/lib/active_support/core_ext/time/calculations.rb +101 -51
- data/lib/active_support/core_ext/time/compatibility.rb +16 -0
- data/lib/active_support/core_ext/time/conversions.rb +20 -13
- data/lib/active_support/core_ext/time/zones.rb +41 -7
- data/lib/active_support/core_ext/uri.rb +5 -4
- data/lib/active_support/current_attributes.rb +195 -0
- data/lib/active_support/dependencies.rb +143 -160
- data/lib/active_support/dependencies/autoload.rb +2 -0
- data/lib/active_support/dependencies/interlock.rb +57 -0
- data/lib/active_support/deprecation.rb +12 -9
- data/lib/active_support/deprecation/behaviors.rb +41 -12
- data/lib/active_support/deprecation/constant_accessor.rb +52 -0
- data/lib/active_support/deprecation/instance_delegator.rb +17 -2
- data/lib/active_support/deprecation/method_wrappers.rb +54 -21
- data/lib/active_support/deprecation/proxy_wrappers.rb +56 -28
- data/lib/active_support/deprecation/reporting.rb +32 -12
- data/lib/active_support/descendants_tracker.rb +2 -0
- data/lib/active_support/digest.rb +20 -0
- data/lib/active_support/duration.rb +326 -30
- data/lib/active_support/duration/iso8601_parser.rb +125 -0
- data/lib/active_support/duration/iso8601_serializer.rb +55 -0
- data/lib/active_support/encrypted_configuration.rb +49 -0
- data/lib/active_support/encrypted_file.rb +99 -0
- data/lib/active_support/evented_file_update_checker.rb +205 -0
- data/lib/active_support/execution_wrapper.rb +128 -0
- data/lib/active_support/executor.rb +8 -0
- data/lib/active_support/file_update_checker.rb +63 -37
- data/lib/active_support/gem_version.rb +4 -2
- data/lib/active_support/gzip.rb +7 -5
- data/lib/active_support/hash_with_indifferent_access.rb +130 -30
- data/lib/active_support/i18n.rb +8 -6
- data/lib/active_support/i18n_railtie.rb +34 -14
- data/lib/active_support/inflections.rb +13 -11
- data/lib/active_support/inflector.rb +7 -5
- data/lib/active_support/inflector/inflections.rb +61 -12
- data/lib/active_support/inflector/methods.rb +161 -136
- data/lib/active_support/inflector/transliterate.rb +48 -27
- data/lib/active_support/json.rb +4 -2
- data/lib/active_support/json/decoding.rb +16 -13
- data/lib/active_support/json/encoding.rb +15 -57
- data/lib/active_support/key_generator.rb +25 -25
- data/lib/active_support/lazy_load_hooks.rb +50 -20
- data/lib/active_support/locale/en.yml +2 -0
- data/lib/active_support/log_subscriber.rb +13 -10
- data/lib/active_support/log_subscriber/test_helper.rb +14 -12
- data/lib/active_support/logger.rb +54 -3
- data/lib/active_support/logger_silence.rb +12 -7
- data/lib/active_support/logger_thread_safe_level.rb +33 -0
- data/lib/active_support/message_encryptor.rb +173 -51
- data/lib/active_support/message_verifier.rb +150 -17
- data/lib/active_support/messages/metadata.rb +71 -0
- data/lib/active_support/messages/rotation_configuration.rb +22 -0
- data/lib/active_support/messages/rotator.rb +56 -0
- data/lib/active_support/multibyte.rb +4 -2
- data/lib/active_support/multibyte/chars.rb +37 -24
- data/lib/active_support/multibyte/unicode.rb +100 -96
- data/lib/active_support/notifications.rb +11 -7
- data/lib/active_support/notifications/fanout.rb +10 -8
- data/lib/active_support/notifications/instrumenter.rb +27 -7
- data/lib/active_support/number_helper.rb +94 -68
- data/lib/active_support/number_helper/number_converter.rb +13 -11
- data/lib/active_support/number_helper/number_to_currency_converter.rb +9 -9
- data/lib/active_support/number_helper/number_to_delimited_converter.rb +9 -3
- data/lib/active_support/number_helper/number_to_human_converter.rb +11 -9
- data/lib/active_support/number_helper/number_to_human_size_converter.rb +9 -8
- data/lib/active_support/number_helper/number_to_percentage_converter.rb +3 -1
- data/lib/active_support/number_helper/number_to_phone_converter.rb +13 -4
- data/lib/active_support/number_helper/number_to_rounded_converter.rb +23 -56
- data/lib/active_support/number_helper/rounding_helper.rb +66 -0
- data/lib/active_support/option_merger.rb +3 -1
- data/lib/active_support/ordered_hash.rb +6 -4
- data/lib/active_support/ordered_options.rb +22 -4
- data/lib/active_support/per_thread_registry.rb +13 -6
- data/lib/active_support/proxy_object.rb +2 -0
- data/lib/active_support/rails.rb +16 -8
- data/lib/active_support/railtie.rb +43 -9
- data/lib/active_support/reloader.rb +131 -0
- data/lib/active_support/rescuable.rb +108 -53
- data/lib/active_support/security_utils.rb +17 -6
- data/lib/active_support/string_inquirer.rb +11 -3
- data/lib/active_support/subscriber.rb +15 -14
- data/lib/active_support/tagged_logging.rb +14 -11
- data/lib/active_support/test_case.rb +18 -46
- data/lib/active_support/testing/assertions.rb +137 -20
- data/lib/active_support/testing/autorun.rb +4 -2
- data/lib/active_support/testing/constant_lookup.rb +2 -1
- data/lib/active_support/testing/declarative.rb +3 -1
- data/lib/active_support/testing/deprecation.rb +14 -10
- data/lib/active_support/testing/file_fixtures.rb +36 -0
- data/lib/active_support/testing/isolation.rb +34 -25
- data/lib/active_support/testing/method_call_assertions.rb +43 -0
- data/lib/active_support/testing/setup_and_teardown.rb +12 -3
- data/lib/active_support/testing/stream.rb +44 -0
- data/lib/active_support/testing/tagged_logging.rb +3 -1
- data/lib/active_support/testing/time_helpers.rb +96 -27
- data/lib/active_support/time.rb +14 -12
- data/lib/active_support/time_with_zone.rb +195 -53
- data/lib/active_support/values/time_zone.rb +200 -61
- data/lib/active_support/values/unicode_tables.dat +0 -0
- data/lib/active_support/version.rb +3 -1
- data/lib/active_support/xml_mini.rb +69 -51
- data/lib/active_support/xml_mini/jdom.rb +116 -113
- data/lib/active_support/xml_mini/libxml.rb +17 -16
- data/lib/active_support/xml_mini/libxmlsax.rb +16 -18
- data/lib/active_support/xml_mini/nokogiri.rb +15 -15
- data/lib/active_support/xml_mini/nokogirisax.rb +15 -16
- data/lib/active_support/xml_mini/rexml.rb +17 -16
- metadata +55 -43
- data/lib/active_support/concurrency/latch.rb +0 -27
- data/lib/active_support/core_ext/big_decimal/yaml_conversions.rb +0 -14
- data/lib/active_support/core_ext/class/delegating_attributes.rb +0 -45
- data/lib/active_support/core_ext/date_time/zones.rb +0 -6
- data/lib/active_support/core_ext/kernel/debugger.rb +0 -10
- data/lib/active_support/core_ext/module/method_transplanting.rb +0 -11
- data/lib/active_support/core_ext/module/qualified_const.rb +0 -52
- data/lib/active_support/core_ext/object/itself.rb +0 -15
- data/lib/active_support/core_ext/struct.rb +0 -6
- data/lib/active_support/core_ext/thread.rb +0 -86
- data/lib/active_support/core_ext/time/marshal.rb +0 -30
@@ -1,9 +1,11 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
3
|
+
raise "JRuby is required to use the JDOM backend for XmlMini" unless RUBY_PLATFORM.include?("java")
|
4
|
+
|
5
|
+
require "jruby"
|
4
6
|
include Java
|
5
7
|
|
6
|
-
require
|
8
|
+
require "active_support/core_ext/object/blank"
|
7
9
|
|
8
10
|
java_import javax.xml.parsers.DocumentBuilder unless defined? DocumentBuilder
|
9
11
|
java_import javax.xml.parsers.DocumentBuilderFactory unless defined? DocumentBuilderFactory
|
@@ -16,7 +18,7 @@ module ActiveSupport
|
|
16
18
|
module XmlMini_JDOM #:nodoc:
|
17
19
|
extend self
|
18
20
|
|
19
|
-
CONTENT_KEY =
|
21
|
+
CONTENT_KEY = "__content__".freeze
|
20
22
|
|
21
23
|
NODE_TYPE_NAMES = %w{ATTRIBUTE_NODE CDATA_SECTION_NODE COMMENT_NODE DOCUMENT_FRAGMENT_NODE
|
22
24
|
DOCUMENT_NODE DOCUMENT_TYPE_NODE ELEMENT_NODE ENTITY_NODE ENTITY_REFERENCE_NODE NOTATION_NODE
|
@@ -38,7 +40,7 @@ module ActiveSupport
|
|
38
40
|
else
|
39
41
|
@dbf = DocumentBuilderFactory.new_instance
|
40
42
|
# secure processing of java xml
|
41
|
-
#
|
43
|
+
# https://archive.is/9xcQQ
|
42
44
|
@dbf.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false)
|
43
45
|
@dbf.setFeature("http://xml.org/sax/features/external-general-entities", false)
|
44
46
|
@dbf.setFeature("http://xml.org/sax/features/external-parameter-entities", false)
|
@@ -46,135 +48,136 @@ module ActiveSupport
|
|
46
48
|
xml_string_reader = StringReader.new(data)
|
47
49
|
xml_input_source = InputSource.new(xml_string_reader)
|
48
50
|
doc = @dbf.new_document_builder.parse(xml_input_source)
|
49
|
-
merge_element!({CONTENT_KEY =>
|
51
|
+
merge_element!({ CONTENT_KEY => "" }, doc.document_element, XmlMini.depth)
|
50
52
|
end
|
51
53
|
end
|
52
54
|
|
53
55
|
private
|
54
56
|
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
def delete_empty(hash)
|
67
|
-
hash.delete(CONTENT_KEY) if hash[CONTENT_KEY] == ''
|
68
|
-
end
|
57
|
+
# Convert an XML element and merge into the hash
|
58
|
+
#
|
59
|
+
# hash::
|
60
|
+
# Hash to merge the converted element into.
|
61
|
+
# element::
|
62
|
+
# XML element to merge into hash
|
63
|
+
def merge_element!(hash, element, depth)
|
64
|
+
raise "Document too deep!" if depth == 0
|
65
|
+
delete_empty(hash)
|
66
|
+
merge!(hash, element.tag_name, collapse(element, depth))
|
67
|
+
end
|
69
68
|
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
# The document element to be collapsed.
|
74
|
-
def collapse(element)
|
75
|
-
hash = get_attributes(element)
|
69
|
+
def delete_empty(hash)
|
70
|
+
hash.delete(CONTENT_KEY) if hash[CONTENT_KEY] == ""
|
71
|
+
end
|
76
72
|
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
73
|
+
# Actually converts an XML document element into a data structure.
|
74
|
+
#
|
75
|
+
# element::
|
76
|
+
# The document element to be collapsed.
|
77
|
+
def collapse(element, depth)
|
78
|
+
hash = get_attributes(element)
|
79
|
+
|
80
|
+
child_nodes = element.child_nodes
|
81
|
+
if child_nodes.length > 0
|
82
|
+
(0...child_nodes.length).each do |i|
|
83
|
+
child = child_nodes.item(i)
|
84
|
+
merge_element!(hash, child, depth - 1) unless child.node_type == Node.TEXT_NODE
|
85
|
+
end
|
86
|
+
merge_texts!(hash, element) unless empty_content?(element)
|
87
|
+
hash
|
88
|
+
else
|
89
|
+
merge_texts!(hash, element)
|
82
90
|
end
|
83
|
-
merge_texts!(hash, element) unless empty_content?(element)
|
84
|
-
hash
|
85
|
-
else
|
86
|
-
merge_texts!(hash, element)
|
87
91
|
end
|
88
|
-
end
|
89
92
|
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
93
|
+
# Merge all the texts of an element into the hash
|
94
|
+
#
|
95
|
+
# hash::
|
96
|
+
# Hash to add the converted element to.
|
97
|
+
# element::
|
98
|
+
# XML element whose texts are to me merged into the hash
|
99
|
+
def merge_texts!(hash, element)
|
100
|
+
delete_empty(hash)
|
101
|
+
text_children = texts(element)
|
102
|
+
if text_children.join.empty?
|
103
|
+
hash
|
104
|
+
else
|
105
|
+
# must use value to prevent double-escaping
|
106
|
+
merge!(hash, CONTENT_KEY, text_children.join)
|
107
|
+
end
|
104
108
|
end
|
105
|
-
end
|
106
109
|
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
110
|
+
# Adds a new key/value pair to an existing Hash. If the key to be added
|
111
|
+
# already exists and the existing value associated with key is not
|
112
|
+
# an Array, it will be wrapped in an Array. Then the new value is
|
113
|
+
# appended to that Array.
|
114
|
+
#
|
115
|
+
# hash::
|
116
|
+
# Hash to add key/value pair to.
|
117
|
+
# key::
|
118
|
+
# Key to be added.
|
119
|
+
# value::
|
120
|
+
# Value to be associated with key.
|
121
|
+
def merge!(hash, key, value)
|
122
|
+
if hash.has_key?(key)
|
123
|
+
if hash[key].instance_of?(Array)
|
124
|
+
hash[key] << value
|
125
|
+
else
|
126
|
+
hash[key] = [hash[key], value]
|
127
|
+
end
|
128
|
+
elsif value.instance_of?(Array)
|
129
|
+
hash[key] = [value]
|
122
130
|
else
|
123
|
-
hash[key] =
|
131
|
+
hash[key] = value
|
124
132
|
end
|
125
|
-
|
126
|
-
hash[key] = [value]
|
127
|
-
else
|
128
|
-
hash[key] = value
|
133
|
+
hash
|
129
134
|
end
|
130
|
-
hash
|
131
|
-
end
|
132
135
|
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
136
|
+
# Converts the attributes array of an XML element into a hash.
|
137
|
+
# Returns an empty Hash if node has no attributes.
|
138
|
+
#
|
139
|
+
# element::
|
140
|
+
# XML element to extract attributes from.
|
141
|
+
def get_attributes(element)
|
142
|
+
attribute_hash = {}
|
143
|
+
attributes = element.attributes
|
144
|
+
(0...attributes.length).each do |i|
|
145
|
+
attribute_hash[CONTENT_KEY] ||= ""
|
146
|
+
attribute_hash[attributes.item(i).name] = attributes.item(i).value
|
147
|
+
end
|
148
|
+
attribute_hash
|
149
|
+
end
|
147
150
|
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
151
|
+
# Determines if a document element has text content
|
152
|
+
#
|
153
|
+
# element::
|
154
|
+
# XML element to be checked.
|
155
|
+
def texts(element)
|
156
|
+
texts = []
|
157
|
+
child_nodes = element.child_nodes
|
158
|
+
(0...child_nodes.length).each do |i|
|
159
|
+
item = child_nodes.item(i)
|
160
|
+
if item.node_type == Node.TEXT_NODE
|
161
|
+
texts << item.get_data
|
162
|
+
end
|
159
163
|
end
|
164
|
+
texts
|
160
165
|
end
|
161
|
-
texts
|
162
|
-
end
|
163
166
|
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
167
|
+
# Determines if a document element has text content
|
168
|
+
#
|
169
|
+
# element::
|
170
|
+
# XML element to be checked.
|
171
|
+
def empty_content?(element)
|
172
|
+
text = "".dup
|
173
|
+
child_nodes = element.child_nodes
|
174
|
+
(0...child_nodes.length).each do |i|
|
175
|
+
item = child_nodes.item(i)
|
176
|
+
if item.node_type == Node.TEXT_NODE
|
177
|
+
text << item.get_data.strip
|
178
|
+
end
|
175
179
|
end
|
180
|
+
text.strip.length == 0
|
176
181
|
end
|
177
|
-
text.strip.length == 0
|
178
|
-
end
|
179
182
|
end
|
180
183
|
end
|
@@ -1,6 +1,8 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
require
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "libxml"
|
4
|
+
require "active_support/core_ext/object/blank"
|
5
|
+
require "stringio"
|
4
6
|
|
5
7
|
module ActiveSupport
|
6
8
|
module XmlMini_LibXML #:nodoc:
|
@@ -11,18 +13,15 @@ module ActiveSupport
|
|
11
13
|
# XML Document string or IO to parse
|
12
14
|
def parse(data)
|
13
15
|
if !data.respond_to?(:read)
|
14
|
-
data = StringIO.new(data ||
|
16
|
+
data = StringIO.new(data || "")
|
15
17
|
end
|
16
18
|
|
17
|
-
|
18
|
-
if char.nil?
|
19
|
+
if data.eof?
|
19
20
|
{}
|
20
21
|
else
|
21
|
-
data.ungetc(char)
|
22
22
|
LibXML::XML::Parser.io(data).parse.to_hash
|
23
23
|
end
|
24
24
|
end
|
25
|
-
|
26
25
|
end
|
27
26
|
end
|
28
27
|
|
@@ -35,20 +34,20 @@ module LibXML #:nodoc:
|
|
35
34
|
end
|
36
35
|
|
37
36
|
module Node #:nodoc:
|
38
|
-
CONTENT_ROOT =
|
37
|
+
CONTENT_ROOT = "__content__".freeze
|
39
38
|
|
40
39
|
# Convert XML document to hash.
|
41
40
|
#
|
42
41
|
# hash::
|
43
42
|
# Hash to merge the converted element into.
|
44
|
-
def to_hash(hash={})
|
43
|
+
def to_hash(hash = {})
|
45
44
|
node_hash = {}
|
46
45
|
|
47
46
|
# Insert node hash into parent hash correctly.
|
48
47
|
case hash[name]
|
49
|
-
|
50
|
-
|
51
|
-
|
48
|
+
when Array then hash[name] << node_hash
|
49
|
+
when Hash then hash[name] = [hash[name], node_hash]
|
50
|
+
when nil then hash[name] = node_hash
|
52
51
|
end
|
53
52
|
|
54
53
|
# Handle child elements
|
@@ -56,7 +55,7 @@ module LibXML #:nodoc:
|
|
56
55
|
if c.element?
|
57
56
|
c.to_hash(node_hash)
|
58
57
|
elsif c.text? || c.cdata?
|
59
|
-
node_hash[CONTENT_ROOT] ||=
|
58
|
+
node_hash[CONTENT_ROOT] ||= "".dup
|
60
59
|
node_hash[CONTENT_ROOT] << c.content
|
61
60
|
end
|
62
61
|
end
|
@@ -75,5 +74,7 @@ module LibXML #:nodoc:
|
|
75
74
|
end
|
76
75
|
end
|
77
76
|
|
78
|
-
|
79
|
-
|
77
|
+
# :enddoc:
|
78
|
+
|
79
|
+
LibXML::XML::Document.include(LibXML::Conversions::Document)
|
80
|
+
LibXML::XML::Node.include(LibXML::Conversions::Node)
|
@@ -1,6 +1,8 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
require
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "libxml"
|
4
|
+
require "active_support/core_ext/object/blank"
|
5
|
+
require "stringio"
|
4
6
|
|
5
7
|
module ActiveSupport
|
6
8
|
module XmlMini_LibXMLSAX #:nodoc:
|
@@ -9,11 +11,10 @@ module ActiveSupport
|
|
9
11
|
# Class that will build the hash while the XML document
|
10
12
|
# is being parsed using SAX events.
|
11
13
|
class HashBuilder
|
12
|
-
|
13
14
|
include LibXML::XML::SaxParser::Callbacks
|
14
15
|
|
15
|
-
CONTENT_KEY =
|
16
|
-
HASH_SIZE_KEY =
|
16
|
+
CONTENT_KEY = "__content__".freeze
|
17
|
+
HASH_SIZE_KEY = "__hash_size__".freeze
|
17
18
|
|
18
19
|
attr_reader :hash
|
19
20
|
|
@@ -22,7 +23,7 @@ module ActiveSupport
|
|
22
23
|
end
|
23
24
|
|
24
25
|
def on_start_document
|
25
|
-
@hash = { CONTENT_KEY =>
|
26
|
+
@hash = { CONTENT_KEY => "".dup }
|
26
27
|
@hash_stack = [@hash]
|
27
28
|
end
|
28
29
|
|
@@ -32,20 +33,20 @@ module ActiveSupport
|
|
32
33
|
end
|
33
34
|
|
34
35
|
def on_start_element(name, attrs = {})
|
35
|
-
new_hash = { CONTENT_KEY =>
|
36
|
+
new_hash = { CONTENT_KEY => "".dup }.merge!(attrs)
|
36
37
|
new_hash[HASH_SIZE_KEY] = new_hash.size + 1
|
37
38
|
|
38
39
|
case current_hash[name]
|
39
|
-
|
40
|
-
|
41
|
-
|
40
|
+
when Array then current_hash[name] << new_hash
|
41
|
+
when Hash then current_hash[name] = [current_hash[name], new_hash]
|
42
|
+
when nil then current_hash[name] = new_hash
|
42
43
|
end
|
43
44
|
|
44
45
|
@hash_stack.push(new_hash)
|
45
46
|
end
|
46
47
|
|
47
48
|
def on_end_element(name)
|
48
|
-
if current_hash.length > current_hash.delete(HASH_SIZE_KEY) && current_hash[CONTENT_KEY].blank? || current_hash[CONTENT_KEY] ==
|
49
|
+
if current_hash.length > current_hash.delete(HASH_SIZE_KEY) && current_hash[CONTENT_KEY].blank? || current_hash[CONTENT_KEY] == ""
|
49
50
|
current_hash.delete(CONTENT_KEY)
|
50
51
|
end
|
51
52
|
@hash_stack.pop
|
@@ -63,18 +64,15 @@ module ActiveSupport
|
|
63
64
|
|
64
65
|
def parse(data)
|
65
66
|
if !data.respond_to?(:read)
|
66
|
-
data = StringIO.new(data ||
|
67
|
+
data = StringIO.new(data || "")
|
67
68
|
end
|
68
69
|
|
69
|
-
|
70
|
-
if char.nil?
|
70
|
+
if data.eof?
|
71
71
|
{}
|
72
72
|
else
|
73
|
-
data.ungetc(char)
|
74
|
-
|
75
73
|
LibXML::XML::Error.set_handler(&LibXML::XML::Error::QUIET_HANDLER)
|
76
74
|
parser = LibXML::XML::SaxParser.io(data)
|
77
|
-
document =
|
75
|
+
document = document_class.new
|
78
76
|
|
79
77
|
parser.callbacks = document
|
80
78
|
parser.parse
|
@@ -1,11 +1,13 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
begin
|
2
|
-
require
|
4
|
+
require "nokogiri"
|
3
5
|
rescue LoadError => e
|
4
6
|
$stderr.puts "You don't have nokogiri installed in your application. Please add it to your Gemfile and run bundle install"
|
5
7
|
raise e
|
6
8
|
end
|
7
|
-
require
|
8
|
-
require
|
9
|
+
require "active_support/core_ext/object/blank"
|
10
|
+
require "stringio"
|
9
11
|
|
10
12
|
module ActiveSupport
|
11
13
|
module XmlMini_Nokogiri #:nodoc:
|
@@ -16,14 +18,12 @@ module ActiveSupport
|
|
16
18
|
# XML Document string or IO to parse
|
17
19
|
def parse(data)
|
18
20
|
if !data.respond_to?(:read)
|
19
|
-
data = StringIO.new(data ||
|
21
|
+
data = StringIO.new(data || "")
|
20
22
|
end
|
21
23
|
|
22
|
-
|
23
|
-
if char.nil?
|
24
|
+
if data.eof?
|
24
25
|
{}
|
25
26
|
else
|
26
|
-
data.ungetc(char)
|
27
27
|
doc = Nokogiri::XML(data)
|
28
28
|
raise doc.errors.first if doc.errors.length > 0
|
29
29
|
doc.to_hash
|
@@ -38,20 +38,20 @@ module ActiveSupport
|
|
38
38
|
end
|
39
39
|
|
40
40
|
module Node #:nodoc:
|
41
|
-
CONTENT_ROOT =
|
41
|
+
CONTENT_ROOT = "__content__".freeze
|
42
42
|
|
43
43
|
# Convert XML document to hash.
|
44
44
|
#
|
45
45
|
# hash::
|
46
46
|
# Hash to merge the converted element into.
|
47
|
-
def to_hash(hash={})
|
47
|
+
def to_hash(hash = {})
|
48
48
|
node_hash = {}
|
49
49
|
|
50
50
|
# Insert node hash into parent hash correctly.
|
51
51
|
case hash[name]
|
52
|
-
|
53
|
-
|
54
|
-
|
52
|
+
when Array then hash[name] << node_hash
|
53
|
+
when Hash then hash[name] = [hash[name], node_hash]
|
54
|
+
when nil then hash[name] = node_hash
|
55
55
|
end
|
56
56
|
|
57
57
|
# Handle child elements
|
@@ -59,7 +59,7 @@ module ActiveSupport
|
|
59
59
|
if c.element?
|
60
60
|
c.to_hash(node_hash)
|
61
61
|
elsif c.text? || c.cdata?
|
62
|
-
node_hash[CONTENT_ROOT] ||=
|
62
|
+
node_hash[CONTENT_ROOT] ||= "".dup
|
63
63
|
node_hash[CONTENT_ROOT] << c.content
|
64
64
|
end
|
65
65
|
end
|
@@ -77,7 +77,7 @@ module ActiveSupport
|
|
77
77
|
end
|
78
78
|
end
|
79
79
|
|
80
|
-
Nokogiri::XML::Document.
|
81
|
-
Nokogiri::XML::Node.
|
80
|
+
Nokogiri::XML::Document.include(Conversions::Document)
|
81
|
+
Nokogiri::XML::Node.include(Conversions::Node)
|
82
82
|
end
|
83
83
|
end
|