001package org.asciidoctor.asciidoclet;
002
003import com.google.common.base.Optional;
004import com.google.common.base.Splitter;
005import com.google.common.collect.ImmutableList;
006import com.sun.javadoc.DocErrorReporter;
007import com.sun.javadoc.RootDoc;
008
009import java.io.File;
010import java.nio.charset.Charset;
011import java.util.List;
012
013/**
014 * Provides an interface to the doclet options we are interested in.
015 */
016public class DocletOptions {
017    public static final String ENCODING = "-encoding";
018    public static final String OVERVIEW = "-overview";
019    public static final String BASEDIR = "--base-dir";
020    public static final String STYLESHEET = "-stylesheetfile";
021    public static final String DESTDIR = "-d";
022    public static final String ATTRIBUTE = "-a";
023    public static final String ATTRIBUTE_LONG = "--attribute";
024    public static final String ATTRIBUTES_FILE = "--attributes-file";
025    public static final String GEM_PATH = "--gem-path";
026    public static final String REQUIRE = "-r";
027    public static final String REQUIRE_LONG = "--require";
028
029    private final Optional<File> basedir;
030    private final Optional<File> overview;
031    private final Optional<File> stylesheet;
032    private final Optional<File> destdir;
033    private final Optional<File> attributesFile;
034    private final String gemPath;
035    private final List<String> requires;
036    private final Charset encoding;
037    private final List<String> attributes;
038
039    public static final DocletOptions NONE = new DocletOptions(new String[][]{});
040
041    public DocletOptions(RootDoc rootDoc) {
042        this(rootDoc.options());
043    }
044
045    public DocletOptions(String[][] options) {
046        File basedir = null;
047        File overview = null;
048        File stylesheet = null;
049        File destdir = null;
050        File attrsFile = null;
051        String gemPath = null;
052        ImmutableList.Builder<String> requires = ImmutableList.builder();
053        Charset encoding = Charset.defaultCharset();
054        ImmutableList.Builder<String> attrs = ImmutableList.builder();
055        for (String[] option : options) {
056            if (option.length > 0) {
057                if (BASEDIR.equals(option[0])) {
058                    basedir = new File(option[1]);
059                }
060                else if (OVERVIEW.equals(option[0])) {
061                    overview = new File(option[1]);
062                }
063                else if (STYLESHEET.equals(option[0])) {
064                    stylesheet = new File(option[1]);
065                }
066                else if (DESTDIR.equals(option[0])) {
067                    destdir = new File(option[1]);
068                }
069                else if (ENCODING.equals(option[0])) {
070                    encoding = Charset.forName(option[1]);
071                }
072                else if (ATTRIBUTE.equals(option[0]) || ATTRIBUTE_LONG.equals(option[0])) {
073                    attrs.addAll(COMMA_WS.split(option[1]));
074                }
075                else if (ATTRIBUTES_FILE.equals(option[0])) {
076                    attrsFile = new File(option[1]);
077                }
078                else if (GEM_PATH.equals(option[0])) {
079                    gemPath = option[1];
080                }
081                else if (REQUIRE.equals(option[0]) || REQUIRE_LONG.equals(option[0])) {
082                    requires.addAll(COMMA_WS.split(option[1]));
083                }
084            }
085        }
086
087        this.basedir = Optional.fromNullable(basedir);
088        this.overview = Optional.fromNullable(overview);
089        this.stylesheet = Optional.fromNullable(stylesheet);
090        this.destdir = Optional.fromNullable(destdir);
091        this.encoding = encoding;
092        this.attributes = attrs.build();
093        this.attributesFile = Optional.fromNullable(attrsFile);
094        this.gemPath = gemPath;
095        this.requires = requires.build();
096    }
097
098    public Optional<File> overview() {
099        return overview;
100    }
101
102    public Optional<File> stylesheet() {
103        return stylesheet;
104    }
105
106    public Optional<File> baseDir() {
107        return basedir;
108    }
109
110    public Optional<File> destDir() {
111        return destdir;
112    }
113
114    public Charset encoding() {
115        return encoding;
116    }
117
118    public List<String> attributes() {
119        return attributes;
120    }
121
122    Optional<File> attributesFile() {
123        if (!attributesFile.isPresent()) return attributesFile;
124        File f = attributesFile.get();
125        if (!f.isAbsolute() && basedir.isPresent()) {
126            f = new File(basedir.get(), f.getPath());
127        }
128        return Optional.of(f);
129    }
130
131    public String gemPath() {
132        return gemPath;
133    }
134
135    public List<String> requires() {
136        return requires;
137    }
138
139    public static boolean validOptions(String[][] options, DocErrorReporter errorReporter, StandardAdapter standardDoclet) {
140        DocletOptions docletOptions = new DocletOptions(options);
141
142        if (!docletOptions.baseDir().isPresent()) {
143            errorReporter.printWarning(BASEDIR + " must be present for includes or file reference features to work properly.");
144        }
145
146        Optional<File> attrsFile = docletOptions.attributesFile();
147        if (attrsFile.isPresent() && !attrsFile.get().canRead()) {
148            errorReporter.printWarning("Cannot read attributes file " + attrsFile.get());
149        }
150
151        return standardDoclet.validOptions(options, errorReporter);
152    }
153
154    public static int optionLength(String option, StandardAdapter standardDoclet) {
155        if (BASEDIR.equals(option)) {
156            return 2;
157        }
158        if (ATTRIBUTE.equals(option) || ATTRIBUTE_LONG.equals(option)) {
159            return 2;
160        }
161        if (ATTRIBUTES_FILE.equals(option)) {
162            return 2;
163        }
164        if (GEM_PATH.equals(option)) {
165            return 2;
166        }
167        if (REQUIRE.equals(option) || REQUIRE_LONG.equals(option)) {
168            return 2;
169        }
170        return standardDoclet.optionLength(option);
171    }
172
173    // Split on comma with optional whitespace
174    private static final Splitter COMMA_WS = Splitter.onPattern("\\s*,\\s*").omitEmptyStrings().trimResults();
175}