比如读取rtf文本,把文本写入rtf
解决方案 »
- finally block does not complete normally
- 大家帮忙看看这几道题选什么,谢谢!!
- 社区没有可以串口编程拔号的吗?
- 未来 JAVA 的 GUI 开发人员会怎么样? 讨论者全有分
- 谁能帮我看一下是为什么?空指针的问题
- java 空指针异常找不到原因? 这怎么解决?
- 怎么用JAVA产生唯一ID号?
- 100分! 寻找电子工业出版社的<<java2从入门到精通>>的源代码文件, 迫切需要,help!
- 小弟以前学的是C++,现在学习java,有点极其菜鸟的问题???希望不要介意
- 用什么样的工具(除installshield外)能方便给解压缩的jdk和Tomcat打包,而且能往环境变量写JAVA_HOME
- 数据插入成功后如何得到数据库自动生成的标识列
- 请教 不重复随机数?
import java.io.PrintWriter;
import java.io.FileOutputStream;
import java.io.FileNotFoundException;
import java.util.StringTokenizer;
import com.sun.javadoc.*;import com.quiotix.html.parser.*;
/**
* RTF Doclet.<p>
* This class is a plugin for javadoc producing documentation under in RTF format. The file
* generated is usualy incorporated into a Microsod Word document.
*
* By default this doclet will write into a file called javaDocumentation.rtf file. The name of this file
* can be overriden using the "-filename" parameter to javadoc.<p>
* Example: "javadoc -doclet RTFDoclet -filename report.rtf *.java".
* <p>
* Note:
* <ul>
* <li> The comments are parsed by a simple html parser. This parser understands
* the P,UL/LI, BR, A, TT, B and TABLE tags.
* <li> The produced RTF file can not be easily read by some RTF readers (for example Wordpad)
* due to the array that are present in the document.
* </ul><p>
* Curiously, JavaDoc doe snot process @link contructs ,so this doclet does them, but
* the entire spec must be on the same physical line in the original comment block.
*
* The tags @unpublished and @unsupported can be used to suppress printing a package, class or method
* by specifying -published on the command line.
*
* @author Nicolas Zin ([email protected]) Feb 1999
* @author David Cozens (([email protected]) June 1999
* @author Joe Panko ([email protected]) January 2000
* @author Thierry Bodhuin (([email protected]) April 2000
* @author Ian Klein ([email protected]) April 2004
*/
public class RTFDoclet
{
// to specify if we have to print all arguments on one line
private boolean collapseParameters = false;
// the output stream
private PrintWriter file;
private boolean published = false; //Only print published if true /**
* Constructor.<BR>
* The constructor could raise a FileNotFoundException if it doesn't manage to open
* the RTF file to write into.
*
* @param filenameP String contsining output file
* @param collapseParametersP boolean if parameters are to be written on 1 line
* @param publishedP boolean indicating only published items are printed
* @throws FileNotFoundException
*/
public RTFDoclet(String filenameP, boolean collapseParametersP, boolean publishedP) throws FileNotFoundException
{
file = new PrintWriter(new FileOutputStream(filenameP));
collapseParameters = collapseParametersP;
published = publishedP; file.println("{\\rtf\\pard\\plain"); // Removed \qj // fix the font table
file.println("{\\fonttbl");
file.println(" {\\f1\\froman\\fcharset238\\fprq2 Times New Roman CE;}");
file.println(" {\\f2\\fmodern\\fcharset238\\fprq1 Courier New Tur;}");
file.println(" {\\f3\\fswiss\\fcharset0\\fprq2{\\*\\panose 020b0604020202020204}Arial;}");
file.println("}"); // fix the color table
file.print("{\\colortbl;");
file.print("\\red0\\green0\\blue0;"); //Black
file.print("\\red0\\green0\\blue192;"); //Blue
file.print("\\red0\\green128\\blue0;"); //Green
file.print("\\red192\\green0\\blue64;"); //Red
file.print("\\red230\\green234\\blue255;"); //Light blue
file.print("\\red230\\green230\\blue230;"); //Gray
file.println("}"); // set the font size
file.println("\\f1\\fs20"); // new section begin at a new page
file.println("\\sbkpage");
} /**
* Method called at the end to put the rtf end block
*/
public void closeFile()
{
// close the '{\rtf' block
file.println("}");
file.close();
} /**
* Method called by Javadoc to recognize the -filename parameter
*
* @param option String containing command line option
* @return int number of command line args it takes
*/
public static int optionLength(String option)
{
if (option.equals("-filename"))
{
return 2;
}
if (option.equals("-collapseParameters"))
{
return 1;
}
if (option.equals("-published"))
{
return 1;
}
return 0;
} /**
* Main procedure
* @param rootP RootDoc of the javadoc parse
* @return boolean indicating succcess
*/
public static boolean start(RootDoc rootP)
{
String filenameP = "javaDocumentation.rtf";
String optionsP[][] = rootP.options();
boolean collapseParametersP = false;
boolean publishedP = false; int iP;
for (iP = 0; iP < optionsP.length; iP++)
{
if ( (optionsP[iP][0].equals("-filename")) && (optionsP[iP].length > 1))
{
filenameP = optionsP[iP][1];
}
if (optionsP[iP][0].equals("-collapseParameters"))
{
collapseParametersP = true;
}
if (optionsP[iP][0].equals("-published"))
{
publishedP = true;
}
}
try
{
RTFDoclet docP = new RTFDoclet(filenameP, collapseParametersP, publishedP); if (rootP.specifiedClasses() != null)
docP.listClasses(rootP); if (rootP.specifiedPackages() != null)
docP.listPackages(rootP); docP.closeFile();
}
catch (FileNotFoundException eP)
{
System.out.println("java.io.FileNotFoundException caught!");
System.out.println("Impossible to write the file \"" + filenameP + "\"");
System.out.println("Be sure that the location is correct or the file is not already used by another application");
}
return true;
} /**
* Generate documentation for a particular package
* @param rootP RootDoc of javadoc parse
*/
public void listPackages(RootDoc rootP)
{
PackageDoc packagesP[] = rootP.specifiedPackages(); for (int iP = 0; iP < packagesP.length; iP++)
{
if ( isToBePrinted(packagesP[iP]) )
{
printPackage(packagesP[iP]);
ClassDoc classesP[] = packagesP[iP].allClasses();
for (int jP = 0; jP < classesP.length; jP++)
{
printClass(classesP[jP]);
}
file.println("\\sect ");
}
}
}
* Print the name of a package
* @param packageP PackageDoc of a package
*/
public void printPackage(PackageDoc packageP)
{
file.println("\\trowd \\trgraph70 \\trleft1134 \\cellx8650\\pard ");
file.print("\\nowidctlpar\\widctlpar\\intbl\\f1\\fs32\\cbpat6\\b ");
file.print("Package " + packageP.name());
file.println("\\b0\\cell {\\row }\\pard\\par "); // Comment about the whole package
file.print("{\\f1\\fs20\\cf1");
printComment(packageP, "package " + packageP.name());
file.println("\\par}");
} public void listClasses(RootDoc rootP)
{
ClassDoc classesP[] = rootP.specifiedClasses(); for (int jP = 0; jP < classesP.length; jP++)
{
if ( isToBePrinted(classesP[jP]) )
{
printClass(classesP[jP]);
}
}
} /**
* Print documentation for a particular class
* @param classeP ClassDoc of a class
*/
public void printClass(ClassDoc classeP)
{
file.println("{\\par}"); //Print the header
String strClass;
boolean isInterface = classeP.modifiers().endsWith("interface");
strClass = ( isInterface ? "Interface" : "Class" ); file.println("\\trowd \\trgraph70 \\trleft1134 \\cellx8650\\pard ");
file.print("\\nowidctlpar\\widctlpar\\intbl\\f1\\fs28\\cbpat6\\b ");
file.print(strClass + " " + classeP.name());
file.println("\\b0\\cell {\\row }\\pard\\par "); file.print("{");
strClass = ( isInterface ? " " : " class " ); // Don't print the word "class" if this is an interface file.print("\\f2\\fs20\\li1134 " + classeP.modifiers() + strClass + "{\\b " + classeP.name() + "}"); // Top level interfaces don't have a superclass
if (null != classeP.superclass())
{
// by the way don't print superclass of top level class.
if (classeP.superclass().toString().equals("java.lang.Object") == false)
{
file.print(" extends {\\f2\\cb1\\cf1 " + classeP.superclass().name() + "}");
}
} ClassDoc implementationsP[] = classeP.interfaces();
for (int kP = 0; kP < implementationsP.length; kP++)
{
if (kP == 0)
file.print(" implements ");
file.print("{\\f2\\cb1\\cf1 " + implementationsP[kP].name() + "}");
if (kP != implementationsP.length - 1)
file.print(", ");
} file.print("\\par\\par}"); // Comment about the whole class
file.print("{\\f1\\fs20\\cf1");
printComment(classeP, "class " + classeP.name());
file.println("\\par}"); if (classeP.fields().length > 0)
{
file.println("\\trowd \\trgraph70 \\trleft1134 \\cellx8650\\pard ");
file.println("\\nowidctlpar\\widctlpar\\intbl\\f1\\fs24\\cbpat5\\b Fields\\b0\\cell {\\row }\\pard");
FieldDoc fieldsP[] = classeP.fields();
boolean doSeparator = false;
for (int kP = 0; kP < fieldsP.length; kP++)
{
if ( isToBePrinted(fieldsP[kP]) )
{
if (doSeparator)
{
file.println("\\f1\\fs20\\li1134 _______________________________________________________________________\\par");
}
doSeparator = true; file.println("\\par\\li1134 ");
file.println("{\\f1\\fs20\\b " + fieldsP[kP].name() + "\\b0}\\par\\par"); printElement(fieldsP[kP]);
printComment(fieldsP[kP], "method " + classeP.name() + "." + fieldsP[kP].name());
}
}
file.println("\\par"); } if (classeP.constructors().length > 0)
{
file.println("\\trowd \\trgraph70 \\trleft1134 \\cellx8650\\pard ");
file.println("\\nowidctlpar\\widctlpar\\intbl\\f1\\fs24\\cbpat5\\b Constructors\\b0\\cell {\\row }\\pard");
ConstructorDoc constructeursP[] = classeP.constructors();
boolean doSeparator = false;
for (int kP = 0; kP < constructeursP.length; kP++)
{
if ( isToBePrinted(constructeursP[kP]) )
{
if (doSeparator)
{
file.println("\\f1\\fs20\\li1134 _______________________________________________________________________\\par");
}
doSeparator = true; file.println("\\par\\li1134 ");
file.println("{\\f1\\fs20\\li1134\\b " + constructeursP[kP].name() + "\\b0}\\par\\par"); printElement(constructeursP[kP]);
printComment(constructeursP[kP], "method " + classeP.name() + "." + constructeursP[kP].name());
}
}
file.println("\\par");
} if (classeP.methods().length > 0)
{
file.println("\\trowd \\trgraph70 \\trleft1134 \\cellx8650\\pard ");
file.println("\\nowidctlpar\\widctlpar\\intbl\\f1\\fs24\\cbpat5\\b Methods\\b0\\cell {\\row }\\pard");
MethodDoc methodesP[] = classeP.methods();
boolean doSeparator = false;
for (int kP = 0; kP < methodesP.length; kP++)
{
if ( isToBePrinted(methodesP[kP]) )
{
if (doSeparator)
{
file.println("\\f1\\fs20\\li1134 _______________________________________________________________________\\par");
}
doSeparator = true; file.println("\\par\\li1134 ");
file.println("{\\f1\\fs20\\b " + methodesP[kP].name() + "\\b0}\\par\\par"); printElement(methodesP[kP]);
printComment(methodesP[kP], "method " + classeP.name() + "." + methodesP[kP].name());
}
}
file.println("\\par");
}
}
* Print a comment block
* @param documentP Doc containing the comment
* @param elementName String with friendly name of what it is for error reporting
*/
public void printComment(Doc documentP, String elementName)
{
// print general comment
printParagraph(documentP.commentText(), elementName); printParamTag(documentP, "@param", "Parameters", elementName + ":Parameters");
printTag(documentP, "@return", "Returns", elementName + ":Returns");
printParamTag(documentP, "@throws", "Throws", elementName + ":Throws");
printTag(documentP, "@see", "See Also", elementName + ":See Also");
printTag(documentP, "@since", "Since", elementName + ":Since");
printTag(documentP, "@version", "Version", elementName + ":Version");
file.println("\\par");
} /**
* Print a normal tag
* @param documentP Doc element containing the tag
* @param tagNameP String with tag name
* @param logicalNameP String with friendly name for printing
* @param elementName String with friendly name of what it is for error reporting
*/
public void printTag(Doc documentP, String tagNameP, String logicalNameP, String elementName)
{
// print tags
Tag tagsP[] = documentP.tags(tagNameP);
for (int iP = 0; iP < tagsP.length; iP++)
{
if (iP == 0)
{
file.println("{\\f1\\cb1\\cf3\\li1134 \\b " + logicalNameP + "} \\par ");
}
file.print("\\tab ");
printParagraph(tagsP[iP].text(), elementName);
}
} /**
* @unpublished
* Print a 'parameter' tag. The difference with the normal is that the string found
* is separate into the header (normaly the name of the parameter) and the body (the
* explanation linked to the parameter)
*
* @param documentP Doc element containing the tag
* @param tagNameP String with tag name
* @param logicalNameP String with friendly name for printing
* @param elementName String with friendly name of what it is for error reporting
*/
public void printParamTag(Doc documentP, String tagNameP, String logicalNameP, String elementName)
{
// print tags
Tag tagsP[] = documentP.tags(tagNameP);
for (int iP = 0; iP < tagsP.length; iP++)
{
if (iP == 0)
{
file.println("{\\f1\\cb1\\cf3\\li1134 \\b " + logicalNameP + "} \\par ");
} int separator = tagsP[iP].text().indexOf(" ");
String begining = "";
String ending = "";
if (separator >= 0)
{
begining = tagsP[iP].text().substring(0, separator);
ending = tagsP[iP].text().substring(separator);
} file.print("\\tab \\b1" + begining + " - \\b0 ");
printParagraph(ending, elementName);
}
} /**
* @unsupported
* Method used to print the name of a method and its parameters
*
* @param elementP ProgramElementDoc to be printed
*/
public void printElement(ProgramElementDoc elementP)
{
if (elementP instanceof ExecutableMemberDoc)
{
ExecutableMemberDoc memberP = (ExecutableMemberDoc) elementP; String returnType = " ";
if (memberP instanceof MethodDoc)
{
MethodDoc method = (MethodDoc) memberP;
Type aType = method.returnType();
if (null != aType)
{
returnType = " " + aType.qualifiedTypeName() + aType.dimension() + " ";
}
}
file.print("{\\f2\\fs18\\li1134 " + elementP.modifiers() + returnType + "\\b1 " + elementP.name() + "\\b0 ("); Parameter[] parameters = memberP.parameters();
for (int parNo = 0; parNo < parameters.length; parNo++)
{
if (parNo != 0)
{
file.print(", ");
}
Type thisType = parameters[parNo].type(); if (!collapseParameters)
{
file.println();
file.print("\\par\\li1134\\tab ");
} file.print(thisType.typeName() + thisType.dimension() + " " + parameters[parNo].name());
}
file.println(")} \\par\\li1134 ");
}
else
{
file.println("{\\f2\\fs18\\li1134 " + elementP.modifiers() + "\\b1 " + elementP.name() + "\\b0} \\par ");
}
file.println("\\par"); } /**
* Print a paragraph of text using the HTML parser for Quixotic.
*
* @param text String containing the prargraph
* @param which String containing which one it is for error reporting
*/
private void printParagraph(String text, String which)
{
if ( ! "".equals(text) )
{
try
{
HtmlDocument document = new HtmlParser(new StringReader(text)).HtmlDocument();
RTFDocletVisitor v = new RTFDocletVisitor();
document.accept(v);
file.print("\\f1\\fs20\\li1134 " + v.getText() + "\\par ");
}
catch (Exception eP)
{ System.out.println("Parse error in" + which + ":");
System.out.println(text);
eP.printStackTrace();
}
}
} /**
* Determine if the package/class/methid etc should appaear in the output.
* Enables poublished and unpublished ot unsupported
*
* @param d Doc element to check
* @return boolean true of it should be priinted
*/
private boolean isToBePrinted(Doc d)
{
if ( d.tags("@unsupported").length > 0 || d.tags("@unpublished").length > 0 )
{
//Unpublished doc
return !published; // Only print if the -published flag is missing
}
else
{
return true;
}
}
}