Newer
Older
/* **************************************************************************
*
* Copyright (C) 2002-2005 Octet String, Inc. All Rights Reserved.
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
*
* THIS WORK IS SUBJECT TO U.S. AND INTERNATIONAL COPYRIGHT LAWS AND
* TREATIES. USE, MODIFICATION, AND REDISTRIBUTION OF THIS WORK IS SUBJECT
* TO VERSION 2.0.1 OF THE OPENLDAP PUBLIC LICENSE, A COPY OF WHICH IS
* AVAILABLE AT HTTP://WWW.OPENLDAP.ORG/LICENSE.HTML OR IN THE FILE "LICENSE"
* IN THE TOP-LEVEL DIRECTORY OF THE DISTRIBUTION. ANY USE OR EXPLOITATION
* OF THIS WORK OTHER THAN AS AUTHORIZED IN VERSION 2.0.1 OF THE OPENLDAP
* PUBLIC LICENSE, OR OTHER PRIOR WRITTEN CONSENT FROM OCTET STRING, INC.,
* COULD SUBJECT THE PERPETRATOR TO CRIMINAL AND CIVIL LIABILITY.
******************************************************************************/
package com.octetstring.jdbcLdap.util;
import java.util.*;
import java.sql.*;
import java.io.*;
/**
* @author mlb
*
* To change the template for this generated type comment go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
public class LDIF {
/** The DN */
public static final String DN = "dn";
/** The seperator of the attribs and val */
public static final String SEP = ": ";
/** The seperator of the attribs and binary val */
public static final String BIN_SEP = ":: ";
/** Stores The ldif */
LinkedList ldif;
/** Debug mode */
boolean debug;
public LDIF() {
this.ldif = new LinkedList();
}
/**
* Loads LDIF from a string
* @param ldif The ldif to load
*/
public LDIF(String ldif, boolean debug) throws Exception {
this.debug = debug;
StringReader r = new StringReader(ldif);
BufferedReader in = new BufferedReader(r);
String attr=null, val=null;
String line;
this.ldif = new LinkedList();
HashMap entry = null;
LinkedList attrib;
String sep;
while ((line = in.readLine()) != null) {
//don't do anything if there is a blank line
if (line.trim().length() != 0) {
//split the line
try {
attr = line.substring(0, line.indexOf(SEP)).trim();
val = line.substring(line.indexOf(SEP) + SEP.length());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace(System.out);
System.out.println("Error on line : " + line);
System.exit(1);
}
//if this is a dn, we need to create a new entry
if (attr.equalsIgnoreCase(DN)) {
entry = new HashMap();
this.ldif.add(new Entry(val, entry));
} else {
attrib = (LinkedList) entry.get(attr);
//if it doesn't exist, add it
if (attrib == null) {
attrib = new LinkedList();
entry.put(attr, attrib);
}
attrib.add(val);
}
}
}
}
/**
* Loads and ldif from a result set. each row is an entry
* @param rs the ResultSet
*/
public LDIF(ResultSet rs, String dnField, boolean debug) throws Exception {
this.debug = debug;
String dn;
String attr, val;
StringTokenizer tok;
this.ldif = new LinkedList();
HashMap entry = null;
LinkedList attrib;
//each row is an entry
while (rs.next()) {
//create an entry
entry = new HashMap();
dn = rs.getString(dnField);
ldif.add(new Entry(dn, entry));
ResultSetMetaData rsmd = rs.getMetaData();
for (int i = 1; i <= rsmd.getColumnCount(); i++) {
//get the attribute and value
attr = rsmd.getColumnName(i);
//don't want to check the dn field
val = rs.getString(attr);
if (!attr.equals(dnField) && val.trim().length() != 0) {
//create the attribute
attrib = new LinkedList();
entry.put(attr, attrib);
//if the entry is a concatinated field, primarily for the jdbcLdapDrivers
if (val.charAt(0) == '['
&& val.charAt(val.length() - 1) == ']') {
tok =
new StringTokenizer(
val.substring(1, val.length() - 1),
"][",
false);
while (tok.hasMoreTokens()) {
attrib.add(tok.nextToken());
}
} else {
attrib.add(val);
}
}
}
}
}
public boolean compareLdif(LDIF o, LDIF diffLdif) {
if (!(o instanceof LDIF)) {
return false;
}
LinkedList ldif1, ldif2, diff;
String tmpval;
ldif1 = (LinkedList) ldif.clone();
ldif2 = (LinkedList) ((LDIF) o).ldif.clone();
diff = new LinkedList();
boolean match = true;
boolean equals = true;
if (debug) {
System.out.println("ldif 1: " + ldif1.size());
System.out.println("ldif 2: " + ldif2.size());
}
Entry ent, ent2 = null;
String dn = null;
Iterator entries2 = null;
Iterator entries = ldif1.iterator();
boolean found = false;
LinkedList ts1, ts2;
Iterator attribsKeys;
Iterator tsi, it,itat;
HashMap attribs, attribs2;
String val;
String attrib1, attrib2, val2;
while (entries.hasNext()) {
ent = (Entry) entries.next();
dn = ent.getDn();
//System.out.println("In DN :" + dn);
entries2 = ldif2.iterator();
found = false;
match = true;
while (entries2.hasNext()) {
ent2 = (Entry) entries2.next();
if (ent2.getDn().equalsIgnoreCase(dn)) {
found = true;
//System.out.println("removing: " + ent2.getDn() + "--" + ldif2.size());
ldif2.remove(ent2);
//System.out.println("still has : " + ldif2.contains(ent2) + " -- " + ldif2.size());
break;
}
}
if (!found) {
System.out.println("DN " + dn + " not found");
match = false;
}
attribs = ent.getAtts();
attribs2 = null;
if (found) attribs2 = (HashMap) ent2.getAtts().clone();
attribsKeys = attribs.keySet().iterator();
while (found && attribsKeys.hasNext()) {
attrib1 = (String) attribsKeys.next();
if (debug)
System.out.println("Looking for : " + attrib1);
ts1 = (LinkedList) attribs.get(attrib1);
itat = attribs2.keySet().iterator();
ts2 = null;
while (itat.hasNext()) {
tmpval = (String) itat.next();
if (tmpval.equalsIgnoreCase(attrib1)) {
ts2 = ((LinkedList) attribs2.remove(tmpval));
ts2 = (LinkedList) ts2.clone();
break;
}
}
if (ts2 == null) {
match = false;
System.out.println("FAILED : " + dn);
break;
}
tsi = ts1.iterator();
while (tsi.hasNext()) {
val = (String) tsi.next();
if (debug) {
System.out.println("\tAttib : " + attrib1);
System.out.println("\tVal : " + val);
}
found = false;
it = ts2.iterator();
while (it.hasNext()) {
val2 = (String) it.next();
if (val2.equalsIgnoreCase(val)) {
found = true;
it.remove();
tsi.remove();
break;
}
}
if (debug) System.out.println("\tContains : " + found);
if (!found) {
System.out.println("FAILED : " + dn);
match = false;
}
}
if (debug) {
System.out.println("ts2.size() : " + ts2.size());
System.out.println("ts1.size() : " + ts1.size());
}
if (ts2.size() != 0) {
System.out.println("FAILED : " + dn);
match = false;
}
}
if (debug)
System.out.println("attribs2.size() : " + (attribs2 != null ? Integer.toString(attribs2.size()) : "null"));
if (attribs2 == null || attribs2.size() != 0) {
System.out.println("FAILED : " + dn);
match = false;
}
if (!match) {
System.out.println("FAILED : " + dn);
equals = false;
diff.add(ent);
}
}
if (debug)
System.out.println("ldif2.size() : " + ldif2.size());
if (ldif2.size() != 0) {
entries2 = ldif2.iterator();
while (entries2.hasNext()) {
ent2 = (Entry) entries2.next();
System.out.println("FAILED : " + (dn != null ? dn : ""));
diff.add(ent2);
}
equals = false;
}
if (!equals)
diffLdif.ldif = diff;
return equals;
//return ((LDIF) o).ldif.equals(this.ldif);
}
public String toString() {
StringBuffer ldif = new StringBuffer();
String dn, attrib;
Iterator entries = this.ldif.iterator();
HashMap entry;
Iterator atts;
Iterator vals;
LinkedList ts;
Entry ent;
while (entries.hasNext()) {
ent = (Entry) entries.next();
dn = ent.getDn();
ldif.append("dn: ").append(dn).append('\n');
entry = ent.getAtts();
atts = entry.keySet().iterator();
while (atts.hasNext()) {
attrib = (String) atts.next();
ts = (LinkedList) entry.get(attrib);
vals = ts.iterator();
while (vals.hasNext()) {
ldif.append(attrib).append(SEP).append(vals.next()).append(
'\n');
}
}
ldif.append('\n');
}
return ldif.toString();
}
}
class Entry {
String dn;
HashMap atts;
public Entry(String dn, HashMap atts) {
this.dn = dn;
this.atts = atts;
}
/**
* @return
*/
public HashMap getAtts() {
return atts;
}
/**
* @return
*/
public String getDn() {
return dn;
}
public boolean equals(Object o) {
Entry e = (Entry) o;
return e.getDn().equalsIgnoreCase(this.dn);
}
}