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

This is NOT part of any supported API. jjg@1412: * If you write code that depends on this, you do so at your own risk. jjg@1412: * This code and its internal interfaces are subject to change or jjg@1412: * deletion without notice. jjg@1412: * jjg@1412: * @since 1.8 jjg@1412: */ jjg@1412: class SimpleDocFileFactory extends DocFileFactory { jjg@1412: jjg@1412: public SimpleDocFileFactory(Configuration configuration) { jjg@1412: super(configuration); jjg@1412: } jjg@1412: jjg@1412: public DocFile createFileForDirectory(String file) { jjg@1412: return new SimpleDocFile(new File(file)); jjg@1412: } jjg@1412: jjg@1412: public DocFile createFileForInput(String file) { jjg@1412: return new SimpleDocFile(new File(file)); jjg@1412: } jjg@1412: jjg@1412: public DocFile createFileForOutput(DocPath path) { jjg@1413: return new SimpleDocFile(DocumentationTool.Location.DOCUMENTATION_OUTPUT, path); jjg@1412: } jjg@1412: jjg@1412: @Override jjg@1412: Iterable list(Location location, DocPath path) { jjg@1412: if (location != StandardLocation.SOURCE_PATH) jjg@1412: throw new IllegalArgumentException(); jjg@1412: jjg@1412: Set files = new LinkedHashSet(); jjg@1412: for (String s : configuration.sourcepath.split(File.pathSeparator)) { jjg@1412: if (s.isEmpty()) jjg@1412: continue; jjg@1412: File f = new File(s); jjg@1412: if (f.isDirectory()) { jjg@1412: f = new File(f, path.getPath()); jjg@1412: if (f.exists()) jjg@1412: files.add(new SimpleDocFile(f)); jjg@1412: } jjg@1412: } jjg@1412: return files; jjg@1412: } jjg@1412: jjg@1412: class SimpleDocFile extends DocFile { jjg@1412: private File file; jjg@1412: jjg@1412: /** Create a DocFile for a given file. */ jjg@1412: private SimpleDocFile(File file) { jjg@1412: super(configuration); jjg@1412: this.file = file; jjg@1412: } jjg@1412: jjg@1412: /** Create a DocFile for a given location and relative path. */ jjg@1412: private SimpleDocFile(Location location, DocPath path) { jjg@1412: super(configuration, location, path); jjg@1412: String destDirName = configuration.destDirName; jjg@1412: this.file = destDirName.isEmpty() ? new File(path.getPath()) jjg@1412: : new File(destDirName, path.getPath()); jjg@1412: } jjg@1412: jjg@1412: /** Open an input stream for the file. */ jjg@1412: public InputStream openInputStream() throws FileNotFoundException { jjg@1412: return new BufferedInputStream(new FileInputStream(file)); jjg@1412: } jjg@1412: jjg@1412: /** jjg@1412: * Open an output stream for the file. jjg@1412: * The file must have been created with a location of jjg@1413: * {@link DocumentationTool.Location#DOCUMENTATION_OUTPUT} and a corresponding relative path. jjg@1412: */ jjg@1412: public OutputStream openOutputStream() throws IOException, UnsupportedEncodingException { jjg@1413: if (location != DocumentationTool.Location.DOCUMENTATION_OUTPUT) jjg@1412: throw new IllegalStateException(); jjg@1412: jjg@1412: createDirectoryForFile(file); jjg@1412: return new BufferedOutputStream(new FileOutputStream(file)); jjg@1412: } jjg@1412: jjg@1412: /** jjg@1412: * Open an writer for the file, using the encoding (if any) given in the jjg@1412: * doclet configuration. jjg@1412: * The file must have been created with a location of jjg@1413: * {@link DocumentationTool.Location#DOCUMENTATION_OUTPUT} and a corresponding relative path. jjg@1412: */ jjg@1412: public Writer openWriter() throws IOException, UnsupportedEncodingException { jjg@1413: if (location != DocumentationTool.Location.DOCUMENTATION_OUTPUT) jjg@1412: throw new IllegalStateException(); jjg@1412: jjg@1412: createDirectoryForFile(file); jjg@1412: FileOutputStream fos = new FileOutputStream(file); jjg@1412: if (configuration.docencoding == null) { jjg@1412: return new BufferedWriter(new OutputStreamWriter(fos)); jjg@1412: } else { jjg@1412: return new BufferedWriter(new OutputStreamWriter(fos, configuration.docencoding)); jjg@1412: } jjg@1412: } jjg@1412: jjg@1412: /** Return true if the file can be read. */ jjg@1412: public boolean canRead() { jjg@1412: return file.canRead(); jjg@1412: } jjg@1412: jjg@1412: /** Return true if the file can be written. */ jjg@1412: public boolean canWrite() { jjg@1412: return file.canRead(); jjg@1412: } jjg@1412: jjg@1412: /** Return true if the file exists. */ jjg@1412: public boolean exists() { jjg@1412: return file.exists(); jjg@1412: } jjg@1412: jjg@1412: /** Return the base name (last component) of the file name. */ jjg@1412: public String getName() { jjg@1412: return file.getName(); jjg@1412: } jjg@1412: jjg@1412: /** Return the file system path for this file. */ jjg@1412: public String getPath() { jjg@1412: return file.getPath(); jjg@1412: } jjg@1412: jjg@1412: /** Return true is file has an absolute path name. */ jjg@1412: public boolean isAbsolute() { jjg@1412: return file.isAbsolute(); jjg@1412: } jjg@1412: jjg@1412: /** Return true is file identifies a directory. */ jjg@1412: public boolean isDirectory() { jjg@1412: return file.isDirectory(); jjg@1412: } jjg@1412: jjg@1412: /** Return true is file identifies a file. */ jjg@1412: public boolean isFile() { jjg@1412: return file.isFile(); jjg@1412: } jjg@1412: jjg@1412: /** Return true if this file is the same as another. */ jjg@1412: public boolean isSameFile(DocFile other) { jjg@1412: if (!(other instanceof SimpleDocFile)) jjg@1412: return false; jjg@1412: jjg@1412: try { jjg@1412: return file.exists() jjg@1412: && file.getCanonicalFile().equals(((SimpleDocFile)other).file.getCanonicalFile()); jjg@1412: } catch (IOException e) { jjg@1412: return false; jjg@1412: } jjg@1412: } jjg@1412: jjg@1412: /** If the file is a directory, list its contents. */ jjg@1412: public Iterable list() { jjg@1412: List files = new ArrayList(); jjg@1412: for (File f: file.listFiles()) { jjg@1412: files.add(new SimpleDocFile(f)); jjg@1412: } jjg@1412: return files; jjg@1412: } jjg@1412: jjg@1412: /** Create the file as a directory, including any parent directories. */ jjg@1412: public boolean mkdirs() { jjg@1412: return file.mkdirs(); jjg@1412: } jjg@1412: jjg@1412: /** jjg@1412: * Derive a new file by resolving a relative path against this file. jjg@1412: * The new file will inherit the configuration and location of this file jjg@1412: * If this file has a path set, the new file will have a corresponding jjg@1412: * new path. jjg@1412: */ jjg@1412: public DocFile resolve(DocPath p) { jjg@1412: return resolve(p.getPath()); jjg@1412: } jjg@1412: jjg@1412: /** jjg@1412: * Derive a new file by resolving a relative path against this file. jjg@1412: * The new file will inherit the configuration and location of this file jjg@1412: * If this file has a path set, the new file will have a corresponding jjg@1412: * new path. jjg@1412: */ jjg@1412: public DocFile resolve(String p) { jjg@1412: if (location == null && path == null) { jjg@1412: return new SimpleDocFile(new File(file, p)); jjg@1412: } else { jjg@1412: return new SimpleDocFile(location, path.resolve(p)); jjg@1412: } jjg@1412: } jjg@1412: jjg@1412: /** jjg@1412: * 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@1412: */ jjg@1413: public DocFile resolveAgainst(Location locn) { jjg@1413: if (locn != DocumentationTool.Location.DOCUMENTATION_OUTPUT) jjg@1412: throw new IllegalArgumentException(); jjg@1412: return new SimpleDocFile( jjg@1412: new File(configuration.destDirName, file.getPath())); jjg@1412: } jjg@1412: jjg@1412: /** jjg@1412: * Given a path string create all the directories in the path. For example, jjg@1412: * if the path string is "java/applet", the method will create directory jjg@1412: * "java" and then "java/applet" if they don't exist. The file separator jjg@1412: * string "/" is platform dependent system property. jjg@1412: * jjg@1412: * @param path Directory path string. jjg@1412: */ jjg@1412: private void createDirectoryForFile(File file) { jjg@1412: File dir = file.getParentFile(); jjg@1412: if (dir == null || dir.exists() || dir.mkdirs()) jjg@1412: return; jjg@1412: jjg@1412: configuration.message.error( jjg@1412: "doclet.Unable_to_create_directory_0", dir.getPath()); jjg@1985: throw new DocletAbortException("can't create directory"); jjg@1412: } jjg@1412: jjg@1412: /** Return a string to identify the contents of this object, jjg@1412: * for debugging purposes. jjg@1412: */ jjg@1412: @Override jjg@1412: public String toString() { jjg@1412: StringBuilder sb = new StringBuilder(); jjg@1412: sb.append("DocFile["); jjg@1412: if (location != null) jjg@1412: sb.append("locn:").append(location).append(","); jjg@1412: if (path != null) jjg@1412: sb.append("path:").append(path.getPath()).append(","); jjg@1412: sb.append("file:").append(file); jjg@1412: sb.append("]"); jjg@1412: return sb.toString(); jjg@1412: } jjg@1412: jjg@1412: } jjg@1412: }