Thu, 15 Nov 2012 23:07:24 -0800
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
Reviewed-by: darcy
jjg@1412 | 1 | /* |
jjg@1412 | 2 | * Copyright (c) 1998, 2012, Oracle and/or its affiliates. All rights reserved. |
jjg@1412 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
jjg@1412 | 4 | * |
jjg@1412 | 5 | * This code is free software; you can redistribute it and/or modify it |
jjg@1412 | 6 | * under the terms of the GNU General Public License version 2 only, as |
jjg@1412 | 7 | * published by the Free Software Foundation. Oracle designates this |
jjg@1412 | 8 | * particular file as subject to the "Classpath" exception as provided |
jjg@1412 | 9 | * by Oracle in the LICENSE file that accompanied this code. |
jjg@1412 | 10 | * |
jjg@1412 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
jjg@1412 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
jjg@1412 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
jjg@1412 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
jjg@1412 | 15 | * accompanied this code). |
jjg@1412 | 16 | * |
jjg@1412 | 17 | * You should have received a copy of the GNU General Public License version |
jjg@1412 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
jjg@1412 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
jjg@1412 | 20 | * |
jjg@1412 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
jjg@1412 | 22 | * or visit www.oracle.com if you need additional information or have any |
jjg@1412 | 23 | * questions. |
jjg@1412 | 24 | */ |
jjg@1412 | 25 | |
jjg@1412 | 26 | package com.sun.tools.doclets.internal.toolkit.util; |
jjg@1412 | 27 | |
jjg@1412 | 28 | import java.io.BufferedInputStream; |
jjg@1412 | 29 | import java.io.BufferedOutputStream; |
jjg@1412 | 30 | import java.io.BufferedWriter; |
jjg@1412 | 31 | import java.io.File; |
jjg@1412 | 32 | import java.io.FileInputStream; |
jjg@1412 | 33 | import java.io.FileNotFoundException; |
jjg@1412 | 34 | import java.io.FileOutputStream; |
jjg@1412 | 35 | import java.io.IOException; |
jjg@1412 | 36 | import java.io.InputStream; |
jjg@1412 | 37 | import java.io.OutputStream; |
jjg@1412 | 38 | import java.io.OutputStreamWriter; |
jjg@1412 | 39 | import java.io.UnsupportedEncodingException; |
jjg@1412 | 40 | import java.io.Writer; |
jjg@1412 | 41 | import java.util.ArrayList; |
jjg@1412 | 42 | import java.util.LinkedHashSet; |
jjg@1412 | 43 | import java.util.List; |
jjg@1412 | 44 | import java.util.Set; |
jjg@1412 | 45 | |
jjg@1413 | 46 | import javax.tools.DocumentationTool; |
jjg@1412 | 47 | import javax.tools.JavaFileManager.Location; |
jjg@1412 | 48 | import javax.tools.StandardLocation; |
jjg@1412 | 49 | |
jjg@1412 | 50 | import com.sun.tools.doclets.internal.toolkit.Configuration; |
jjg@1412 | 51 | |
jjg@1412 | 52 | /** |
jjg@1412 | 53 | * Implementation of DocFileFactory that just uses java.io.File API, |
jjg@1412 | 54 | * and does not use a JavaFileManager.. |
jjg@1412 | 55 | * |
jjg@1412 | 56 | * <p><b>This is NOT part of any supported API. |
jjg@1412 | 57 | * If you write code that depends on this, you do so at your own risk. |
jjg@1412 | 58 | * This code and its internal interfaces are subject to change or |
jjg@1412 | 59 | * deletion without notice.</b> |
jjg@1412 | 60 | * |
jjg@1412 | 61 | * @since 1.8 |
jjg@1412 | 62 | */ |
jjg@1412 | 63 | class SimpleDocFileFactory extends DocFileFactory { |
jjg@1412 | 64 | |
jjg@1412 | 65 | public SimpleDocFileFactory(Configuration configuration) { |
jjg@1412 | 66 | super(configuration); |
jjg@1412 | 67 | } |
jjg@1412 | 68 | |
jjg@1412 | 69 | public DocFile createFileForDirectory(String file) { |
jjg@1412 | 70 | return new SimpleDocFile(new File(file)); |
jjg@1412 | 71 | } |
jjg@1412 | 72 | |
jjg@1412 | 73 | public DocFile createFileForInput(String file) { |
jjg@1412 | 74 | return new SimpleDocFile(new File(file)); |
jjg@1412 | 75 | } |
jjg@1412 | 76 | |
jjg@1412 | 77 | public DocFile createFileForOutput(DocPath path) { |
jjg@1413 | 78 | return new SimpleDocFile(DocumentationTool.Location.DOCUMENTATION_OUTPUT, path); |
jjg@1412 | 79 | } |
jjg@1412 | 80 | |
jjg@1412 | 81 | @Override |
jjg@1412 | 82 | Iterable<DocFile> list(Location location, DocPath path) { |
jjg@1412 | 83 | if (location != StandardLocation.SOURCE_PATH) |
jjg@1412 | 84 | throw new IllegalArgumentException(); |
jjg@1412 | 85 | |
jjg@1412 | 86 | Set<DocFile> files = new LinkedHashSet<DocFile>(); |
jjg@1412 | 87 | for (String s : configuration.sourcepath.split(File.pathSeparator)) { |
jjg@1412 | 88 | if (s.isEmpty()) |
jjg@1412 | 89 | continue; |
jjg@1412 | 90 | File f = new File(s); |
jjg@1412 | 91 | if (f.isDirectory()) { |
jjg@1412 | 92 | f = new File(f, path.getPath()); |
jjg@1412 | 93 | if (f.exists()) |
jjg@1412 | 94 | files.add(new SimpleDocFile(f)); |
jjg@1412 | 95 | } |
jjg@1412 | 96 | } |
jjg@1412 | 97 | return files; |
jjg@1412 | 98 | } |
jjg@1412 | 99 | |
jjg@1412 | 100 | class SimpleDocFile extends DocFile { |
jjg@1412 | 101 | private File file; |
jjg@1412 | 102 | |
jjg@1412 | 103 | /** Create a DocFile for a given file. */ |
jjg@1412 | 104 | private SimpleDocFile(File file) { |
jjg@1412 | 105 | super(configuration); |
jjg@1412 | 106 | this.file = file; |
jjg@1412 | 107 | } |
jjg@1412 | 108 | |
jjg@1412 | 109 | /** Create a DocFile for a given location and relative path. */ |
jjg@1412 | 110 | private SimpleDocFile(Location location, DocPath path) { |
jjg@1412 | 111 | super(configuration, location, path); |
jjg@1412 | 112 | String destDirName = configuration.destDirName; |
jjg@1412 | 113 | this.file = destDirName.isEmpty() ? new File(path.getPath()) |
jjg@1412 | 114 | : new File(destDirName, path.getPath()); |
jjg@1412 | 115 | } |
jjg@1412 | 116 | |
jjg@1412 | 117 | /** Open an input stream for the file. */ |
jjg@1412 | 118 | public InputStream openInputStream() throws FileNotFoundException { |
jjg@1412 | 119 | return new BufferedInputStream(new FileInputStream(file)); |
jjg@1412 | 120 | } |
jjg@1412 | 121 | |
jjg@1412 | 122 | /** |
jjg@1412 | 123 | * Open an output stream for the file. |
jjg@1412 | 124 | * The file must have been created with a location of |
jjg@1413 | 125 | * {@link DocumentationTool.Location#DOCUMENTATION_OUTPUT} and a corresponding relative path. |
jjg@1412 | 126 | */ |
jjg@1412 | 127 | public OutputStream openOutputStream() throws IOException, UnsupportedEncodingException { |
jjg@1413 | 128 | if (location != DocumentationTool.Location.DOCUMENTATION_OUTPUT) |
jjg@1412 | 129 | throw new IllegalStateException(); |
jjg@1412 | 130 | |
jjg@1412 | 131 | createDirectoryForFile(file); |
jjg@1412 | 132 | return new BufferedOutputStream(new FileOutputStream(file)); |
jjg@1412 | 133 | } |
jjg@1412 | 134 | |
jjg@1412 | 135 | /** |
jjg@1412 | 136 | * Open an writer for the file, using the encoding (if any) given in the |
jjg@1412 | 137 | * doclet configuration. |
jjg@1412 | 138 | * The file must have been created with a location of |
jjg@1413 | 139 | * {@link DocumentationTool.Location#DOCUMENTATION_OUTPUT} and a corresponding relative path. |
jjg@1412 | 140 | */ |
jjg@1412 | 141 | public Writer openWriter() throws IOException, UnsupportedEncodingException { |
jjg@1413 | 142 | if (location != DocumentationTool.Location.DOCUMENTATION_OUTPUT) |
jjg@1412 | 143 | throw new IllegalStateException(); |
jjg@1412 | 144 | |
jjg@1412 | 145 | createDirectoryForFile(file); |
jjg@1412 | 146 | FileOutputStream fos = new FileOutputStream(file); |
jjg@1412 | 147 | if (configuration.docencoding == null) { |
jjg@1412 | 148 | return new BufferedWriter(new OutputStreamWriter(fos)); |
jjg@1412 | 149 | } else { |
jjg@1412 | 150 | return new BufferedWriter(new OutputStreamWriter(fos, configuration.docencoding)); |
jjg@1412 | 151 | } |
jjg@1412 | 152 | } |
jjg@1412 | 153 | |
jjg@1412 | 154 | /** Return true if the file can be read. */ |
jjg@1412 | 155 | public boolean canRead() { |
jjg@1412 | 156 | return file.canRead(); |
jjg@1412 | 157 | } |
jjg@1412 | 158 | |
jjg@1412 | 159 | /** Return true if the file can be written. */ |
jjg@1412 | 160 | public boolean canWrite() { |
jjg@1412 | 161 | return file.canRead(); |
jjg@1412 | 162 | } |
jjg@1412 | 163 | |
jjg@1412 | 164 | /** Return true if the file exists. */ |
jjg@1412 | 165 | public boolean exists() { |
jjg@1412 | 166 | return file.exists(); |
jjg@1412 | 167 | } |
jjg@1412 | 168 | |
jjg@1412 | 169 | /** Return the base name (last component) of the file name. */ |
jjg@1412 | 170 | public String getName() { |
jjg@1412 | 171 | return file.getName(); |
jjg@1412 | 172 | } |
jjg@1412 | 173 | |
jjg@1412 | 174 | /** Return the file system path for this file. */ |
jjg@1412 | 175 | public String getPath() { |
jjg@1412 | 176 | return file.getPath(); |
jjg@1412 | 177 | } |
jjg@1412 | 178 | |
jjg@1412 | 179 | /** Return true is file has an absolute path name. */ |
jjg@1412 | 180 | public boolean isAbsolute() { |
jjg@1412 | 181 | return file.isAbsolute(); |
jjg@1412 | 182 | } |
jjg@1412 | 183 | |
jjg@1412 | 184 | /** Return true is file identifies a directory. */ |
jjg@1412 | 185 | public boolean isDirectory() { |
jjg@1412 | 186 | return file.isDirectory(); |
jjg@1412 | 187 | } |
jjg@1412 | 188 | |
jjg@1412 | 189 | /** Return true is file identifies a file. */ |
jjg@1412 | 190 | public boolean isFile() { |
jjg@1412 | 191 | return file.isFile(); |
jjg@1412 | 192 | } |
jjg@1412 | 193 | |
jjg@1412 | 194 | /** Return true if this file is the same as another. */ |
jjg@1412 | 195 | public boolean isSameFile(DocFile other) { |
jjg@1412 | 196 | if (!(other instanceof SimpleDocFile)) |
jjg@1412 | 197 | return false; |
jjg@1412 | 198 | |
jjg@1412 | 199 | try { |
jjg@1412 | 200 | return file.exists() |
jjg@1412 | 201 | && file.getCanonicalFile().equals(((SimpleDocFile)other).file.getCanonicalFile()); |
jjg@1412 | 202 | } catch (IOException e) { |
jjg@1412 | 203 | return false; |
jjg@1412 | 204 | } |
jjg@1412 | 205 | } |
jjg@1412 | 206 | |
jjg@1412 | 207 | /** If the file is a directory, list its contents. */ |
jjg@1412 | 208 | public Iterable<DocFile> list() { |
jjg@1412 | 209 | List<DocFile> files = new ArrayList<DocFile>(); |
jjg@1412 | 210 | for (File f: file.listFiles()) { |
jjg@1412 | 211 | files.add(new SimpleDocFile(f)); |
jjg@1412 | 212 | } |
jjg@1412 | 213 | return files; |
jjg@1412 | 214 | } |
jjg@1412 | 215 | |
jjg@1412 | 216 | /** Create the file as a directory, including any parent directories. */ |
jjg@1412 | 217 | public boolean mkdirs() { |
jjg@1412 | 218 | return file.mkdirs(); |
jjg@1412 | 219 | } |
jjg@1412 | 220 | |
jjg@1412 | 221 | /** |
jjg@1412 | 222 | * Derive a new file by resolving a relative path against this file. |
jjg@1412 | 223 | * The new file will inherit the configuration and location of this file |
jjg@1412 | 224 | * If this file has a path set, the new file will have a corresponding |
jjg@1412 | 225 | * new path. |
jjg@1412 | 226 | */ |
jjg@1412 | 227 | public DocFile resolve(DocPath p) { |
jjg@1412 | 228 | return resolve(p.getPath()); |
jjg@1412 | 229 | } |
jjg@1412 | 230 | |
jjg@1412 | 231 | /** |
jjg@1412 | 232 | * Derive a new file by resolving a relative path against this file. |
jjg@1412 | 233 | * The new file will inherit the configuration and location of this file |
jjg@1412 | 234 | * If this file has a path set, the new file will have a corresponding |
jjg@1412 | 235 | * new path. |
jjg@1412 | 236 | */ |
jjg@1412 | 237 | public DocFile resolve(String p) { |
jjg@1412 | 238 | if (location == null && path == null) { |
jjg@1412 | 239 | return new SimpleDocFile(new File(file, p)); |
jjg@1412 | 240 | } else { |
jjg@1412 | 241 | return new SimpleDocFile(location, path.resolve(p)); |
jjg@1412 | 242 | } |
jjg@1412 | 243 | } |
jjg@1412 | 244 | |
jjg@1412 | 245 | /** |
jjg@1412 | 246 | * Resolve a relative file against the given output location. |
jjg@1413 | 247 | * @param locn Currently, only |
jjg@1413 | 248 | * {@link DocumentationTool.Location#DOCUMENTATION_OUTPUT} is supported. |
jjg@1412 | 249 | */ |
jjg@1413 | 250 | public DocFile resolveAgainst(Location locn) { |
jjg@1413 | 251 | if (locn != DocumentationTool.Location.DOCUMENTATION_OUTPUT) |
jjg@1412 | 252 | throw new IllegalArgumentException(); |
jjg@1412 | 253 | return new SimpleDocFile( |
jjg@1412 | 254 | new File(configuration.destDirName, file.getPath())); |
jjg@1412 | 255 | } |
jjg@1412 | 256 | |
jjg@1412 | 257 | /** |
jjg@1412 | 258 | * Given a path string create all the directories in the path. For example, |
jjg@1412 | 259 | * if the path string is "java/applet", the method will create directory |
jjg@1412 | 260 | * "java" and then "java/applet" if they don't exist. The file separator |
jjg@1412 | 261 | * string "/" is platform dependent system property. |
jjg@1412 | 262 | * |
jjg@1412 | 263 | * @param path Directory path string. |
jjg@1412 | 264 | */ |
jjg@1412 | 265 | private void createDirectoryForFile(File file) { |
jjg@1412 | 266 | File dir = file.getParentFile(); |
jjg@1412 | 267 | if (dir == null || dir.exists() || dir.mkdirs()) |
jjg@1412 | 268 | return; |
jjg@1412 | 269 | |
jjg@1412 | 270 | configuration.message.error( |
jjg@1412 | 271 | "doclet.Unable_to_create_directory_0", dir.getPath()); |
jjg@1412 | 272 | throw new DocletAbortException(); |
jjg@1412 | 273 | } |
jjg@1412 | 274 | |
jjg@1412 | 275 | /** Return a string to identify the contents of this object, |
jjg@1412 | 276 | * for debugging purposes. |
jjg@1412 | 277 | */ |
jjg@1412 | 278 | @Override |
jjg@1412 | 279 | public String toString() { |
jjg@1412 | 280 | StringBuilder sb = new StringBuilder(); |
jjg@1412 | 281 | sb.append("DocFile["); |
jjg@1412 | 282 | if (location != null) |
jjg@1412 | 283 | sb.append("locn:").append(location).append(","); |
jjg@1412 | 284 | if (path != null) |
jjg@1412 | 285 | sb.append("path:").append(path.getPath()).append(","); |
jjg@1412 | 286 | sb.append("file:").append(file); |
jjg@1412 | 287 | sb.append("]"); |
jjg@1412 | 288 | return sb.toString(); |
jjg@1412 | 289 | } |
jjg@1412 | 290 | |
jjg@1412 | 291 | } |
jjg@1412 | 292 | } |