1 /* 2 * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package com.sun.tools.javadoc.api; 27 28 import java.io.InputStream; 29 import java.io.OutputStream; 30 import java.io.OutputStreamWriter; 31 import java.io.PrintWriter; 32 import java.io.Writer; 33 import java.nio.charset.Charset; 34 import java.util.Collections; 35 import java.util.EnumSet; 36 import java.util.Locale; 37 import java.util.Set; 38 39 import javax.lang.model.SourceVersion; 40 import javax.tools.DiagnosticListener; 41 import javax.tools.DocumentationTool; 42 import javax.tools.JavaFileManager; 43 import javax.tools.JavaFileObject; 44 import javax.tools.StandardJavaFileManager; 45 46 import com.sun.tools.javac.api.ClientCodeWrapper; 47 import com.sun.tools.javac.file.JavacFileManager; 48 import com.sun.tools.javac.util.ClientCodeException; 49 import com.sun.tools.javac.util.Context; 50 import com.sun.tools.javac.util.Log; 51 import com.sun.tools.javadoc.ToolOption; 52 53 /** 54 * Provides access to functionality specific to the JDK documentation tool, 55 * javadoc. 56 * 57 * <p><b>This is NOT part of any supported API. 58 * If you write code that depends on this, you do so at your own 59 * risk. This code and its internal interfaces are subject to change 60 * or deletion without notice.</b></p> 61 */ 62 public class JavadocTool implements DocumentationTool { 63 @Override getTask( Writer out, JavaFileManager fileManager, DiagnosticListener<? super JavaFileObject> diagnosticListener, Class<?> docletClass, Iterable<String> options, Iterable<? extends JavaFileObject> compilationUnits)64 public DocumentationTask getTask( 65 Writer out, 66 JavaFileManager fileManager, 67 DiagnosticListener<? super JavaFileObject> diagnosticListener, 68 Class<?> docletClass, 69 Iterable<String> options, 70 Iterable<? extends JavaFileObject> compilationUnits) { 71 Context context = new Context(); 72 return getTask(out, fileManager, diagnosticListener, 73 docletClass, options, compilationUnits, context); 74 } 75 getTask( Writer out, JavaFileManager fileManager, DiagnosticListener<? super JavaFileObject> diagnosticListener, Class<?> docletClass, Iterable<String> options, Iterable<? extends JavaFileObject> compilationUnits, Context context)76 public DocumentationTask getTask( 77 Writer out, 78 JavaFileManager fileManager, 79 DiagnosticListener<? super JavaFileObject> diagnosticListener, 80 Class<?> docletClass, 81 Iterable<String> options, 82 Iterable<? extends JavaFileObject> compilationUnits, 83 Context context) { 84 try { 85 ClientCodeWrapper ccw = ClientCodeWrapper.instance(context); 86 87 if (options != null) { 88 for (String option : options) 89 option.getClass(); // null check 90 } 91 92 if (compilationUnits != null) { 93 compilationUnits = ccw.wrapJavaFileObjects(compilationUnits); // implicit null check 94 for (JavaFileObject cu : compilationUnits) { 95 if (cu.getKind() != JavaFileObject.Kind.SOURCE) { 96 final String kindMsg = "All compilation units must be of SOURCE kind"; 97 throw new IllegalArgumentException(kindMsg); 98 } 99 } 100 } 101 102 if (diagnosticListener != null) 103 context.put(DiagnosticListener.class, ccw.wrap(diagnosticListener)); 104 105 if (out == null) 106 context.put(Log.outKey, new PrintWriter(System.err, true)); 107 else if (out instanceof PrintWriter) 108 context.put(Log.outKey, ((PrintWriter) out)); 109 else 110 context.put(Log.outKey, new PrintWriter(out, true)); 111 112 if (fileManager == null) 113 fileManager = getStandardFileManager(diagnosticListener, null, null); 114 fileManager = ccw.wrap(fileManager); 115 context.put(JavaFileManager.class, fileManager); 116 117 return new JavadocTaskImpl(context, docletClass, options, compilationUnits); 118 } catch (ClientCodeException ex) { 119 throw new RuntimeException(ex.getCause()); 120 } 121 } 122 123 // TODO: used shared static method in JavacFileManager 124 @Override getStandardFileManager( DiagnosticListener<? super JavaFileObject> diagnosticListener, Locale locale, Charset charset)125 public StandardJavaFileManager getStandardFileManager( 126 DiagnosticListener<? super JavaFileObject> diagnosticListener, 127 Locale locale, 128 Charset charset) { 129 Context context = new Context(); 130 context.put(Locale.class, locale); 131 if (diagnosticListener != null) 132 context.put(DiagnosticListener.class, diagnosticListener); 133 PrintWriter pw = (charset == null) 134 ? new PrintWriter(System.err, true) 135 : new PrintWriter(new OutputStreamWriter(System.err, charset), true); 136 context.put(Log.outKey, pw); 137 return new JavacFileManager(context, true, charset); 138 } 139 140 @Override run(InputStream in, OutputStream out, OutputStream err, String... arguments)141 public int run(InputStream in, OutputStream out, OutputStream err, String... arguments) { 142 PrintWriter err_pw = new PrintWriter(err == null ? System.err : err, true); 143 PrintWriter out_pw = new PrintWriter(out == null ? System.out : out); 144 try { 145 String standardDocletName = "com.sun.tools.doclets.standard.Standard"; 146 ClassLoader cl = getClass().getClassLoader(); 147 return com.sun.tools.javadoc.Main.execute( 148 "javadoc", err_pw, err_pw, out_pw, standardDocletName, cl, arguments); 149 } finally { 150 err_pw.flush(); 151 out_pw.flush(); 152 } 153 } 154 155 @Override getSourceVersions()156 public Set<SourceVersion> getSourceVersions() { 157 return Collections.unmodifiableSet( 158 EnumSet.range(SourceVersion.RELEASE_3, SourceVersion.latest())); 159 } 160 161 @Override isSupportedOption(String option)162 public int isSupportedOption(String option) { 163 if (option == null) 164 throw new NullPointerException(); 165 for (ToolOption o: ToolOption.values()) { 166 if (o.opt.equals(option)) 167 return o.hasArg ? 1 : 0; 168 } 169 return -1; 170 } 171 172 } 173