由于项目的需要要根据类来生成javadoc但是还不能用javadoc命令,所以就在qdox的基础上写了这个功能。
package com.highcom.hcgip.javadoc;
import java.io.*;
import com.highcom.hcgip.basic.common.*;
import com.highcom.hcgip.cm.model.*;
import com.thoughtworks.qdox.model.*;
import java.util.*;
import com.highcom.hcgip.cm.util.*;
public class JavaDocCreater {
private HtmlWriter htmlWriter = null;
private JavaDoc javaDoc = null;
/**
* 生成javadoc函数
* @param path javadoc存放的路径
* @param filename javadoc文件名
* @param docencoding 编码格式
* @param classFileName .java文件包含路径
* @throws IOException
* @throws UnsupportedEncodingException
*/
public void genJavaDoc(String path, String filename,
String docencoding, String classFileName,
JavaDocBean javaDocBean) throws
IOException,
UnsupportedEncodingException, JavaDocException {
htmlWriter = new HtmlWriter(path, filename,
docencoding);
if ( (new File(classFileName)).exists()) {
javaDoc = new JavaDoc(new File(classFileName));
excute(javaDocBean,filename);
}
else
throw new JavaDocException("the classFile(" + classFileName +
") is not existed!");
htmlWriter.close();
}
private void excute(JavaDocBean javaDocBean,String filename) {
htmlWriter.println("<SCRIPT LANGUAGE='JavaScript'><!--");
htmlWriter.println("function src(){");
htmlWriter.println("document.javadocsrc.submit();}");
htmlWriter.println("function example(){");
htmlWriter.println("document.javadocexample.submit();}//--></SCRIPT>");
htmlWriter.printPartialHeader(javaDoc.getClassName());
htmlWriter.print("<BODY BGCOLOR='white'>");
htmlWriter.println("<table border='0' width='100%' cellpadding='1' cellspacing='0'><tr><td colspan=3 bgcolor='#EEEEFF' class='NavBarCell1'>");
htmlWriter.println(
"<form name ='javadocsrc' action ='/download' method='post'>");
htmlWriter.println("<input type ='hidden' name='filename' ");
htmlWriter.println("value='" +
OppositePath.getSolidusPath(javaDocBean.getSrcpath()) +
"'>");
htmlWriter.println("</form>");
htmlWriter.println(
"<form name ='javadocexample' action ='/download' method='post'>");
htmlWriter.println("<input type ='hidden' name='filename' ");
htmlWriter.println("value='" +
OppositePath.getSolidusPath(javaDocBean.getExamplepath()) +
"'>");
htmlWriter.println("</form>");
htmlWriter.p();
htmlWriter.print("原代码下载:");
htmlWriter.print(htmlWriter.getHyperLink("javascript:src();", "", "下载", true,
""));
htmlWriter.printNbsps(4);
htmlWriter.print("示例下载:");
if (javaDocBean.getExamplepath() != null &&
!javaDocBean.getExamplepath().equals(""))
htmlWriter.print(htmlWriter.getHyperLink("javascript:example();", "",
"下载", true,
""));
else
htmlWriter.print(htmlWriter.getHyperLink("#", "", "下载", true,
""));
htmlWriter.printNbsps(4);
htmlWriter.print(htmlWriter.getHyperLink("/controller?parentid="+javaDocBean.getId()+"&requestType=propositioncommand&subRequestType=getMessage", "",
"意见或建议", true,
""));
htmlWriter.pEnd();
htmlWriter.p();
htmlWriter.print("依赖关系:");
htmlWriter.print(javaDocBean.getDepend()); //
htmlWriter.pEnd();
htmlWriter.print("</td></tr></table>");
htmlWriter.hr();
htmlWriter.println("<H2><FONT SIZE='-1'>");
htmlWriter.print(javaDoc.getPackage());
htmlWriter.print("</FONT>");
htmlWriter.br();
if (!javaDoc.isInterface()) {
htmlWriter.print("Class ");
}
else {
htmlWriter.print("Interface ");
}
htmlWriter.println(javaDoc.getClassName());
htmlWriter.print("</H2>");
htmlWriter.println("<PRE>");
htmlWriter.println(javaDoc.getSuperClass());
htmlWriter.println(" |");
htmlWriter.print(" +--");
htmlWriter.bold();
htmlWriter.print(javaDoc.getFullClassName());
htmlWriter.boldEnd();
htmlWriter.println("</PRE>");
htmlWriter.hr();
htmlWriter.br();
if (javaDoc.getClassComment() != null) {
htmlWriter.println(javaDoc.getFullClassName());
htmlWriter.println(javaDoc.getClassComment());
}
DocletTag[] doclet = javaDoc.getClassTags();
for (int i = 0; i < doclet.length; i++) {
htmlWriter.p();
htmlWriter.bold();
htmlWriter.print(doclet[i].getName() + ": ");
htmlWriter.boldEnd();
htmlWriter.print(doclet[i].getValue());
htmlWriter.pEnd();
}
if (javaDoc.getClassComment() != null)
htmlWriter.hr();
htmlWriter.p();
//////////成员变量
Vector it = new Vector();
FieldBean fieldBean = new FieldBean();
it = javaDoc.getFieldInfo();
if (it != null && it.size() > 0) {
htmlWriter.println("<TABLE BORDER='1' CELLPADDING='3' CELLSPACING='0' WIDTH='100%'><TR BGCOLOR='#CCCCFF' CLASS='TableHeadingColor'><TD COLSPAN=2><FONT SIZE='+2'><B>Field Summary</B></FONT></TD></TR>");
for (int i = 0; i < it.size(); i++) {
fieldBean = (FieldBean) it.get(i);
htmlWriter.print("<TR BGCOLOR='white' CLASS='TableRowColor'><TD ALIGN='right' VALIGN='top' WIDTH='32%'><FONT SIZE='-1'><CODE>");
htmlWriter.print(fieldBean.getType());
htmlWriter.print("</CODE></FONT></TD>");
htmlWriter.print("<TD width='68%'><CODE>");
htmlWriter.print(htmlWriter.getHyperLink(filename, fieldBean.getName(),
fieldBean.getName(), true, ""));
htmlWriter.print("</CODE>");
htmlWriter.br();
htmlWriter.printNbsps(10);
htmlWriter.print(fieldBean.getComment() == null ? "" :
fieldBean.getComment());
htmlWriter.print("</TD></TR>");
}
htmlWriter.print("</TABLE>");
htmlWriter.br();
}
///////////////构造函数
it = javaDoc.getConstructor();
MethodBean methodBean = new MethodBean();
if (it != null && it.size() > 0) {
htmlWriter.println("<TABLE BORDER='1' CELLPADDING='3' CELLSPACING='0' WIDTH='100%'><TR BGCOLOR='#CCCCFF' CLASS='TableHeadingColor'><TD COLSPAN=2><FONT SIZE='+2'><B>Constructor Summary</B></FONT></TD></TR>");
for (int j = 0; j < it.size(); j++) {
methodBean = (MethodBean) it.get(j);
htmlWriter.print(
"<TR BGCOLOR='white' CLASS='TableRowColor'><TD><CODE><B>");
String temp = methodBean.getParameter();
if (temp == null || temp.equals("null"))
temp = "";
//temp = ln(temp);
htmlWriter.print(htmlWriter.getHyperLink(filename,
methodBean.getName() + "(" +
temp + ")",
methodBean.getName() + "(" +
temp + ")", true, ""));
htmlWriter.br();
htmlWriter.printNbsps(10);
htmlWriter.print(methodBean.getComment() == null ? "" :
methodBean.getComment());
}
htmlWriter.println("</TD></TR></TABLE>");
htmlWriter.br();
}
////////////////////////////////
//成员函数
it = javaDoc.getMethodInfo();
MethodBean methodBean1 = new MethodBean();
if (it != null && it.size() > 0) {
htmlWriter.println("<TABLE BORDER='1' CELLPADDING='3' CELLSPACING='0' WIDTH='100%'><TR BGCOLOR='#CCCCFF' CLASS='TableHeadingColor'><TD COLSPAN=2><FONT SIZE='+2'><B>Method Summary</B></FONT></TD></TR>");
for (int k = 0; k < it.size(); k++) {
methodBean1 = (MethodBean) it.get(k);
htmlWriter.print(
"<TR BGCOLOR='white' CLASS='TableRowColor'><TD ALIGN='right' VALIGN='top' WIDTH='32%'><FONT SIZE='-1'><CODE>");
htmlWriter.printNbsps(1);
htmlWriter.print(methodBean1.getType());
htmlWriter.print("</CODE></FONT></TD>");
htmlWriter.print("<TD><CODE>");
String temp1 = methodBean1.getParameter();
if (temp1 == null || temp1.equals("null"))
temp1 = "";
//temp1 = ln(temp1);
htmlWriter.print(htmlWriter.getHyperLink(filename,
methodBean1.getName() + "(" +
temp1 + ")",
methodBean1.getName() + "(" +
temp1 + ")", true, ""));
htmlWriter.println("</CODE>");
htmlWriter.br();
htmlWriter.printNbsps(10);
htmlWriter.print(methodBean1.getComment() == null ? "" :
methodBean1.getComment());
htmlWriter.println("</TD></TR>");
}
htmlWriter.println("</TABLE>");
htmlWriter.br();
}
//////////////////
//成员变量的详细信息
it = new Vector();
FieldBean fieldBean1 = new FieldBean();
it = javaDoc.getFieldInfo();
if (it != null && it.size() > 0) {
htmlWriter.println("<TABLE BORDER='1' CELLPADDING='3' CELLSPACING='0' WIDTH='100%'><TR BGCOLOR='#CCCCFF' CLASS='TableHeadingColor'><TD COLSPAN=1><FONT SIZE='+2'><B>Field Detail</B></FONT></TD></TR></TABLE>");
for (int q = 0; q < it.size(); q++) {
fieldBean1 = (FieldBean) it.get(q);
htmlWriter.print("<A name=" + fieldBean1.getName() +
"><!-- --></A>");
htmlWriter.print("<H3>");
htmlWriter.print(fieldBean1.getName());
htmlWriter.print("</H3>");
htmlWriter.print("<PRE>");
htmlWriter.print(fieldBean1.getType());
htmlWriter.printNbsps(2);
htmlWriter.bold();
htmlWriter.print(fieldBean1.getName());
htmlWriter.boldEnd();
htmlWriter.print("</PRE>");
htmlWriter.println("<DL>");
htmlWriter.print("<DD>");
htmlWriter.print(fieldBean1.getComment() == null ? "" :
fieldBean1.getComment());
htmlWriter.println("<P><DL></DL></DD></DL>");
htmlWriter.hr();
}
}
///////////////构造函数详细信息
it = javaDoc.getConstructor();
MethodBean methodBean2 = new MethodBean();
if (it != null && it.size() > 0) {
htmlWriter.println("<TABLE cellSpacing=0 cellPadding=3 width='100%' border=1><TBODY><TR class=TableHeadingColor bgColor=#ccccff><TD><FONT size=+2><B>Constructor Detail</B></FONT></TD></TR></TBODY></TABLE>");
for (int a = 0; a < it.size(); a++) {
methodBean2 = (MethodBean) it.get(a);
String temp = methodBean2.getParameter();
if (temp == null || temp.equals("null"))
temp = "";
String nontemp = temp;
temp = ln(temp);
htmlWriter.print("<A name=" + methodBean2.getName() + "(" +
nontemp + ")" +
"><!-- --></A>");
htmlWriter.print("<H3>");
htmlWriter.print(methodBean2.getName());
htmlWriter.print("</H3>");
htmlWriter.print("<PRE>");
htmlWriter.print(methodBean2.getType() == null ? "public" :
methodBean2.getType());
htmlWriter.printNbsps(2);
htmlWriter.bold();
htmlWriter.print(methodBean2.getName() + "(" +
temp + ")");
htmlWriter.boldEnd();
htmlWriter.print("</PRE>");
htmlWriter.println("<DL>");
htmlWriter.print("<DD>");
htmlWriter.print(methodBean2.getComment() == null ? "" :
methodBean2.getComment());
htmlWriter.println("<P>");
htmlWriter.println("<DT><B>Parameters:</B>");
DocletTag[] doclettag = methodBean2.getTag();
for (int i = 0; i < doclettag.length; i++) {
htmlWriter.print("<DD><CODE>" + doclettag[i].getName() + "</CODE> - " +
doclettag[i].getValue());
}
htmlWriter.print("</DL>");
htmlWriter.hr();
}
}
///////////////成员函数详细信息
it = javaDoc.getMethodInfo();
MethodBean methodBean3 = new MethodBean();
if (it != null && it.size() > 0) {
htmlWriter.println("<TABLE cellSpacing=0 cellPadding=3 width='100%' border=1><TBODY><TR class=TableHeadingColor bgColor=#ccccff><TD><FONT size=+2><B>Method Detail</B></FONT></TD></TR></TBODY></TABLE>");
for (int a = 0; a < it.size(); a++) {
methodBean3 = (MethodBean) it.get(a);
String temp = methodBean3.getParameter();
if (temp == null || temp.equals("null"))
temp = "";
String nontemp = temp;
temp = ln(temp);
htmlWriter.print("<A name=" + methodBean3.getName() + "(" +
nontemp + ")" +
"><!-- --></A>");
htmlWriter.print("<H3>");
htmlWriter.print(methodBean3.getName());
htmlWriter.print("</H3>");
htmlWriter.print("<PRE>");
htmlWriter.print(methodBean3.getType() == null ? "public" :
methodBean3.getType());
htmlWriter.printNbsps(2);
htmlWriter.bold();
htmlWriter.print(methodBean3.getName() + "(" +
temp + ")");
htmlWriter.boldEnd();
htmlWriter.print("</PRE>");
htmlWriter.println("<DL>");
htmlWriter.print("<DD>");
htmlWriter.print(methodBean3.getComment() == null ? "" :
methodBean3.getComment());
htmlWriter.println("<P>");
htmlWriter.println("<DT><B>Parameters:</B>");
DocletTag[] doclettag = methodBean3.getTag();
for (int i = 0; i < doclettag.length; i++) {
htmlWriter.print("<DD><CODE>" + doclettag[i].getName() + "</CODE> - " +
doclettag[i].getValue());
}
htmlWriter.print("</DL>");
htmlWriter.hr();
}
}
htmlWriter.println("<table border='0' width='100%' cellpadding='1' cellspacing='0'><tr><td colspan=3 bgcolor='#EEEEFF' class='NavBarCell1'>");
htmlWriter.p();
htmlWriter.print("原代码下载:");
htmlWriter.print(htmlWriter.getHyperLink("javascript:src();", "", "下载", true,
""));
htmlWriter.printNbsps(4);
htmlWriter.print("示例下载:");
if (javaDocBean.getExamplepath() != null &&
!javaDocBean.getExamplepath().equals(""))
htmlWriter.print(htmlWriter.getHyperLink("javascript:example();", "",
"下载", true,
""));
else
htmlWriter.print(htmlWriter.getHyperLink("#", "", "下载", true,
""));
htmlWriter.printNbsps(4);
htmlWriter.print(htmlWriter.getHyperLink("/controller?parentid="+javaDocBean.getId()+"&requestType=propositioncommand&subRequestType=getMessage", "",
"意见或建议", true,
""));
htmlWriter.pEnd();
htmlWriter.p();
htmlWriter.print("依赖关系:");
htmlWriter.print(javaDocBean.getDepend()); //
htmlWriter.pEnd();
htmlWriter.print("</td></tr></table>");
htmlWriter.hr();
htmlWriter.bodyEnd();
htmlWriter.htmlEnd();
}
private static String ln(String str) {
StringTokenizer st = new StringTokenizer(str, ",");
StringBuffer sb = new StringBuffer();
while (st.hasMoreTokens()) {
sb.append(st.nextToken());
if (st.hasMoreTokens()) {
sb.append(",");
sb.append("\n");
}
}
str = sb.toString();
return str;
}
public static void main(String[] atr) {
JavaDocCreater javaDocCreater = new JavaDocCreater();
try {
javaDocCreater.genJavaDoc("d:\\aa",
"RequestHelper.html",
"gb2312", "d:\\aa\\RequestHelper.java", null);
}
catch (Exception ex) {
ex.printStackTrace();
}
}
}
package com.highcom.hcgip.javadoc;
import java.io.*;
import com.thoughtworks.qdox.*;
import com.thoughtworks.qdox.model.*;
import java.util.*;
public class JavaDoc {
private File file;
private JavaDocBuilder builder = null;
private JavaSource src = null;
private JavaClass cls[] = null;
private JavaField[] jf = null;
private JavaMethod[] jm = null;
/**
* 根据参数的不同生成不同的javasource
* @param file 具体的文件
*/
public JavaDoc(File file) {
this.file = file;
try {
builder = new JavaDocBuilder();
this.src = builder.addSource(file);
cls = src.getClasses();
jf = cls[0].getFields();
jm = cls[0].getMethods();
}
catch (IOException ex) {
ex.printStackTrace();
}
}
/**
* 获取包名
* @return
*/
public String getPackage() {
return src.getPackage();
}
/**
* 返回都包含什么类
* @return String数组
*/
public String[] getImports() {
return src.getImports();
}
/**
* 获得完整类名
* @return
*/
public String getFullClassName() {
return cls[0].getFullyQualifiedName();
}
/**
* 只获得类名,不包含包名
* @return
*/
public String getClassName() {
return cls[0].getName();
}
/**
* 判断是否是接口
* @return
*/
public boolean isInterface() {
return cls[0].isInterface();
}
/**
* 判断是否是public
* @return
*/
public boolean isClassPublic() {
return cls[0].isPublic();
}
/**
* 判断次类是不是Abstract
* @return
*/
public boolean isCalssAbstract() {
return cls[0].isAbstract();
}
public boolean isClassFinal() {
return cls[0].isFinal();
}
/**
* 获得超类
* @return
*/
public String getSuperClass() {
return cls[0].getSuperClass().getValue();
}
/**
* 获得类的注释
* @return
*/
public String getClassComment() {
return cls[0].getComment();
}
public String[] getImplements() {
String it[] = null;
for (int i = 0; i < cls[0].getImplements().length; i++) {
it[i] = cls[0].getImplements()[i].getValue();
}
return it;
}
/**
* get DocletTags
* @return
*/
public DocletTag[] getClassTags() {
return cls[0].getTags();
}
/**
* 获取定义的成员变量,只获取public 类型的
* @return 返回数值是数组类型的FieldBean
*/
public Vector getFieldInfo() {
Vector it = new Vector();
String tmp = "";
for (int i = 0; i < jf.length; i++) {
if (jf[i].isPublic()) {
FieldBean fieldBean = new FieldBean();
tmp = "public ";
if (jf[i].isStatic())
tmp += "static ";
if (jf[i].isFinal())
tmp += "final ";
if (jf[i].getType().isArray())
fieldBean.setType(tmp + jf[i].getType().getValue() + "[]");
else
fieldBean.setType(tmp + jf[i].getType().getValue());
fieldBean.setName(jf[i].getName());
fieldBean.setComment(jf[i].getComment());
it.add(fieldBean);
}
}
return it;
}
/**
* 获取构造函数
* @return
*/
public Vector getConstructor() {
Vector it = new Vector();
String tmp = "";
int l = 0;
for (int i = 0; i < jm.length; i++) {
if (jm[i].isConstructor()) {
MethodBean methodBean = new MethodBean();
JavaParameter parmeter[] = jm[i].getParameters();
for (int j = 0; j < parmeter.length; j++) {
l++;
if (parmeter[j].getType().isArray())
tmp += parmeter[j].getType().getValue() + "[]";
else
tmp += parmeter[j].getType().getValue();
tmp += " " + parmeter[j].getName();
if (parmeter.length > l)
tmp += ",";
}
methodBean.setName(jm[i].getName());
methodBean.setParameter(tmp);
tmp="";
l=0;
methodBean.setComment(jm[i].getComment());
methodBean.setTag(jm[i].getTags());
it.add(methodBean);
}
}
return it;
}
/**
* 获取成员函数
* @return
*/
public Vector getMethodInfo() {
Vector it = new Vector();
String tmp = "";
String str = "";
int l = 0;
for (int i = 0; i < jm.length; i++) {
if (!jm[i].isConstructor()) {
if (jm[i].isPublic() || jm[i].isProtected()) {
if (jm[i].isAbstract())
str = "abstract ";
if (jm[i].isStatic())
str += "static ";
if (jm[i].isSynchronized())
str += "synchronized ";
MethodBean methodBean = new MethodBean();
JavaParameter parmeter[] = jm[i].getParameters();
for (int j = 0; j < parmeter.length; j++) {
l++;
if (parmeter[j].getType().isArray())
tmp += parmeter[j].getType().getValue() + "[]";
else
tmp += parmeter[j].getType().getValue();
tmp += " " + parmeter[j].getName();
if (parmeter.length > l)
tmp += ",";
}
if (jm[i].isProtected()) {
if (jm[i].getReturns().isArray())
methodBean.setType("protected " + str +
jm[i].getReturns().getValue() + "[]");
else
methodBean.setType("protected " + str +
jm[i].getReturns().getValue());
}
else {
if (jm[i].getReturns().isArray())
methodBean.setType("public " + str + jm[i].getReturns().getValue() +
"[]");
else
methodBean.setType("public " + str + jm[i].getReturns().getValue());
}
methodBean.setName(jm[i].getName());
methodBean.setParameter(tmp);
tmp="";
str="";
l=0;
methodBean.setComment(jm[i].getComment());
methodBean.setTag(jm[i].getTags());
it.add(methodBean);
}
}
}
return it;
}
public static void main(String[] args) {
File f = new File("D:\\ParamUtils.java");
JavaDoc j = new JavaDoc(f);
Vector b = j.getMethodInfo();
for (int q = 0; q < b.size(); q++) {
MethodBean mb = (MethodBean) b.get(q);
p(mb.getType());p(" ");
p(mb.getName());p(" (");
p(mb.getParameter());
pt(")");
// pt(mb.getComment());
}
// DocletTag[] t = mb.getTag();
// for (int a = 0; a < t.length; a++) {
// pt(t[a].getName());
// pt(t[a].getValue());
// }
// }
// Vector it = j.getFieldInfo();
// FieldBean a = null;
// for (int i = 0; i < it.size(); i++) {
// a = (FieldBean) it.get(i);
// pt(a.getType());
// pt(a.getName());
// pt(a.getComment());
// }
/*
pt("PackageName:" + j.getPackage());
pt("ClassName:" + j.getClassName());
pt("ClassFullName:" + j.getFullClassName());
pt("superCalss:" + j.getSuperClass());
pt("ClassComment:" + j.getClassComment());
DocletTag[] d =j.getClassTags();
for(int i=0;i<d.length;i++)
{
pt("name:"+d[i].getName());
pt("value:"+d[i].getValue());
}
*/
}
static void pt(String str) {
System.out.println(str);
}
static void p(String str) {
System.out.print(str);
}
}
package com.highcom.hcgip.javadoc;
import java.io.*;
import java.lang.*;
import java.util.*;
public class HtmlWriter extends PrintWriter {
/**
* Name of the file, to which this writer is writing to.
*/
protected final String htmlFilename;
/**
* URL file separator string("/").
*/
public static final String fileseparator="/";
/**
* Initializes PrintWriter with the FileWriter.
*
* @param filename File Name to which the PrintWriter will do the Output.
* @param docencoding Encoding to be used for this file.
* @exception IOException Exception raised by the FileWriter is passed on
* to next level.
* @exception UnSupportedEncodingException Exception raised by the
* OutputStreamWriter is passed on to next level.
*/
public HtmlWriter(String filename, String docencoding)
throws IOException, UnsupportedEncodingException {
super(genWriter(null, filename, docencoding));
htmlFilename = filename;
}
/**
* Initializes PrintWriter with the FileWriter.
*
* @param path The directory path to be created for this file.
* @param filename File Name to which the PrintWriter will do the Output.
* @param docencoding Encoding to be used for this file.
* @exception IOException Exception raised by the FileWriter is passed on
* to next level.
* @exception UnSupportedEncodingException Exception raised by the
* OutputStreamWriter is passed on to next level.
*/
public HtmlWriter(String path, String filename, String docencoding)
throws IOException, UnsupportedEncodingException {
super(genWriter(path, filename, docencoding));
htmlFilename = filename;
}
/**
* Create the directory path for the file to be generated, construct
* FileOutputStream and OutputStreamWriter depending upon docencoding.
*
* @param path The directory path to be created for this file.
* @param filename File Name to which the PrintWriter will do the Output.
* @param docencoding Encoding to be used for this file.
* @exception IOException Exception raised by the FileWriter is passed on
* to next level.
* @exception UnSupportedEncodingException Exception raised by the
* OutputStreamWriter is passed on to next level.
* @return Writer Writer for the file getting generated.
* @see java.io.FileOutputStream
* @see java.io.OutputStreamWriter
*/
static Writer genWriter(String path, String filename, String docencoding)
throws IOException, UnsupportedEncodingException {
FileOutputStream fos;
if (path != null) {
DirectoryManager.createDirectory(path);
fos = new FileOutputStream(((path.length() > 0)?
path + fileseparator: "") + filename);
} else {
fos = new FileOutputStream(filename);
}
if (docencoding == null) {
OutputStreamWriter oswriter = new OutputStreamWriter(fos);
docencoding = oswriter.getEncoding();
return oswriter;
} else {
return new OutputStreamWriter(fos, docencoding);
}
}
/**
* Print <HTML> tag. Add a newline character at the end.
*/
public void html() {
println("<HTML>");
}
/**
* Print </HTML> tag. Add a newline character at the end.
*/
public void htmlEnd() {
println("</HTML>");
}
/**
* Print <BODY> tag. Add a newline character at the end.
*/
public void body() {
println("<BODY>");
}
/**
* Print <BODY BGCOLOR="bgcolor"> tag. Add a newline character at the end.
*
* @param bgcolor BackGroung color.
*/
public void body(String bgcolor) {
println("<BODY BGCOLOR=\"" + bgcolor + "\">");
}
/**
* Print </BODY> tag. Add a newline character at the end.
*/
public void bodyEnd() {
println("</BODY>");
}
/**
* Print <TITLE> tag. Add a newline character at the end.
*/
public void title() {
println("<TITLE>");
}
/**
* Print </TITLE> tag. Add a newline character at the end.
*/
public void titleEnd() {
println("</TITLE>");
}
/**
* Print <UL> tag. Add a newline character at the end.
*/
public void ul() {
println("<UL>");
}
/**
* Print </UL> tag. Add a newline character at the end.
*/
public void ulEnd() {
println("</UL>");
}
/**
* Print <LI> tag.
*/
public void li() {
print("<LI>");
}
/**
* Print <LI TYPE="type"> tag.
*
* @param type Type string.
*/
public void li(String type) {
print("<LI TYPE=\"" + type + "\">");
}
/**
* Print <H1> tag. Add a newline character at the end.
*/
public void h1() {
println("<H1>");
}
/**
* Print </H1> tag. Add a newline character at the end.
*/
public void h1End() {
println("</H1>");
}
/**
* Print text with <H1> tag. Also adds </H1> tag. Add a newline character
* at the end of the text.
*
* @param text Text to be printed with <H1> format.
*/
public void h1(String text) {
h1();
println(text);
h1End();
}
/**
* Print <H2> tag. Add a newline character at the end.
*/
public void h2() {
println("<H2>");
}
/**
* Print text with <H2> tag. Also adds </H2> tag. Add a newline character
* at the end of the text.
*
* @param text Text to be printed with <H2> format.
*/
public void h2(String text) {
h2();
println(text);
h2End();
}
/**
* Print </H2> tag. Add a newline character at the end.
*/
public void h2End() {
println("</H2>");
}
/**
* Print <H3> tag. Add a newline character at the end.
*/
public void h3() {
println("<H3>");
}
/**
* Print text with <H3> tag. Also adds </H3> tag. Add a newline character
* at the end of the text.
*
* @param text Text to be printed with <H3> format.
*/
public void h3(String text) {
h3();
println(text);
h3End();
}
/**
* Print </H3> tag. Add a newline character at the end.
*/
public void h3End() {
println("</H3>");
}
/**
* Print <H4> tag. Add a newline character at the end.
*/
public void h4() {
println("<H4>");
}
/**
* Print </H4> tag. Add a newline character at the end.
*/
public void h4End() {
println("</H4>");
}
/**
* Print text with <H4> tag. Also adds </H4> tag. Add a newline character
* at the end of the text.
*
* @param text Text to be printed with <H4> format.
*/
public void h4(String text) {
h4();
println(text);
h4End();
}
/**
* Print <H5> tag. Add a newline character at the end.
*/
public void h5() {
println("<H5>");
}
/**
* Print </H5> tag. Add a newline character at the end.
*/
public void h5End() {
println("</H5>");
}
/**
* Print HTML <IMG SRC="imggif" WIDTH="width" HEIGHT="height" ALT="imgname>
* tag. It prepends the "images" directory name to the "imggif". This
* method is used for oneone format generation. Add a newline character
* at the end.
*
* @param imggif Image GIF file.
* @param imgname Image name.
* @param width Width of the image.
* @param height Height of the image.
*/
public void img(String imggif, String imgname, int width, int height) {
println("<IMG SRC=\"images/" + imggif + ".gif\""
+ " WIDTH=\"" + width + "\" HEIGHT=\"" + height
+ "\" ALT=\"" + imgname + "\">");
}
/**
* Print <MENU> tag. Add a newline character at the end.
*/
public void menu() {
println("<MENU>");
}
/**
* Print </MENU> tag. Add a newline character at the end.
*/
public void menuEnd() {
println("</MENU>");
}
/**
* Print <PRE> tag. Add a newline character at the end.
*/
public void pre() {
println("<PRE>");
}
/**
* Print </PRE> tag. Add a newline character at the end.
*/
public void preEnd() {
println("</PRE>");
}
/**
* Print <HR> tag. Add a newline character at the end.
*/
public void hr() {
println("<HR>");
}
/**
* Print <HR SIZE="size" WIDTH="widthpercent%"> tag. Add a newline
* character at the end.
*
* @param size Size of the ruler.
* @param widthPercent Percentage Width of the ruler
*/
public void hr(int size, int widthPercent) {
println("<HR SIZE=\"" + size + "\" WIDTH=\"" + widthPercent + "%\">");
}
/**
* Print <HR SIZE="size" NOSHADE> tag. Add a newline character at the end.
*
* @param size Size of the ruler.
* @param noshade noshade string.
*/
public void hr(int size, String noshade) {
println("<HR SIZE=\"" + size + "\" NOSHADE>");
}
/**
* Get the "<B>" string.
*
* @return String Return String "<B>";
*/
public String getBold() {
return "<B>";
}
/**
* Get the "</B>" string.
*
* @return String Return String "</B>";
*/
public String getBoldEnd() {
return "</B>";
}
/**
* Print <B> tag.
*/
public void bold() {
print("<B>");
}
/**
* Print </B> tag.
*/
public void boldEnd() {
print("</B>");
}
/**
* Print text passed, in bold format using <B> and </B> tags.
*
* @param text String to be printed in between <B> and </B> tags.
*/
public void bold(String text) {
bold();
print(text);
boldEnd();
}
/**
* Print text passed, in Italics using <I> and </I> tags.
*
* @param text String to be printed in between <I> and </I> tags.
*/
public void italics(String text) {
print("<I>");
print(text);
println("</I>");
}
/**
* Return, text passed, with Italics <I> and </I> tags, surrounding it.
* So if the text passed is "Hi", then string returned will be "<I>Hi</I>".
*
* @param text String to be printed in between <I> and </I> tags.
*/
public String italicsText(String text) {
return "<I>" + text + "</I>";
}
/**
* Print "&nbsp;", non-breaking space.
*/
public void space() {
print(" ");
}
/**
* Print <DL> tag. Add a newline character at the end.
*/
public void dl() {
println("<DL>");
}
/**
* Print </DL> tag. Add a newline character at the end.
*/
public void dlEnd() {
println("</DL>");
}
/**
* Print <DT> tag.
*/
public void dt() {
print("<DT>");
}
/**
* Print <DT> tag.
*/
public void dd() {
print("<DD>");
}
/**
* Print </DD> tag. Add a newline character at the end.
*/
public void ddEnd() {
println("</DD>");
}
/**
* Print <SUP> tag. Add a newline character at the end.
*/
public void sup() {
println("<SUP>");
}
/**
* Print </SUP> tag. Add a newline character at the end.
*/
public void supEnd() {
println("</SUP>");
}
/**
* Print <FONT SIZE="size"> tag. Add a newline character at the end.
*
* @param size String size.
*/
public void font(String size) {
println("<FONT SIZE=\"" + size + "\">");
}
/**
* Print <FONT ID="stylename"> tag. Add a newline character at the end.
*
* @param stylename String stylename.
*/
public void fontStyle(String stylename) {
print("<FONT ID=\"" + stylename + "\">");
}
/**
* Print <FONT SIZE="size" ID="stylename"> tag. Add a newline character
* at the end.
*
* @param size String size.
* @param stylename String stylename.
*/
public void fontSizeStyle(String size, String stylename) {
println("<FONT size=\"" + size + "\" ID=\"" + stylename + "\">");
}
/**
* Print </FONT> tag.
*/
public void fontEnd() {
print("</FONT>");
}
/**
* Get the "<FONT COLOR="color">" string.
*
* @param color String color.
* @return String Return String "<FONT COLOR="color">".
*/
public String getFontColor(String color) {
return "<FONT COLOR=\"" + color + "\">";
}
/**
* Get the "</FONT>" string.
*
* @return String Return String "</FONT>";
*/
public String getFontEnd() {
return "</FONT>";
}
/**
* Print <CENTER> tag. Add a newline character at the end.
*/
public void center() {
println("<CENTER>");
}
/**
* Print </CENTER> tag. Add a newline character at the end.
*/
public void centerEnd() {
println("</CENTER>");
}
/**
* Print anchor <A NAME="name"> tag.
*
* @param name Name String.
*/
public void aName(String name) {
print("<A NAME=\"" + name + "\">");
}
/**
* Print </A> tag.
*/
public void aEnd() {
print("</A>");
}
/**
* Print <I> tag.
*/
public void italic() {
print("<I>");
}
/**
* Print </I> tag.
*/
public void italicEnd() {
print("</I>");
}
/**
* Print contents within anchor <A NAME="name"> tags.
*
* @param name String name.
* @param content String contents.
*/
public void anchor(String name, String content) {
aName(name);
print(content);
aEnd();
}
/**
* Print anchor <A NAME="name"> and </A>tags. Print comment string
* "<!-- -->" within those tags.
*
* @param name String name.
*/
public void anchor(String name) {
aName(name);
print("<!-- -->");
aEnd();
}
/**
* Print newline and then print <P> tag. Add a newline character at the
* end.
*/
public void p() {
println();
println("<P>");
}
public void pEnd(){
println();
println("</P>");
}
/**
* Print newline and then print <BR> tag. Add a newline character at the
* end.
*/
public void br() {
println();
println("<BR>");
}
/**
* Print <ADDRESS> tag. Add a newline character at the end.
*/
public void address() {
println("<ADDRESS>");
}
/**
* Print </ADDRESS> tag. Add a newline character at the end.
*/
public void addressEnd() {
println("</ADDRESS>");
}
/**
* Print <HEAD> tag. Add a newline character at the end.
*/
public void head() {
println("<HEAD>");
}
/**
* Print </HEAD> tag. Add a newline character at the end.
*/
public void headEnd() {
println("</HEAD>");
}
/**
* Print <CODE> tag.
*/
public void code() {
print("<CODE>");
}
/**
* Print </CODE> tag.
*/
public void codeEnd() {
print("</CODE>");
}
/**
* Print <EM> tag. Add a newline character at the end.
*/
public void em() {
println("<EM>");
}
/**
* Print </EM> tag. Add a newline character at the end.
*/
public void emEnd() {
println("</EM>");
}
/**
* Print HTML <TABLE BORDER="border" WIDTH="width"
* CELLPADDING="cellpadding" CELLSPACING="cellspacing"> tag.
*
* @param border Border size.
* @param width Width of the table.
* @param cellpadding Cellpadding for the table cells.
* @param cellspacing Cellspacing for the table cells.
*/
public void table(int border, String width, int cellpadding,
int cellspacing) {
println("\n<TABLE BORDER=\"" + border +
"\" WIDTH=\"" + width +
"\" CELLPADDING=\"" + cellpadding +
"\" CELLSPACING=\"" + cellspacing + "\">");
}
/**
* Print HTML <TABLE BORDER="border" CELLPADDING="cellpadding"
* CELLSPACING="cellspacing"> tag.
*
* @param border Border size.
* @param cellpadding Cellpadding for the table cells.
* @param cellspacing Cellspacing for the table cells.
*/
public void table(int border, int cellpadding, int cellspacing) {
println("\n<TABLE BORDER=\"" + border +
"\" CELLPADDING=\"" + cellpadding +
"\" CELLSPACING=\"" + cellspacing + "\">");
}
/**
* Print HTML <TABLE BORDER="border" WIDTH="width">
*
* @param border Border size.
* @param width Width of the table.
*/
public void table(int border, String width) {
println("\n<TABLE BORDER=\"" + border +
"\" WIDTH=\"" + width + "\">");
}
/**
* Print the HTML table tag with border size 0 and width 100%.
*/
public void table() {
table(0, "100%");
}
/**
* Print </TABLE> tag. Add a newline character at the end.
*/
public void tableEnd() {
println("</TABLE>");
}
/**
* Print <TR> tag. Add a newline character at the end.
*/
public void tr() {
println("<TR>");
}
/**
* Print </TR> tag. Add a newline character at the end.
*/
public void trEnd() {
println("</TR>");
}
/**
* Print <TD> tag.
*/
public void td() {
print("<TD>");
}
/**
* Print <TD NOWRAP> tag.
*/
public void tdNowrap() {
print("<TD NOWRAP>");
}
/**
* Print <TD WIDTH="width"> tag.
*
* @param width String width.
*/
public void tdWidth(String width) {
print("<TD WIDTH=\"" + width + "\">");
}
/**
* Print </TD> tag. Add a newline character at the end.
*/
public void tdEnd() {
println("</TD>");
}
/**
* Print <LINK str> tag.
*
* @param str String.
*/
public void link(String str) {
println("<LINK " + str + ">");
}
/**
* Print "<!-- " comment start string.
*/
public void commentStart() {
print("<!-- ");
}
/**
* Print "-->" comment end string. Add a newline character at the end.
*/
public void commentEnd() {
println("-->");
}
/**
* Print <TR BGCOLOR="color" ID="stylename"> tag. Adds a newline character
* at the end.
*
* @param color String color.
* @param stylename String stylename.
*/
public void trBgcolorStyle(String color, String stylename) {
println("<TR BGCOLOR=\"" + color + "\" ID=\"" + stylename + "\">");
}
/**
* Print <TR BGCOLOR="color"> tag. Adds a newline character at the end.
*
* @param color String color.
*/
public void trBgcolor(String color) {
println("<TR BGCOLOR=\"" + color + "\">");
}
/**
* Print <TR ALIGN="align" VALIGN="valign"> tag. Adds a newline character
* at the end.
*
* @param align String align.
* @param valign String valign.
*/
public void trAlignVAlign(String align, String valign) {
println("<TR ALIGN=\"" + align + "\" VALIGN=\"" + valign + "\">");
}
/**
* Print <TD COLSPAN=i> tag.
*
* @param i integer.
*/
public void tdColspan(int i) {
print("<TD COLSPAN=" + i + ">");
}
/**
* Print <TD BGCOLOR="color" ID="stylename"> tag.
*
* @param color String color.
* @param stylename String stylename.
*/
public void tdBgcolorStyle(String color, String stylename) {
print("<TD BGCOLOR=\"" + color + "\" ID=\"" + stylename + "\">");
}
/**
* Print <TD COLSPAN=i BGCOLOR="color" ID="stylename"> tag.
*
* @param i integer.
* @param color String color.
* @param stylename String stylename.
*/
public void tdColspanBgcolorStyle(int i, String color, String stylename) {
print("<TD COLSPAN=" + i + " BGCOLOR=\"" + color + "\" ID=\"" +
stylename + "\">");
}
/**
* Print <TD ALIGN="align"> tag. Adds a newline character
* at the end.
*
* @param align String align.
*/
public void tdAlign(String align) {
print("<TD ALIGN=\"" + align + "\">");
}
/**
* Print <TD ALIGN="align" ID="stylename"> tag.
*
* @param align String align.
* @param stylename String stylename.
*/
public void tdVAlignClass(String align, String stylename) {
print("<TD VALIGN=\"" + align + "\" ID=\"" + stylename + "\">");
}
/**
* Print <TD VALIGN="valign"> tag.
*
* @param valign String valign.
*/
public void tdVAlign(String valign) {
print("<TD VALIGN=\"" + valign + "\">");
}
/**
* Print <TD ALIGN="align" VALIGN="valign"> tag.
*
* @param align String align.
* @param valign String valign.
*/
public void tdAlignVAlign(String align, String valign) {
print("<TD ALIGN=\"" + align + "\" VALIGN=\"" + valign + "\">");
}
/**
* Print <TD ALIGN="align" ROWSPAN=rowspan> tag.
*
* @param align String align.
* @param rowspan integer rowspan.
*/
public void tdAlignRowspan(String align, int rowspan) {
print("<TD ALIGN=\"" + align + "\" ROWSPAN=" + rowspan + ">");
}
/**
* Print <TD ALIGN="align" VALIGN="valign" ROWSPAN=rowspan> tag.
*
* @param align String align.
* @param valign String valign.
* @param rowspan integer rowspan.
*/
public void tdAlignVAlignRowspan(String align, String valign,
int rowspan) {
print("<TD ALIGN=\"" + align + "\" VALIGN=\"" + valign
+ "\" ROWSPAN=" + rowspan + ">");
}
/**
* Print <BLOCKQUOTE> tag. Add a newline character at the end.
*/
public void blockquote() {
println("<BLOCKQUOTE>");
}
/**
* Print </BLOCKQUOTE> tag. Add a newline character at the end.
*/
public void blockquoteEnd() {
println("</BLOCKQUOTE>");
}
/**
* Get the "<CODE>" string.
*
* @return String Return String "<CODE>";
*/
public String getCode() {
return "<CODE>";
}
/**
* Get the "</CODE>" string.
*
* @return String Return String "</CODE>";
*/
public String getCodeEnd() {
return "</CODE>";
}
/**
* Print <NOFRAMES> tag. Add a newline character at the end.
*/
public void noFrames() {
println("<NOFRAMES>");
}
/**
* Print </NOFRAMES> tag. Add a newline character at the end.
*/
public void noFramesEnd() {
println("</NOFRAMES>");
}
/**
* print
* @param num
*/
public void printNbsps(int num) {
for(int i=0;i<num;i++){
print(" ");
}
}
/**
* Print some part of the Html file header.
*
* @param title Title of this HTML document.
*/
public void printPartialHeader(String title) {
println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Frameset//EN\"" +
"\"http://www.w3.org/TR/REC-html40/frameset.dtd\">");
html();
head();
title();
println(title);
titleEnd();
println("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=gb2312\">");
headEnd();
}
/**
* Get Html Hyper Link string.
*
* @param link String name of the file.
* @param where Position of the link in the file. Character '#' is not
* needed.
* @param label Tag for the link.
* @param bold Boolean that sets label to bold.
* @param stylename String style of text defined in style sheet.
* @return String Hyper Link.
*/
public String getHyperLink(String link, String where,
String label, boolean bold,
String stylename) {
StringBuffer retlink = new StringBuffer();
retlink.append("<A HREF=\"");
retlink.append(link);
if (where.length() != 0) {
retlink.append("#");
retlink.append(where);
}
retlink.append("\">");
if (stylename.length() != 0) {
retlink.append("<FONT ID=\"");
retlink.append(stylename);
retlink.append("\">");
}
if (bold) {
retlink.append("<B>");
}
retlink.append(label);
if (bold) {
retlink.append("</B>");
}
if (stylename.length() != 0) {
retlink.append("</FONT>");
}
retlink.append("</A>");
return retlink.toString();
}
public static void main(String[] args){
try {
HtmlWriter h = new HtmlWriter("a\\b\\c","a.html", "gb2312");
h.html();
h.htmlEnd();
h.close();
}
catch (IOException ex) {
}
}
}
这些是主要的几个类,希望能对这个感兴趣的人有帮助
本文地址:http://com.8s8s.com/it/it15072.htm