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