Search in sources :

Example 1 with Configuration

use of freemarker.template.Configuration in project eweb4j-framework by laiweiwei.

the class PetControlTest method testFreeMarker.

@Test
public void testFreeMarker() throws Exception {
    Configuration cfg = new Configuration();
    // 指定模板从何处加载的数据源,这里设置成一个文件目录。
    cfg.setDirectoryForTemplateLoading(new File("src/test/java/test/ftl"));
    // 指定模板如何检索数据模型
    cfg.setObjectWrapper(new DefaultObjectWrapper());
    Map root = new HashMap();
    root.put("user", "Big Joe");
    Map latest = new HashMap();
    root.put("latestProduct", latest);
    latest.put("url", "produces/greenmouse.html");
    latest.put("name", "green mouse");
    Template template = cfg.getTemplate("hello.html");
    Writer out = new OutputStreamWriter(System.out);
    template.process(root, out);
    out.flush();
}
Also used : Configuration(freemarker.template.Configuration) HashMap(java.util.HashMap) DefaultObjectWrapper(freemarker.template.DefaultObjectWrapper) OutputStreamWriter(java.io.OutputStreamWriter) File(java.io.File) HashMap(java.util.HashMap) Map(java.util.Map) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter) Template(freemarker.template.Template) Test(org.junit.Test)

Example 2 with Configuration

use of freemarker.template.Configuration in project spark by perwendel.

the class FreeMarkerTemplateEngine method createFreemarkerConfiguration.

private Configuration createFreemarkerConfiguration() {
    Configuration retVal = new Configuration(Configuration.DEFAULT_INCOMPATIBLE_IMPROVEMENTS);
    retVal.setClassForTemplateLoading(FreeMarkerTemplateEngine.class, "freemarker");
    return retVal;
}
Also used : Configuration(freemarker.template.Configuration)

Example 3 with Configuration

use of freemarker.template.Configuration in project OpenAM by OpenRock.

the class OpenAMResourceOwnerSessionValidatorTest method mockCustomLoginUrlTemplate.

private void mockCustomLoginUrlTemplate(String customLoginUrlTemplate) throws ServerException, IOException {
    Template template = new Template("", new StringReader(customLoginUrlTemplate), new Configuration());
    given(providerSettings.getCustomLoginUrlTemplate()).willReturn(template);
}
Also used : Configuration(freemarker.template.Configuration) StringReader(java.io.StringReader) Template(freemarker.template.Template)

Example 4 with Configuration

use of freemarker.template.Configuration in project pcgen by PCGen.

the class VariableReport method outputReport.

/**
	 * Output the variable report for the supplied data in a particular format.
	 * 
	 * @param gameModeVarMap The map of variable definitions for each game mode. 
	 * @param gameModeVarCountMap The map of the number of variables for each game mode.
	 * @param reportFormat The format in which to output the report.
	 * @param outputWriter The writer to output the report to.
	 * @throws IOException If the template cannot be accessed or the writer cannot be written to.
	 * @throws TemplateException If there is an error in processing the template.
	 */
public void outputReport(Map<String, List<VarDefine>> gameModeVarMap, Map<String, Integer> gameModeVarCountMap, ReportFormat reportFormat, Writer outputWriter) throws IOException, TemplateException {
    // Configuration
    Writer file = null;
    Configuration cfg = new Configuration();
    int dataPathLen = ConfigurationSettings.getPccFilesDir().length();
    try {
        // Set Directory for templates
        File codeDir = new File("code");
        File templateDir = new File(codeDir, "templates");
        cfg.setDirectoryForTemplateLoading(templateDir);
        // load template
        Template template = cfg.getTemplate(reportFormat.getTemplate());
        // data-model
        Map<String, Object> input = new HashMap<>();
        input.put("gameModeVarMap", gameModeVarMap);
        input.put("gameModeVarCountMap", gameModeVarCountMap);
        input.put("pathIgnoreLen", dataPathLen + 1);
        // Process the template
        template.process(input, outputWriter);
        outputWriter.flush();
    } finally {
        if (file != null) {
            try {
                file.close();
            } catch (Exception e2) {
            }
        }
    }
}
Also used : Configuration(freemarker.template.Configuration) HashMap(java.util.HashMap) PCGFile(pcgen.io.PCGFile) File(java.io.File) FileWriter(java.io.FileWriter) Writer(java.io.Writer) TemplateException(freemarker.template.TemplateException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) Template(freemarker.template.Template)

Example 5 with Configuration

use of freemarker.template.Configuration in project OpenClinica by OpenClinica.

the class EnketoUrlService method populateInstance.

private String populateInstance(CrfVersion crfVersion, FormLayout formLayout, EventCrf eventCrf, String studyOid, String flavor) throws Exception {
    Map<String, Object> data = new HashMap<String, Object>();
    List<ItemGroup> igs = itemGroupDao.findByCrfVersionId(crfVersion.getCrfVersionId());
    for (ItemGroup ig : igs) {
        List<HashMap<String, Object>> hashMapList = new ArrayList<HashMap<String, Object>>();
        List<ItemGroupMetadata> igms = itemGroupMetadataDao.findByItemGroupCrfVersion(ig.getItemGroupId(), crfVersion.getCrfVersionId());
        int maxRowCount = itemDataDao.getMaxCountByEventCrfGroup(eventCrf.getEventCrfId(), ig.getItemGroupId());
        HashMap<String, Object> hashMap = null;
        if (igms.get(0).isRepeatingGroup() && maxRowCount == 0) {
            hashMap = new HashMap<>();
            hashMap.put("index", 1);
            hashMap.put("lastUsedOrdinal", 1);
            for (ItemGroupMetadata igm : igms) {
                hashMap.put(igm.getItem().getName(), "");
                if (flavor.equals(QUERY_FLAVOR))
                    hashMap.put(igm.getItem().getName() + QUERY_SUFFIX, "");
            }
            hashMapList.add(hashMap);
            data.put(ig.getName(), hashMapList);
        }
        boolean rowDeleted = false;
        if (igms.get(0).isRepeatingGroup()) {
            for (int i = 0; i < maxRowCount; i++) {
                rowDeleted = false;
                for (ItemGroupMetadata igm : igms) {
                    ItemData itemData = itemDataDao.findByItemEventCrfOrdinalDeleted(igm.getItem().getItemId(), eventCrf.getEventCrfId(), i + 1);
                    if (itemData != null) {
                        rowDeleted = true;
                        break;
                    }
                }
                if (!rowDeleted) {
                    hashMap = new HashMap<>();
                    hashMap.put("index", i + 1);
                    if (i == 0) {
                        hashMap.put("lastUsedOrdinal", maxRowCount);
                    }
                    for (ItemGroupMetadata igm : igms) {
                        ItemData itemData = itemDataDao.findByItemEventCrfOrdinal(igm.getItem().getItemId(), eventCrf.getEventCrfId(), i + 1);
                        String itemValue = getItemValue(itemData, crfVersion);
                        hashMap.put(igm.getItem().getName(), itemData != null ? itemValue : "");
                        if (flavor.equals(QUERY_FLAVOR)) {
                            if (itemData != null) {
                                ObjectMapper mapper = new ObjectMapper();
                                QueriesBean queriesBean = buildQueryElement(itemData);
                                hashMap.put(igm.getItem().getName() + QUERY_SUFFIX, queriesBean != null ? mapper.writeValueAsString(queriesBean) : "");
                            } else {
                                hashMap.put(igm.getItem().getName() + QUERY_SUFFIX, "");
                            }
                        }
                    }
                    hashMapList.add(hashMap);
                }
            }
        }
        if (igms.get(0).isRepeatingGroup() && maxRowCount != 0) {
            data.put(ig.getName(), hashMapList);
        }
        if (!igms.get(0).isRepeatingGroup()) {
            for (ItemGroupMetadata igm : igms) {
                ItemData itemData = itemDataDao.findByItemEventCrfOrdinal(igm.getItem().getItemId(), eventCrf.getEventCrfId(), 1);
                String itemValue = getItemValue(itemData, crfVersion);
                data.put(igm.getItem().getName(), itemData != null ? itemValue : "");
                if (flavor.equals(QUERY_FLAVOR)) {
                    if (itemData != null) {
                        ObjectMapper mapper = new ObjectMapper();
                        QueriesBean queriesBean = buildQueryElement(itemData);
                        data.put(igm.getItem().getName() + QUERY_SUFFIX, queriesBean != null ? mapper.writeValueAsString(queriesBean) : "");
                    } else {
                        data.put(igm.getItem().getName() + QUERY_SUFFIX, "");
                    }
                }
            }
        }
    }
    String templateStr = null;
    CrfBean crfBean = crfDao.findById(formLayout.getCrf().getCrfId());
    String directoryPath = Utils.getCrfMediaFilePath(crfBean.getOcOid(), formLayout.getOcOid());
    File dir = new File(directoryPath);
    File[] directoryListing = dir.listFiles();
    if (directoryListing != null) {
        for (File child : directoryListing) {
            if (flavor.equals(QUERY_FLAVOR) && child.getName().endsWith(INSTANCE_QUERIES_SUFFIX) || flavor.equals(NO_FLAVOR) && child.getName().endsWith(INSTANCE_SUFFIX)) {
                templateStr = new String(Files.readAllBytes(Paths.get(child.getPath())));
                break;
            }
        }
    }
    Template template = new Template("template name", new StringReader(templateStr), new Configuration());
    StringWriter wtr = new StringWriter();
    template.process(data, wtr);
    String instance = wtr.toString();
    System.out.println(instance);
    return instance;
}
Also used : CrfBean(org.akaza.openclinica.domain.datamap.CrfBean) Configuration(freemarker.template.Configuration) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Template(freemarker.template.Template) ItemGroup(org.akaza.openclinica.domain.datamap.ItemGroup) StringWriter(java.io.StringWriter) ItemGroupMetadata(org.akaza.openclinica.domain.datamap.ItemGroupMetadata) QueriesBean(org.akaza.openclinica.core.form.xform.QueriesBean) StringReader(java.io.StringReader) File(java.io.File) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) ItemData(org.akaza.openclinica.domain.datamap.ItemData)

Aggregations

Configuration (freemarker.template.Configuration)282 Template (freemarker.template.Template)106 Test (org.junit.Test)86 TemplateTest (freemarker.test.TemplateTest)52 HashMap (java.util.HashMap)51 IOException (java.io.IOException)41 StringWriter (java.io.StringWriter)39 File (java.io.File)33 TemplateException (freemarker.template.TemplateException)29 Writer (java.io.Writer)27 ClassTemplateLoader (freemarker.cache.ClassTemplateLoader)19 ConditionalTemplateConfigurationFactory (freemarker.cache.ConditionalTemplateConfigurationFactory)19 StringTemplateLoader (freemarker.cache.StringTemplateLoader)18 DefaultObjectWrapper (freemarker.template.DefaultObjectWrapper)18 FileNameGlobMatcher (freemarker.cache.FileNameGlobMatcher)17 TemplateLoader (freemarker.cache.TemplateLoader)16 MultiTemplateLoader (freemarker.cache.MultiTemplateLoader)15 FileWriter (java.io.FileWriter)13 ArrayList (java.util.ArrayList)13 User (org.vcell.util.document.User)13