| Class | ActiveLdap::Xml::Serializer |
| In: |
lib/active_ldap/xml.rb
|
| Parent: | Object |
| PRINTABLE_STRING | = | /[\x20-\x7e\w\s]*/ |
# File lib/active_ldap/xml.rb, line 10
10: def initialize(dn, attributes, schema, options={})
11: @dn = dn
12: @attributes = attributes
13: @schema = schema
14: @options = options
15: end
# File lib/active_ldap/xml.rb, line 17
17: def to_s
18: root = @options[:root]
19: result = "<#{root}>\n"
20: target_attributes.each do |key, values|
21: values = normalize_values(values).sort_by {|value, _| value}
22: if @schema.attribute(key).single_value?
23: result << " #{serialize_attribute_value(key, *values[0])}\n"
24: else
25: result << serialize_attribute_values(key, values)
26: end
27: end
28: result << "</#{root}>\n"
29: result
30: end
# File lib/active_ldap/xml.rb, line 56
56: def normalize_value(value, options=[])
57: targets = []
58: if value.is_a?(Hash)
59: value.each do |real_option, real_value|
60: targets.concat(normalize_value(real_value, options + [real_option]))
61: end
62: elsif value.is_a?(Array)
63: value.each do |real_value|
64: targets.concat(normalize_value(real_value, options))
65: end
66: else
67: if /\A#{PRINTABLE_STRING}\z/ !~ value
68: value = [value].pack("m").gsub(/\n/u, '')
69: options += ["base64"]
70: end
71: xml_attributes = {}
72: options.each do |name, val|
73: xml_attributes[name] = val || "true"
74: end
75: targets << [value, xml_attributes]
76: end
77: targets
78: end
# File lib/active_ldap/xml.rb, line 48
48: def normalize_values(values)
49: targets = []
50: values.each do |value|
51: targets.concat(normalize_value(value))
52: end
53: targets
54: end
# File lib/active_ldap/xml.rb, line 101
101: def serialize_attribute_value(name, value, xml_attributes)
102: if xml_attributes.blank?
103: xml_attributes = ""
104: else
105: xml_attributes = " " + xml_attributes.collect do |n, v|
106: "#{ERB::Util.h(n)}=\"#{ERB::Util.h(v)}\""
107: end.join(" ")
108: end
109: "<#{name}#{xml_attributes}>#{ERB::Util.h(value)}</#{name}>"
110: end
# File lib/active_ldap/xml.rb, line 80
80: def serialize_attribute_values(name, values)
81: return "" if values.blank?
82:
83: result = ""
84: if name == "dn" or @options[:type].to_s.downcase == "ldif"
85: values.collect do |value, xml_attributes|
86: xml = serialize_attribute_value(name, value, xml_attributes)
87: result << " #{xml}\n"
88: end
89: else
90: plural_name = name.pluralize
91: result << " <#{plural_name} type=\"array\">\n"
92: values.each do |value, xml_attributes|
93: xml = serialize_attribute_value(name, value, xml_attributes)
94: result << " #{xml}\n"
95: end
96: result << " </#{plural_name}>\n"
97: end
98: result
99: end
# File lib/active_ldap/xml.rb, line 33
33: def target_attributes
34: except_dn = false
35: attributes = @attributes.dup
36: (@options[:except] || []).each do |name|
37: if name == "dn"
38: except_dn = true
39: else
40: attributes.delete(name)
41: end
42: end
43: attributes = attributes.sort_by {|key, values| key}
44: attributes.unshift(["dn", [@dn]]) unless except_dn
45: attributes
46: end