class NdrImport::Xml::ControlCharEscaper

A class to remove control characters, and XML entities representing them

Constants

CHARACTER_REFERENCES

Matches XML character reference entities

Attributes

data[R]

Public Class Methods

new(data) click to toggle source
# File lib/ndr_import/xml/control_char_escaper.rb, line 14
def initialize(data)
  @data = data
end

Public Instance Methods

escape!() click to toggle source
# File lib/ndr_import/xml/control_char_escaper.rb, line 18
def escape!
  unescape_control_char_references!(data)
  escape_control_chars!(data)
end

Private Instance Methods

try_to_extract_char_from(match) click to toggle source
# File lib/ndr_import/xml/control_char_escaper.rb, line 37
def try_to_extract_char_from(match)
  if match.nil?
    nil
  elsif match[:decimal]
    match[:decimal].to_i(10).chr
  elsif match[:hex]
    match[:hex].to_i(16).chr
  end
rescue RangeError
  # Return everything if the match was against junk:
  match.to_s
end
unescape_control_char_references!(data) click to toggle source
# File lib/ndr_import/xml/control_char_escaper.rb, line 25
def unescape_control_char_references!(data)
  data.gsub!(CHARACTER_REFERENCES) do |reference|
    char = try_to_extract_char_from(Regexp.last_match)

    if char&.match?(CONTROL_CHARACTERS)
      escape_control_chars!(char)
    else
      reference
    end
  end
end