src/share/classes/com/sun/tools/javadoc/api/JavadocTool.java

changeset 0
959103a6100f
child 2525
2eb010b6cb22
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/classes/com/sun/tools/javadoc/api/JavadocTool.java	Wed Apr 27 01:34:52 2016 +0800
     1.3 @@ -0,0 +1,172 @@
     1.4 +/*
     1.5 + * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +package com.sun.tools.javadoc.api;
    1.30 +
    1.31 +import java.io.InputStream;
    1.32 +import java.io.OutputStream;
    1.33 +import java.io.OutputStreamWriter;
    1.34 +import java.io.PrintWriter;
    1.35 +import java.io.Writer;
    1.36 +import java.nio.charset.Charset;
    1.37 +import java.util.Collections;
    1.38 +import java.util.EnumSet;
    1.39 +import java.util.Locale;
    1.40 +import java.util.Set;
    1.41 +
    1.42 +import javax.lang.model.SourceVersion;
    1.43 +import javax.tools.DiagnosticListener;
    1.44 +import javax.tools.DocumentationTool;
    1.45 +import javax.tools.JavaFileManager;
    1.46 +import javax.tools.JavaFileObject;
    1.47 +import javax.tools.StandardJavaFileManager;
    1.48 +
    1.49 +import com.sun.tools.javac.api.ClientCodeWrapper;
    1.50 +import com.sun.tools.javac.file.JavacFileManager;
    1.51 +import com.sun.tools.javac.util.ClientCodeException;
    1.52 +import com.sun.tools.javac.util.Context;
    1.53 +import com.sun.tools.javac.util.Log;
    1.54 +import com.sun.tools.javadoc.ToolOption;
    1.55 +
    1.56 +/**
    1.57 + * Provides access to functionality specific to the JDK documentation tool,
    1.58 + * javadoc.
    1.59 + *
    1.60 + * <p><b>This is NOT part of any supported API.
    1.61 + * If you write code that depends on this, you do so at your own
    1.62 + * risk.  This code and its internal interfaces are subject to change
    1.63 + * or deletion without notice.</b></p>
    1.64 + */
    1.65 +public class JavadocTool implements DocumentationTool {
    1.66 +    @Override
    1.67 +    public DocumentationTask getTask(
    1.68 +            Writer out,
    1.69 +            JavaFileManager fileManager,
    1.70 +            DiagnosticListener<? super JavaFileObject> diagnosticListener,
    1.71 +            Class<?> docletClass,
    1.72 +            Iterable<String> options,
    1.73 +            Iterable<? extends JavaFileObject> compilationUnits) {
    1.74 +        Context context = new Context();
    1.75 +        return getTask(out, fileManager, diagnosticListener,
    1.76 +                docletClass, options, compilationUnits, context);
    1.77 +    }
    1.78 +
    1.79 +    public DocumentationTask getTask(
    1.80 +            Writer out,
    1.81 +            JavaFileManager fileManager,
    1.82 +            DiagnosticListener<? super JavaFileObject> diagnosticListener,
    1.83 +            Class<?> docletClass,
    1.84 +            Iterable<String> options,
    1.85 +            Iterable<? extends JavaFileObject> compilationUnits,
    1.86 +            Context context) {
    1.87 +        try {
    1.88 +            ClientCodeWrapper ccw = ClientCodeWrapper.instance(context);
    1.89 +
    1.90 +            if (options != null) {
    1.91 +                for (String option : options)
    1.92 +                    option.getClass(); // null check
    1.93 +            }
    1.94 +
    1.95 +            if (compilationUnits != null) {
    1.96 +                compilationUnits = ccw.wrapJavaFileObjects(compilationUnits); // implicit null check
    1.97 +                for (JavaFileObject cu : compilationUnits) {
    1.98 +                    if (cu.getKind() != JavaFileObject.Kind.SOURCE) {
    1.99 +                        final String kindMsg = "All compilation units must be of SOURCE kind";
   1.100 +                        throw new IllegalArgumentException(kindMsg);
   1.101 +                    }
   1.102 +                }
   1.103 +            }
   1.104 +
   1.105 +            if (diagnosticListener != null)
   1.106 +                context.put(DiagnosticListener.class, ccw.wrap(diagnosticListener));
   1.107 +
   1.108 +            if (out == null)
   1.109 +                context.put(Log.outKey, new PrintWriter(System.err, true));
   1.110 +            else if (out instanceof PrintWriter)
   1.111 +                context.put(Log.outKey, ((PrintWriter) out));
   1.112 +            else
   1.113 +                context.put(Log.outKey, new PrintWriter(out, true));
   1.114 +
   1.115 +            if (fileManager == null)
   1.116 +                fileManager = getStandardFileManager(diagnosticListener, null, null);
   1.117 +            fileManager = ccw.wrap(fileManager);
   1.118 +            context.put(JavaFileManager.class, fileManager);
   1.119 +
   1.120 +            return new JavadocTaskImpl(context, docletClass, options, compilationUnits);
   1.121 +        } catch (ClientCodeException ex) {
   1.122 +            throw new RuntimeException(ex.getCause());
   1.123 +        }
   1.124 +    }
   1.125 +
   1.126 +    // TODO: used shared static method in JavacFileManager
   1.127 +    @Override
   1.128 +    public StandardJavaFileManager getStandardFileManager(
   1.129 +            DiagnosticListener<? super JavaFileObject> diagnosticListener,
   1.130 +            Locale locale,
   1.131 +            Charset charset) {
   1.132 +        Context context = new Context();
   1.133 +        context.put(Locale.class, locale);
   1.134 +        if (diagnosticListener != null)
   1.135 +            context.put(DiagnosticListener.class, diagnosticListener);
   1.136 +        PrintWriter pw = (charset == null)
   1.137 +                ? new PrintWriter(System.err, true)
   1.138 +                : new PrintWriter(new OutputStreamWriter(System.err, charset), true);
   1.139 +        context.put(Log.outKey, pw);
   1.140 +        return new JavacFileManager(context, true, charset);
   1.141 +    }
   1.142 +
   1.143 +    @Override
   1.144 +    public int run(InputStream in, OutputStream out, OutputStream err, String... arguments) {
   1.145 +        PrintWriter err_pw = new PrintWriter(err == null ? System.err : err, true);
   1.146 +        PrintWriter out_pw = new PrintWriter(out == null ? System.out : out);
   1.147 +        try {
   1.148 +            String standardDocletName = "com.sun.tools.doclets.standard.Standard";
   1.149 +            ClassLoader cl = getClass().getClassLoader();
   1.150 +            return com.sun.tools.javadoc.Main.execute(
   1.151 +                    "javadoc", err_pw, err_pw, out_pw, standardDocletName, cl, arguments);
   1.152 +        } finally {
   1.153 +            err_pw.flush();
   1.154 +            out_pw.flush();
   1.155 +        }
   1.156 +    }
   1.157 +
   1.158 +    @Override
   1.159 +    public Set<SourceVersion> getSourceVersions() {
   1.160 +        return Collections.unmodifiableSet(
   1.161 +                EnumSet.range(SourceVersion.RELEASE_3, SourceVersion.latest()));
   1.162 +    }
   1.163 +
   1.164 +    @Override
   1.165 +    public int isSupportedOption(String option) {
   1.166 +        if (option == null)
   1.167 +            throw new NullPointerException();
   1.168 +        for (ToolOption o: ToolOption.values()) {
   1.169 +            if (o.opt.equals(option))
   1.170 +                return o.hasArg ? 1 : 0;
   1.171 +        }
   1.172 +        return -1;
   1.173 +    }
   1.174 +
   1.175 +}

mercurial