已阅读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河北秦皇岛市市政设计院有限公司招聘专业技术人员有关笔试笔试历年参考题库附带答案详解
- 2026新疆铁门关市开发区投资开发有限责任公司经理层成员市场化选聘4人笔试历年参考题库附带答案详解
- 2026广东依顿电子科技股份有限公司招聘工艺部长测试笔试历年参考题库附带答案详解
- 2025-2030年售后维修备件库存管理企业制定与实施新质生产力战略分析研究报告
- 2026山东省新动能基金管理有限公司校园招聘8人笔试历年参考题库附带答案详解
- 2026四川爱众发展集团有限公司招聘笔试历年参考题库附带答案详解
- 2026中国中检云南公司滇东片区文山公司招聘1人笔试历年参考题库附带答案详解
- 2025秋季贵州黔西南州贞丰县赴省内外高校引进县属国有企业高层次人才和急需紧缺人才45人笔试历年参考题库附带答案详解
- 2025安徽合肥市庐江县工业投资有限公司招聘总笔试历年参考题库附带答案详解
- 投标文件技术响应准备方案
- (2025版)淋巴瘤相关噬血细胞综合征诊治专家共识课件
- (2026年版)《中华人民共和国危险化学品安全法》培训课件
- 2026年3D打印食品制造工艺报告及未来五至十年餐饮业变革报告
- 在2026年全区医疗机构医保基金管理突出问题整治工作会议上的讲话
- 2026天津海关所属事业单位招聘8人建设考试参考试题及答案解析
- 2026年党章党纪党规应知应会知识测试题库(含答案)
- 阿里巴巴校园招聘素质测评题
- (T8联考河北版)2026届高三4月第二次质量检测政治试卷(含答案解析)
- 智慧树知到《巴蜀文化(四川大学)》章节测试附案
- 2025年代码审计服务合同
- GB/T 33855-2026母婴保健服务机构通用要求
评论
0/150
提交评论