一、前言
基于infinispan的infinispan-tools-4.2.1.FINAL.jar源码包org.infinispan.tools.doclet.html.HtmlGenerator代码生成器,按照指定标题title、编码encoding、底部bottom、脚部footer、头部header、媒体类型描述metaDescription及媒体类型关键字metaKeywords等进行自定义生成HTML代码文件内容,详情源码说明部分。
二、源码说明
package org.infinispan.tools.doclet.html;@b@@b@import java.io.FileOutputStream;@b@import java.io.IOException;@b@import java.io.OutputStreamWriter;@b@import java.io.PrintWriter;@b@import java.util.List;@b@@b@public abstract class HtmlGenerator@b@{@b@ String encoding;@b@ String title;@b@ String bottom;@b@ String footer;@b@ String header;@b@ String metaDescription;@b@ List<String> metaKeywords;@b@@b@ public HtmlGenerator(String encoding, String title, String bottom, String footer, String header, String metaDescription, List<String> metaKeywords)@b@ {@b@ this.encoding = encoding;@b@ this.title = title;@b@ this.footer = footer;@b@ this.header = header;@b@ this.bottom = bottom;@b@ this.metaDescription = metaDescription;@b@ this.metaKeywords = metaKeywords;@b@ }@b@@b@ public void generateHtml(String fileName) throws IOException {@b@ generateHtml(fileName, "stylesheet.css");@b@ }@b@@b@ public void generateHtml(String fileName, String styleSheetName) throws IOException {@b@ FileOutputStream fos = new FileOutputStream(fileName);@b@ OutputStreamWriter osw = new OutputStreamWriter(fos);@b@ PrintWriter writer = new PrintWriter(osw);@b@ try {@b@ writer.println("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"\n\t\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">");@b@@b@ writer.println("<HTML xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" lang=\"en\">");@b@ writer.println("<HEAD>");@b@ if (isValid(this.metaDescription))@b@ writer.println(new StringBuilder().append("<META NAME=\"description\" content=\"").append(this.metaDescription).append("\" />").toString());@b@ if ((this.metaKeywords != null) && (!(this.metaKeywords.isEmpty()))) {@b@ StringBuilder sb = new StringBuilder();@b@ sb.append("<META NAME=\"keywords\" content=\"");@b@ for (String keyword : this.metaKeywords) sb.append(keyword).append(", ");@b@ sb.append("\" />");@b@ }@b@ writer.println("<TITLE>");@b@ writer.println(this.title);@b@ writer.println("</TITLE>");@b@ writer.println(new StringBuilder().append("<LINK REL=\"stylesheet\" HREF=\"").append(styleSheetName).append("\" TYPE=\"text/css\"/>").toString());@b@@b@ writer.println("</HEAD>");@b@ writer.println("<BODY>");@b@@b@ if (isValid(this.header)) {@b@ writer.println(this.header);@b@ writer.println("<HR />");@b@ }@b@@b@ writer.println(generateContents());@b@@b@ if (isValid(this.bottom)) {@b@ writer.println("<HR />");@b@ writer.println(this.bottom);@b@ }@b@@b@ if (isValid(this.footer)) { writer.println(this.footer);@b@ }@b@@b@ writer.println("</BODY>");@b@ writer.println("</HTML>");@b@ } finally {@b@ writer.close();@b@ osw.close();@b@ fos.close();@b@ }@b@ }@b@@b@ protected abstract String generateContents();@b@@b@ protected boolean isValid(String s) {@b@ return ((s != null) && (s.trim().length() != 0));@b@ }@b@}