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

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