已阅读5页,还剩56页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
chapter.html模板(每一章的内容)html view plaincopyprint?$chapter.ttitle!$section.htmlContent!$chapter.ttitle!$section.htmlContent!content.html(目录信息) html view plaincopyprint?目录目录$chapter.ttitle!目录目录$chapter.ttitle!converpage.html(封面) html view plaincopyprint?$ebook.ttitle!$ebook.ttitle!ncx文件html view plaincopyprint?$ebook.ttitle!$ebook.tauthor!封面$chapter.ttitle! $ebook.ttitle!$ebook.tauthor!封面$chapter.ttitle!opf文件 html view plaincopyprint?$ebook.ttitle!en$ebook.tauthor!Freesourcelcsh:Novel$ebook.ttitle!en$ebook.tauthor!Freesourcelcsh: Novel以上这些文件必须放在模板处理的类所在的子目录resources里面,当然自己可以调整下面贴出模板处理的两个类TemplateException.javajava view plaincopyprint?packagecom.dangdang.template;/*模板异常类.*/publicclassTemplateExceptionextendsExceptionprivatestaticfinallongserialVersionUID=-1532247619099231683L;publicTemplateException()publicTemplateException(finalStringmessage)super(message);publicTemplateException(finalThrowablecause)super(cause);publicTemplateException(finalStringmessage,finalThrowablecause)super(message,cause);package com.dangdang.template;/*模板异常类.* */public class TemplateException extends Exception private static final long serialVersionUID = -1532247619099231683L;public TemplateException() public TemplateException(final String message) super(message);public TemplateException(final Throwable cause) super(cause);public TemplateException(final String message, final Throwable cause) super(message, cause);TemplateProcess.java java view plaincopyprint?packagecom.dangdang.template;importjava.io.BufferedOutputStream;importjava.io.File;importjava.io.IOException;importjava.io.OutputStream;importjava.io.OutputStreamWriter;importjava.io.StringWriter;importjava.io.Writer;importjava.util.Map;mons.io.IOUtils;importorg.slf4j.Logger;importorg.slf4j.LoggerFactory;importcom.dangdang.util.FileUtils;importfreemarker.cache.ClassTemplateLoader;importfreemarker.cache.TemplateLoader;importfreemarker.template.Configuration;importfreemarker.template.DefaultObjectWrapper;importfreemarker.template.Template;/*基于freemarker的模板处理程序.*/publicabstractclassTemplateProcessprivateTemplateProcess()privatestaticLoggerlog=LoggerFactory.getLogger(TemplateProcess.class);privatestaticfinalConfigurationCFG=newConfiguration()TemplateLoadertemplateLoader=newClassTemplateLoader(TemplateProcess.class,resources);setTemplateLoader(templateLoader);setDefaultEncoding(UTF-8);setObjectWrapper(newDefaultObjectWrapper();/*处理模板,返回结果.*paramtpl相对于classpath:/tpl的模板文件路径*parammodel模板数据*return合成结果*throwsTemplateException*/publicstaticStringprocess(finalStringtpl,finalMapmodel)throwsTemplateExceptionfinalWriterout=newStringWriter();process(tpl,model,out);returnout.toString();/*处理模板,输出结果到文件.如果文件不存在,将创建文件及路径上的所有目录.*paramtpl相对于classpath:/tpl的模板文件路径*parammodel模板数据*paramfile目标文件,不存在是将新建*paramoverwrite目标文件存在时是否覆盖*throwsTemplateException*/publicstaticvoidprocess(finalStringtpl,finalMapmodel,finalFilefile,finalbooleanoverwrite)throwsExceptionif(file.exists()if(overwrite)FileUtils.deleteQuietly(file);elselog.warn(目标文件已存在);return;OutputStreamout;tryout=FileUtils.openOutputStream(file);catch(finalIOExceptione)finalStringmsg=处理模板失败-打开目标文件失败;log.error(msg,e);thrownewTemplateException(msg,e);finalWriterwriter=newOutputStreamWriter(newBufferedOutputStream(out,8192),UTF-8);process(tpl,model,writer);IOUtils.closeQuietly(writer);IOUtils.closeQuietly(out);/*处理模板,输出结果到writer参数.*paramtpl相对于classpath:/tpl的模板文件路径*parammodel模板数据*paramwriter输出目标*throwsTemplateException*/publicstaticvoidprocess(finalStringtpl,finalMapmodel,finalWriterwriter)throwsTemplateExceptiontryfinalTemplatetemplate=CFG.getTemplate(tpl);template.setEncoding(UTF-8);cess(model,writer);catch(finalExceptione)finalStringmsg=处理模板失败;log.error(msg,e);thrownewTemplateException(msg,e);package com.dangdang.template;import java.io.BufferedOutputStream;import java.io.File;import java.io.IOException;import java.io.OutputStream;import java.io.OutputStreamWriter;import java.io.StringWriter;import java.io.Writer;import java.util.Map;import mons.io.IOUtils;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import com.dangdang.util.FileUtils;import freemarker.cache.ClassTemplateLoader;import freemarker.cache.TemplateLoader;import freemarker.template.Configuration;import freemarker.template.DefaultObjectWrapper;import freemarker.template.Template;/* * 基于freemarker的模板处理程序. * * */public abstract class TemplateProcess private TemplateProcess() private static Logger log = LoggerFactory.getLogger(TemplateProcess.class);private static final Configuration CFG = new Configuration() TemplateLoader templateLoader=new ClassTemplateLoader(TemplateProcess.class,resources);setTemplateLoader(templateLoader);setDefaultEncoding(UTF-8);setObjectWrapper(new DefaultObjectWrapper(); ;/* * 处理模板,返回结果. * * param tpl 相对于classpath:/tpl的模板文件路径 * param model 模板数据 * return 合成结果 * throws TemplateException */public static String process(final String tpl, final Map model) throws TemplateException final Writer out = new StringWriter();process(tpl, model, out);return out.toString();/* * 处理模板,输出结果到文件. 如果文件不存在,将创建文件及路径上的所有目录. * * param tpl 相对于classpath:/tpl的模板文件路径 * param model 模板数据 * param file 目标文件,不存在是将新建 * param overwrite 目标文件存在时是否覆盖 * throws TemplateException */public static void process(final String tpl, final Map model, final File file, final boolean overwrite) throws Exception if (file.exists() if (overwrite) FileUtils.deleteQuietly(file); else log.warn(目标文件已存在);return;OutputStream out;try out = FileUtils.openOutputStream(file); catch (final IOException e) final String msg = 处理模板失败-打开目标文件失败;log.error(msg, e);throw new TemplateException(msg, e);final Writer writer = new OutputStreamWriter(new BufferedOutputStream(out, 8192), UTF-8);process(tpl, model, writer);IOUtils.closeQuietly(writer);IOUtils.closeQuietly(out);/* * 处理模板,输出结果到writer参数. * * param tpl 相对于classpath:/tpl的模板文件路径 * param model 模板数据 * param writer 输出目标 * throws TemplateException */public static void process(final String tpl, final Map model, final Writer writer) throws TemplateException try final Template template = CFG.getTemplate(tpl);template.setEncoding(UTF-8);cess(model, writer); catch (final Exception e) final String msg = 处理模板失败;log.error(msg, e);throw new TemplateException(msg, e);例如这两个文件放在com.zimo.template包下 那么为了方便起见,可以将上面的五个文件放在com.zimo.template.resources下面,当然这里面涉及到模板处理的时候的,类加载器的问题也可以自己定义存放文件的位置,下面再给出mimetype文件和css文件,contain.xml文件这些可以放在项目根目录下的resource下面mimetype文件里面的内容application/epub+zipcss文件css view plaincopyprint?bodyfont-size:100%;font-family:HiFontSongGB;.coverheight:100%;.smy-smfont-size:2.7500em;font-family:HiFontHeiGB;margin-top:1.5em;margin-bottom:1.5em;text-align:center;font-weight:normal;.smy-sm1font-size:2.7500em;font-family:HiFontHeiGB;margin-top:1em;margin-bottom:0em;text-align:center;font-weight:normal;.smy-fsmfont-size:2.0000em;font-family:HiFontHeiGB;margin-bottom:2em;margin-top:0em;text-align:center;font-weight:normal;.smy-fsm1font-size:2.0000em;font-family:HiFontHeiGB;margin-bottom:0.5em;margin-top:0.5em;text-align:center;font-weight:normal;.smy-zzfont-size:1.8750em;font-family:HiFontHeiGB;margin-top:1em;margin-bottom:0em;text-align:center;font-weight:normal;.smy-zz1font-size:1.8750em;font-family:HiFontHeiGB;margin-top:0em;margin-bottom:0em;text-align:center;font-weight:normal;.smy-cbsfont-size:1.7500em;font-family:HiFontHeiGB;margin-top:4em;text-align:center;font-weight:normal;.smy-cbs1font-size:1.7500em;font-family:HiFontHeiGB;margin-top:3em;text-align:center;font-weight:normal;.smy-qtfont-size:1.7500em;font-family:HiFontHeiGB;text-align:center;font-weight:normal;.bq-smfont-size:1.3750em;line-height:1.6000em;font-family:HiFontHeiGB;margin-top:2em;font-weight:normal;.bqfont-size:1.2500em;line-height:1.6000em;font-family:HiFontSongGB;.ml-btfont-size:2.1875em;font-family:HiFontHeiGB;margin-bottom:1em;margin-top:1em;text-align:center;font-weight:normal;.ml-1font-size:1.5000em;margin-left:0em;font-family:HiFontSongGB;line-height:1.6000em;text-align:justify;.ml-2font-size:1.3750em;margin-left:2em;font-family:HiFontSongGB;line-height:1.6000em;text-align:justify;.ml-3font-size:1.2500em;margin-left:4em;font-family:HiFontSongGB;line-height:1.6000em;text-align:justify;.ml-4font-size:1.2500em;margin-left:6em;font-family:HiFontSongGB;line-height:1.6000em;text-align:justify;h1font-size:1.5000em;font-family:HiFontHeiGB;margin-bottom:1em;margin-top:1em;text-align:center;font-weight:normal;h2font-size:1.3750em;font-family:HiFontHeiGB;font-weight:normal;margin-bottom:1em;margin-top:1em;h3font-size:1.2500em;font-family:HiFontHeiGB;font-weight:normal;margin-top:1em;margin-bottom:1em;h4font-size:1.2500em;font-family:HiFontHeiGB;font-weight:normal;margin-top:1em;margin-bottom:0em;h5font-size:1.2500em;font-family:HiFontHeiGB;font-weight:normal;ma
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 危险化学品企业员工安全生产培训试题及答案
- 五级保育师理论考核试题题库及答案
- 西昌学院园林植物学遗传育种期末考试复习题及参考答案
- 巡游出租汽车司机职业道德考核试卷及答案
- 眼科视光师岗前培训试题及答案
- 新编企业管理纲要与题库及答案
- 药品仓储管理知识培训试卷及答案
- 证券投资顾问业务考试题库及答案
- 医疗器械岗位职责培训考试试题(附答案)
- 职业技能鉴定国家题库管道工考试试题及参考答案
- 2026下半年杭州市数据资源管理局所属杭州市大数据管理服务中心招聘9人考试参考题库及答案详解
- 公路路基工程施工质量验收规范
- 《剧本写作》考试复习(重点)题库及答案
- 2026年娄底职业技术学院高职单招笔试职业适应性测验试题库含答案解析2套试卷
- 2026越秀区白云街道公开招聘公共服务办辅助人员1人考试模拟试题及答案详解
- CSCO肾癌诊疗指南(2026版)
- 2026年统编版九年级语文上册期末复习:古诗词+文言文 默写练习题(含答案)
- 2026年北京市高考英语试卷(含答案及解析)
- 吉利汽车GEELY+品牌VI手册 Geely Auto Communication Guidelines (New Energy 2025)
- 2026年教科版五年级科学上册2.2《独轮车省力的秘密》课件
- 义务教育英语课程标准解读完整版课件小学初中版
评论
0/150
提交评论