com.rimerosolutions.ant.git.AntTaskDoclet Maven / Gradle / Ivy
/*
* Copyright 2013, Rimero Solutions
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.rimerosolutions.ant.git;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
import com.sun.javadoc.ClassDoc;
import com.sun.javadoc.LanguageVersion;
import com.sun.javadoc.MethodDoc;
import com.sun.javadoc.RootDoc;
import com.sun.tools.doclets.standard.Standard;
/**
* Quick and very dirty doclet for Ant tasks documentation.
* Self-Contained for now, no template engine or external dependencies.
*
* @author Yves Zoundi
*/
public final class AntTaskDoclet extends Standard {
private static final String ENCODING_UTF_8 = "UTF-8";
private static final String ANT_TASK_CLASS_NAME = "org.apache.tools.ant.Task";
private static String destDir;
private static String doctitle = "Ant tasks documentation";
private static String header = "Ant tasks documentation";
private static String windowtitle = "Ant tasks documentation";
private static String bottom;
private static String overview;
private static final Logger LOG = Logger.getLogger(AntTaskDoclet.class.getName());
private static final List ELEMENT_PREFIXES = Arrays.asList("create", "add", "addConfigured");
private static final List ATTRIBUTE_PREFIXES = Arrays.asList("set");
private static final String TAG_ANTDOC_NOTREQUIRED = "antdoc.notrequired";
private static Map classDataMap = new HashMap();
private static final String HTML_INDEX_PAGE = "index.html";
private static final String HTML_HEADER_PAGE = "header.html";
private static final String HTML_BODY_PAGE = "body.html";
private static final String HTML_NAV_PAGE = "nav.html";
private static final class ElementData {
String name;
String description;
static ElementData fromMethodDoc(MethodDoc methodDoc) {
ElementData data = new ElementData();
data.name = sanitizeName(methodDoc.name(), ELEMENT_PREFIXES);
data.description = methodDoc.commentText();
return data;
}
}
private static final class AttributeData {
String name;
String description;
boolean required;
static AttributeData fromMethodDoc(MethodDoc methodDoc) {
AttributeData data = new AttributeData();
data.description = methodDoc.commentText();
data.required = !(methodDoc.tags(TAG_ANTDOC_NOTREQUIRED).length > 0);
data.name = sanitizeName(methodDoc.name(), ATTRIBUTE_PREFIXES);
return data;
}
}
private static final class ClassData {
String qualifiedName;
String simpleName;
String description;
List parentClassDatas = new ArrayList();
List attributesList = new ArrayList();
List elementsList = new ArrayList();
boolean hidden;
}
static interface WriterCallback {
void doWithWriter(Writer w) throws IOException;
}
private static void copyFile(File sourceFile, File destFile) throws IOException {
try (InputStream in = new FileInputStream(sourceFile);
OutputStream out = new FileOutputStream(destFile)) {
byte[] buf = new byte[4096];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
}
}
public static final boolean start(RootDoc root) {
readDestDirOption(root.options());
try {
writeContents(root.classes());
} catch (IOException ioe) {
LOG.log(Level.SEVERE, "Ant tasks documentation failed", ioe);
return false;
}
return true;
}
public static void readDestDirOption(String options[][]) {
for (int i = 0; i < options.length; i++) {
String[] opt = options[i];
if (opt[0].equals("-d")) {
destDir = opt[1];
} else if (opt[0].equals("-header")) {
header = opt[1];
} else if (opt[0].equals("-doctitle")) {
doctitle = opt[1];
} else if (opt[0].equals("-windowtitle")) {
windowtitle = opt[1];
} else if (opt[0].equals("-overview")) {
overview = opt[1];
} else if (opt[0].equals("-bottom")) {
bottom = opt[1];
}
}
}
private static String sanitizeName(String objectName, List prefixes) {
String newName = objectName;
for (String prefix : prefixes) {
if (newName.startsWith(prefix)) {
String methodName = newName.substring(prefix.length() + 1);
return (("" + newName.charAt(prefix.length())).toLowerCase() + methodName).toLowerCase();
}
}
return newName;
}
static void withWriter(File f, WriterCallback wc) throws IOException {
try (Writer w = new OutputStreamWriter(new FileOutputStream(f), ENCODING_UTF_8)) {
wc.doWithWriter(w);
}
}
private static void writeIndexPage() throws IOException {
withWriter(new File(new File(destDir), HTML_INDEX_PAGE), new WriterCallback() {
@Override
public void doWithWriter(Writer w) throws IOException {
StringBuilder sb = new StringBuilder();
sb.append("");
sb.append(htmlElement("title", windowtitle));
sb.append("");
sb.append("");
sb.append("