Skip to content

Commit fb2e2f3

Browse files
heka1024구경회
authored andcommitted
Support hexBinary format in XML
1 parent 734719d commit fb2e2f3

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

activesupport/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
* Support `hexBinary` type in `ActiveSupport::XmlMini`.
2+
3+
*heka1024*
4+
15
* Deprecate `ActiveSupport::ProxyObject` in favor of Ruby's buildin `BasicObject`
26

37
*Earlopain*

activesupport/lib/active_support/xml_mini.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ def content_type
7979
"string" => Proc.new { |string| string.to_s },
8080
"yaml" => Proc.new { |yaml| YAML.load(yaml) rescue yaml },
8181
"base64Binary" => Proc.new { |bin| ::Base64.decode64(bin) },
82+
"hexBinary" => Proc.new { |bin| parse_hex_binary(bin) },
8283
"binary" => Proc.new { |bin, entity| _parse_binary(bin, entity) },
8384
"file" => Proc.new { |file, entity| _parse_file(file, entity) }
8485
}
@@ -162,11 +163,12 @@ def _dasherize(key)
162163
"#{left}#{middle.tr('_ ', '--')}#{right}"
163164
end
164165

165-
# TODO: Add support for other encodings
166166
def _parse_binary(bin, entity)
167167
case entity["encoding"]
168168
when "base64"
169169
::Base64.decode64(bin)
170+
when "hex", "hexBinary"
171+
parse_hex_binary(bin)
170172
else
171173
bin
172174
end
@@ -180,6 +182,10 @@ def _parse_file(file, entity)
180182
f
181183
end
182184

185+
def parse_hex_binary(bin)
186+
[bin].pack("H*")
187+
end
188+
183189
def current_thread_backend
184190
IsolatedExecutionState[:xml_mini_backend]
185191
end

activesupport/test/xml_mini_test.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,19 @@ def test_yaml
337337
assert_equal({ "1 => 'test'" => nil }, parser.call("{1 => 'test'}"))
338338
end
339339

340+
def test_hexBinary
341+
parser = @parsing["hexBinary"]
342+
343+
expected = "Hello, World!"
344+
hex_binary = "48656C6C6F2C20576F726C6421"
345+
346+
assert_equal expected, parser.call(hex_binary)
347+
348+
parser = @parsing["binary"]
349+
assert_equal expected, parser.call(hex_binary, "encoding" => "hexBinary")
350+
assert_equal expected, parser.call(hex_binary, "encoding" => "hex")
351+
end
352+
340353
def test_base64Binary_and_binary
341354
base64 = <<BASE64
342355
TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlz

0 commit comments

Comments
 (0)