1

Given a starting path I want to create a tree like object representing the filesystem using Java, showing all the folders down to level x but not ordinary files. . So using FileVisitor I know that every time just before Im going to browse a new subfolder that the preVisitDirectory() method will be called and once it had completed parsing all its children the postVisitDirectory() will be called, but my problem is knowing how to attach this directory to its parent.

i.e in my case I want to create data for jstree using ul/li/ul/li elements, and Im doing this with j2html lib. So create root using ul(), then when I go into preVisitDirectory() I would create a li() element and in postVisitDirectory() would want to attach to ul() using ul().with(li) but I cant see how to keep track of where I am in building my tree.

e.g static hard coded example not actually browsing tree

public Tag createBrowseTreeAsHtml()
    {
        Tag ulTag = ul(
                li("ChildNode 2").withId("child_node_1"),
                li("ChildNode")
        );

        Tag divTag= div(
                    ul(
                        li("Root Node 1").with(ulTag),
                        li("Root Node 2")
                    )
                )
                .withId("jstree");
        return div().with(divTag);
    }

I see Guava has support for Graphs, should I be utilising this somehow ?

Paul Taylor
  • 13,411
  • 42
  • 184
  • 351
  • Sorry, it's not clear to me what you're looking to achieve. I understand that you want to interact with a filesystem and do something with j2html, but I do not understand yet what the connection between the two is and why you want to do all of this. Can you clarify? – jbduncan Jan 23 '18 at 21:50
  • Im creating a webpage with j2html that forms part of a we application, and this particular component will allow user to select a folder on the server because the idea is that the user can then access the server folders remotely via their browser (on phone/ipad etc). The j2html is rendering html that is understood by Jstree. – Paul Taylor Jan 24 '18 at 07:56
  • Actually things have moved on from this since Im now wanting to generate json data for use by jstree, so j2html is irrevelant. So I have already got something working but its seems very inefficient. I use FileVisitor to traverse tree and put all the folders found into a Set, I then use another class to convert that set into a recursive structure comprising node and children, (and then another class to convert this to json). It seems I should be able to combine first and second steps but I cannot see how. – Paul Taylor Jan 24 '18 at 08:01
  • It sounds to me then that this problem might be better solved using graphs, like how you suggested in your OP. Try using Guava's `Graph` class, modelling each folder as a node, and putting directed edges between each folder and its children folders. – jbduncan Jan 24 '18 at 17:21

0 Answers0