Tip: Javadoc as XML
2005-05-26 08:17
399 查看
Tip: Javadoc as XML | |||||
Editor-in-Chief, Code Generation Network 14 Apr 2005 A lot of value is locked up in your Java code: all your classes and interfaces, as well as their instance variables and methods. You can use these data to create documentation, to build code generators, or to provide metrics for project reporting.The Javadoc tool is a very well-factored application. Many people think it?s just a program that creates HTML from reading the code and comments in a set of Java™ files. But in reality, the tool is divided into two sections. The first is the code-analysis engine, which parses the code and the comments. The second generates the HTML, but as it turns out, you can change that section with extensions called doclets. For this tip, I use JELDoclet (see Resources) to generate an XML output file from a set of test Java files. Then, I use XSL to format the XML into a simple HTML file that demonstrates what data are in the XML file. After downloading JELDoclet, I run it on a set of files using the following syntax:
testdirectory of JELDoclet includes a set of test Java files. This command parses all the Java files in the test directory and creates a file called out.xml, which contains all the information in the Javadoc tree. Listing 1 shows a portion of this output XML file. Listing 1. The JELDoclet output XML file
jeltag contains a series of classtags -- one for each class. Within the classtags are the fields, methods, and constructors. The XML file also includes the associated comments. Create HTML from the XML Your first step toward generating the HTML for this XML (in Listing 1) is to start with the base tag template in Listing 2. Listing 2. The base HTML template
htmlroot tag and the headsection. Then, I iterate through every class in the input file. For each class, I output the class or interface name in an h1tag and apply templates for the fields, constructors, and methods. Then, I wrap it all up by closing the bodyand htmltags. The template for building the field HTML is very simple, as Listing 3 shows. Listing 3. The field template
paragraphtag with the classfield, which I?ll use later in the CSS. The method and constructor templates are similar to the field template, as Listing 4 shows. Listing 4. The method and constructor templates
xsl:apply-templatestag, which finds the correct template for the paramstag -- in this case, the template shown in Listing 5. Listing 5. The params template
xsl:ifdirective to insert a comma only if I?m on the second parameter or later. The last part of the XSL template is the CSS portion, which makes the output a bit more readable (see Listing 6). Listing 6. The CSS template
Figure 1. The resultant HTML in a browser Summary Formatting HTML is just one of the things you can do with XML output from the Javadoc tool. You can use the information in the Javadoc tree (exported here as XML) to power code generators, as XDoclet does. You can perform code-reference analysis and refactoring like Eclipse or IntelliJ. You can also get metrics on your code base. Having a complete, structured representation of your Java code in XML is a powerful tool that can help increase your productivity. Resources Visit the XSL standards site at the W3C, a handy reference to XSL technologies and standards. Check out the XPath page at the W3C, which provides version and standard information. Download Saxon, the popular XSL processor that was used in the creation of this article. Read Michael Kay?s XSLT 2.0 Programmer's Reference, the bible of XSLT. It?s a fantastic introduction and a valuable reference work. While you're at it, pick up XPath 2.0 Programmer's Reference by Michael Kay -- the ultimate reference by the man who wrote the W3C specification. Read Code Generation in Action by Jack D. Herrington, which covers generating code for a wide variety of targets not limited to database access. See the Javadoc home page for more information about Sun Microsystems? Javadoc tool. Find out more about JELDoclet, the Doclet extension used in this article to create the input XML. Explore Doclet.com, a repository of Doclet extensions for the Javadoc tool. Try XDoclet, the acclaimed code generator that was originally based on the Javadoc framework. Find hundreds more XML resources on the developerWorks XML zone. Learn how you can become an IBM Certified Developer in XML and related technologies.
|
相关文章推荐
- java xml -doc - string 转换
- Display XML in a javadoc
- Units Problem: How to read text size as custom attr from xml and set it to TextView in java code
- java用openoffice,poi+itext,jacob实现预览doc,xml的方法
- Java POI组件——简单提取Word、word转html、text、xml(仅支持doc,不支持docx)
- Units Problem: How to read text size as custom attr from xml and set it to TextView in java code
- at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:54
- java导出word(2003版doc格式) FreeMarker+XML实现
- 「android」as javadoc乱码
- 【解决方案】cvc-complex-type.2.4.a: Invalid content was found starting with element 'init-param'. One of '{"http://java.sun.com/xml/ns/javaee":run-as, "http://java.sun.com/xml/ns/javaee":security-role-r
- Write & Read xml as java class
- Units Problem: How to read text size as custom attr from xml and set it to TextView in java code
- 用Java实现基于SOAP的XML文档网络传输及远程过程调用(RPC).doc
- java解析xml的4种经典方法
- MyEclipse/Eclipse 生成Java Doc
- java操作xml(sax,dom,jdom,dom4j)增删改查
- Java中四种操作xml方式的比较
- How to insert a SWF file in an ASDoc API documentation ?
- JAVA通过XPath解析XML性能比较(原创)
- apache poi解析word(doc)文档成xml及导出成html