package com.sun.tools.javadoc.main;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Objects;
import javax.tools.JavaFileManager;
import javax.tools.JavaFileObject;
import javax.tools.StandardJavaFileManager;
import javax.tools.StandardLocation;
import com.sun.javadoc.*;
import com.sun.tools.javac.file.JavacFileManager;
import com.sun.tools.javac.main.CommandLine;
import com.sun.tools.javac.main.Option;
import com.sun.tools.javac.file.BaseFileManager;
import com.sun.tools.javac.main.Arguments;
import com.sun.tools.javac.main.OptionHelper;
import com.sun.tools.javac.main.OptionHelper.GrumpyHelper;
import com.sun.tools.javac.platform.PlatformDescription;
import com.sun.tools.javac.platform.PlatformUtils;
import com.sun.tools.javac.util.ClientCodeException;
import com.sun.tools.javac.util.Context;
import com.sun.tools.javac.util.List;
import com.sun.tools.javac.util.ListBuffer;
import com.sun.tools.javac.util.Log;
import com.sun.tools.javac.util.Options;
import static com.sun.tools.javac.code.Flags.*;
@Deprecated
public class Start extends ToolOption.Helper {
private final Context context;
private final String defaultDocletClassName;
private final ClassLoader docletParentClassLoader;
private static final String javadocName = "javadoc";
private static final String standardDocletClassName =
"com.sun.tools.doclets.standard.Standard";
private final long defaultFilter = PUBLIC | PROTECTED;
private final Messager messager;
private DocletInvoker docletInvoker;
private boolean apiMode;
private JavaFileManager fileManager;
public Start(String programName,
PrintWriter errWriter,
PrintWriter warnWriter,
PrintWriter noticeWriter,
String defaultDocletClassName) {
this(programName, errWriter, warnWriter, noticeWriter, defaultDocletClassName, null);
}
public Start(PrintWriter pw) {
this(javadocName, pw, pw, pw, standardDocletClassName);
}
public Start(String programName,
PrintWriter errWriter,
PrintWriter warnWriter,
PrintWriter noticeWriter,
String defaultDocletClassName,
ClassLoader docletParentClassLoader) {
context = new Context();
messager = new Messager(context, programName, errWriter, warnWriter, noticeWriter);
this.defaultDocletClassName = defaultDocletClassName;
this.docletParentClassLoader = docletParentClassLoader;
}
public Start(String programName, String defaultDocletClassName) {
this(programName, defaultDocletClassName, null);
}
public Start(String programName, String defaultDocletClassName,
ClassLoader docletParentClassLoader) {
context = new Context();
messager = new Messager(context, programName);
this.defaultDocletClassName = defaultDocletClassName;
this.docletParentClassLoader = docletParentClassLoader;
}
public Start(String programName, ClassLoader docletParentClassLoader) {
this(programName, standardDocletClassName, docletParentClassLoader);
}
public Start(String programName) {
this(programName, standardDocletClassName);
}
public Start(ClassLoader docletParentClassLoader) {
this(javadocName, docletParentClassLoader);
}
public Start() {
this(javadocName);
}
public Start(Context context) {
this.context = Objects.requireNonNull(context);
apiMode = true;
defaultDocletClassName = standardDocletClassName;
docletParentClassLoader = null;
Log log = context.get(Log.logKey);
if (log instanceof Messager)
messager = (Messager) log;
else {
PrintWriter out = context.get(Log.errKey);
messager = (out == null) ? new Messager(context, javadocName)
: new Messager(context, javadocName, out, out, out);
}
}
@Override
void usage() {
usage(true);
}
void usage(boolean exit) {
usage("main.usage", "-help", "main.usage.foot", exit);
}
@Override
void Xusage() {
Xusage(true);
}
void Xusage(boolean exit) {
usage("main.Xusage", "-X", "main.Xusage.foot", exit);
}
private void usage(String main, String doclet, String foot, boolean exit) {
messager.notice(main);
if (docletInvoker != null) {
docletInvoker.optionLength(doclet);
}
if (foot != null)
messager.notice(foot);
if (exit) exit();
}
private void exit() {
messager.exit();
}
public int begin(String... argv) {
boolean ok = begin(null, argv, Collections.emptySet());
return ok ? 0 : 1;
}
public boolean begin(Class<?> docletClass, Iterable<String> options, Iterable<? extends JavaFileObject> fileObjects) {
Collection<String> opts = new ArrayList<>();
for (String opt: options) opts.add(opt);
return begin(docletClass, opts.toArray(new String[opts.size()]), fileObjects);
}
private boolean begin(Class<?> docletClass, String[] options, Iterable<? extends JavaFileObject> fileObjects) {
boolean failed = false;
try {
failed = !parseAndExecute(docletClass, options, fileObjects);
} catch (Messager.ExitJavadoc exc) {
} catch (OutOfMemoryError ee) {
messager.error(Messager.NOPOS, "main.out.of.memory");
failed = true;
} catch (ClientCodeException e) {
throw e;
} catch (Error ee) {
ee.printStackTrace(System.err);
messager.error(Messager.NOPOS, "main.fatal.error");
failed = true;
} catch (Exception ee) {
ee.printStackTrace(System.err);
messager.error(Messager.NOPOS, "main.fatal.exception");
failed = true;
} finally {
if (fileManager != null
&& fileManager instanceof BaseFileManager
&& ((BaseFileManager) fileManager).autoClose) {
try {
fileManager.close();
} catch (IOException ignore) {
}
}
messager.exitNotice();
messager.flush();
}
failed |= messager.nerrors() > 0;
failed |= rejectWarnings && messager.nwarnings() > 0;
return !failed;
}
private boolean parseAndExecute(
Class<?> docletClass,
String[] argv,
Iterable<? extends JavaFileObject> fileObjects) throws IOException {
long tm = System.currentTimeMillis();
ListBuffer<String> javaNames = new ListBuffer<>();
try {
argv = CommandLine.parse(argv);
} catch (FileNotFoundException e) {
messager.error(Messager.NOPOS, "main.cant.read", e.getMessage());
exit();
} catch (IOException e) {
e.printStackTrace(System.err);
exit();
}
fileManager = context.get(JavaFileManager.class);
setDocletInvoker(docletClass, fileManager, argv);
compOpts = Options.instance(context);
compOpts.put("-Xlint:-options", "-Xlint:-options");
for (int i = 0 ; i < argv.length ; i++) {
String arg = argv[i];
ToolOption o = ToolOption.get(arg);
if (o != null) {
if (o == ToolOption.LOCALE && i > 0)
usageError("main.locale_first");
try {
if (o.hasArg) {
oneArg(argv, i++);
o.process(this, argv[i]);
} else {
setOption(arg);
o.process(this);
}
} catch (Option.InvalidValueException e) {
usageError("main.option.invalid.value", e.getMessage());
}
} else if (arg.equals("-XDaccessInternalAPI")) {
if (docletInvoker.optionLength("-XDaccessInternalAPI") == 1) {
setOption(arg);
}
} else if (arg.startsWith("-XD")) {
String s = arg.substring("-XD".length());
int eq = s.indexOf('=');
String key = (eq < 0) ? s : s.substring(0, eq);
String value = (eq < 0) ? s : s.substring(eq+1);
compOpts.put(key, value);
}
else if (arg.startsWith("-")) {
int optionLength;
optionLength = docletInvoker.optionLength(arg);
if (optionLength < 0) {
exit();
} else if (optionLength == 0) {
usageError("main.invalid_flag", arg);
} else {
if ((i + optionLength) > argv.length) {
usageError("main.requires_argument", arg);
}
ListBuffer<String> args = new ListBuffer<>();
for (int j = 0; j < optionLength-1; ++j) {
args.append(argv[++i]);
}
setOption(arg, args.toList());
}
} else {
javaNames.append(arg);
}
}
if (fileManager == null) {
JavacFileManager.preRegister(context);
fileManager = context.get(JavaFileManager.class);
if (fileManager instanceof BaseFileManager) {
((BaseFileManager) fileManager).autoClose = true;
}
}
if (fileManager instanceof BaseFileManager) {
((BaseFileManager) fileManager).handleOptions(fileManagerOpts);
}
Arguments arguments = Arguments.instance(context);
arguments.init(messager.programName);
arguments.allowEmpty();
arguments.validate();
String platformString = compOpts.get("--release");
if (platformString != null) {
if (compOpts.isSet("-source")) {
usageError("main.release.bootclasspath.conflict", "-source");
}
if (fileManagerOpts.containsKey(Option.BOOT_CLASS_PATH)) {
usageError("main.release.bootclasspath.conflict", Option.BOOT_CLASS_PATH.getPrimaryName());
}
PlatformDescription platformDescription =
PlatformUtils.lookupPlatformDescription(platformString);
if (platformDescription == null) {
usageError("main.unsupported.release.version", platformString);
}
compOpts.put(Option.SOURCE, platformDescription.getSourceVersion());
context.put(PlatformDescription.class, platformDescription);
Collection<Path> platformCP = platformDescription.getPlatformPath();
if (platformCP != null) {
if (fileManager instanceof StandardJavaFileManager) {
StandardJavaFileManager sfm = (StandardJavaFileManager) fileManager;
sfm.setLocationFromPaths(StandardLocation.PLATFORM_CLASS_PATH, platformCP);
} else {
usageError("main.release.not.standard.file.manager", platformString);
}
}
}
compOpts.notifyListeners();
if (javaNames.isEmpty() && subPackages.isEmpty() && isEmpty(fileObjects)) {
usageError("main.No_packages_or_classes_specified");
}
if (!docletInvoker.validOptions(options.toList())) {
exit();
}
JavadocTool comp = JavadocTool.make0(context);
if (comp == null) return false;
if (showAccess == null) {
setFilter(defaultFilter);
}
LanguageVersion languageVersion = docletInvoker.languageVersion();
RootDocImpl root = comp.getRootDocImpl(
docLocale,
encoding,
showAccess,
javaNames.toList(),
options.toList(),
fileObjects,
breakiterator,
subPackages.toList(),
excludedPackages.toList(),
docClasses,
languageVersion == null || languageVersion == LanguageVersion.JAVA_1_1,
quiet);
comp = null;
boolean ok = root != null;
if (ok) ok = docletInvoker.start(root);
if (compOpts.get("-verbose") != null) {
tm = System.currentTimeMillis() - tm;
messager.notice("main.done_in", Long.toString(tm));
}
return ok;
}
private <T> boolean isEmpty(Iterable<T> iter) {
return !iter.iterator().hasNext();
}
private void setDocletInvoker(Class<?> docletClass, JavaFileManager fileManager, String[] argv) {
boolean exportInternalAPI = false;
String docletClassName = null;
String docletPath = null;
for (int i = 0 ; i < argv.length ; i++) {
String arg = argv[i];
if (arg.equals(ToolOption.DOCLET.opt)) {
oneArg(argv, i++);
if (docletClassName != null) {
usageError("main.more_than_one_doclet_specified_0_and_1",
docletClassName, argv[i]);
}
docletClassName = argv[i];
} else if (arg.equals(ToolOption.DOCLETPATH.opt)) {
oneArg(argv, i++);
if (docletPath == null) {
docletPath = argv[i];
} else {
docletPath += File.pathSeparator + argv[i];
}
} else if (arg.equals("-XDaccessInternalAPI")) {
exportInternalAPI = true;
}
}
if (docletClass != null) {
docletInvoker = new DocletInvoker(messager, docletClass, apiMode, exportInternalAPI);
} else {
if (docletClassName == null) {
docletClassName = defaultDocletClassName;
}
docletInvoker = new DocletInvoker(messager, fileManager,
docletClassName, docletPath,
docletParentClassLoader,
apiMode,
exportInternalAPI);
}
}
private void oneArg(String[] args, int index) {
if ((index + 1) < args.length) {
setOption(args[index], args[index+1]);
} else {
usageError("main.requires_argument", args[index]);
}
}
@Override
void usageError(String key, Object... args) {
messager.error(Messager.NOPOS, key, args);
usage(true);
}
private void setOption(String opt) {
String[] option = { opt };
options.append(option);
}
private void setOption(String opt, String argument) {
String[] option = { opt, argument };
options.append(option);
}
private void setOption(String opt, List<String> arguments) {
String[] args = new String[arguments.length() + 1];
int k = 0;
args[k++] = opt;
for (List<String> i = arguments; i.nonEmpty(); i=i.tail) {
args[k++] = i.head;
}
options.append(args);
}
@Override
OptionHelper getOptionHelper() {
return new GrumpyHelper(messager) {
@Override
public String get(com.sun.tools.javac.main.Option option) {
return compOpts.get(option);
}
@Override
public void put(String name, String value) {
compOpts.put(name, value);
}
};
}
}