Java Code Examples for freemarker.template.Configuration#setObjectWrapper()
The following examples show how to use
freemarker.template.Configuration#setObjectWrapper() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CreateFileUtil.java From Vert.X-generator with MIT License | 7 votes |
/** * 执行创建文件 * * @param content * 模板所需要的上下文 * @param templeteName * 模板的名字 示例:Entity.ftl * @param projectPath * 生成的项目路径 示例:D://create * @param packageName * 包名 示例:com.szmirren * @param fileName * 文件名 示例:Entity.java * @param codeFormat * 输出的字符编码格式 示例:UTF-8 * @throws Exception */ public static void createFile(GeneratorContent content, String templeteName, String projectPath, String packageName, String fileName, String codeFormat, boolean isOverride) throws Exception { String outputPath = projectPath + "/" + packageName.replace(".", "/") + "/"; if (!isOverride) { if (Files.exists(Paths.get(outputPath + fileName))) { LOG.debug("设置了文件存在不覆盖,文件已经存在,忽略本文件的创建"); return; } } Configuration config = new Configuration(Configuration.VERSION_2_3_23); String tempPath = Paths.get(Constant.TEMPLATE_DIR_NAME).toFile().getName(); config.setDirectoryForTemplateLoading(new File(tempPath)); config.setObjectWrapper(new DefaultObjectWrapper(Configuration.VERSION_2_3_23)); config.setDefaultEncoding("utf-8"); Template template = config.getTemplate(templeteName); Map<String, Object> item = new HashMap<>(); item.put("content", content); if (!Files.exists(Paths.get(outputPath))) { Files.createDirectories(Paths.get(outputPath)); } try (Writer writer = new OutputStreamWriter(new FileOutputStream(outputPath + fileName), codeFormat)) { template.process(item, writer); } }
Example 2
Source File: DatabaseGenerator.java From j-road with Apache License 2.0 | 6 votes |
public static void generate(DatabaseClasses classes, String outputdir) throws IOException, TemplateException { Configuration cfg = new Configuration(); cfg.setClassForTemplateLoading(TypeGen.class, "/"); cfg.setObjectWrapper(new DefaultObjectWrapper()); Template interfaceTemp = cfg.getTemplate(DATABASE_TEMPLATE_FILE); Template implTemp = cfg.getTemplate(DATABASE_IMPL_TEMPLATE_FILE); for (DatabaseClass databaseClass : classes.getClasses().values()) { Map<String, DatabaseClass> root = new HashMap<String, DatabaseClass>(); root.put("databaseClass", databaseClass); Writer out = FileUtil.createAndGetOutputStream(databaseClass.getQualifiedInterfaceName(), outputdir); interfaceTemp.process(root, out); out.flush(); out = FileUtil.createAndGetOutputStream(databaseClass.getQualifiedImplementationName(), outputdir); implTemp.process(root, out); out.flush(); } }
Example 3
Source File: LocalFeedTaskProcessor.java From alfresco-repository with GNU Lesser General Public License v3.0 | 6 votes |
@Override protected Configuration getFreemarkerConfiguration(RepoCtx ctx) { if (useRemoteCallbacks) { // as per 3.0, 3.1 return super.getFreemarkerConfiguration(ctx); } else { Configuration cfg = new Configuration(); cfg.setObjectWrapper(new DefaultObjectWrapper()); cfg.setTemplateLoader(new ClassPathRepoTemplateLoader(nodeService, contentService, defaultEncoding)); // TODO review i18n cfg.setLocalizedLookup(false); cfg.setIncompatibleImprovements(new Version(2, 3, 20)); cfg.setNewBuiltinClassResolver(TemplateClassResolver.SAFER_RESOLVER); return cfg; } }
Example 4
Source File: FreemarkerTemplateHandler.java From myexcel with Apache License 2.0 | 6 votes |
private void setObjectWrapper(Configuration configuration) { configuration.setObjectWrapper(new DefaultObjectWrapper(Configuration.VERSION_2_3_23) { @Override public TemplateModel wrap(Object object) throws TemplateModelException { if (object instanceof LocalDate) { return new SimpleDate(Date.valueOf((LocalDate) object)); } if (object instanceof LocalTime) { return new SimpleDate(Time.valueOf((LocalTime) object)); } if (object instanceof LocalDateTime) { return new SimpleDate(Timestamp.valueOf((LocalDateTime) object)); } return super.wrap(object); } }); }
Example 5
Source File: MapperGenerator.java From fast-family-master with Apache License 2.0 | 6 votes |
private static void genMapperXml(Map<String, Object> params, String className, String resourcePath, GeneratorConfig generatorConfig) { Version version = new Version("2.3.27"); Configuration configuration = new Configuration(version); try { URL url = MapperGenerator.class.getClassLoader().getResource(resourcePath); configuration.setDirectoryForTemplateLoading(new File(url.getPath())); configuration.setObjectWrapper(new DefaultObjectWrapper(version)); String filePath = generatorConfig.getSrcBasePath() + "mapper//"; String savePath = filePath + className + "Mapper.xml"; File dirPath = new File(filePath); if (!dirPath.exists()) { dirPath.mkdirs(); } try (FileWriter out = new FileWriter(new File(savePath))) { Template template = configuration.getTemplate("mapper_xml.ftl"); template.process(params, out); } } catch (Exception e) { e.printStackTrace(); } }
Example 6
Source File: AbstractTestExecutorService.java From entando-core with GNU Lesser General Public License v3.0 | 6 votes |
@Override protected void setUp() throws Exception { super.setUp(); try { Configuration config = new Configuration(); DefaultObjectWrapper wrapper = new DefaultObjectWrapper(); config.setTemplateExceptionHandler(TemplateExceptionHandler.HTML_DEBUG_HANDLER); config.setObjectWrapper(wrapper); config.setTemplateExceptionHandler(TemplateExceptionHandler.DEBUG_HANDLER); TemplateModel templateModel = this.createModel(wrapper); ExecutorBeanContainer ebc = new ExecutorBeanContainer(config, templateModel); super.getRequestContext().addExtraParam(SystemConstants.EXTRAPAR_EXECUTOR_BEAN_CONTAINER, ebc); } catch (Throwable t) { throw new Exception(t); } }
Example 7
Source File: AbstractHalProcessor.java From core with GNU Lesser General Public License v2.1 | 5 votes |
public AbstractHalProcessor() { Version version = new Version(2, 3, 22); config = new Configuration(version); config.setDefaultEncoding("UTF-8"); config.setClassForTemplateLoading(getClass(), "templates"); config.setObjectWrapper(new DefaultObjectWrapperBuilder(version).build()); }
Example 8
Source File: SpeedTest.java From netbeans with Apache License 2.0 | 5 votes |
public void testSpeedOfFreemarker() throws Exception { Configuration cfg = new Configuration(); cfg.setDirectoryForTemplateLoading(getWorkDir()); cfg.setObjectWrapper(new BeansWrapper()); Template templ = cfg.getTemplate("template.txt"); for(int i = 0; i < whereTo.length; i++) { Writer out = new BufferedWriter(new FileWriter(whereTo[i])); templ.process(parameters, out); out.close(); } }
Example 9
Source File: TemplateWrapperFactory.java From sailfish-core with Apache License 2.0 | 5 votes |
public TemplateWrapperFactory(String templatesPackagePath) { configuration = new Configuration(Configuration.VERSION_2_3_24); configuration.setTemplateLoader(new ClassTemplateLoader(HtmlReport.class, templatesPackagePath)); configuration.setDefaultEncoding("UTF-8"); configuration.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER); configuration.setAPIBuiltinEnabled(true); configuration.setObjectWrapper(new BeansWrapperBuilder(Configuration.VERSION_2_3_24).build()); configuration.setRecognizeStandardFileExtensions(true); }
Example 10
Source File: TemplateConfiguration.java From opencps-v2 with GNU Affero General Public License v3.0 | 5 votes |
private void init() { try { Configuration configuration = new Configuration(Configuration.getVersion()); configuration.setDefaultEncoding("UTF-8"); configuration.setObjectWrapper(new DefaultObjectWrapper(configuration.getIncompatibleImprovements())); this.setConfiguration(configuration); } catch (Exception e) { _log.error(e); } }
Example 11
Source File: TextReporter.java From revapi with Apache License 2.0 | 5 votes |
/** * Creates a new FreeMarker configuration. * By default, it is configured as follows: * <ul> * <li>compatibility level is set to 2.3.23 * <li>the object wrapper is configured to expose fields * <li>API builtins are enabled * <li>there are 2 template loaders - 1 for loading templates from /META-INF using a classloader and a second * one to load templates from files. * </ul> * @return */ protected Configuration createFreeMarkerConfiguration() { DefaultObjectWrapperBuilder bld = new DefaultObjectWrapperBuilder(Configuration.VERSION_2_3_23); bld.setExposeFields(true); Configuration freeMarker = new Configuration(Configuration.VERSION_2_3_23); freeMarker.setObjectWrapper(bld.build()); freeMarker.setAPIBuiltinEnabled(true); freeMarker.setTemplateLoader(new MultiTemplateLoader( new TemplateLoader[]{new ClassTemplateLoader(getClass(), "/META-INF"), new NaiveFileTemplateLoader()})); return freeMarker; }
Example 12
Source File: Freemarker.java From freeacs with MIT License | 5 votes |
public Configuration initFreemarker() { Configuration config = new Configuration(); config.setTemplateLoader(new ClassTemplateLoader(Freemarker.class, "/templates")); config.setTemplateUpdateDelay(0); config.setTemplateExceptionHandler(TemplateExceptionHandler.HTML_DEBUG_HANDLER); config.setObjectWrapper(ObjectWrapper.DEFAULT_WRAPPER); config.setDefaultEncoding("UTF-8"); config.setOutputEncoding("UTF-8"); return config; }
Example 13
Source File: MolgenisWebAppConfig.java From molgenis with GNU Lesser General Public License v3.0 | 5 votes |
/** * Configure freemarker. All freemarker templates should be on the classpath in a package called * 'freemarker' */ @Bean public FreeMarkerConfigurer freeMarkerConfigurer() { FreeMarkerConfigurer result = new FreeMarkerConfigurer() { @Override protected void postProcessConfiguration(Configuration config) { config.setObjectWrapper(new MolgenisFreemarkerObjectWrapper(VERSION_2_3_23)); } @Override protected void postProcessTemplateLoaders(List<TemplateLoader> templateLoaders) { templateLoaders.add(new ClassTemplateLoader(FreeMarkerConfigurer.class, "")); } }; result.setPreferFileSystemAccess(false); result.setTemplateLoaderPath("classpath:/templates/"); result.setDefaultEncoding("UTF-8"); Properties freemarkerSettings = new Properties(); freemarkerSettings.setProperty(Configuration.LOCALIZED_LOOKUP_KEY, Boolean.FALSE.toString()); freemarkerSettings.setProperty(Configuration.NUMBER_FORMAT_KEY, "computer"); result.setFreemarkerSettings(freemarkerSettings); Map<String, Object> freemarkerVariables = Maps.newHashMap(); freemarkerVariables.put("hasPermission", new HasPermissionDirective(permissionService)); freemarkerVariables.put("notHasPermission", new NotHasPermissionDirective(permissionService)); addFreemarkerVariables(freemarkerVariables); result.setFreemarkerVariables(freemarkerVariables); return result; }
Example 14
Source File: MapperGenerator.java From fast-family-master with Apache License 2.0 | 5 votes |
private static void genMapper(String className, String classComment, String resourcePath, GeneratorConfig generatorConfig) { Map<String, Object> paramMap = new HashMap<>(); paramMap.put("className", className); paramMap.put("classComment", classComment); paramMap.put("sysTime", new Date()); paramMap.put("packageName", generatorConfig.getPackageName()); ; Version version = new Version("2.3.27"); Configuration configuration = new Configuration(version); try { URL url = MapperGenerator.class.getClassLoader().getResource(resourcePath); configuration.setDirectoryForTemplateLoading(new File(url.getPath())); configuration.setObjectWrapper(new DefaultObjectWrapper(version)); String filePath = generatorConfig.getSrcBasePath() + "mapper//"; String savePath = filePath + className + "Mapper.java"; File dirPath = new File(filePath); if (!dirPath.exists()) { dirPath.mkdirs(); } try (FileWriter out = new FileWriter(new File(savePath))) { Template template = configuration.getTemplate("mapper.ftl"); template.process(paramMap, out); } } catch (Exception e) { e.printStackTrace(); } }
Example 15
Source File: ServiceGenerator.java From fast-family-master with Apache License 2.0 | 5 votes |
private static void genServiceInterface(String className, String classComment, String resourcePath, GeneratorConfig generatorConfig) { Map<String, Object> paramMap = new HashMap<>(); paramMap.put("className", className); paramMap.put("classComment", classComment); paramMap.put("sysTime", new Date()); paramMap.put("packageName", generatorConfig.getPackageName()); Version version = new Version("2.3.27"); Configuration configuration = new Configuration(version); try { URL url = ServiceGenerator.class.getClassLoader().getResource(resourcePath); configuration.setDirectoryForTemplateLoading(new File(url.getPath())); configuration.setObjectWrapper(new DefaultObjectWrapper(version)); String filePath = generatorConfig.getSrcBasePath() + "service//"; String savePath = filePath + className + "Service.java"; File dirPath = new File(filePath); if (!dirPath.exists()) { dirPath.mkdirs(); } try (FileWriter fileWriter = new FileWriter(new File(savePath))) { Template template = configuration.getTemplate("service.ftl"); template.process(paramMap, fileWriter); } } catch (Exception e) { e.printStackTrace(); } }
Example 16
Source File: ServiceGenerator.java From fast-family-master with Apache License 2.0 | 5 votes |
private static void genServiceImpl(String className, String classComment, String resourcePath, GeneratorConfig generatorConfig) { Map<String, Object> paramMap = new HashMap<>(); paramMap.put("className", className); paramMap.put("classComment", classComment); paramMap.put("sysTime", new Date()); paramMap.put("packageName", generatorConfig.getPackageName()); Version version = new Version("2.3.27"); Configuration configuration = new Configuration(version); try { URL url = ServiceGenerator.class.getClassLoader().getResource(resourcePath); configuration.setDirectoryForTemplateLoading(new File(url.getPath())); configuration.setObjectWrapper(new DefaultObjectWrapper(version)); String filePath = generatorConfig.getSrcBasePath() + "service//impl//"; String savePath = filePath + className + "ServiceImpl.java"; File dirPath = new File(filePath); if (!dirPath.exists()) { dirPath.mkdirs(); } try (FileWriter fileWriter = new FileWriter(new File(savePath))) { Template template = configuration.getTemplate("service_impl.ftl"); template.process(paramMap, fileWriter); } } catch (Exception e) { e.printStackTrace(); } }
Example 17
Source File: EntityGenerator.java From fast-family-master with Apache License 2.0 | 5 votes |
public static void generatorSingleEntity(String tableName, String className, String classComment, GeneratorConfig generatorConfig) { TableInfo tableInfo = AnalysisDB.getTableInfoByName(tableName, generatorConfig); Map<String, Object> paramMap = new HashMap<>(); paramMap.put("className", className); paramMap.put("tableInfo", tableInfo); paramMap.put("sysTime", new Date()); paramMap.put("classComment", classComment); paramMap.put("packageName", generatorConfig.getPackageName()); Version version = new Version("2.3.27"); Configuration configuration = new Configuration(version); try { configuration.setObjectWrapper(new DefaultObjectWrapper(version)); configuration.setDirectoryForTemplateLoading(new File(EntityGenerator.class.getClassLoader() .getResource("ftl").getPath())); String savePath = PackageDirUtils.getPackageEntityDir(generatorConfig.getSrcBasePath()); String filePath = generatorConfig.getSrcBasePath() + "entity//"; savePath = filePath + className + ".java"; File dirPath = new File(filePath); if (!dirPath.exists()) { dirPath.mkdirs(); } try (FileWriter fileWriter = new FileWriter(savePath)) { Template temporal = configuration.getTemplate("entity.ftl"); temporal.process(paramMap, fileWriter); } System.out.println("************" + savePath + "************"); } catch (Exception e) { e.printStackTrace(); } }
Example 18
Source File: ControllerGenerator.java From fast-family-master with Apache License 2.0 | 5 votes |
private static void genController(String className, String classComment, String urlStr, String resourcePath, GeneratorConfig generatorConfig) { Map<String, Object> paramMap = new HashMap<>(); paramMap.put("className", className); paramMap.put("classComment", classComment); paramMap.put("sysTime", new Date()); paramMap.put("packageName", generatorConfig.getPackageName()); paramMap.put("url", urlStr); Version version = new Version("2.3.27"); Configuration configuration = new Configuration(version); try { URL url = ControllerGenerator.class.getClassLoader().getResource(resourcePath); configuration.setDirectoryForTemplateLoading(new File(url.getPath())); configuration.setObjectWrapper(new DefaultObjectWrapper(version)); String filePath = generatorConfig.getSrcBasePath() + "controller//"; String savePath = filePath + className + "Controller.java"; File dirPath = new File(filePath); if (!dirPath.exists()) { dirPath.mkdirs(); } try (FileWriter fileWriter = new FileWriter(new File(savePath))) { Template template = configuration.getTemplate("/controller.ftl"); template.process(paramMap, fileWriter); } } catch (Exception e) { e.printStackTrace(); } }
Example 19
Source File: FreemarkerWebServiceDisplayEngine.java From document-management-software with GNU Lesser General Public License v3.0 | 4 votes |
public FreemarkerWebServiceDisplayEngine(JavaNameDisplayStrategy nameDisplayingStrategy) { super(nameDisplayingStrategy); configuration = new Configuration(); configuration.setLocalizedLookup(false); configuration.setObjectWrapper(new DefaultObjectWrapper()); }
Example 20
Source File: ReportGeneratorProvider.java From testability-explorer with Apache License 2.0 | 4 votes |
/** * Method to allow retaining a handle on preconfigured model objects. * * @param costModel Cost Model for the {@link ReportGenerator} * @param reportModel Can be {@code null} if {@link ReportFormat} is not * {@link ReportFormat#html} or {@link ReportFormat#about} * @param sourceLoader Source Loader used by {@link ReportFormat#source} reports. * @return a ready to use {@link ReportGenerator} */ public ReportGenerator build(CostModel costModel, ReportModel reportModel, SourceLoader sourceLoader) { SourceLinker linker = new SourceLinker( options.getSrcFileLineUrl(), options.getSrcFileUrl()); Configuration cfg = new Configuration(); cfg.setTemplateLoader(new ClassPathTemplateLoader(PREFIX)); BeansWrapper objectWrapper = new DefaultObjectWrapper(); cfg.setObjectWrapper(objectWrapper); ResourceBundleModel bundleModel = new ResourceBundleModel(getBundle("messages"), objectWrapper); ReportGenerator report; switch (reportFormat) { case summary: report = new TextReportGenerator(out, costModel, options); break; case html: reportModel.setMessageBundle(bundleModel); reportModel.setSourceLinker(new SourceLinkerModel(linker)); report = new FreemarkerReportGenerator(reportModel, out, FreemarkerReportGenerator.HTML_REPORT_TEMPLATE, cfg); break; case props: report = new PropertiesReportGenerator(out, costModel); break; case source: GradeCategories gradeCategories = new GradeCategories(options.getMaxExcellentCost(), options.getMaxAcceptableCost()); report = new SourceReportGenerator(gradeCategories, sourceLoader, new File("te-report"), costModel, new Date(), options.getWorstOffenderCount(), cfg); break; case xml: XMLSerializer xmlSerializer = new XMLSerializer(); xmlSerializer.setOutputByteStream(out); OutputFormat format = new OutputFormat(); format.setIndenting(true); xmlSerializer.setOutputFormat(format); report = new XMLReportGenerator(xmlSerializer, costModel, options); break; case about: reportModel.setMessageBundle(bundleModel); reportModel.setSourceLinker(new SourceLinkerModel(linker)); report = new FreemarkerReportGenerator(reportModel, out, "about/Report.html", cfg); break; default: throw new IllegalStateException("Unknown report format " + reportFormat); } return report; }