1

So, I believe the answer is likely going to be rather simple. Though, I have been trying to figure this out for over a week, and have had no luck. I'm trying to create a FileSystem for a Desktop/OS Simulator Game.

Currently, the FileSystem looks like this: (Stripped down slightly)

<system>
    <info> <!-- Default information/data for this system -->
        <top dir="/"/>
    </info>

    <files> <!-- File and Directory structure for this system -->
        <dir name="home" owner="0" group="0" ownerp="rwx" groupp="r-x" otherp="r-x">
            <dir name="cyanite" owner="1000" group="1000" ownerp="rwx" groupp="rwx" otherp="r-x">
                <dir name="folder" owner="1000" group="1000" ownerp="rwx" groupp="rwx" otherp="r-x">
                    <dir name="folder" owner="1000" group="1000" ownerp="rwx" groupp="rwx" otherp="r-x">
                        <dir name="folder" owner="1000" group="1000" ownerp="rwx" groupp="rwx" otherp="r-x">
                        </dir>
                    </dir>
                </dir>
            </dir>
        </dir>
        <dir name="bin" owner="0" group="0" ownerp="rwx" groupp="r-x" otherp="r-x">
        </dir>
        <dir name="etc" owner="0" group="0" ownerp="rwx" groupp="r-x" otherp="r-x">

        </dir>
        <dir name="usr" owner="0" group="0" ownerp="rwx" groupp="r-x" otherp="r-x">
        </dir>
    </files>
</system>

Here is my sloppy WIP code to attempt to read from the file:

System.out.println(fileNavigator.getDirectory("home"));

public ArrayList<String> getDirectory(String path) {
    Document doc = ExitParser.getDocFromZip(system); // This loads the save file and returns the doc
    NodeList dataList = doc.getElementsByTagName("files");
    NodeList dataList2 = parsePath(dataList, path);
    ArrayList<String> stringList = new ArrayList<String>();
    try {
        for(int i=0;  i < dataList2.getLength(); i++) {
            Node dataItem = dataList2.item(i);
            Element elementDataItem = (Element)dataItem;
            stringList.add(elementDataItem.getAttribute("name"));
        }
    } catch (NullPointerException e) {
        stringList.add("NullPointerException");
    }
    return stringList;
}

public NodeList parsePath(NodeList nodelist, String path) {
    Document doc = ExitParser.getDocFromZip(system); // This loads the save file and returns the doc
    NodeList dataList = doc.getElementsByTagName("info");
    if (path.startsWith(ExitParser.getAttributeValue(dataList, "top", "dir"))) {
        // This simply grabs "top" from the FileSystem <info> area and strips it.
        path.replaceFirst(Pattern.quote(ExitParser.getAttributeValue(dataList, "top", "dir")), "");
    }
    List<String> pathList = new ArrayList<String>();
    if (path.contains("/")) {
        pathList = new ArrayList<String>(Arrays.asList(path.split("/")));
    } else if (path.contains("\\")) {
        pathList = new ArrayList<String>(Arrays.asList(path.split("\\")));
    } else {
        pathList.add(path);
    }


    for (String string : pathList) {
        System.out.println(string);
        nodelist = ExitParser.getNextSetAttr(nodelist, string);
    }


    return nodelist;
}

public static NodeList getNextSetAttr(NodeList data, String attrName) {
    try {
        for(int i=0;  i < data.getLength(); i++){
            Element dataElement = (Element)data.item(i);
            NodeList nodeList = dataElement.getElementsByTagName("dir");
            Element nodeElement = (Element)nodeList.item(0);
            if(nodeElement.hasAttribute("name")){
                if (nodeElement.getAttribute("name").contains(attrName)) {
                    System.out.println(true);
                    return nodeList;
                }
            }
        }
    }
    catch(Exception ex) {
        System.out.println(ex.getMessage());
    }
    return null;
}

Using the code above, I expect the output to be [cyanite] but instead I get [home, cyanite, folder, folder, folder, bin, etc, usr].

If I change System.out.println(fileNavigator.getDirectory("home")); to System.out.println(fileNavigator.getDirectory("home/cyanite")); then I get [cyanite]. and if I change "home/cyanite" to "bin" I get [NullPointerException].

For [home, cyanite, folder, folder, folder, bin, etc, usr] the issue seems to be a problem with the parent node being added to the list (i.e home, bin, etc, usr) as well as all child Nodes (i.e: folder, folder, folder). In my usage case I only wish to obtain the childs of the current Node, and NOT the children of their Nodes.

Though, I'm not sure why I cannot access other folders aside from home.

1 Answers1

0

The issue is caused by the fact that the code is receiving all same-name Nodes at once. One could, however, only get the immediate children by doing something similar to what was suggested in this answer (StackOverflow | Get XML only immediate children elements by name). There, they take advantage of .getFirstChild() and .getNextSibling() in order to only loop through the current level.

Kieee
  • 126
  • 2
  • 15