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();
}
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;
}
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);
}
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) {
}
}
}
}
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;
}
Aggregations