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.BufferedInputStream;
aoqi@0: import java.io.BufferedOutputStream;
aoqi@0: import java.io.BufferedWriter;
aoqi@0: import java.io.File;
aoqi@0: import java.io.FileInputStream;
aoqi@0: import java.io.FileNotFoundException;
aoqi@0: import java.io.FileOutputStream;
aoqi@0: import java.io.IOException;
aoqi@0: import java.io.InputStream;
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: import java.util.ArrayList;
aoqi@0: import java.util.LinkedHashSet;
aoqi@0: import java.util.List;
aoqi@0: import java.util.Set;
aoqi@0:
aoqi@0: import javax.tools.DocumentationTool;
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: * Implementation of DocFileFactory that just uses java.io.File API,
aoqi@0: * and does not use a JavaFileManager..
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 1.8
aoqi@0: */
aoqi@0: class SimpleDocFileFactory extends DocFileFactory {
aoqi@0:
aoqi@0: public SimpleDocFileFactory(Configuration configuration) {
aoqi@0: super(configuration);
aoqi@0: }
aoqi@0:
aoqi@0: public DocFile createFileForDirectory(String file) {
aoqi@0: return new SimpleDocFile(new File(file));
aoqi@0: }
aoqi@0:
aoqi@0: public DocFile createFileForInput(String file) {
aoqi@0: return new SimpleDocFile(new File(file));
aoqi@0: }
aoqi@0:
aoqi@0: public DocFile createFileForOutput(DocPath path) {
aoqi@0: return new SimpleDocFile(DocumentationTool.Location.DOCUMENTATION_OUTPUT, path);
aoqi@0: }
aoqi@0:
aoqi@0: @Override
aoqi@0: Iterable list(Location location, DocPath path) {
aoqi@0: if (location != StandardLocation.SOURCE_PATH)
aoqi@0: throw new IllegalArgumentException();
aoqi@0:
aoqi@0: Set files = new LinkedHashSet();
aoqi@0: for (String s : configuration.sourcepath.split(File.pathSeparator)) {
aoqi@0: if (s.isEmpty())
aoqi@0: continue;
aoqi@0: File f = new File(s);
aoqi@0: if (f.isDirectory()) {
aoqi@0: f = new File(f, path.getPath());
aoqi@0: if (f.exists())
aoqi@0: files.add(new SimpleDocFile(f));
aoqi@0: }
aoqi@0: }
aoqi@0: return files;
aoqi@0: }
aoqi@0:
aoqi@0: class SimpleDocFile extends DocFile {
aoqi@0: private File file;
aoqi@0:
aoqi@0: /** Create a DocFile for a given file. */
aoqi@0: private SimpleDocFile(File file) {
aoqi@0: super(configuration);
aoqi@0: this.file = file;
aoqi@0: }
aoqi@0:
aoqi@0: /** Create a DocFile for a given location and relative path. */
aoqi@0: private SimpleDocFile(Location location, DocPath path) {
aoqi@0: super(configuration, location, path);
aoqi@0: String destDirName = configuration.destDirName;
aoqi@0: this.file = destDirName.isEmpty() ? new File(path.getPath())
aoqi@0: : new File(destDirName, path.getPath());
aoqi@0: }
aoqi@0:
aoqi@0: /** Open an input stream for the file. */
aoqi@0: public InputStream openInputStream() throws FileNotFoundException {
aoqi@0: return new BufferedInputStream(new FileInputStream(file));
aoqi@0: }
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} and a corresponding relative path.
aoqi@0: */
aoqi@0: public OutputStream openOutputStream() throws IOException, UnsupportedEncodingException {
aoqi@0: if (location != DocumentationTool.Location.DOCUMENTATION_OUTPUT)
aoqi@0: throw new IllegalStateException();
aoqi@0:
aoqi@0: createDirectoryForFile(file);
aoqi@0: return new BufferedOutputStream(new FileOutputStream(file));
aoqi@0: }
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 Writer openWriter() throws IOException, UnsupportedEncodingException {
aoqi@0: if (location != DocumentationTool.Location.DOCUMENTATION_OUTPUT)
aoqi@0: throw new IllegalStateException();
aoqi@0:
aoqi@0: createDirectoryForFile(file);
aoqi@0: FileOutputStream fos = new FileOutputStream(file);
aoqi@0: if (configuration.docencoding == null) {
aoqi@0: return new BufferedWriter(new OutputStreamWriter(fos));
aoqi@0: } else {
aoqi@0: return new BufferedWriter(new OutputStreamWriter(fos, configuration.docencoding));
aoqi@0: }
aoqi@0: }
aoqi@0:
aoqi@0: /** Return true if the file can be read. */
aoqi@0: public boolean canRead() {
aoqi@0: return file.canRead();
aoqi@0: }
aoqi@0:
aoqi@0: /** Return true if the file can be written. */
aoqi@0: public boolean canWrite() {
aoqi@0: return file.canRead();
aoqi@0: }
aoqi@0:
aoqi@0: /** Return true if the file exists. */
aoqi@0: public boolean exists() {
aoqi@0: return file.exists();
aoqi@0: }
aoqi@0:
aoqi@0: /** Return the base name (last component) of the file name. */
aoqi@0: public String getName() {
aoqi@0: return file.getName();
aoqi@0: }
aoqi@0:
aoqi@0: /** Return the file system path for this file. */
aoqi@0: public String getPath() {
aoqi@0: return file.getPath();
aoqi@0: }
aoqi@0:
aoqi@0: /** Return true is file has an absolute path name. */
aoqi@0: public boolean isAbsolute() {
aoqi@0: return file.isAbsolute();
aoqi@0: }
aoqi@0:
aoqi@0: /** Return true is file identifies a directory. */
aoqi@0: public boolean isDirectory() {
aoqi@0: return file.isDirectory();
aoqi@0: }
aoqi@0:
aoqi@0: /** Return true is file identifies a file. */
aoqi@0: public boolean isFile() {
aoqi@0: return file.isFile();
aoqi@0: }
aoqi@0:
aoqi@0: /** Return true if this file is the same as another. */
aoqi@0: public boolean isSameFile(DocFile other) {
aoqi@0: if (!(other instanceof SimpleDocFile))
aoqi@0: return false;
aoqi@0:
aoqi@0: try {
aoqi@0: return file.exists()
aoqi@0: && file.getCanonicalFile().equals(((SimpleDocFile)other).file.getCanonicalFile());
aoqi@0: } catch (IOException e) {
aoqi@0: return false;
aoqi@0: }
aoqi@0: }
aoqi@0:
aoqi@0: /** If the file is a directory, list its contents. */
aoqi@0: public Iterable list() {
aoqi@0: List files = new ArrayList();
aoqi@0: for (File f: file.listFiles()) {
aoqi@0: files.add(new SimpleDocFile(f));
aoqi@0: }
aoqi@0: return files;
aoqi@0: }
aoqi@0:
aoqi@0: /** Create the file as a directory, including any parent directories. */
aoqi@0: public boolean mkdirs() {
aoqi@0: return file.mkdirs();
aoqi@0: }
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 DocFile resolve(DocPath p) {
aoqi@0: return resolve(p.getPath());
aoqi@0: }
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 DocFile resolve(String p) {
aoqi@0: if (location == null && path == null) {
aoqi@0: return new SimpleDocFile(new File(file, p));
aoqi@0: } else {
aoqi@0: return new SimpleDocFile(location, path.resolve(p));
aoqi@0: }
aoqi@0: }
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 DocFile resolveAgainst(Location locn) {
aoqi@0: if (locn != DocumentationTool.Location.DOCUMENTATION_OUTPUT)
aoqi@0: throw new IllegalArgumentException();
aoqi@0: return new SimpleDocFile(
aoqi@0: new File(configuration.destDirName, file.getPath()));
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Given a path string create all the directories in the path. For example,
aoqi@0: * if the path string is "java/applet", the method will create directory
aoqi@0: * "java" and then "java/applet" if they don't exist. The file separator
aoqi@0: * string "/" is platform dependent system property.
aoqi@0: *
aoqi@0: * @param path Directory path string.
aoqi@0: */
aoqi@0: private void createDirectoryForFile(File file) {
aoqi@0: File dir = file.getParentFile();
aoqi@0: if (dir == null || dir.exists() || dir.mkdirs())
aoqi@0: return;
aoqi@0:
aoqi@0: configuration.message.error(
aoqi@0: "doclet.Unable_to_create_directory_0", dir.getPath());
aoqi@0: throw new DocletAbortException("can't create directory");
aoqi@0: }
aoqi@0:
aoqi@0: /** Return a string to identify the contents of this object,
aoqi@0: * for debugging purposes.
aoqi@0: */
aoqi@0: @Override
aoqi@0: public String toString() {
aoqi@0: StringBuilder sb = new StringBuilder();
aoqi@0: sb.append("DocFile[");
aoqi@0: if (location != null)
aoqi@0: sb.append("locn:").append(location).append(",");
aoqi@0: if (path != null)
aoqi@0: sb.append("path:").append(path.getPath()).append(",");
aoqi@0: sb.append("file:").append(file);
aoqi@0: sb.append("]");
aoqi@0: return sb.toString();
aoqi@0: }
aoqi@0:
aoqi@0: }
aoqi@0: }