Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How do i read .ldif file through java?

 
R . Ravi Kumar
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I want to read the data from .ldif file using java. how can i read it. The contents of .ldif file are like:

dn: cn=ravi ravi,mail=xyz@yahoo.com
objectclass: top
objectclass: person
objectclass: organizationalPerson
objectclass: inetOrgPerson
objectclass: mozillaAbPersonAlpha
givenName: ravi
sn: ravi
cn: ravi ravi
mozillaNickname: ravi
mail: xyz@yahoo.com
modifytimestamp: 0Z
...
In this i want to get the value mail attribue. how can i get that value? Please help.

Thanks in advance.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49756
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Quickest way might be to read each line in the usual fashion and look for the line beginning mail:

It would be better to put the whole lot into a Map like a properties file but that won't work because there are five lines with the same first part "objectclass".
 
Rob Spoor
Sheriff
Pie
Posts: 20606
60
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
java.util.Properties supports : as well as = as separator.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49756
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But will properties support several objectclass keys?
 
Rob Spoor
Sheriff
Pie
Posts: 20606
60
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hadn't seen those, and the answer is no.

So reading line for line, then splitting at :

Apache Commons Collections has an interface called MultiMap that could store it all. Alternatively, just using a Map<String,List> or Map<String,Collection> could be useful as well.
 
Ove Lindström
Ranch Hand
Posts: 326
Android Firefox Browser Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I use OpenLDAP or Apache Directory to read my ldif-files...
[ September 29, 2008: Message edited by: Ove Lindstr�m ]
 
jijun lx
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
a library is provided by apache directory.
maybe the url follows will do a favor to you

http://grepcode.com/snapshot/repo1.maven.org/maven2/org.apache.directory.shared/shared-ldif/0.9.19
sample:

---------
Jijun
Best regards
 
Campbell Ritchie
Sheriff
Pie
Posts: 49756
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic