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