Module: XmlConvert

Defined in:
lib/xml_convert.rb,
lib/xml_convert/version.rb

Constant Summary collapse

VERSION =
"0.0.1"

Class Method Summary collapse

Class Method Details

.encode_local_name(name) ⇒ String?

Converts the name to a valid XML local name.

Examples:

XmlConvert.encode_local_name('a:b') #=> 'a_x003a_b'


XmlConvert.encode_name('Order Details') #=> 'Order_x0020_Details'


Parameters:

  • name (String)

    the name to be encoded.

Returns:

  • (String, nil)

    the encoded name or nil if the name cannot be converted to a string.



35
36
37
# File 'lib/xml_convert.rb', line 35

def self.encode_local_name(name)
  encode_name(name).gsub(':', '_x003a_')
end

.encode_name(name) ⇒ String?

Converts the name to a valid XML name.

Examples:

XmlConvert.encode_name('Order Details') #=> 'Order_x0020_Details'


Parameters:

  • name (String)

    the name to be encoded.

Returns:

  • (String, nil)

    the encoded name or nil if the name cannot be converted to a string.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/xml_convert.rb', line 12

def self.encode_name(name)
  return name if name.nil? || name.length == 0

  encoded_name = ""
  name.chars.each_with_index do |c, i|
    if is_invalid?(c, encoded_name == "")
      encoded_name << "_x#{'%04x' % c.ord}_"
    elsif c == '_' && i+6 < name.length && name[i+1] == 'x' && name[i+6] == '_'
      encoded_name << '_x005f_'
    else
      encoded_name << c
    end
  end
  encoded_name
end

.is_invalid?(char, is_first_letter) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/xml_convert.rb', line 41

def self.is_invalid?(char, is_first_letter)
  !self.is_valid?(char, is_first_letter)
end

.is_name_char?(char) ⇒ Boolean

NameChar ::= NameStartChar | "-" | "." | [0-9] | #xB7 | [#x0300-#x036F] | [#x203F-#x2040]

Returns:

  • (Boolean)


77
78
79
80
81
82
83
84
85
86
# File 'lib/xml_convert.rb', line 77

def self.is_name_char?(char)
  ord = char.ord

  is_name_start_char?(char)               ||
  (ord == '-'.ord)   || (ord == '.'.ord)  ||
  (ord >= '0'.ord    && ord <= '9'.ord)   ||
  (ord == 'B7'.hex)                       ||
  (ord >= '300'.hex  && ord <= '36F'.hex) ||
  (ord >= '203F'.hex && ord <= '2040'.hex)
end

.is_name_start_char?(char) ⇒ Boolean

NameStartChar ::= ":" | [A-Z] | "_" | [a-z] | [#xC0-#xD6] | [#xD8-#xF6] | [#xF8-#x2FF] | [#x370-#x37D] | [#x37F-#x1FFF] | [#x200C-#x200D] | [#x2070-#x218F] | [#x2C00-#x2FEF] | [#x3001-#xD7FF] | [#xF900-#xFDCF] | [#xFDF0-#xFFFD] | [#x10000-#xEFFFF]

Returns:

  • (Boolean)


54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/xml_convert.rb', line 54

def self.is_name_start_char?(char)
  ord = char.ord

  (ord == ':'.ord)                          ||
  (ord >= 'A'.ord     && ord <= 'Z'.ord)    ||
  (ord == '_'.ord)                          ||
  (ord >= 'a'.ord     && ord <= 'z'.ord)    ||
  (ord >= 'C0'.hex    && ord <= 'D6'.hex)   ||
  (ord >= 'D8'.hex    && ord <= 'F6'.hex)   ||
  (ord >= 'F8'.hex    && ord <= '2FF'.hex)  ||
  (ord >= '370'.hex   && ord <= '37D'.hex)  ||
  (ord >= '37F'.hex   && ord <= '1FFF'.hex) ||
  (ord >= '200C'.hex  && ord <= '200D'.hex) ||
  (ord >= '2070'.hex  && ord <= '218F'.hex) ||
  (ord >= '2C00'.hex  && ord <= '2FEF'.hex) ||
  (ord >= '3001'.hex  && ord <= 'D7FF'.hex) ||
  (ord >= 'F900'.hex  && ord <= 'FDCF'.hex) ||
  (ord >= 'FDFO'.hex  && ord <= 'FFFD'.hex) ||
  (ord >= '10000'.hex && ord <= 'EFFFF'.hex)
end

.is_valid?(char, is_first_letter) ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/xml_convert.rb', line 45

def self.is_valid?(char, is_first_letter)
  is_first_letter ? is_name_start_char?(char) : is_name_char?(char)
end