package jdk.javadoc.internal.doclets.formats.html;
import java.net.*;
import java.util.*;
import java.util.stream.Collectors;
import javax.lang.model.element.Element;
import javax.lang.model.element.PackageElement;
import javax.lang.model.element.TypeElement;
import javax.tools.JavaFileManager;
import javax.tools.JavaFileObject;
import javax.tools.StandardJavaFileManager;
import com.sun.source.util.DocTreePath;
import com.sun.tools.doclint.DocLint;
import jdk.javadoc.doclet.Doclet;
import jdk.javadoc.doclet.DocletEnvironment;
import jdk.javadoc.internal.doclets.toolkit.BaseConfiguration;
import jdk.javadoc.internal.doclets.toolkit.DocletException;
import jdk.javadoc.internal.doclets.toolkit.Messages;
import jdk.javadoc.internal.doclets.toolkit.Resources;
import jdk.javadoc.internal.doclets.toolkit.WriterFactory;
import jdk.javadoc.internal.doclets.toolkit.util.DocFile;
import jdk.javadoc.internal.doclets.toolkit.util.DocPath;
import jdk.javadoc.internal.doclets.toolkit.util.DocPaths;
import static javax.tools.Diagnostic.Kind.*;
public class HtmlConfiguration extends BaseConfiguration {
public static final String HTML_DEFAULT_CHARSET = "utf-8";
public String header = "";
public String packagesheader = "";
public String footer = "";
public String doctitle = "";
public String windowtitle = "";
public String top = "";
public String bottom = "";
public String helpfile = "";
public String stylesheetfile = "";
public List<String> additionalStylesheets = new ArrayList<>();
public String docrootparent = "";
public boolean nohelp = false;
public boolean splitindex = false;
public boolean createindex = true;
public boolean classuse = false;
public boolean createtree = true;
public String charset = null;
public boolean nodeprecatedlist = false;
public boolean nonavbar = false;
private boolean nooverview = false;
public String overviewpath = null;
public boolean createoverview = false;
public Map<Doclet.Option, String> doclintOpts = new LinkedHashMap<>();
public final Resources resources;
public DocPath topFile = DocPath.empty;
public TypeElement currentTypeElement = null;
protected SortedSet<SearchIndexItem> memberSearchIndex;
protected SortedSet<SearchIndexItem> moduleSearchIndex;
protected SortedSet<SearchIndexItem> packageSearchIndex;
protected SortedSet<SearchIndexItem> tagSearchIndex;
protected SortedSet<SearchIndexItem> typeSearchIndex;
protected Map<Character,List<SearchIndexItem>> tagSearchIndexMap = new HashMap<>();
protected Set<Character> tagSearchIndexKeys;
public final Contents contents;
protected final Messages messages;
public DocPaths docPaths;
public Map<Element, List<DocPath>> localStylesheetMap = new HashMap<>();
public HtmlConfiguration(Doclet doclet) {
super(doclet);
resources = new Resources(this,
BaseConfiguration.sharedResourceBundleName,
"jdk.javadoc.internal.doclets.formats.html.resources.standard");
messages = new Messages(this);
contents = new Contents(this);
String v;
try {
ResourceBundle rb = ResourceBundle.getBundle(versionBundleName, getLocale());
try {
v = rb.getString("release");
} catch (MissingResourceException e) {
v = defaultDocletVersion;
}
} catch (MissingResourceException e) {
v = defaultDocletVersion;
}
docletVersion = v;
}
private static final String versionBundleName = "jdk.javadoc.internal.tool.resources.version";
private static final String defaultDocletVersion = System.getProperty("java.version");
public final String docletVersion;
@Override
public String getDocletVersion() {
return docletVersion;
}
@Override
public Resources getResources() {
return resources;
}
public Contents getContents() {
return contents;
}
@Override
public Messages getMessages() {
return messages;
}
protected boolean validateOptions() {
if (!generalValidOptions()) {
return false;
}
if (!helpfile.isEmpty()) {
DocFile help = DocFile.createFileForInput(this, helpfile);
if (!help.exists()) {
reporter.print(ERROR, resources.getText("doclet.File_not_found", helpfile));
return false;
}
}
if (!stylesheetfile.isEmpty()) {
DocFile stylesheet = DocFile.createFileForInput(this, stylesheetfile);
if (!stylesheet.exists()) {
reporter.print(ERROR, resources.getText("doclet.File_not_found", stylesheetfile));
return false;
}
}
for (String ssheet : additionalStylesheets) {
DocFile ssfile = DocFile.createFileForInput(this, ssheet);
if (!ssfile.exists()) {
reporter.print(ERROR, resources.getText("doclet.File_not_found", ssheet));
return false;
}
}
utils.checkJavaScriptInOption("-header", header);
utils.checkJavaScriptInOption("-footer", footer);
utils.checkJavaScriptInOption("-top", top);
utils.checkJavaScriptInOption("-bottom", bottom);
utils.checkJavaScriptInOption("-doctitle", doctitle);
utils.checkJavaScriptInOption("-packagesheader", packagesheader);
return true;
}
@Override
public boolean finishOptionSettings() {
if (!validateOptions()) {
return false;
}
if (!getSpecifiedTypeElements().isEmpty()) {
Map<String, PackageElement> map = new HashMap<>();
PackageElement pkg;
for (TypeElement aClass : getIncludedTypeElements()) {
pkg = utils.containingPackage(aClass);
if (!map.containsKey(utils.getPackageName(pkg))) {
map.put(utils.getPackageName(pkg), pkg);
}
}
}
docPaths = new DocPaths(utils);
setCreateOverview();
setTopFile(docEnv);
workArounds.initDocLint(doclintOpts.values(), tagletManager.getAllTagletNames());
return true;
}
protected void setTopFile(DocletEnvironment docEnv) {
if (!checkForDeprecation(docEnv)) {
return;
}
if (createoverview) {
topFile = DocPaths.INDEX;
} else {
if (showModules) {
topFile = DocPath.empty.resolve(docPaths.moduleSummary(modules.first()));
} else if (packages.size() == 1 && packages.first().isUnnamed()) {
List<TypeElement> classes = new ArrayList<>(getIncludedTypeElements());
if (!classes.isEmpty()) {
TypeElement te = getValidClass(classes);
topFile = docPaths.forClass(te);
}
} else if (!packages.isEmpty()) {
topFile = docPaths.forPackage(packages.first()).resolve(DocPaths.PACKAGE_SUMMARY);
}
}
}
protected TypeElement getValidClass(List<TypeElement> classes) {
if (!nodeprecated) {
return classes.get(0);
}
for (TypeElement te : classes) {
if (!utils.isDeprecated(te)) {
return te;
}
}
return null;
}
protected boolean checkForDeprecation(DocletEnvironment docEnv) {
for (TypeElement te : getIncludedTypeElements()) {
if (isGeneratedDoc(te)) {
return true;
}
}
return false;
}
protected void setCreateOverview() {
if (!nooverview) {
if (overviewpath != null
|| modules.size() > 1
|| (modules.isEmpty() && packages.size() > 1)) {
createoverview = true;
}
}
}
@Override
public WriterFactory getWriterFactory() {
return new WriterFactoryImpl(this);
}
@Override
public Locale getLocale() {
if (locale == null)
return Locale.getDefault();
return locale;
}
@Override
public JavaFileObject getOverviewPath() {
if (overviewpath != null && getFileManager() instanceof StandardJavaFileManager) {
StandardJavaFileManager fm = (StandardJavaFileManager) getFileManager();
return fm.getJavaFileObjects(overviewpath).iterator().next();
}
return null;
}
public DocPath getMainStylesheet() {
if(!stylesheetfile.isEmpty()){
DocFile docFile = DocFile.createFileForInput(this, stylesheetfile);
return DocPath.create(docFile.getName());
}
return null;
}
public List<DocPath> getAdditionalStylesheets() {
return additionalStylesheets.stream()
.map(ssf -> DocFile.createFileForInput(this, ssf)).map(file -> DocPath.create(file.getName()))
.collect(Collectors.toList());
}
@Override
public JavaFileManager getFileManager() {
return docEnv.getJavaFileManager();
}
@Override
public boolean showMessage(DocTreePath path, String key) {
return (path == null || workArounds.haveDocLint());
}
@Override
public boolean showMessage(Element e, String key) {
return (e == null || workArounds.haveDocLint());
}
protected void buildSearchTagIndex() {
for (SearchIndexItem sii : tagSearchIndex) {
String tagLabel = sii.getLabel();
Character unicode = (tagLabel.length() == 0)
? '*'
: Character.toUpperCase(tagLabel.charAt(0));
List<SearchIndexItem> list = tagSearchIndexMap.get(unicode);
if (list == null) {
list = new ArrayList<>();
tagSearchIndexMap.put(unicode, list);
}
list.add(sii);
}
tagSearchIndexKeys = tagSearchIndexMap.keySet();
}
@Override
public Set<Doclet.Option> getSupportedOptions() {
Resources resources = getResources();
Doclet.Option[] options = {
new Option(resources, "--add-stylesheet", 1) {
@Override
public boolean process(String opt, List<String> args) {
additionalStylesheets.add(args.get(0));
return true;
}
},
new Option(resources, "-bottom", 1) {
@Override
public boolean process(String opt, List<String> args) {
bottom = args.get(0);
return true;
}
},
new Option(resources, "-charset", 1) {
@Override
public boolean process(String opt, List<String> args) {
charset = args.get(0);
return true;
}
},
new Option(resources, "-doctitle", 1) {
@Override
public boolean process(String opt, List<String> args) {
doctitle = args.get(0);
return true;
}
},
new Option(resources, "-footer", 1) {
@Override
public boolean process(String opt, List<String> args) {
footer = args.get(0);
return true;
}
},
new Option(resources, "-header", 1) {
@Override
public boolean process(String opt, List<String> args) {
header = args.get(0);
return true;
}
},
new Option(resources, "-helpfile", 1) {
@Override
public boolean process(String opt, List<String> args) {
if (nohelp == true) {
reporter.print(ERROR, resources.getText("doclet.Option_conflict",
"-helpfile", "-nohelp"));
return false;
}
if (!helpfile.isEmpty()) {
reporter.print(ERROR, resources.getText("doclet.Option_reuse",
"-helpfile"));
return false;
}
helpfile = args.get(0);
return true;
}
},
new Option(resources, "-html5") {
@Override
public boolean process(String opt, List<String> args) {
return true;
}
},
new Option(resources, "-nohelp") {
@Override
public boolean process(String opt, List<String> args) {
nohelp = true;
if (!helpfile.isEmpty()) {
reporter.print(ERROR, resources.getText("doclet.Option_conflict",
"-nohelp", "-helpfile"));
return false;
}
return true;
}
},
new Option(resources, "-nodeprecatedlist") {
@Override
public boolean process(String opt, List<String> args) {
nodeprecatedlist = true;
return true;
}
},
new Option(resources, "-noindex") {
@Override
public boolean process(String opt, List<String> args) {
createindex = false;
if (splitindex == true) {
reporter.print(ERROR, resources.getText("doclet.Option_conflict",
"-noindex", "-splitindex"));
return false;
}
return true;
}
},
new Option(resources, "-nonavbar") {
@Override
public boolean process(String opt, List<String> args) {
nonavbar = true;
return true;
}
},
new Hidden(resources, "-nooverview") {
@Override
public boolean process(String opt, List<String> args) {
nooverview = true;
if (overviewpath != null) {
reporter.print(ERROR, resources.getText("doclet.Option_conflict",
"-nooverview", "-overview"));
return false;
}
return true;
}
},
new Option(resources, "-notree") {
@Override
public boolean process(String opt, List<String> args) {
createtree = false;
return true;
}
},
new Option(resources, "-overview", 1) {
@Override
public boolean process(String opt, List<String> args) {
overviewpath = args.get(0);
if (nooverview == true) {
reporter.print(ERROR, resources.getText("doclet.Option_conflict",
"-overview", "-nooverview"));
return false;
}
return true;
}
},
new Hidden(resources, "-packagesheader", 1) {
@Override
public boolean process(String opt, List<String> args) {
packagesheader = args.get(0);
return true;
}
},
new Option(resources, "-splitindex") {
@Override
public boolean process(String opt, List<String> args) {
splitindex = true;
if (createindex == false) {
reporter.print(ERROR, resources.getText("doclet.Option_conflict",
"-splitindex", "-noindex"));
return false;
}
return true;
}
},
new Option(resources, "--main-stylesheet -stylesheetfile", 1) {
@Override
public boolean process(String opt, List<String> args) {
stylesheetfile = args.get(0);
return true;
}
},
new Option(resources, "-top", 1) {
@Override
public boolean process(String opt, List<String> args) {
top = args.get(0);
return true;
}
},
new Option(resources, "-use") {
@Override
public boolean process(String opt, List<String> args) {
classuse = true;
return true;
}
},
new Option(resources, "-windowtitle", 1) {
@Override
public boolean process(String opt, List<String> args) {
windowtitle = args.get(0).replaceAll("\\<.*?>", "");
return true;
}
},
new XOption(resources, "-Xdoclint") {
@Override
public boolean process(String opt, List<String> args) {
doclintOpts.put(this, DocLint.XMSGS_OPTION);
return true;
}
},
new XOption(resources, "-Xdocrootparent", 1) {
@Override
public boolean process(String opt, List<String> args) {
docrootparent = args.get(0);
try {
URL ignored = new URL(docrootparent);
} catch (MalformedURLException e) {
reporter.print(ERROR, resources.getText("doclet.MalformedURL", docrootparent));
return false;
}
return true;
}
},
new XOption(resources, "doclet.usage.xdoclint-extended", "-Xdoclint:", 0) {
@Override
public boolean process(String opt, List<String> args) {
String dopt = opt.replace("-Xdoclint:", DocLint.XMSGS_CUSTOM_PREFIX);
doclintOpts.put(this, dopt);
if (dopt.contains("/")) {