本文介绍了如何借助Groovy语言,通过QDox包来解决项目接口文档的创建。 由于项目在早期开发过程中,业务接口越来越多,维护也越来麻烦,光有源码,但是不便于查阅。因此,通过脚本自动创建接口文档,方便浏览和查阅。 源码如下:
/**
* ===================================================
* @author: rain(http://rainboyan.com)
* @changelog
*
* v1 2008/12/10 20:25
* - 实现了基本的接口生成文档功能
* ===================================================
*/
import com.thoughtworks.qdox.*
import com.thoughtworks.qdox.model.*
import org.codehaus.groovy.scriptom.*
True = 1
False = 0
wdStyleHeading1 = -2
wdStyleHeading2 = -3
wdStyleHeading3 = -4
wdStyleHeading4 = -5
wdStyleHeading5 = -6
wdStyleHeading6 = -7
wdStyleBodyText = -67
wdTexture10Percent = 100
wdTexture20Percent = 200
wdTexture12Pt5Percent = 125
wdTexture15Percent = 150
wdTexture17Pt5Percent = 175
wdTexture22Pt5Percent = 225
wdAlignRowLeft = 0
wdAlignRowCenter = 1
wdAlignRowRight = 2
wdAlignVerticalTop = 0
wdAlignVerticalCenter = 1
wdAlignVerticalJustify = 2
wdAlignVerticalBottom = 3
wdLineBreak = 6
wdPageBreak = 7
wdBulletGallery = 1
wdNumberGallery = 2
wdOutlineNumberGallery = 3
wdListNumberStyleArabic = 0
def printClass = { c ->
def methods = c.methods
range = doc.Range()
range.SetRange(doc.Range().End, doc.Range().End)
range.Style = wdStyleHeading4
range.InsertAfter(c.name)
range.InsertParagraphAfter()
range.SetRange(doc.Range().End, doc.Range().End)
range.Style = wdStyleBodyText
table = doc.Tables.Add(range, methods.size() + 1, 3)
row = table.Rows(1)
row.Cells.Height = 30
row.Cells.Shading.Texture = wdTexture20Percent
row.Alignment = wdAlignRowCenter
row.Cells.VerticalAlignment = wdAlignVerticalCenter
row.range.Style = wdStyleBodyText
row.Range.Bold = True
row.Cells(1).Range.InsertAfter "CLASS NAME"
row.Cells(2).Range.InsertAfter "METHOD NAME"
row.Cells(3).Range.InsertAfter "DESCRIPTION"
for (int i = 1; i < methods.size() + 1; i++) {
row = table.Rows(i+1)
row.Cells(1).Range.InsertAfter i.toString()
row.Cells(2).Range.InsertAfter methods[i-1].name
}
}
def word = new ActiveXObject("Word.Application")
word.Visible = false
doc = word.Documents.Add()
template = doc.ListTemplates().Add()
level1 = template.ListLevels(1)
level1.NumberFormat = "%1"
level1.NumberStyle = wdListNumberStyleArabic
level1.StartAt = 1
doc.Styles(wdStyleHeading4).LinkToListTemplate(template)
Properties conf = new Properties()
conf.load(getClass().getResourceAsStream("/conf.properties"))
def builder = new JavaDocBuilder()
builder.addSourceTree(new File(conf['src.dir']))
JavaClass[] classes = builder.classes
for(int i=0; i < classes.size(); i++) {
if(classes[i].isInterface())
printClass classes[i]
}
word.Visible = true
word.Quit()

2008-12-10 21:17:0
Posted in
Tags:
Comments: 


