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.

2

1 Answer 1

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);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.