4 replies [Last post]
HoSpiTaL_gHoSt
Offline
Joined: 2010-07-02

Hello,

I would like to store my templates in a subfolder "docmosisTemplates" of the Java package I'm running my Docmosis code from. I tried the following:


//Method of class 'ReportGenerator'
private static void registerTemplates(){
//Registering templates
DropStoreHelper storeHelper = new DropStoreHelper(TemplateStoreFactory.getStore());
try
{
URL templateFolder = ReportGenerator.class.getClassLoader().getResource("be/xxx/abc/docmosis/docmosisTemplates");
storeHelper.process(new File(templateFolder.getFile()),new TemplateContext("docmosisTemplates"), false);
}
catch (Exception e)
{
log.error(e.getMessage());
}
}

The "templateFolder" contains the correct package:
file:\C:\Users\me\.m2\repository\be\xxx\abc\abc-server\1.0-SNAPSHOT\abc-server-1.0-SNAPSHOT.jar!\be\xxx\abc\docmosis\docmosisTemplates

But I keep getting the following error (on line 'storeHelper.process(...)'):

"the filename, directory name or volume label syntax is incorrect"

I checked the jar to make sure it really contains the subfolder "docmosisTemplates" and the templates, and it does.
I googled this error and apparently you can't make a File object of a file stored in a jar. You can reference the file as an InputStream using "ReportGenerator.class.getClassLoader().getResourceAsStream(...)". That way I would have to create a temporary file, write the contents of the InputStream to it, and then pass this tempFile to the storeHelper.process() method. I would prefer to read the templates directly from the jar and not copying them to some temp folder, but I suppose there is no way I can do this?

Many thanks in advance!

modman
Offline
Joined: 2010-11-25
2.0 solution

Hello HoSpiTaL_gHoSt.

The coming release has the ability to read templates from Jars as part of the DropStoreHelper. The current release (2.0) doesn't expose the functionality directly so you'll need to write the code to locate each template, retrieve the stream to it, and register the template in stream form.

Use the ClassLoader as you've done to locate the folder for the templates and get the Entry for each template as a stream. Once you've done that, you can register the template stream with Docmosis using the TemplateStore interface. This pseudo-code should help:


TemplateStore store = TemplateStoreFactory.getStore();
for each template in the requisite folder {
String templateStream = stream from getResourceAsStream();
String templateNameAndPath = pathFromJarFile (or simply filename);
TemplateIdentifier templateId = new TemplateIdentifier(templateNameAndPath);
store.storeTemplate(templateId, templateStream, null, null, null, true);
}

Hope that helps.

HoSpiTaL_gHoSt
Offline
Joined: 2010-07-02
Thanks a lot! That was what I

Thanks a lot! That was what I needed indeed.

admin
Offline
Joined: 2010-09-06
Docmosis 2.1 Released

Hello HoSpiTaL_gHoSt.

The 2.1 release has just gone live on the Website. You can now use DropStoreHelper.processResource(URL, TemplateContext, boolean) to do what you require. For example:

DropStoreHelper storeHelper = new DropStoreHelper(TemplateStoreFactory.getStore());
try
{
URL templateFolder = ReportGenerator.class.getClassLoader().getResource("be/xxx/abc/docmosis/docmosisTemplates");
// load all templates from our url into the "root" of the store, logging errors rather than aborting.
storeHelper.processResource(templateFolder, null, true);
}
catch (Exception e)
{
log.error(e.getMessage());
}

HoSpiTaL_gHoSt
Offline
Joined: 2010-07-02
Thanks for the update that

Thanks for the update that this is fixed now!