package br.com.diogoko;

import com.sun.javadoc.DocErrorReporter;
import com.sun.javadoc.RootDoc;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateExceptionHandler;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;

/**
 * Hello world!
 */
public class FreeMarkerDoclet {

    private static String output = null;

    private static String template = null;

    public static boolean start(RootDoc root) {
        try {
            Configuration cfg = createConfiguration();
            Template t = cfg.getTemplate(new File(template).getName());
            Writer w = new FileWriter(new File(output));
            t.process(root, w);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return true;
    }

    private static Configuration createConfiguration() throws IOException {
        Configuration cfg = new Configuration(Configuration.VERSION_2_3_22);
        cfg.setDirectoryForTemplateLoading(new File(template).getParentFile());
        cfg.setDefaultEncoding("UTF-8");
        cfg.setTemplateExceptionHandler(TemplateExceptionHandler.DEBUG_HANDLER);

        return cfg;
    }

    public static int optionLength(String option) {
        if (option.equals("-o")) {
            return 2;
        } else if (option.equals("-t")) {
            return 2;
        }

        return 0;
    }

    public static boolean validOptions(String options[][],
                                       DocErrorReporter reporter) {
        boolean foundOutput = false;
        boolean foundTemplate = false;

        for (int i = 0; i < options.length; i++) {
            String[] opt = options[i];
            if (opt[0].equals("-o")) {
                foundOutput = true;
                output = opt[1];
            } else if (opt[0].equals("-t")) {
                foundTemplate = true;
                template = opt[1];
            }
        }

        if (!foundOutput || !foundTemplate) {
            reporter.printError("Usage: javadoc -o OUTPUT_FILE -t TEMPLATE_FILE ...");
            return false;
        }

        return true;
    }

}

@@ -239,20 +239,17 @@
 java.io.
-File
+*
 ;%0Aimport
@@ -258,98 +258,29 @@
 ava.
+n
 io.
-FileWriter;%0Aimport java.io.IOException;%0Aimport java.io.Writer;%0A%0A/**%0A * Hello world!%0A */
+charset.Charset;%0A
 %0Apub
@@ -580,24 +580,25 @@
 getName());%0A
+%0A
             
@@ -601,16 +601,14 @@
     
-Writer w
+File f
  = n
@@ -618,34 +618,169 @@
 File
-Writer(new File(output
+(output);%0A            FileOutputStream fos = new FileOutputStream(f);%0A            OutputStreamWriter w = new OutputStreamWriter(fos, Charset.forName(%22UTF-8%22
 ));%0A
+%0A
     

Forcing output encoding to UTF-8