Hi, I am doing a project in which I have to "scan" through a file and retrieved the class names and variable names and put those names into a tree structure.
For example, given a file (data.txt):
(class Man (attribute Name ) (attribute Address) ) //file ends here
The resultant tree produced will be as followed:
structure |_ class man |_ attribute name |_ attribute address
I am able to form the tree for the above simple example and the codes are as follows:
try { in = new BufferedReader(new FileReader("hello.h")); } catch(FileNotFoundException e){ e.printStackTrace(); }
DefaultMutableTreeNode top = new DefaultMutableTreeNode("structure"); DefaultMutableTreeNode A = new DefaultMutableTreeNode(); DefaultMutableTreeNode B = new DefaultMutableTreeNode();
try{ while((line = in.readLine())!=null) { l = strMatch.matcher( line );
tree = new JTree(top); int v = ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED; int h = ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED; JScrollPane jsp = new JScrollPane(tree,v,h); contentPane.add(jsp, BorderLayout.CENTER); jtf = new JTextField("",20); contentPane.add(jtf, BorderLayout.SOUTH);
tree.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent me) { doMouseClicked (me); } }); }
So my question is how can I alter my codes so that when there are more than one class defnitions in a file, a tree structure similar to the one given below can be built?
File: (class Man (attribute Name ) (attribute Address) )
(class Machine (attribute ID ) (attribute Parts) ) //File ends here
Resultant tree produced should be :
structure |_ class Man | |_ attribute Name | |_ attribute Address | |_ class Machine |_ attribute ID |_ attribute Parts