AssetManager documentation

Hello,

I was looking through the documentation for JME, and didn’t find any sort of documentation for the AssetManager class. I’m having a problem with using InputSource objects, and would like to see if I can track the problem down in the AssetManager documentation.

If there isn’t any/it doesn’t help, I can post the relevant code.

Thanks!

You mean something beyond this?
http://hub.jmonkeyengine.org/javadoc/com/jme3/asset/AssetManager.html

…that and the associated class docs was all I ever needed to implement custom loaders.

Perhaps you should talk about your problem some more.

1 Like

@pspeed,

Thanks for the link, I had been using:
http://www.jmonkeyengine.org/doc/

Which seems to be quite outdated.

I looked through the documentation to try to see if I could figure out my problem, and I’ve done a pretty extensive amount of debugging but I just can’t seem to find the root of my problem.

I’ll go into more detail about my problem:

I have been working on using XML generation to lay out levels for my game, which grew out of the game that you can make using the jME beginner’s guide. I originally used DOM, which proved to be really frustrating, but worked. I recently rewrote my entire level generator class, using XPath instead of DOM.

I have working code that parses my levels, but it will only work in the context of the IDE. The XPath library that I used is javax.xml.xpath. In order to evaluate XPath expressions, you need an InputSource object.

Here is some relevant code:

Constructor for map generator with InputSource that works, but is specific to the IDE.
[java]
public MapGenerator(String level, SimpleApplication app) {
xpath = XPathFactory.newInstance().newXPath();
this.assetManager = app.getAssetManager();
this.inputSource = new InputSource(“assets/XML/” + level + “.lvl.xml”);
}[/java]

Method for evaluating XPath expressions:
[java]
public String getValue(String expression) {
try {
return xpath.evaluate(expression, inputSource);
} catch (XPathExpressionException ex) {
System.out.println(ex.getCause());
return null;
}
}[/java]

I got DOM working by creating a class that implements AssetLoader, and built a Document object from the InputStream. To get XPath working, I created a very similar class to the Document object one:
[java]
public class XMLLoader implements AssetLoader {

public InputSource load(AssetInfo assetInfo) {
    return createSourceFromStream(assetInfo.openStream());
}

public static InputSource createSourceFromStream(InputStream inputStream) {
    try {
        return new InputSource(inputStream);
    } catch (Exception ex) {
        return null;
    }
}

}[/java]

To try to implement this, I changed the constructor to this:
[java]
xpath = XPathFactory.newInstance().newXPath();
this.assetManager = app.getAssetManager();
assetManager.registerLoader(XMLLoader.class, “lvl.xml”);
this.level = level;
this.inputSource = (InputSource) assetManager.loadAsset(“XML/” + level + “.lvl.xml”);
[/java]

Here is where my problem finally comes in. When the program begins parsing through the XML file, it can get the very first item from the XML file. I confirmed this with a System.out.println() call. The problem seems to be that when I use the AssetManager to load the XML file, it closes the InputSource’s byte stream. I was trying to look through the documentation to see if I could figure anything out, but I couldn’t.

If someone actually read through all of this, wow, thank you, you are awesome and thanks for trying to take the time to help.

If this isn’t enough code or explanation I’d be happy to post more.

Thanks!

Also, when I say “very first item in the XML file”, I’m not referring to the root node. It returns the correct value which is right in the middle of the XML file. So, it works, but only once. And trying to reload the inputSource object every time does not work.

@Screamconjoiner said: Also, when I say "very first item in the XML file", I'm not referring to the root node. It returns the correct value which is right in the middle of the XML file. So, it works, but only once. And trying to reload the inputSource object every time does not work.

I’m not sure I understand. You have many assets in the same XML file or you can load the first asset fine but not the second?

Of course the input stream will be closed after the first file is read but a second file would be a different stream.

I have many variables stored in the XML file. Things like tower positions, parameters for the enemies, etc. I can only get one variable out, and then the stream is closed.

The stream only gets closed when I load it through an AssetManager. When I just declare an InputSource with the directory, I can do my parsing/loading just fine. It’s when I use AssetManager than I’m running into a problem.

AssetManager is designed to load assets. File name in, object (which may contain other objects) out. Your AssetLoader should be loading the objects, not just providing an InputSource.

Perhaps you really want to use something else. Things in the assets folder are also available as class resources.

1 Like

If anyone stumbles upon this via a search engine, I fixed my problem.

XPath uses InputSources to parse expressions, but it can also use Document objects. You can use a loader like:
[java]
public class XMLLoader implements AssetLoader {

public Document load(AssetInfo assetInfo) {
    return createDocFromStream(assetInfo.openStream());
}

public static Document createDocFromStream(InputStream inputStream) {
    try {
        DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        return builder.parse(inputStream);
    } catch (Exception ex) {
        return null;
    }
}

}
[/java]
to create a Document object from your XML file. You will need to call:
[java] assetManager.registerLoader(XMLLoader.class, “ext”) [/java]
to register your loader. You can then grab your XML file and evaluate XPath expressions the same way you would with an InputSource.

Thanks, @pspeed, for trying to help :slight_smile:

1 Like