tomj74 / chunk-templates

Chunk Templates, a template engine for Java
http://www.x5software.com/chunk/
MIT License
77 stars 10 forks source link

Get chunk template from file on Android #4

Closed sashatrn closed 9 years ago

sashatrn commented 9 years ago

I create templates dynamicaly and want to have possibility to load it from specefied file (not from asset). I didn't find the way how to do it. I see that I can load chunk template only from asset folder. It isn't approipriate for me. A non-appropriate code I used: AndroidTemplates loader = new AndroidTemplates(getContext(), ""); Chunk chunk = theme.makeChunk("/data/data/com.my/files/invoice.prn", "");

Thanks.

sashatrn commented 9 years ago

I solved the issue with class:

public class MyAndroidTemplates extends TemplateProvider {
private Context context;

public MyAndroidTemplates(Context androidContext) {
    this.context = androidContext;
}

public String getProtocol()
{
    return "my_android";
}

public String loadContainerDoc(String docName)
        throws java.io.IOException
{
    String path = context.getFilesDir() + "/" + docName;
    InputStream in = new FileInputStream(new File(path));
    java.util.Scanner s = new java.util.Scanner(in).useDelimiter("\\A");
    return s.hasNext() ? s.next() : "";
}
}

Anyway it makes uncomfortable to split file name and its extension in: Chunk chunk = theme.makeChunk("invoice", "prn");

It would be great if I can simply create template from any file.

tomj74 commented 9 years ago

Interesting use case. I made some tweaks to allow theme.makeChunk("invoice.prn"). Just make sure to call loader.setDefaultExtension(null) (new in 2.6.1) before you pass the loader to the Theme constructor (or you could do this directly in the MyAndroidTemplates constructor).

Please try it out: chunk-templates-2.6.1.jar and let me know if it works for you.