package net.sourceforge.fenixedu.webServices.linkare; import java.io.BufferedReader; import java.io.File; import java.io.FileOutputStream; import java.io.FileReader; import java.io.IOException; import java.io.OutputStreamWriter; import java.util.Collection; import java.util.Map; import net.sourceforge.fenixedu._development.PropertiesManager; import net.sourceforge.fenixedu.domain.DomainObject; import com.linkare.commons.metainfo.Linkare; import freemarker.template.Configuration; import freemarker.template.Template; @Linkare(author = "Paulo Zenida") public abstract class ExportingData { private static final String FREEMARKER_TEMPLATES_DIRECTORY = "freemarker.templates.directory"; private static Configuration configuration; static { configuration = new Configuration(); try { configuration.setDirectoryForTemplateLoading(new File(PropertiesManager.getProperty(FREEMARKER_TEMPLATES_DIRECTORY))); } catch (IOException e) { e.printStackTrace(); } } protected static String process(final Map> dataModel, final String templateName, final String generatedFilename) { try { return freemarkerDo(dataModel, templateName, generatedFilename); } catch (Exception e) { e.printStackTrace(); } return null; } // Process a template using FreeMarker and print the results private static String freemarkerDo(Map> datamodel, final String templateName, final String generatedFilename) throws Exception { Template tpl = configuration.getTemplate(templateName); OutputStreamWriter output = new OutputStreamWriter(new FileOutputStream(generatedFilename)); tpl.process(datamodel, output); final FileReader in = new FileReader(generatedFilename); final BufferedReader buff = new BufferedReader(in); StringBuilder builder = new StringBuilder(); String line = null; while ((line = buff.readLine()) != null) { builder.append(line); } return builder.toString(); } }