0

My challenge is this... I'm getting a string path (e.g x1/x2/x3/Xn, Xn/x2/x3, etc.) (e.g String path = Project.getName() ). So basically, I can have any type of path (any level and any depth). I'm trying to find a way to convert this to a data structure so I can implement it in my gwt CellTree. So far, I have implemented the CellTree from this example GWT Cell tree, how to use? .

Since, I have no idea of the level or the depth, I am a bit lost... I think I should go with a recursive function which split my string path then add them, but check if they already existe, etc...

I also looked over this question Construct a tree structure from list of string paths. But I'm kind of new to java and design patterns, so any pointers or bits of code (example) would help.

Community
  • 1
  • 1
fneron
  • 1,057
  • 3
  • 15
  • 39
  • If I find any solutions, I will show it up! – fneron Nov 22 '11 at 23:07
  • This is pretty much what I am attempting to do. Except I will add up the gwt UI to it. http://stackoverflow.com/questions/5158961/unzip-into-treemap-in-java – fneron Nov 23 '11 at 20:32

1 Answers1

0
    String fullPath = mystringpath;
    String[] paths = fullPath.split("/");
    String combinedPaths = "";
    Node current = this.root;

    // We will never process the last node since it could be a repository
    // or a wildcard. The last step of this process would be doing that.
    for (int i = 0; i < paths.length - 1; i++) {
        combinedPaths += "/" + paths[i];
        if (this.collected.containsKey(combinedPaths)) {
            current = this.collected.get(combinedPaths);
        }
        else {
            String name = paths[i];
            Node childItem = new Node(name);
            current.addItem(childItem);
            current = childItem;
            this.collected.put(combinedPaths, current);
        }
    }

    // Process the last node since it represents the repository name.
        String name = paths[paths.length - 1];
        Node leafItem = new Node(name);
        combinedPaths += "/" + name;
        current.addItem(leafItem);
        current = leafItem;
        this.collected.put(combinedPaths, current);
fneron
  • 1,057
  • 3
  • 15
  • 39