Skip to content

Instantly share code, notes, and snippets.

@RainerW
Forked from chbaranowski/DocDoclet.java
Created February 21, 2011 21:15
Show Gist options
  • Save RainerW/837706 to your computer and use it in GitHub Desktop.
Save RainerW/837706 to your computer and use it in GitHub Desktop.
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import com.sun.javadoc.*;
import com.thoughtworks.xstream.XStream;
public class DocDoclet {
private static final String DOCU_XML_FILE = "docu.xml";
private static final String JAVADOC_DESCRIPTION_TAG = "description";
private static final String JAVADOC_TITLE_TAG = "title";
private final File reportDirectory = new File("target/test-docu/");
private final XStream xStream = new XStream();
public static boolean start(RootDoc root) throws Exception {
new DocDoclet().createTestDocumentation(root.classes());
return true;
}
protected final void createTestDocumentation(ClassDoc[] classes) throws Exception {
final Documentation documentation = new Documentation();
for (ClassDoc classDoc : classes) {
final TestClassDoc testClassDoc = new TestClassDoc(classDoc.name());
documentation.addTestClassDoc(testClassDoc);
for (MethodDoc methodDoc : classDoc.methods()) {
TestDoc testDoc = new TestDoc(methodDoc.name());
testClassDoc.addTestDoc(testDoc);
extractTitle(methodDoc, testDoc);
extractDescription(methodDoc, testDoc);
}
}
writeXmlModel(documentation);
}
private void extractDescription(MethodDoc methodDoc, TestDoc testDoc) {
final Tag[] tags = methodDoc.tags(JAVADOC_DESCRIPTION_TAG);
if (tags.length > 0) {
testDoc.setDescription(tags[0].text());
}
}
private void extractTitle(MethodDoc methodDoc, TestDoc testDoc) {
final Tag[] tags = methodDoc.tags(JAVADOC_TITLE_TAG);
if (tags.length > 0) {
testDoc.setTestTitle(tags[0].text());
}
}
private void writeXmlModel(Documentation documentation) throws FileNotFoundException {
getReportDirectory().mkdirs();
xStream.toXML(documentation, new FileOutputStream(getDocumentationFile()));
}
public File getDocumentationFile() {
return new File(getReportDirectory(), DOCU_XML_FILE);
}
public File getReportDirectory() {
return reportDirectory;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment