comparison vendor/plugins/ruby-net-ldap-0.0.4/lib/net/ldap/dataset.rb @ 0:513646585e45

* Import Redmine trunk SVN rev 3859
author Chris Cannam
date Fri, 23 Jul 2010 15:52:44 +0100
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:513646585e45
1 # $Id: dataset.rb 78 2006-04-26 02:57:34Z blackhedd $
2 #
3 #
4 #----------------------------------------------------------------------------
5 #
6 # Copyright (C) 2006 by Francis Cianfrocca. All Rights Reserved.
7 #
8 # Gmail: garbagecat10
9 #
10 # This program is free software; you can redistribute it and/or modify
11 # it under the terms of the GNU General Public License as published by
12 # the Free Software Foundation; either version 2 of the License, or
13 # (at your option) any later version.
14 #
15 # This program is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU General Public License for more details.
19 #
20 # You should have received a copy of the GNU General Public License
21 # along with this program; if not, write to the Free Software
22 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 #
24 #---------------------------------------------------------------------------
25 #
26 #
27
28
29
30
31 module Net
32 class LDAP
33
34 class Dataset < Hash
35
36 attr_reader :comments
37
38
39 def Dataset::read_ldif io
40 ds = Dataset.new
41
42 line = io.gets && chomp
43 dn = nil
44
45 while line
46 io.gets and chomp
47 if $_ =~ /^[\s]+/
48 line << " " << $'
49 else
50 nextline = $_
51
52 if line =~ /^\#/
53 ds.comments << line
54 elsif line =~ /^dn:[\s]*/i
55 dn = $'
56 ds[dn] = Hash.new {|k,v| k[v] = []}
57 elsif line.length == 0
58 dn = nil
59 elsif line =~ /^([^:]+):([\:]?)[\s]*/
60 # $1 is the attribute name
61 # $2 is a colon iff the attr-value is base-64 encoded
62 # $' is the attr-value
63 # Avoid the Base64 class because not all Ruby versions have it.
64 attrvalue = ($2 == ":") ? $'.unpack('m').shift : $'
65 ds[dn][$1.downcase.intern] << attrvalue
66 end
67
68 line = nextline
69 end
70 end
71
72 ds
73 end
74
75
76 def initialize
77 @comments = []
78 end
79
80
81 def to_ldif
82 ary = []
83 ary += (@comments || [])
84
85 keys.sort.each {|dn|
86 ary << "dn: #{dn}"
87
88 self[dn].keys.map {|sym| sym.to_s}.sort.each {|attr|
89 self[dn][attr.intern].each {|val|
90 ary << "#{attr}: #{val}"
91 }
92 }
93
94 ary << ""
95 }
96
97 block_given? and ary.each {|line| yield line}
98
99 ary
100 end
101
102
103 end # Dataset
104
105 end # LDAP
106 end # Net
107
108