activesupport 4.0.12 → 7.0.2.4
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 +249 -501
- data/MIT-LICENSE +2 -2
- data/README.rdoc +10 -5
- data/lib/active_support/actionable_error.rb +48 -0
- data/lib/active_support/all.rb +5 -3
- data/lib/active_support/array_inquirer.rb +48 -0
- data/lib/active_support/backtrace_cleaner.rb +41 -13
- data/lib/active_support/benchmarkable.rb +7 -15
- data/lib/active_support/builder.rb +3 -1
- data/lib/active_support/cache/file_store.rb +96 -74
- data/lib/active_support/cache/mem_cache_store.rb +211 -103
- data/lib/active_support/cache/memory_store.rb +90 -58
- data/lib/active_support/cache/null_store.rb +19 -7
- data/lib/active_support/cache/redis_cache_store.rb +468 -0
- data/lib/active_support/cache/strategy/local_cache.rb +86 -83
- data/lib/active_support/cache/strategy/local_cache_middleware.rb +45 -0
- data/lib/active_support/cache.rb +580 -241
- data/lib/active_support/callbacks.rb +812 -425
- data/lib/active_support/code_generator.rb +65 -0
- data/lib/active_support/concern.rb +103 -14
- data/lib/active_support/concurrency/load_interlock_aware_monitor.rb +33 -0
- data/lib/active_support/concurrency/share_lock.rb +226 -0
- data/lib/active_support/configurable.rb +21 -19
- data/lib/active_support/configuration_file.rb +51 -0
- data/lib/active_support/core_ext/array/access.rb +47 -1
- data/lib/active_support/core_ext/array/conversions.rb +35 -44
- data/lib/active_support/core_ext/array/deprecated_conversions.rb +25 -0
- data/lib/active_support/core_ext/array/extract.rb +21 -0
- data/lib/active_support/core_ext/array/extract_options.rb +2 -0
- data/lib/active_support/core_ext/array/grouping.rb +26 -16
- data/lib/active_support/core_ext/array/inquiry.rb +19 -0
- data/lib/active_support/core_ext/array/wrap.rb +7 -4
- data/lib/active_support/core_ext/array.rb +10 -7
- data/lib/active_support/core_ext/benchmark.rb +5 -3
- data/lib/active_support/core_ext/big_decimal/conversions.rb +9 -26
- data/lib/active_support/core_ext/big_decimal.rb +3 -1
- data/lib/active_support/core_ext/class/attribute.rb +52 -49
- data/lib/active_support/core_ext/class/attribute_accessors.rb +5 -169
- data/lib/active_support/core_ext/class/subclasses.rb +25 -26
- data/lib/active_support/core_ext/class.rb +4 -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 +31 -18
- data/lib/active_support/core_ext/date/conversions.rb +43 -32
- data/lib/active_support/core_ext/date/deprecated_conversions.rb +26 -0
- data/lib/active_support/core_ext/date/zones.rb +5 -34
- data/lib/active_support/core_ext/date.rb +7 -4
- data/lib/active_support/core_ext/date_and_time/calculations.rb +198 -66
- data/lib/active_support/core_ext/date_and_time/compatibility.rb +31 -0
- data/lib/active_support/core_ext/date_and_time/zones.rb +40 -0
- 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 +79 -38
- data/lib/active_support/core_ext/date_time/compatibility.rb +18 -0
- data/lib/active_support/core_ext/date_time/conversions.rb +31 -26
- data/lib/active_support/core_ext/date_time/deprecated_conversions.rb +22 -0
- data/lib/active_support/core_ext/date_time.rb +8 -4
- data/lib/active_support/core_ext/digest/uuid.rb +79 -0
- data/lib/active_support/core_ext/digest.rb +3 -0
- data/lib/active_support/core_ext/enumerable.rb +249 -17
- data/lib/active_support/core_ext/file/atomic.rb +41 -32
- data/lib/active_support/core_ext/file.rb +3 -1
- data/lib/active_support/core_ext/hash/conversions.rb +71 -49
- data/lib/active_support/core_ext/hash/deep_merge.rb +9 -13
- data/lib/active_support/core_ext/hash/deep_transform_values.rb +46 -0
- data/lib/active_support/core_ext/hash/except.rb +14 -5
- data/lib/active_support/core_ext/hash/indifferent_access.rb +5 -3
- data/lib/active_support/core_ext/hash/keys.rb +39 -56
- data/lib/active_support/core_ext/hash/reverse_merge.rb +5 -2
- data/lib/active_support/core_ext/hash/slice.rb +8 -23
- data/lib/active_support/core_ext/hash.rb +10 -8
- data/lib/active_support/core_ext/integer/inflections.rb +3 -1
- data/lib/active_support/core_ext/integer/multiple.rb +3 -1
- data/lib/active_support/core_ext/integer/time.rb +11 -33
- data/lib/active_support/core_ext/integer.rb +5 -3
- data/lib/active_support/core_ext/kernel/concern.rb +14 -0
- data/lib/active_support/core_ext/kernel/reporting.rb +9 -78
- data/lib/active_support/core_ext/kernel/singleton_class.rb +2 -0
- data/lib/active_support/core_ext/kernel.rb +5 -4
- data/lib/active_support/core_ext/load_error.rb +5 -21
- 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 -8
- data/lib/active_support/core_ext/module/attribute_accessors.rb +186 -44
- data/lib/active_support/core_ext/module/attribute_accessors_per_thread.rb +157 -0
- data/lib/active_support/core_ext/module/concerning.rb +140 -0
- data/lib/active_support/core_ext/module/delegation.rb +172 -45
- data/lib/active_support/core_ext/module/deprecation.rb +3 -3
- data/lib/active_support/core_ext/module/introspection.rb +23 -38
- data/lib/active_support/core_ext/module/redefine_method.rb +40 -0
- data/lib/active_support/core_ext/module/remove_method.rb +8 -3
- data/lib/active_support/core_ext/module.rb +13 -10
- data/lib/active_support/core_ext/name_error.rb +45 -4
- data/lib/active_support/core_ext/numeric/bytes.rb +22 -0
- data/lib/active_support/core_ext/numeric/conversions.rb +135 -127
- data/lib/active_support/core_ext/numeric/deprecated_conversions.rb +60 -0
- data/lib/active_support/core_ext/numeric/time.rb +37 -50
- data/lib/active_support/core_ext/numeric.rb +6 -3
- data/lib/active_support/core_ext/object/acts_like.rb +41 -6
- data/lib/active_support/core_ext/object/blank.rb +70 -20
- data/lib/active_support/core_ext/object/conversions.rb +6 -4
- data/lib/active_support/core_ext/object/deep_dup.rb +19 -10
- data/lib/active_support/core_ext/object/duplicable.rb +17 -47
- data/lib/active_support/core_ext/object/inclusion.rb +18 -15
- data/lib/active_support/core_ext/object/instance_variables.rb +3 -1
- data/lib/active_support/core_ext/object/json.rb +244 -0
- data/lib/active_support/core_ext/object/to_param.rb +3 -1
- data/lib/active_support/core_ext/object/to_query.rb +21 -8
- data/lib/active_support/core_ext/object/try.rb +106 -26
- data/lib/active_support/core_ext/object/with_options.rb +64 -5
- data/lib/active_support/core_ext/object.rb +14 -12
- data/lib/active_support/core_ext/pathname/existence.rb +21 -0
- data/lib/active_support/core_ext/pathname.rb +3 -0
- data/lib/active_support/core_ext/range/compare_range.rb +57 -0
- data/lib/active_support/core_ext/range/conversions.rb +37 -15
- data/lib/active_support/core_ext/range/deprecated_conversions.rb +26 -0
- data/lib/active_support/core_ext/range/each.rb +18 -17
- data/lib/active_support/core_ext/range/include_time_with_zone.rb +7 -0
- data/lib/active_support/core_ext/range/overlaps.rb +2 -0
- data/lib/active_support/core_ext/range.rb +7 -4
- data/lib/active_support/core_ext/regexp.rb +10 -1
- data/lib/active_support/core_ext/securerandom.rb +45 -0
- data/lib/active_support/core_ext/string/access.rb +42 -51
- data/lib/active_support/core_ext/string/behavior.rb +3 -1
- data/lib/active_support/core_ext/string/conversions.rb +18 -13
- data/lib/active_support/core_ext/string/exclude.rb +5 -3
- data/lib/active_support/core_ext/string/filters.rb +97 -7
- data/lib/active_support/core_ext/string/indent.rb +6 -4
- data/lib/active_support/core_ext/string/inflections.rb +106 -25
- data/lib/active_support/core_ext/string/inquiry.rb +4 -1
- data/lib/active_support/core_ext/string/multibyte.rb +18 -9
- data/lib/active_support/core_ext/string/output_safety.rb +227 -54
- data/lib/active_support/core_ext/string/starts_ends_with.rb +4 -2
- data/lib/active_support/core_ext/string/strip.rb +6 -5
- data/lib/active_support/core_ext/string/zones.rb +4 -1
- data/lib/active_support/core_ext/string.rb +15 -13
- data/lib/active_support/core_ext/symbol/starts_ends_with.rb +6 -0
- data/lib/active_support/core_ext/symbol.rb +3 -0
- data/lib/active_support/core_ext/time/acts_like.rb +3 -1
- data/lib/active_support/core_ext/time/calculations.rb +178 -116
- data/lib/active_support/core_ext/time/compatibility.rb +16 -0
- data/lib/active_support/core_ext/time/conversions.rb +37 -25
- data/lib/active_support/core_ext/time/deprecated_conversions.rb +22 -0
- data/lib/active_support/core_ext/time/zones.rb +44 -42
- data/lib/active_support/core_ext/time.rb +8 -5
- data/lib/active_support/core_ext/uri.rb +4 -25
- data/lib/active_support/core_ext.rb +4 -2
- data/lib/active_support/current_attributes/test_helper.rb +13 -0
- data/lib/active_support/current_attributes.rb +226 -0
- data/lib/active_support/dependencies/autoload.rb +3 -1
- data/lib/active_support/dependencies/interlock.rb +49 -0
- data/lib/active_support/dependencies/require_dependency.rb +28 -0
- data/lib/active_support/dependencies.rb +71 -696
- data/lib/active_support/deprecation/behaviors.rb +65 -16
- data/lib/active_support/deprecation/constant_accessor.rb +52 -0
- data/lib/active_support/deprecation/disallowed.rb +56 -0
- data/lib/active_support/deprecation/instance_delegator.rb +16 -2
- data/lib/active_support/deprecation/method_wrappers.rb +62 -21
- data/lib/active_support/deprecation/proxy_wrappers.rb +82 -31
- data/lib/active_support/deprecation/reporting.rb +81 -18
- data/lib/active_support/deprecation.rb +19 -11
- data/lib/active_support/descendants_tracker.rb +192 -34
- data/lib/active_support/digest.rb +22 -0
- data/lib/active_support/duration/iso8601_parser.rb +123 -0
- data/lib/active_support/duration/iso8601_serializer.rb +67 -0
- data/lib/active_support/duration.rb +437 -39
- data/lib/active_support/encrypted_configuration.rb +56 -0
- data/lib/active_support/encrypted_file.rb +117 -0
- data/lib/active_support/environment_inquirer.rb +20 -0
- data/lib/active_support/error_reporter.rb +117 -0
- data/lib/active_support/evented_file_update_checker.rb +170 -0
- data/lib/active_support/execution_context/test_helper.rb +13 -0
- data/lib/active_support/execution_context.rb +53 -0
- data/lib/active_support/execution_wrapper.rb +151 -0
- data/lib/active_support/executor/test_helper.rb +7 -0
- data/lib/active_support/executor.rb +8 -0
- data/lib/active_support/file_update_checker.rb +62 -37
- data/lib/active_support/fork_tracker.rb +71 -0
- data/lib/active_support/gem_version.rb +17 -0
- data/lib/active_support/gzip.rb +7 -5
- data/lib/active_support/hash_with_indifferent_access.rb +207 -54
- data/lib/active_support/html_safe_translation.rb +43 -0
- data/lib/active_support/i18n.rb +10 -6
- data/lib/active_support/i18n_railtie.rb +48 -19
- data/lib/active_support/inflections.rb +19 -12
- data/lib/active_support/inflector/inflections.rb +97 -37
- data/lib/active_support/inflector/methods.rb +192 -157
- data/lib/active_support/inflector/transliterate.rb +83 -33
- data/lib/active_support/inflector.rb +7 -5
- data/lib/active_support/isolated_execution_state.rb +64 -0
- data/lib/active_support/json/decoding.rb +37 -42
- data/lib/active_support/json/encoding.rb +93 -293
- data/lib/active_support/json.rb +4 -2
- data/lib/active_support/key_generator.rb +30 -47
- data/lib/active_support/lazy_load_hooks.rb +54 -21
- data/lib/active_support/locale/en.rb +33 -0
- data/lib/active_support/locale/en.yml +10 -4
- data/lib/active_support/log_subscriber/test_helper.rb +14 -12
- data/lib/active_support/log_subscriber.rb +61 -18
- data/lib/active_support/logger.rb +40 -4
- data/lib/active_support/logger_silence.rb +17 -20
- data/lib/active_support/logger_thread_safe_level.rb +69 -0
- data/lib/active_support/message_encryptor.rb +178 -55
- data/lib/active_support/message_verifier.rb +195 -26
- data/lib/active_support/messages/metadata.rb +80 -0
- data/lib/active_support/messages/rotation_configuration.rb +23 -0
- data/lib/active_support/messages/rotator.rb +57 -0
- data/lib/active_support/multibyte/chars.rb +45 -92
- data/lib/active_support/multibyte/unicode.rb +44 -377
- data/lib/active_support/multibyte.rb +5 -3
- data/lib/active_support/notifications/fanout.rb +177 -44
- data/lib/active_support/notifications/instrumenter.rb +117 -17
- data/lib/active_support/notifications.rb +106 -39
- data/lib/active_support/number_helper/number_converter.rb +181 -0
- data/lib/active_support/number_helper/number_to_currency_converter.rb +46 -0
- data/lib/active_support/number_helper/number_to_delimited_converter.rb +30 -0
- data/lib/active_support/number_helper/number_to_human_converter.rb +69 -0
- data/lib/active_support/number_helper/number_to_human_size_converter.rb +60 -0
- data/lib/active_support/number_helper/number_to_percentage_converter.rb +16 -0
- data/lib/active_support/number_helper/number_to_phone_converter.rb +59 -0
- data/lib/active_support/number_helper/number_to_rounded_converter.rb +59 -0
- data/lib/active_support/number_helper/rounding_helper.rb +46 -0
- data/lib/active_support/number_helper.rb +152 -394
- data/lib/active_support/option_merger.rb +18 -5
- data/lib/active_support/ordered_hash.rb +8 -6
- data/lib/active_support/ordered_options.rb +43 -7
- data/lib/active_support/parameter_filter.rb +138 -0
- data/lib/active_support/per_thread_registry.rb +24 -11
- data/lib/active_support/proxy_object.rb +2 -0
- data/lib/active_support/rails.rb +10 -11
- data/lib/active_support/railtie.rb +118 -12
- data/lib/active_support/reloader.rb +130 -0
- data/lib/active_support/rescuable.rb +112 -57
- data/lib/active_support/ruby_features.rb +7 -0
- data/lib/active_support/secure_compare_rotator.rb +51 -0
- data/lib/active_support/security_utils.rb +38 -0
- data/lib/active_support/string_inquirer.rb +11 -4
- data/lib/active_support/subscriber.rb +109 -39
- data/lib/active_support/tagged_logging.rb +54 -17
- data/lib/active_support/test_case.rb +121 -37
- data/lib/active_support/testing/assertions.rb +177 -39
- data/lib/active_support/testing/autorun.rb +5 -3
- data/lib/active_support/testing/constant_lookup.rb +3 -6
- data/lib/active_support/testing/declarative.rb +10 -22
- data/lib/active_support/testing/deprecation.rb +65 -11
- data/lib/active_support/testing/file_fixtures.rb +38 -0
- data/lib/active_support/testing/isolation.rb +56 -87
- data/lib/active_support/testing/method_call_assertions.rb +70 -0
- data/lib/active_support/testing/parallelization/server.rb +82 -0
- data/lib/active_support/testing/parallelization/worker.rb +103 -0
- data/lib/active_support/testing/parallelization.rb +55 -0
- data/lib/active_support/testing/parallelize_executor.rb +76 -0
- data/lib/active_support/testing/setup_and_teardown.rb +30 -10
- data/lib/active_support/testing/stream.rb +41 -0
- data/lib/active_support/testing/tagged_logging.rb +6 -4
- data/lib/active_support/testing/time_helpers.rb +246 -0
- data/lib/active_support/time.rb +13 -13
- data/lib/active_support/time_with_zone.rb +315 -90
- data/lib/active_support/values/time_zone.rb +306 -135
- data/lib/active_support/version.rb +6 -7
- data/lib/active_support/xml_mini/jdom.rb +117 -115
- data/lib/active_support/xml_mini/libxml.rb +22 -21
- data/lib/active_support/xml_mini/libxmlsax.rb +17 -19
- data/lib/active_support/xml_mini/nokogiri.rb +19 -19
- data/lib/active_support/xml_mini/nokogirisax.rb +16 -17
- data/lib/active_support/xml_mini/rexml.rb +25 -17
- data/lib/active_support/xml_mini.rb +67 -56
- data/lib/active_support.rb +58 -3
- metadata +125 -66
- data/lib/active_support/basic_object.rb +0 -11
- data/lib/active_support/buffered_logger.rb +0 -21
- data/lib/active_support/concurrency/latch.rb +0 -27
- data/lib/active_support/core_ext/array/prepend_and_append.rb +0 -7
- data/lib/active_support/core_ext/array/uniq_by.rb +0 -19
- data/lib/active_support/core_ext/class/delegating_attributes.rb +0 -40
- data/lib/active_support/core_ext/date_time/zones.rb +0 -24
- data/lib/active_support/core_ext/hash/diff.rb +0 -14
- data/lib/active_support/core_ext/kernel/agnostics.rb +0 -11
- data/lib/active_support/core_ext/kernel/debugger.rb +0 -10
- data/lib/active_support/core_ext/logger.rb +0 -67
- data/lib/active_support/core_ext/marshal.rb +0 -21
- data/lib/active_support/core_ext/module/qualified_const.rb +0 -52
- data/lib/active_support/core_ext/module/reachable.rb +0 -8
- data/lib/active_support/core_ext/object/to_json.rb +0 -27
- data/lib/active_support/core_ext/proc.rb +0 -17
- data/lib/active_support/core_ext/range/include_range.rb +0 -23
- data/lib/active_support/core_ext/string/encoding.rb +0 -8
- data/lib/active_support/core_ext/struct.rb +0 -6
- data/lib/active_support/core_ext/thread.rb +0 -79
- data/lib/active_support/core_ext/time/marshal.rb +0 -30
- data/lib/active_support/file_watcher.rb +0 -36
- data/lib/active_support/json/variable.rb +0 -18
- data/lib/active_support/testing/pending.rb +0 -14
- data/lib/active_support/values/unicode_tables.dat +0 -0
@@ -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
|
@@ -13,10 +15,10 @@ java_import org.xml.sax.Attributes unless defined? Attributes
|
|
13
15
|
java_import org.w3c.dom.Node unless defined? Node
|
14
16
|
|
15
17
|
module ActiveSupport
|
16
|
-
module XmlMini_JDOM
|
18
|
+
module XmlMini_JDOM # :nodoc:
|
17
19
|
extend self
|
18
20
|
|
19
|
-
CONTENT_KEY =
|
21
|
+
CONTENT_KEY = "__content__"
|
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,135 @@ 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
|
56
|
+
# Convert an XML element and merge into the hash
|
57
|
+
#
|
58
|
+
# hash::
|
59
|
+
# Hash to merge the converted element into.
|
60
|
+
# element::
|
61
|
+
# XML element to merge into hash
|
62
|
+
def merge_element!(hash, element, depth)
|
63
|
+
raise "Document too deep!" if depth == 0
|
64
|
+
delete_empty(hash)
|
65
|
+
merge!(hash, element.tag_name, collapse(element, depth))
|
66
|
+
end
|
54
67
|
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
# Hash to merge the converted element into.
|
59
|
-
# element::
|
60
|
-
# XML element to merge into hash
|
61
|
-
def merge_element!(hash, element)
|
62
|
-
delete_empty(hash)
|
63
|
-
merge!(hash, element.tag_name, collapse(element))
|
64
|
-
end
|
65
|
-
|
66
|
-
def delete_empty(hash)
|
67
|
-
hash.delete(CONTENT_KEY) if hash[CONTENT_KEY] == ''
|
68
|
-
end
|
69
|
-
|
70
|
-
# Actually converts an XML document element into a data structure.
|
71
|
-
#
|
72
|
-
# element::
|
73
|
-
# The document element to be collapsed.
|
74
|
-
def collapse(element)
|
75
|
-
hash = get_attributes(element)
|
68
|
+
def delete_empty(hash)
|
69
|
+
hash.delete(CONTENT_KEY) if hash[CONTENT_KEY] == ""
|
70
|
+
end
|
76
71
|
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
72
|
+
# Actually converts an XML document element into a data structure.
|
73
|
+
#
|
74
|
+
# element::
|
75
|
+
# The document element to be collapsed.
|
76
|
+
def collapse(element, depth)
|
77
|
+
hash = get_attributes(element)
|
78
|
+
|
79
|
+
child_nodes = element.child_nodes
|
80
|
+
if child_nodes.length > 0
|
81
|
+
(0...child_nodes.length).each do |i|
|
82
|
+
child = child_nodes.item(i)
|
83
|
+
merge_element!(hash, child, depth - 1) unless child.node_type == Node.TEXT_NODE
|
84
|
+
end
|
85
|
+
merge_texts!(hash, element) unless empty_content?(element)
|
86
|
+
hash
|
87
|
+
else
|
88
|
+
merge_texts!(hash, element)
|
82
89
|
end
|
83
|
-
merge_texts!(hash, element) unless empty_content?(element)
|
84
|
-
hash
|
85
|
-
else
|
86
|
-
merge_texts!(hash, element)
|
87
90
|
end
|
88
|
-
end
|
89
91
|
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
92
|
+
# Merge all the texts of an element into the hash
|
93
|
+
#
|
94
|
+
# hash::
|
95
|
+
# Hash to add the converted element to.
|
96
|
+
# element::
|
97
|
+
# XML element whose texts are to me merged into the hash
|
98
|
+
def merge_texts!(hash, element)
|
99
|
+
delete_empty(hash)
|
100
|
+
text_children = texts(element)
|
101
|
+
if text_children.join.empty?
|
102
|
+
hash
|
103
|
+
else
|
104
|
+
# must use value to prevent double-escaping
|
105
|
+
merge!(hash, CONTENT_KEY, text_children.join)
|
106
|
+
end
|
104
107
|
end
|
105
|
-
end
|
106
108
|
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
109
|
+
# Adds a new key/value pair to an existing Hash. If the key to be added
|
110
|
+
# already exists and the existing value associated with key is not
|
111
|
+
# an Array, it will be wrapped in an Array. Then the new value is
|
112
|
+
# appended to that Array.
|
113
|
+
#
|
114
|
+
# hash::
|
115
|
+
# Hash to add key/value pair to.
|
116
|
+
# key::
|
117
|
+
# Key to be added.
|
118
|
+
# value::
|
119
|
+
# Value to be associated with key.
|
120
|
+
def merge!(hash, key, value)
|
121
|
+
if hash.has_key?(key)
|
122
|
+
if hash[key].instance_of?(Array)
|
123
|
+
hash[key] << value
|
124
|
+
else
|
125
|
+
hash[key] = [hash[key], value]
|
126
|
+
end
|
127
|
+
elsif value.instance_of?(Array)
|
128
|
+
hash[key] = [value]
|
122
129
|
else
|
123
|
-
hash[key] =
|
130
|
+
hash[key] = value
|
124
131
|
end
|
125
|
-
|
126
|
-
hash[key] = [value]
|
127
|
-
else
|
128
|
-
hash[key] = value
|
132
|
+
hash
|
129
133
|
end
|
130
|
-
hash
|
131
|
-
end
|
132
134
|
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
135
|
+
# Converts the attributes array of an XML element into a hash.
|
136
|
+
# Returns an empty Hash if node has no attributes.
|
137
|
+
#
|
138
|
+
# element::
|
139
|
+
# XML element to extract attributes from.
|
140
|
+
def get_attributes(element)
|
141
|
+
attribute_hash = {}
|
142
|
+
attributes = element.attributes
|
143
|
+
(0...attributes.length).each do |i|
|
144
|
+
attribute_hash[CONTENT_KEY] ||= ""
|
145
|
+
attribute_hash[attributes.item(i).name] = attributes.item(i).value
|
146
|
+
end
|
147
|
+
attribute_hash
|
148
|
+
end
|
147
149
|
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
150
|
+
# Determines if a document element has text content
|
151
|
+
#
|
152
|
+
# element::
|
153
|
+
# XML element to be checked.
|
154
|
+
def texts(element)
|
155
|
+
texts = []
|
156
|
+
child_nodes = element.child_nodes
|
157
|
+
(0...child_nodes.length).each do |i|
|
158
|
+
item = child_nodes.item(i)
|
159
|
+
if item.node_type == Node.TEXT_NODE
|
160
|
+
texts << item.get_data
|
161
|
+
end
|
159
162
|
end
|
163
|
+
texts
|
160
164
|
end
|
161
|
-
texts
|
162
|
-
end
|
163
165
|
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
166
|
+
# Determines if a document element has text content
|
167
|
+
#
|
168
|
+
# element::
|
169
|
+
# XML element to be checked.
|
170
|
+
def empty_content?(element)
|
171
|
+
text = +""
|
172
|
+
child_nodes = element.child_nodes
|
173
|
+
(0...child_nodes.length).each do |i|
|
174
|
+
item = child_nodes.item(i)
|
175
|
+
if item.node_type == Node.TEXT_NODE
|
176
|
+
text << item.get_data.strip
|
177
|
+
end
|
175
178
|
end
|
179
|
+
text.strip.length == 0
|
176
180
|
end
|
177
|
-
text.strip.length == 0
|
178
|
-
end
|
179
181
|
end
|
180
182
|
end
|
@@ -1,9 +1,11 @@
|
|
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
|
-
module XmlMini_LibXML
|
8
|
+
module XmlMini_LibXML # :nodoc:
|
7
9
|
extend self
|
8
10
|
|
9
11
|
# Parse an XML Document string or IO into a simple hash using libxml.
|
@@ -11,44 +13,41 @@ 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
|
|
29
|
-
module LibXML
|
30
|
-
module Conversions
|
31
|
-
module Document
|
28
|
+
module LibXML # :nodoc:
|
29
|
+
module Conversions # :nodoc:
|
30
|
+
module Document # :nodoc:
|
32
31
|
def to_hash
|
33
32
|
root.to_hash
|
34
33
|
end
|
35
34
|
end
|
36
35
|
|
37
|
-
module Node
|
38
|
-
CONTENT_ROOT =
|
36
|
+
module Node # :nodoc:
|
37
|
+
CONTENT_ROOT = "__content__"
|
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] ||= +""
|
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,19 +1,20 @@
|
|
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
|
-
module XmlMini_LibXMLSAX
|
8
|
+
module XmlMini_LibXMLSAX # :nodoc:
|
7
9
|
extend self
|
8
10
|
|
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__"
|
17
|
+
HASH_SIZE_KEY = "__hash_size__"
|
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 => +"" }
|
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 => +"" }.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,14 +1,16 @@
|
|
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
|
-
module XmlMini_Nokogiri
|
13
|
+
module XmlMini_Nokogiri # :nodoc:
|
12
14
|
extend self
|
13
15
|
|
14
16
|
# Parse an XML Document string or IO into a simple hash using libxml / nokogiri.
|
@@ -16,42 +18,40 @@ 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
|
30
30
|
end
|
31
31
|
end
|
32
32
|
|
33
|
-
module Conversions
|
34
|
-
module Document
|
33
|
+
module Conversions # :nodoc:
|
34
|
+
module Document # :nodoc:
|
35
35
|
def to_hash
|
36
36
|
root.to_hash
|
37
37
|
end
|
38
38
|
end
|
39
39
|
|
40
|
-
module Node
|
41
|
-
CONTENT_ROOT =
|
40
|
+
module Node # :nodoc:
|
41
|
+
CONTENT_ROOT = "__content__"
|
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] ||= +""
|
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
|
@@ -1,22 +1,23 @@
|
|
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
|
-
module XmlMini_NokogiriSAX
|
13
|
+
module XmlMini_NokogiriSAX # :nodoc:
|
12
14
|
extend self
|
13
15
|
|
14
16
|
# Class that will build the hash while the XML document
|
15
17
|
# is being parsed using SAX events.
|
16
18
|
class HashBuilder < Nokogiri::XML::SAX::Document
|
17
|
-
|
18
|
-
|
19
|
-
HASH_SIZE_KEY = '__hash_size__'.freeze
|
19
|
+
CONTENT_KEY = "__content__"
|
20
|
+
HASH_SIZE_KEY = "__hash_size__"
|
20
21
|
|
21
22
|
attr_reader :hash
|
22
23
|
|
@@ -38,20 +39,20 @@ module ActiveSupport
|
|
38
39
|
end
|
39
40
|
|
40
41
|
def start_element(name, attrs = [])
|
41
|
-
new_hash = { CONTENT_KEY =>
|
42
|
+
new_hash = { CONTENT_KEY => +"" }.merge!(Hash[attrs])
|
42
43
|
new_hash[HASH_SIZE_KEY] = new_hash.size + 1
|
43
44
|
|
44
45
|
case current_hash[name]
|
45
|
-
|
46
|
-
|
47
|
-
|
46
|
+
when Array then current_hash[name] << new_hash
|
47
|
+
when Hash then current_hash[name] = [current_hash[name], new_hash]
|
48
|
+
when nil then current_hash[name] = new_hash
|
48
49
|
end
|
49
50
|
|
50
51
|
@hash_stack.push(new_hash)
|
51
52
|
end
|
52
53
|
|
53
54
|
def end_element(name)
|
54
|
-
if current_hash.length > current_hash.delete(HASH_SIZE_KEY) && current_hash[CONTENT_KEY].blank? || current_hash[CONTENT_KEY] ==
|
55
|
+
if current_hash.length > current_hash.delete(HASH_SIZE_KEY) && current_hash[CONTENT_KEY].blank? || current_hash[CONTENT_KEY] == ""
|
55
56
|
current_hash.delete(CONTENT_KEY)
|
56
57
|
end
|
57
58
|
@hash_stack.pop
|
@@ -69,15 +70,13 @@ module ActiveSupport
|
|
69
70
|
|
70
71
|
def parse(data)
|
71
72
|
if !data.respond_to?(:read)
|
72
|
-
data = StringIO.new(data ||
|
73
|
+
data = StringIO.new(data || "")
|
73
74
|
end
|
74
75
|
|
75
|
-
|
76
|
-
if char.nil?
|
76
|
+
if data.eof?
|
77
77
|
{}
|
78
78
|
else
|
79
|
-
|
80
|
-
document = self.document_class.new
|
79
|
+
document = document_class.new
|
81
80
|
parser = Nokogiri::XML::SAX::Parser.new(document)
|
82
81
|
parser.parse(data)
|
83
82
|
document.hash
|