src/share/classes/com/sun/tools/doclets/internal/toolkit/util/DocFile.java

Tue, 14 May 2013 10:14:55 -0700

author
jjg
date
Tue, 14 May 2013 10:14:55 -0700
changeset 1746
bd51ca92c013
parent 1413
bdcef2ef52d2
child 1985
0e6577980181
permissions
-rw-r--r--

8012178: Cleanup use of Util.escapeHtmlChars
Reviewed-by: darcy

jjg@1383 1 /*
jjg@1383 2 * Copyright (c) 1998, 2012, Oracle and/or its affiliates. All rights reserved.
jjg@1383 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jjg@1383 4 *
jjg@1383 5 * This code is free software; you can redistribute it and/or modify it
jjg@1383 6 * under the terms of the GNU General Public License version 2 only, as
jjg@1383 7 * published by the Free Software Foundation. Oracle designates this
jjg@1383 8 * particular file as subject to the "Classpath" exception as provided
jjg@1383 9 * by Oracle in the LICENSE file that accompanied this code.
jjg@1383 10 *
jjg@1383 11 * This code is distributed in the hope that it will be useful, but WITHOUT
jjg@1383 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jjg@1383 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
jjg@1383 14 * version 2 for more details (a copy is included in the LICENSE file that
jjg@1383 15 * accompanied this code).
jjg@1383 16 *
jjg@1383 17 * You should have received a copy of the GNU General Public License version
jjg@1383 18 * 2 along with this work; if not, write to the Free Software Foundation,
jjg@1383 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jjg@1383 20 *
jjg@1383 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
jjg@1383 22 * or visit www.oracle.com if you need additional information or have any
jjg@1383 23 * questions.
jjg@1383 24 */
jjg@1383 25
jjg@1383 26 package com.sun.tools.doclets.internal.toolkit.util;
jjg@1383 27
jjg@1413 28 import java.io.BufferedReader;
jjg@1413 29 import java.io.BufferedWriter;
jjg@1383 30 import java.io.FileNotFoundException;
jjg@1383 31 import java.io.IOException;
jjg@1383 32 import java.io.InputStream;
jjg@1413 33 import java.io.InputStreamReader;
jjg@1383 34 import java.io.OutputStream;
jjg@1413 35 import java.io.OutputStreamWriter;
jjg@1383 36 import java.io.UnsupportedEncodingException;
jjg@1383 37 import java.io.Writer;
jjg@1383 38
jjg@1383 39 import javax.tools.JavaFileManager.Location;
jjg@1383 40 import javax.tools.StandardLocation;
jjg@1383 41
jjg@1383 42 import com.sun.tools.doclets.internal.toolkit.Configuration;
jjg@1383 43
jjg@1383 44 /**
jjg@1383 45 * Abstraction for handling files, which may be specified directly
jjg@1383 46 * (e.g. via a path on the command line) or relative to a Location.
jjg@1383 47 *
jjg@1383 48 * <p><b>This is NOT part of any supported API.
jjg@1383 49 * If you write code that depends on this, you do so at your own risk.
jjg@1383 50 * This code and its internal interfaces are subject to change or
jjg@1383 51 * deletion without notice.</b>
jjg@1383 52 *
jjg@1383 53 * @since 8
jjg@1383 54 */
jjg@1412 55 public abstract class DocFile {
jjg@1383 56
jjg@1412 57 /** Create a DocFile for a directory. */
jjg@1412 58 public static DocFile createFileForDirectory(Configuration configuration, String file) {
jjg@1412 59 return DocFileFactory.getFactory(configuration).createFileForDirectory(file);
jjg@1412 60 }
jjg@1412 61
jjg@1412 62 /** Create a DocFile for a file that will be opened for reading. */
jjg@1412 63 public static DocFile createFileForInput(Configuration configuration, String file) {
jjg@1412 64 return DocFileFactory.getFactory(configuration).createFileForInput(file);
jjg@1412 65 }
jjg@1412 66
jjg@1412 67 /** Create a DocFile for a file that will be opened for writing. */
jjg@1412 68 public static DocFile createFileForOutput(Configuration configuration, DocPath path) {
jjg@1412 69 return DocFileFactory.getFactory(configuration).createFileForOutput(path);
jjg@1412 70 }
jjg@1412 71
jjg@1383 72 private final Configuration configuration;
jjg@1383 73
jjg@1383 74 /**
jjg@1383 75 * The location for this file. Maybe null if the file was created without
jjg@1383 76 * a location or path.
jjg@1383 77 */
jjg@1412 78 protected final Location location;
jjg@1383 79
jjg@1383 80 /**
jjg@1383 81 * The path relative to the (output) location. Maybe null if the file was
jjg@1383 82 * created without a location or path.
jjg@1383 83 */
jjg@1412 84 protected final DocPath path;
jjg@1383 85
jjg@1383 86 /**
jjg@1383 87 * List the directories and files found in subdirectories along the
jjg@1383 88 * elements of the given location.
jjg@1383 89 * @param configuration the doclet configuration
jjg@1383 90 * @param location currently, only {@link StandardLocation#SOURCE_PATH} is supported.
jjg@1383 91 * @param path the subdirectory of the directories of the location for which to
jjg@1383 92 * list files
jjg@1383 93 */
jjg@1383 94 public static Iterable<DocFile> list(Configuration configuration, Location location, DocPath path) {
jjg@1412 95 return DocFileFactory.getFactory(configuration).list(location, path);
jjg@1383 96 }
jjg@1383 97
jjg@1412 98 /** Create a DocFile without a location or path */
jjg@1412 99 protected DocFile(Configuration configuration) {
jjg@1383 100 this.configuration = configuration;
jjg@1383 101 this.location = null;
jjg@1383 102 this.path = null;
jjg@1383 103 }
jjg@1383 104
jjg@1383 105 /** Create a DocFile for a given location and relative path. */
jjg@1412 106 protected DocFile(Configuration configuration, Location location, DocPath path) {
jjg@1383 107 this.configuration = configuration;
jjg@1383 108 this.location = location;
jjg@1383 109 this.path = path;
jjg@1383 110 }
jjg@1383 111
jjg@1383 112 /** Open an input stream for the file. */
jjg@1412 113 public abstract InputStream openInputStream() throws IOException;
jjg@1383 114
jjg@1383 115 /**
jjg@1383 116 * Open an output stream for the file.
jjg@1383 117 * The file must have been created with a location of
jjg@1413 118 * {@link DocumentationTool.Location#DOCUMENTATION_OUTPUT}
jjg@1413 119 * and a corresponding relative path.
jjg@1383 120 */
jjg@1412 121 public abstract OutputStream openOutputStream() throws IOException, UnsupportedEncodingException;
jjg@1383 122
jjg@1383 123 /**
jjg@1383 124 * Open an writer for the file, using the encoding (if any) given in the
jjg@1383 125 * doclet configuration.
jjg@1383 126 * The file must have been created with a location of
jjg@1413 127 * {@link DocumentationTool.Location#DOCUMENTATION_OUTPUT} and a corresponding relative path.
jjg@1383 128 */
jjg@1412 129 public abstract Writer openWriter() throws IOException, UnsupportedEncodingException;
jjg@1383 130
jjg@1383 131 /**
jjg@1383 132 * Copy the contents of another file directly to this file.
jjg@1383 133 */
jjg@1383 134 public void copyFile(DocFile fromFile) throws IOException {
jjg@1383 135 InputStream input = fromFile.openInputStream();
jjg@1383 136 OutputStream output = openOutputStream();
jjg@1383 137 try {
jjg@1383 138 byte[] bytearr = new byte[1024];
jjg@1383 139 int len;
jjg@1383 140 while ((len = input.read(bytearr)) != -1) {
jjg@1383 141 output.write(bytearr, 0, len);
jjg@1383 142 }
jjg@1383 143 } catch (FileNotFoundException exc) {
jjg@1383 144 } catch (SecurityException exc) {
jjg@1383 145 } finally {
jjg@1383 146 input.close();
jjg@1383 147 output.close();
jjg@1383 148 }
jjg@1383 149 }
jjg@1383 150
jjg@1383 151 /**
jjg@1383 152 * Copy the contents of a resource file to this file.
jjg@1383 153 * @param resource the path of the resource, relative to the package of this class
jjg@1383 154 * @param overwrite whether or not to overwrite the file if it already exists
jjg@1383 155 * @param replaceNewLine if false, the file is copied as a binary file;
jjg@1383 156 * if true, the file is written line by line, using the platform line
jjg@1383 157 * separator
jjg@1383 158 */
jjg@1383 159 public void copyResource(DocPath resource, boolean overwrite, boolean replaceNewLine) {
jjg@1412 160 if (exists() && !overwrite)
jjg@1383 161 return;
jjg@1383 162
jjg@1383 163 try {
jjg@1383 164 InputStream in = Configuration.class.getResourceAsStream(resource.getPath());
jjg@1383 165 if (in == null)
jjg@1383 166 return;
jjg@1383 167
jjg@1412 168 OutputStream out = openOutputStream();
jjg@1383 169 try {
jjg@1383 170 if (!replaceNewLine) {
jjg@1383 171 byte[] buf = new byte[2048];
jjg@1383 172 int n;
jjg@1383 173 while((n = in.read(buf))>0) out.write(buf,0,n);
jjg@1383 174 } else {
jjg@1383 175 BufferedReader reader = new BufferedReader(new InputStreamReader(in));
jjg@1383 176 BufferedWriter writer;
jjg@1383 177 if (configuration.docencoding == null) {
jjg@1383 178 writer = new BufferedWriter(new OutputStreamWriter(out));
jjg@1383 179 } else {
jjg@1383 180 writer = new BufferedWriter(new OutputStreamWriter(out,
jjg@1383 181 configuration.docencoding));
jjg@1383 182 }
jjg@1383 183 try {
jjg@1383 184 String line;
jjg@1383 185 while ((line = reader.readLine()) != null) {
jjg@1383 186 writer.write(line);
jjg@1383 187 writer.write(DocletConstants.NL);
jjg@1383 188 }
jjg@1383 189 } finally {
jjg@1383 190 reader.close();
jjg@1383 191 writer.close();
jjg@1383 192 }
jjg@1383 193 }
jjg@1383 194 } finally {
jjg@1383 195 in.close();
jjg@1383 196 out.close();
jjg@1383 197 }
jjg@1383 198 } catch (IOException e) {
jjg@1383 199 e.printStackTrace(System.err);
jjg@1383 200 throw new DocletAbortException();
jjg@1383 201 }
jjg@1383 202 }
jjg@1383 203
jjg@1383 204 /** Return true if the file can be read. */
jjg@1412 205 public abstract boolean canRead();
jjg@1383 206
jjg@1383 207 /** Return true if the file can be written. */
jjg@1412 208 public abstract boolean canWrite();
jjg@1383 209
jjg@1383 210 /** Return true if the file exists. */
jjg@1412 211 public abstract boolean exists();
jjg@1383 212
jjg@1383 213 /** Return the base name (last component) of the file name. */
jjg@1412 214 public abstract String getName();
jjg@1383 215
jjg@1383 216 /** Return the file system path for this file. */
jjg@1412 217 public abstract String getPath();
jjg@1383 218
jjg@1412 219 /** Return true if file has an absolute path name. */
jjg@1412 220 public abstract boolean isAbsolute();
jjg@1383 221
jjg@1412 222 /** Return true if file identifies a directory. */
jjg@1412 223 public abstract boolean isDirectory();
jjg@1383 224
jjg@1412 225 /** Return true if file identifies a file. */
jjg@1412 226 public abstract boolean isFile();
jjg@1383 227
jjg@1383 228 /** Return true if this file is the same as another. */
jjg@1412 229 public abstract boolean isSameFile(DocFile other);
jjg@1383 230
jjg@1383 231 /** If the file is a directory, list its contents. */
jjg@1412 232 public abstract Iterable<DocFile> list() throws IOException;
jjg@1383 233
jjg@1383 234 /** Create the file as a directory, including any parent directories. */
jjg@1412 235 public abstract boolean mkdirs();
jjg@1383 236
jjg@1383 237 /**
jjg@1383 238 * Derive a new file by resolving a relative path against this file.
jjg@1383 239 * The new file will inherit the configuration and location of this file
jjg@1383 240 * If this file has a path set, the new file will have a corresponding
jjg@1383 241 * new path.
jjg@1383 242 */
jjg@1412 243 public abstract DocFile resolve(DocPath p);
jjg@1383 244
jjg@1383 245 /**
jjg@1383 246 * Derive a new file by resolving a relative path against this file.
jjg@1383 247 * The new file will inherit the configuration and location of this file
jjg@1383 248 * If this file has a path set, the new file will have a corresponding
jjg@1383 249 * new path.
jjg@1383 250 */
jjg@1412 251 public abstract DocFile resolve(String p);
jjg@1383 252
jjg@1383 253 /**
jjg@1383 254 * Resolve a relative file against the given output location.
jjg@1413 255 * @param locn Currently, only
jjg@1413 256 * {@link DocumentationTool.Location#DOCUMENTATION_OUTPUT} is supported.
jjg@1383 257 */
jjg@1413 258 public abstract DocFile resolveAgainst(Location locn);
jjg@1383 259 }

mercurial