• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Read LDIF contents and Display in Tree

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

In the below program LDIF contents are mentioned in the program, but i read

the contents from a file using file reader and then it display in a Tree..

if any one knows send me reply its very urgent.. Please..


--------------------------------------------------------------------



import javax.swing.*;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeNode;
import javax.swing.tree.DefaultMutableTreeNode;
import java.io.StringReader;
import java.io.BufferedReader;
import java.io.IOException;
public class LDIFTree{
private static final String INPUT = "dn: CN=John Smith,OU=Legal,DC=example,DC=com\n" +
"changetype: modify\n" +
"replace:employeeID\n" +
"employeeID: 1234\n" +
"-\n" +
"replace:employeeNumber\n" +
"employeeNumber: 98722\n" +
"-\n" +
"replace: extensionAttribute6\n" +
"extensionAttribute6: JSmith98\n" +
"-\n" +
"\n" +
"dn: CN=Jane Smith,OU=Accounting,DC=example,DC=com\n" +
"changetype: modify\n" +
"replace:employeeID\n" +
"employeeID: 5678\n" +
"-\n" +
"replace:employeeNumber\n" +
"employeeNumber: 76543\n" +
"-\n" +
"replace: extensionAttribute6\n" +
"extensionAttribute6: JSmith14\n" +
"-";
public LDIFTree() {
JFrame f = new JFrame("LDIFTree");
BufferedReader reader = new BufferedReader(new StringReader(INPUT));
JTree tree = new JTree(new DefaultTreeModel(buildModel(reader)));
f.getContentPane().add(new JScrollPane(tree));
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(300, 600);
f.setLocationRelativeTo(null);
f.setVisible(true);
}
private TreeNode buildModel(BufferedReader reader)
{
DefaultMutableTreeNode root = new DefaultMutableTreeNode("root", true);
DefaultMutableTreeNode currentChild = null;
try
{
String line = reader.readLine();
while(line != null)
{
line = line.trim();
if (line.startsWith("dn"))
{
// add the current dn to the root
currentChild = new DefaultMutableTreeNode(line, true);
root.add(currentChild);
}
else if (line.startsWith("-") || line.length() < 1)
{ // skip it
} else
{
// add the property to the current dn
DefaultMutableTreeNode propertyNode = new DefaultMutableTreeNode(line, false);
currentChild.add(propertyNode);
}
line = reader.readLine();
}
}
catch (IOException x)
{
x.printStackTrace();
}
return root;
}
public static void main(String[] args)
{
new LDIFTree();
}
}














----------------------
Thanks,
Raga
 
Bartender
Posts: 4121
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I specifically made it a build from a BufferedReader because of this...

The line:


creates a BufferedReader using the static string information I embedded in the example program. To read from a file, you'd do something like -

 
raga menon
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Nathan,

Thanks for the reply.

I could actually read my ldif file using FileReader and I could also display the contents from the file using StringTokenizer() etc...

Now I have the following problem.

I could not display the ldif as a tree...

for eg. for the following LDIF,

dn: dc=foo,dc=bar
createTimestamp: 20060530115557.500Z
dc: foo
modifiersName: cn=Directory Manager
creatorsName: cn=Directory Manager
modifyTimestamp: 20060530115557.500Z
objectclass: top
objectclass: domain

dn: ou=services,dc=foo,dc=bar
createTimestamp: 20060530120040.274Z
modifiersName: cn=Directory Manager
modifyTimestamp: 20060530120040.274Z
creatorsName: cn=Directory Manager
ou: services
objectclass: top
objectclass: organizationalunit

dn: ou=aaa,ou=services,dc=foo,dc=bar
createTimestamp: 20060530120053.649Z
modifiersName: cn=Directory Manager
modifyTimestamp: 20060530121725.967Z
creatorsName: cn=Directory Manager
ou: aaa
description: Dialin Services
objectclass: organizationalUnit
-----------------------

i need to get the follwoing format.
under
dc=foo, dc=bar
|
ou=services,
|
ou=aaa
|
ou=users
ou=clinets
ou=servers

please let me know how i could achieve this.

Thanks,
Raga Menon
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That is something you would have to do in the parsing of your data...

After you find each "dn:" string, you can parse the contents to see if it ends with the contents of the previous "dn:"

If it ends with the same string, pull it out and make the new node a child of the last node. If it doesn't you add it as a child of the root (or at whatever level makes sense).
 
raga menon
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Nathan,

Thanks for your reply.

In my ldif file each and every dn ends with dc=foo, dc=bar. how could i parse this

and make it as Root node and child nodes and properties of each dn is displayed

by splitting the frame. in one frame all Root dn, child dn should be display in a

tree hierarchy and in another frame if i click on root node the its porperties are

displayed on another frame.(properties are createtimestamp, dc,modifiersname etc.. )

for example following is LDIF file :

dn: dc=foo,dc=bar
createTimestamp: 20060530115557.500Z
dc: foo
modifiersName: cn=Directory Manager
creatorsName: cn=Directory Manager
modifyTimestamp: 20060530115557.500Z
objectclass: top
objectclass: domain

dn: ou=services,dc=foo,dc=bar
createTimestamp: 20060530120040.274Z
modifiersName: cn=Directory Manager
modifyTimestamp: 20060530120040.274Z
creatorsName: cn=Directory Manager
ou: services
objectclass: top
objectclass: organizationalunit

dn: ou=aaa,ou=services,dc=foo,dc=bar
createTimestamp: 20060530120053.649Z
modifiersName: cn=Directory Manager
modifyTimestamp: 20060530121725.967Z
creatorsName: cn=Directory Manager
ou: aaa
description: Dialin Services
objectclass: organizationalUnit

dn: ou=clients,ou=aaa,ou=services,dc=foo,dc=bar
createTimestamp: 20060530120103.852Z
modifiersName: cn=Directory Manager
modifyTimestamp: 20060530120103.852Z
creatorsName: cn=Directory Manager
ou: clients
objectclass: organizationalUnit
objectclass: top

dn: ou=servers,ou=aaa,ou=services,dc=foo,dc=bar
createTimestamp: 20060530120103.868Z
modifiersName: cn=Directory Manager
modifyTimestamp: 20060530120103.868Z
creatorsName: cn=Directory Manager
ou: servers
objectclass: organizationalUnit
objectclass: top

dn: ou=users,ou=aaa,ou=services,dc=foo,dc=bar
createTimestamp: 20060530120103.852Z
modifiersName: cn=Directory Manager
modifyTimestamp: 20060530120103.852Z
creatorsName: cn=Directory Manager
ou: users
objectclass: organizationalUnit
objectclass: top

------------------------------------------------

and i need to display the following Tree

dc=foo,dc=bar
|
dn: ou=services,dc=foo,dc=bar
|
dn: ou=aaa,ou=services,dc=foo,dc=bar
|
dn: ou=clients,ou=aaa,ou=services,dc=foo,dc=bar
dn: ou=servers,ou=aaa,ou=services,dc=foo,dc=bar
dn: ou=users,ou=aaa,ou=services,dc=foo,dc=bar
----------------------------------------------

please let me know how to do this

Thanks,

Raga Menon
 
I think I'll just lie down here for a second. And ponder this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic