| Class | ActiveLdap::DistinguishedName::Parser |
| In: |
lib/active_ldap/distinguished_name.rb
|
| Parent: | Object |
| ATTRIBUTE_TYPE_RE | = | /\s*([a-zA-Z][a-zA-Z\d\-]*|\d+(?:\.\d+)*)\s*/ |
| HEX_PAIR | = | "(?:[\\da-fA-F]{2})" |
| STRING_CHARS_RE | = | /[^,=\+<>\#;\\\"]*/ |
| PAIR_RE | = | /\\([,=\+<>\#;]|\\|\"|(#{HEX_PAIR}))/ |
| HEX_STRING_RE | = | /\#(#{HEX_PAIR}+)/ |
| dn | [R] |
# File lib/active_ldap/distinguished_name.rb, line 11
11: def initialize(source)
12: @dn = nil
13: source = source.to_s if source.is_a?(DN)
14: @source = source
15: end
# File lib/active_ldap/distinguished_name.rb, line 17
17: def parse
18: return @dn if @dn
19:
20: rdns = []
21: scanner = StringScanner.new(@source)
22:
23: scanner.scan(/\s*/)
24: raise rdn_is_missing if scanner.scan(/\s*\+\s*/)
25: raise name_component_is_missing if scanner.scan(/\s*,\s*/)
26:
27: rdn = {}
28: until scanner.eos?
29: type = scan_attribute_type(scanner)
30: skip_attribute_type_and_value_separator(scanner)
31: value = scan_attribute_value(scanner)
32: rdn[type] = value
33: if scanner.scan(/\s*\+\s*/)
34: raise rdn_is_missing if scanner.eos?
35: elsif scanner.scan(/\s*\,\s*/)
36: rdns << rdn
37: rdn = {}
38: raise name_component_is_missing if scanner.eos?
39: else
40: scanner.scan(/\s*/)
41: rdns << rdn if scanner.eos?
42: end
43: end
44:
45: @dn = DN.new(*rdns)
46: @dn
47: end
# File lib/active_ldap/distinguished_name.rb, line 139
139: def attribute_type_is_missing
140: invalid_dn(_("attribute type is missing"))
141: end
# File lib/active_ldap/distinguished_name.rb, line 143
143: def attribute_value_is_missing
144: invalid_dn(_("attribute value is missing"))
145: end
# File lib/active_ldap/distinguished_name.rb, line 115
115: def collect_pairs(scanner)
116: result = ""
117: while scanner.scan(PAIR_RE)
118: if scanner[2]
119: result << [scanner[2].hex].pack("C*")
120: else
121: result << scanner[1]
122: end
123: end
124: result
125: end
# File lib/active_ldap/distinguished_name.rb, line 147
147: def found_unmatched_quotation
148: invalid_dn(_("found unmatched quotation"))
149: end
# File lib/active_ldap/distinguished_name.rb, line 127
127: def invalid_dn(reason)
128: DistinguishedNameInvalid.new(@source, reason)
129: end
# File lib/active_ldap/distinguished_name.rb, line 131
131: def name_component_is_missing
132: invalid_dn(_("name component is missing"))
133: end
# File lib/active_ldap/distinguished_name.rb, line 135
135: def rdn_is_missing
136: invalid_dn(_("relative distinguished name (RDN) is missing"))
137: end
# File lib/active_ldap/distinguished_name.rb, line 51
51: def scan_attribute_type(scanner)
52: raise attribute_type_is_missing unless scanner.scan(ATTRIBUTE_TYPE_RE)
53: scanner[1]
54: end
# File lib/active_ldap/distinguished_name.rb, line 64
64: def scan_attribute_value(scanner)
65: if scanner.scan(HEX_STRING_RE)
66: value = scanner[1].scan(/../).collect do |hex_pair|
67: hex_pair.hex
68: end.pack("C*")
69: elsif scanner.scan(/\"/)
70: value = scan_quoted_attribute_value(scanner)
71: else
72: value = scan_not_quoted_attribute_value(scanner)
73: end
74: raise attribute_value_is_missing if value.blank?
75:
76: value
77: end
# File lib/active_ldap/distinguished_name.rb, line 96
96: def scan_not_quoted_attribute_value(scanner)
97: result = ""
98: until scanner.eos?
99: prev_size = result.size
100: pairs = collect_pairs(scanner)
101: strings = scanner.scan(STRING_CHARS_RE)
102: result << pairs if !pairs.nil? and !pairs.empty?
103: unless strings.nil?
104: if scanner.peek(1) == ","
105: result << strings.rstrip
106: else
107: result << strings
108: end
109: end
110: break if prev_size == result.size
111: end
112: result
113: end
# File lib/active_ldap/distinguished_name.rb, line 79
79: def scan_quoted_attribute_value(scanner)
80: result = ""
81: until scanner.scan(/\"/)
82: scanner.scan(/([^\\\"]*)/)
83: quoted_strings = scanner[1]
84: pairs = collect_pairs(scanner)
85:
86: if scanner.eos? or (quoted_strings.empty? and pairs.empty?)
87: raise found_unmatched_quotation
88: end
89:
90: result << quoted_strings
91: result << pairs
92: end
93: result
94: end