jjg@1413: /*
jjg@1543: * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
jjg@1413: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jjg@1413: *
jjg@1413: * This code is free software; you can redistribute it and/or modify it
jjg@1413: * under the terms of the GNU General Public License version 2 only, as
jjg@1413: * published by the Free Software Foundation. Oracle designates this
jjg@1413: * particular file as subject to the "Classpath" exception as provided
jjg@1413: * by Oracle in the LICENSE file that accompanied this code.
jjg@1413: *
jjg@1413: * This code is distributed in the hope that it will be useful, but WITHOUT
jjg@1413: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jjg@1413: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
jjg@1413: * version 2 for more details (a copy is included in the LICENSE file that
jjg@1413: * accompanied this code).
jjg@1413: *
jjg@1413: * You should have received a copy of the GNU General Public License version
jjg@1413: * 2 along with this work; if not, write to the Free Software Foundation,
jjg@1413: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jjg@1413: *
jjg@1413: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
jjg@1413: * or visit www.oracle.com if you need additional information or have any
jjg@1413: * questions.
jjg@1413: */
jjg@1413:
jjg@1413: package com.sun.tools.javadoc.api;
jjg@1413:
jjg@1413: import java.io.InputStream;
jjg@1413: import java.io.OutputStream;
jjg@1413: import java.io.OutputStreamWriter;
jjg@1413: import java.io.PrintWriter;
jjg@1413: import java.io.Writer;
jjg@1413: import java.nio.charset.Charset;
jjg@1413: import java.util.Collections;
jjg@1413: import java.util.EnumSet;
jjg@1413: import java.util.Locale;
jjg@1413: import java.util.Set;
jjg@1413:
jjg@1413: import javax.lang.model.SourceVersion;
jjg@1413: import javax.tools.DiagnosticListener;
jjg@1413: import javax.tools.DocumentationTool;
jjg@1413: import javax.tools.JavaFileManager;
jjg@1413: import javax.tools.JavaFileObject;
jjg@1413: import javax.tools.StandardJavaFileManager;
jjg@1413:
jjg@1413: import com.sun.tools.javac.api.ClientCodeWrapper;
jjg@1413: import com.sun.tools.javac.file.JavacFileManager;
jjg@1413: import com.sun.tools.javac.util.ClientCodeException;
jjg@1413: import com.sun.tools.javac.util.Context;
jjg@1413: import com.sun.tools.javac.util.Log;
jjg@1413: import com.sun.tools.javadoc.ToolOption;
jjg@1413:
jjg@1413: /**
jjg@1413: * Provides access to functionality specific to the JDK documentation tool,
jjg@1413: * javadoc.
jjg@1413: *
jjg@1413: *
This is NOT part of any supported API.
jjg@1413: * If you write code that depends on this, you do so at your own
jjg@1413: * risk. This code and its internal interfaces are subject to change
jjg@1413: * or deletion without notice.
jjg@1413: */
jjg@1413: public class JavadocTool implements DocumentationTool {
jjg@1413: @Override
jjg@1413: public DocumentationTask getTask(
jjg@1413: Writer out,
jjg@1413: JavaFileManager fileManager,
jjg@1413: DiagnosticListener super JavaFileObject> diagnosticListener,
jjg@1413: Class> docletClass,
jjg@1413: Iterable options,
jjg@1413: Iterable extends JavaFileObject> compilationUnits) {
jjg@1413: Context context = new Context();
jjg@1413: return getTask(out, fileManager, diagnosticListener,
jjg@1413: docletClass, options, compilationUnits, context);
jjg@1413: }
jjg@1413:
jjg@1413: public DocumentationTask getTask(
jjg@1413: Writer out,
jjg@1413: JavaFileManager fileManager,
jjg@1413: DiagnosticListener super JavaFileObject> diagnosticListener,
jjg@1413: Class> docletClass,
jjg@1413: Iterable options,
jjg@1413: Iterable extends JavaFileObject> compilationUnits,
jjg@1413: Context context) {
jjg@1413: try {
jjg@1413: ClientCodeWrapper ccw = ClientCodeWrapper.instance(context);
jjg@1413:
jjg@1413: if (options != null) {
jjg@1413: for (String option : options)
jjg@1413: option.getClass(); // null check
jjg@1413: }
jjg@1413:
jjg@1413: if (compilationUnits != null) {
jjg@1413: compilationUnits = ccw.wrapJavaFileObjects(compilationUnits); // implicit null check
jjg@1413: for (JavaFileObject cu : compilationUnits) {
jjg@1413: if (cu.getKind() != JavaFileObject.Kind.SOURCE) {
jjg@1413: final String kindMsg = "All compilation units must be of SOURCE kind";
jjg@1413: throw new IllegalArgumentException(kindMsg);
jjg@1413: }
jjg@1413: }
jjg@1413: }
jjg@1413:
jjg@1413: if (diagnosticListener != null)
jjg@1413: context.put(DiagnosticListener.class, ccw.wrap(diagnosticListener));
jjg@1413:
jjg@1413: if (out == null)
jjg@1413: context.put(Log.outKey, new PrintWriter(System.err, true));
jjg@1413: else if (out instanceof PrintWriter)
jjg@1413: context.put(Log.outKey, ((PrintWriter) out));
jjg@1413: else
jjg@1413: context.put(Log.outKey, new PrintWriter(out, true));
jjg@1413:
jjg@1413: if (fileManager == null)
jjg@1413: fileManager = getStandardFileManager(diagnosticListener, null, null);
jjg@1413: fileManager = ccw.wrap(fileManager);
jjg@1413: context.put(JavaFileManager.class, fileManager);
jjg@1413:
jjg@1413: return new JavadocTaskImpl(context, docletClass, options, compilationUnits);
jjg@1413: } catch (ClientCodeException ex) {
jjg@1413: throw new RuntimeException(ex.getCause());
jjg@1413: }
jjg@1413: }
jjg@1413:
jjg@1413: // TODO: used shared static method in JavacFileManager
jjg@1413: @Override
jjg@1413: public StandardJavaFileManager getStandardFileManager(
jjg@1413: DiagnosticListener super JavaFileObject> diagnosticListener,
jjg@1413: Locale locale,
jjg@1413: Charset charset) {
jjg@1413: Context context = new Context();
jjg@1413: context.put(Locale.class, locale);
jjg@1413: if (diagnosticListener != null)
jjg@1413: context.put(DiagnosticListener.class, diagnosticListener);
jjg@1413: PrintWriter pw = (charset == null)
jjg@1413: ? new PrintWriter(System.err, true)
jjg@1413: : new PrintWriter(new OutputStreamWriter(System.err, charset), true);
jjg@1413: context.put(Log.outKey, pw);
jjg@1413: return new JavacFileManager(context, true, charset);
jjg@1413: }
jjg@1413:
jjg@1413: @Override
jjg@1413: public int run(InputStream in, OutputStream out, OutputStream err, String... arguments) {
jjg@1543: PrintWriter err_pw = new PrintWriter(err == null ? System.err : err, true);
jjg@1543: PrintWriter out_pw = new PrintWriter(out == null ? System.out : out);
jjg@1413: try {
jjg@1413: String standardDocletName = "com.sun.tools.doclets.standard.Standard";
jjg@1544: ClassLoader cl = getClass().getClassLoader();
jjg@1413: return com.sun.tools.javadoc.Main.execute(
jjg@1544: "javadoc", err_pw, err_pw, out_pw, standardDocletName, cl, arguments);
jjg@1413: } finally {
jjg@1413: err_pw.flush();
jjg@1413: out_pw.flush();
jjg@1413: }
jjg@1413: }
jjg@1413:
jjg@1413: @Override
jjg@1413: public Set getSourceVersions() {
jjg@1413: return Collections.unmodifiableSet(
jjg@1413: EnumSet.range(SourceVersion.RELEASE_3, SourceVersion.latest()));
jjg@1413: }
jjg@1413:
jjg@1413: @Override
jjg@1413: public int isSupportedOption(String option) {
jjg@1413: if (option == null)
jjg@1413: throw new NullPointerException();
jjg@1413: for (ToolOption o: ToolOption.values()) {
jjg@1413: if (o.opt.equals(option))
jjg@1413: return o.hasArg ? 1 : 0;
jjg@1413: }
jjg@1413: return -1;
jjg@1413: }
jjg@1413:
jjg@1413: }