aoqi@0: /* aoqi@0: * Copyright (c) 1998, 2013, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. Oracle designates this aoqi@0: * particular file as subject to the "Classpath" exception as provided aoqi@0: * by Oracle in the LICENSE file that accompanied this code. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: package com.sun.tools.doclets.internal.toolkit.util; aoqi@0: aoqi@0: import java.io.BufferedReader; aoqi@0: import java.io.BufferedWriter; aoqi@0: import java.io.FileNotFoundException; aoqi@0: import java.io.IOException; aoqi@0: import java.io.InputStream; aoqi@0: import java.io.InputStreamReader; aoqi@0: import java.io.OutputStream; aoqi@0: import java.io.OutputStreamWriter; aoqi@0: import java.io.UnsupportedEncodingException; aoqi@0: import java.io.Writer; aoqi@0: aoqi@0: import javax.tools.JavaFileManager.Location; aoqi@0: import javax.tools.StandardLocation; aoqi@0: aoqi@0: import com.sun.tools.doclets.internal.toolkit.Configuration; aoqi@0: aoqi@0: /** aoqi@0: * Abstraction for handling files, which may be specified directly aoqi@0: * (e.g. via a path on the command line) or relative to a Location. aoqi@0: * aoqi@0: *

This is NOT part of any supported API. aoqi@0: * If you write code that depends on this, you do so at your own risk. aoqi@0: * This code and its internal interfaces are subject to change or aoqi@0: * deletion without notice. aoqi@0: * aoqi@0: * @since 8 aoqi@0: */ aoqi@0: public abstract class DocFile { aoqi@0: aoqi@0: /** Create a DocFile for a directory. */ aoqi@0: public static DocFile createFileForDirectory(Configuration configuration, String file) { aoqi@0: return DocFileFactory.getFactory(configuration).createFileForDirectory(file); aoqi@0: } aoqi@0: aoqi@0: /** Create a DocFile for a file that will be opened for reading. */ aoqi@0: public static DocFile createFileForInput(Configuration configuration, String file) { aoqi@0: return DocFileFactory.getFactory(configuration).createFileForInput(file); aoqi@0: } aoqi@0: aoqi@0: /** Create a DocFile for a file that will be opened for writing. */ aoqi@0: public static DocFile createFileForOutput(Configuration configuration, DocPath path) { aoqi@0: return DocFileFactory.getFactory(configuration).createFileForOutput(path); aoqi@0: } aoqi@0: aoqi@0: private final Configuration configuration; aoqi@0: aoqi@0: /** aoqi@0: * The location for this file. Maybe null if the file was created without aoqi@0: * a location or path. aoqi@0: */ aoqi@0: protected final Location location; aoqi@0: aoqi@0: /** aoqi@0: * The path relative to the (output) location. Maybe null if the file was aoqi@0: * created without a location or path. aoqi@0: */ aoqi@0: protected final DocPath path; aoqi@0: aoqi@0: /** aoqi@0: * List the directories and files found in subdirectories along the aoqi@0: * elements of the given location. aoqi@0: * @param configuration the doclet configuration aoqi@0: * @param location currently, only {@link StandardLocation#SOURCE_PATH} is supported. aoqi@0: * @param path the subdirectory of the directories of the location for which to aoqi@0: * list files aoqi@0: */ aoqi@0: public static Iterable list(Configuration configuration, Location location, DocPath path) { aoqi@0: return DocFileFactory.getFactory(configuration).list(location, path); aoqi@0: } aoqi@0: aoqi@0: /** Create a DocFile without a location or path */ aoqi@0: protected DocFile(Configuration configuration) { aoqi@0: this.configuration = configuration; aoqi@0: this.location = null; aoqi@0: this.path = null; aoqi@0: } aoqi@0: aoqi@0: /** Create a DocFile for a given location and relative path. */ aoqi@0: protected DocFile(Configuration configuration, Location location, DocPath path) { aoqi@0: this.configuration = configuration; aoqi@0: this.location = location; aoqi@0: this.path = path; aoqi@0: } aoqi@0: aoqi@0: /** Open an input stream for the file. */ aoqi@0: public abstract InputStream openInputStream() throws IOException; aoqi@0: aoqi@0: /** aoqi@0: * Open an output stream for the file. aoqi@0: * The file must have been created with a location of aoqi@0: * {@link DocumentationTool.Location#DOCUMENTATION_OUTPUT} aoqi@0: * and a corresponding relative path. aoqi@0: */ aoqi@0: public abstract OutputStream openOutputStream() throws IOException, UnsupportedEncodingException; aoqi@0: aoqi@0: /** aoqi@0: * Open an writer for the file, using the encoding (if any) given in the aoqi@0: * doclet configuration. aoqi@0: * The file must have been created with a location of aoqi@0: * {@link DocumentationTool.Location#DOCUMENTATION_OUTPUT} and a corresponding relative path. aoqi@0: */ aoqi@0: public abstract Writer openWriter() throws IOException, UnsupportedEncodingException; aoqi@0: aoqi@0: /** aoqi@0: * Copy the contents of another file directly to this file. aoqi@0: */ aoqi@0: public void copyFile(DocFile fromFile) throws IOException { aoqi@0: InputStream input = fromFile.openInputStream(); aoqi@0: OutputStream output = openOutputStream(); aoqi@0: try { aoqi@0: byte[] bytearr = new byte[1024]; aoqi@0: int len; aoqi@0: while ((len = input.read(bytearr)) != -1) { aoqi@0: output.write(bytearr, 0, len); aoqi@0: } aoqi@0: } catch (FileNotFoundException exc) { aoqi@0: } catch (SecurityException exc) { aoqi@0: } finally { aoqi@0: input.close(); aoqi@0: output.close(); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Copy the contents of a resource file to this file. aoqi@0: * @param resource the path of the resource, relative to the package of this class aoqi@0: * @param overwrite whether or not to overwrite the file if it already exists aoqi@0: * @param replaceNewLine if false, the file is copied as a binary file; aoqi@0: * if true, the file is written line by line, using the platform line aoqi@0: * separator aoqi@0: */ aoqi@0: public void copyResource(DocPath resource, boolean overwrite, boolean replaceNewLine) { aoqi@0: if (exists() && !overwrite) aoqi@0: return; aoqi@0: aoqi@0: try { aoqi@0: InputStream in = Configuration.class.getResourceAsStream(resource.getPath()); aoqi@0: if (in == null) aoqi@0: return; aoqi@0: aoqi@0: OutputStream out = openOutputStream(); aoqi@0: try { aoqi@0: if (!replaceNewLine) { aoqi@0: byte[] buf = new byte[2048]; aoqi@0: int n; aoqi@0: while((n = in.read(buf))>0) out.write(buf,0,n); aoqi@0: } else { aoqi@0: BufferedReader reader = new BufferedReader(new InputStreamReader(in)); aoqi@0: BufferedWriter writer; aoqi@0: if (configuration.docencoding == null) { aoqi@0: writer = new BufferedWriter(new OutputStreamWriter(out)); aoqi@0: } else { aoqi@0: writer = new BufferedWriter(new OutputStreamWriter(out, aoqi@0: configuration.docencoding)); aoqi@0: } aoqi@0: try { aoqi@0: String line; aoqi@0: while ((line = reader.readLine()) != null) { aoqi@0: writer.write(line); aoqi@0: writer.write(DocletConstants.NL); aoqi@0: } aoqi@0: } finally { aoqi@0: reader.close(); aoqi@0: writer.close(); aoqi@0: } aoqi@0: } aoqi@0: } finally { aoqi@0: in.close(); aoqi@0: out.close(); aoqi@0: } aoqi@0: } catch (IOException e) { aoqi@0: e.printStackTrace(System.err); aoqi@0: throw new DocletAbortException(e); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: /** Return true if the file can be read. */ aoqi@0: public abstract boolean canRead(); aoqi@0: aoqi@0: /** Return true if the file can be written. */ aoqi@0: public abstract boolean canWrite(); aoqi@0: aoqi@0: /** Return true if the file exists. */ aoqi@0: public abstract boolean exists(); aoqi@0: aoqi@0: /** Return the base name (last component) of the file name. */ aoqi@0: public abstract String getName(); aoqi@0: aoqi@0: /** Return the file system path for this file. */ aoqi@0: public abstract String getPath(); aoqi@0: aoqi@0: /** Return true if file has an absolute path name. */ aoqi@0: public abstract boolean isAbsolute(); aoqi@0: aoqi@0: /** Return true if file identifies a directory. */ aoqi@0: public abstract boolean isDirectory(); aoqi@0: aoqi@0: /** Return true if file identifies a file. */ aoqi@0: public abstract boolean isFile(); aoqi@0: aoqi@0: /** Return true if this file is the same as another. */ aoqi@0: public abstract boolean isSameFile(DocFile other); aoqi@0: aoqi@0: /** If the file is a directory, list its contents. */ aoqi@0: public abstract Iterable list() throws IOException; aoqi@0: aoqi@0: /** Create the file as a directory, including any parent directories. */ aoqi@0: public abstract boolean mkdirs(); aoqi@0: aoqi@0: /** aoqi@0: * Derive a new file by resolving a relative path against this file. aoqi@0: * The new file will inherit the configuration and location of this file aoqi@0: * If this file has a path set, the new file will have a corresponding aoqi@0: * new path. aoqi@0: */ aoqi@0: public abstract DocFile resolve(DocPath p); aoqi@0: aoqi@0: /** aoqi@0: * Derive a new file by resolving a relative path against this file. aoqi@0: * The new file will inherit the configuration and location of this file aoqi@0: * If this file has a path set, the new file will have a corresponding aoqi@0: * new path. aoqi@0: */ aoqi@0: public abstract DocFile resolve(String p); aoqi@0: aoqi@0: /** aoqi@0: * Resolve a relative file against the given output location. aoqi@0: * @param locn Currently, only aoqi@0: * {@link DocumentationTool.Location#DOCUMENTATION_OUTPUT} is supported. aoqi@0: */ aoqi@0: public abstract DocFile resolveAgainst(Location locn); aoqi@0: }