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

changeset 1412
400a4e8accd3
child 1413
bdcef2ef52d2
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/classes/com/sun/tools/doclets/internal/toolkit/util/PathDocFileFactory.java	Thu Nov 15 19:54:20 2012 -0800
     1.3 @@ -0,0 +1,315 @@
     1.4 +/*
     1.5 + * Copyright (c) 1998, 2012, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +package com.sun.tools.doclets.internal.toolkit.util;
    1.29 +
    1.30 +
    1.31 +import java.io.BufferedInputStream;
    1.32 +import java.io.BufferedOutputStream;
    1.33 +import java.io.BufferedWriter;
    1.34 +import java.io.IOException;
    1.35 +import java.io.InputStream;
    1.36 +import java.io.OutputStream;
    1.37 +import java.io.OutputStreamWriter;
    1.38 +import java.io.UnsupportedEncodingException;
    1.39 +import java.io.Writer;
    1.40 +import java.nio.file.Files;
    1.41 +import java.nio.file.Path;
    1.42 +import java.util.ArrayList;
    1.43 +import java.util.Arrays;
    1.44 +import java.util.LinkedHashSet;
    1.45 +import java.util.List;
    1.46 +import java.util.Set;
    1.47 +
    1.48 +import javax.tools.FileObject;
    1.49 +import javax.tools.JavaFileManager.Location;
    1.50 +import javax.tools.JavaFileObject;
    1.51 +import javax.tools.StandardLocation;
    1.52 +
    1.53 +import com.sun.tools.doclets.internal.toolkit.Configuration;
    1.54 +import com.sun.tools.javac.nio.PathFileManager;
    1.55 +
    1.56 +
    1.57 +/**
    1.58 + * Implementation of DocFileFactory using a {@link PathFileManager}.
    1.59 + *
    1.60 + *  <p><b>This is NOT part of any supported API.
    1.61 + *  If you write code that depends on this, you do so at your own risk.
    1.62 + *  This code and its internal interfaces are subject to change or
    1.63 + *  deletion without notice.</b>
    1.64 + *
    1.65 + * @since 1.8
    1.66 + */
    1.67 +class PathDocFileFactory extends DocFileFactory {
    1.68 +    private final PathFileManager fileManager;
    1.69 +    private final Path destDir;
    1.70 +
    1.71 +    public PathDocFileFactory(Configuration configuration) {
    1.72 +        super(configuration);
    1.73 +        fileManager = (PathFileManager) configuration.getFileManager();
    1.74 +
    1.75 +        if (!configuration.destDirName.isEmpty()
    1.76 +                || !fileManager.hasLocation(StandardLocation.CLASS_OUTPUT)) {
    1.77 +            try {
    1.78 +                String dirName = configuration.destDirName.isEmpty() ? "." : configuration.destDirName;
    1.79 +                Path dir = fileManager.getDefaultFileSystem().getPath(dirName);
    1.80 +                fileManager.setLocation(StandardLocation.CLASS_OUTPUT, Arrays.asList(dir));
    1.81 +            } catch (IOException e) {
    1.82 +                throw new DocletAbortException();
    1.83 +            }
    1.84 +        }
    1.85 +
    1.86 +        destDir = fileManager.getLocation(StandardLocation.CLASS_OUTPUT).iterator().next();
    1.87 +    }
    1.88 +
    1.89 +    public DocFile createFileForDirectory(String file) {
    1.90 +        return new StandardDocFile(fileManager.getDefaultFileSystem().getPath(file));
    1.91 +    }
    1.92 +
    1.93 +    public DocFile createFileForInput(String file) {
    1.94 +        return new StandardDocFile(fileManager.getDefaultFileSystem().getPath(file));
    1.95 +    }
    1.96 +
    1.97 +    public DocFile createFileForOutput(DocPath path) {
    1.98 +        return new StandardDocFile(StandardLocation.CLASS_OUTPUT, path);
    1.99 +    }
   1.100 +
   1.101 +    @Override
   1.102 +    Iterable<DocFile> list(Location location, DocPath path) {
   1.103 +        if (location != StandardLocation.SOURCE_PATH)
   1.104 +            throw new IllegalArgumentException();
   1.105 +
   1.106 +        Set<DocFile> files = new LinkedHashSet<DocFile>();
   1.107 +        if (fileManager.hasLocation(location)) {
   1.108 +            for (Path f: fileManager.getLocation(location)) {
   1.109 +                if (Files.isDirectory(f)) {
   1.110 +                    f = f.resolve(path.getPath());
   1.111 +                    if (Files.exists(f))
   1.112 +                        files.add(new StandardDocFile(f));
   1.113 +                }
   1.114 +            }
   1.115 +        }
   1.116 +        return files;
   1.117 +    }
   1.118 +
   1.119 +    class StandardDocFile extends DocFile {
   1.120 +        private Path file;
   1.121 +
   1.122 +        /** Create a StandardDocFile for a given file. */
   1.123 +        private StandardDocFile(Path file) {
   1.124 +            super(configuration);
   1.125 +            this.file = file;
   1.126 +        }
   1.127 +
   1.128 +        /** Create a StandardDocFile for a given location and relative path. */
   1.129 +        private StandardDocFile(Location location, DocPath path) {
   1.130 +            super(configuration, location, path);
   1.131 +            this.file = destDir.resolve(path.getPath());
   1.132 +        }
   1.133 +
   1.134 +        /** Open an input stream for the file. */
   1.135 +        public InputStream openInputStream() throws IOException {
   1.136 +            JavaFileObject fo = getJavaFileObjectForInput(file);
   1.137 +            return new BufferedInputStream(fo.openInputStream());
   1.138 +        }
   1.139 +
   1.140 +        /**
   1.141 +         * Open an output stream for the file.
   1.142 +         * The file must have been created with a location of
   1.143 +         * {@link StandardLocation#CLASS_OUTPUT} and a corresponding relative path.
   1.144 +         */
   1.145 +        public OutputStream openOutputStream() throws IOException, UnsupportedEncodingException {
   1.146 +            if (location != StandardLocation.CLASS_OUTPUT)
   1.147 +                throw new IllegalStateException();
   1.148 +
   1.149 +            OutputStream out = getFileObjectForOutput(path).openOutputStream();
   1.150 +            return new BufferedOutputStream(out);
   1.151 +        }
   1.152 +
   1.153 +        /**
   1.154 +         * Open an writer for the file, using the encoding (if any) given in the
   1.155 +         * doclet configuration.
   1.156 +         * The file must have been created with a location of
   1.157 +         * {@link StandardLocation#CLASS_OUTPUT} and a corresponding relative path.
   1.158 +         */
   1.159 +        public Writer openWriter() throws IOException, UnsupportedEncodingException {
   1.160 +            if (location != StandardLocation.CLASS_OUTPUT)
   1.161 +                throw new IllegalStateException();
   1.162 +
   1.163 +            OutputStream out = getFileObjectForOutput(path).openOutputStream();
   1.164 +            if (configuration.docencoding == null) {
   1.165 +                return new BufferedWriter(new OutputStreamWriter(out));
   1.166 +            } else {
   1.167 +                return new BufferedWriter(new OutputStreamWriter(out, configuration.docencoding));
   1.168 +            }
   1.169 +        }
   1.170 +
   1.171 +        /** Return true if the file can be read. */
   1.172 +        public boolean canRead() {
   1.173 +            return Files.isReadable(file);
   1.174 +        }
   1.175 +
   1.176 +        /** Return true if the file can be written. */
   1.177 +        public boolean canWrite() {
   1.178 +            return Files.isWritable(file);
   1.179 +        }
   1.180 +
   1.181 +        /** Return true if the file exists. */
   1.182 +        public boolean exists() {
   1.183 +            return Files.exists(file);
   1.184 +        }
   1.185 +
   1.186 +        /** Return the base name (last component) of the file name. */
   1.187 +        public String getName() {
   1.188 +            return file.getFileName().toString();
   1.189 +        }
   1.190 +
   1.191 +        /** Return the file system path for this file. */
   1.192 +        public String getPath() {
   1.193 +            return file.toString();
   1.194 +        }
   1.195 +
   1.196 +        /** Return true is file has an absolute path name. */
   1.197 +        public boolean isAbsolute() {
   1.198 +            return file.isAbsolute();
   1.199 +        }
   1.200 +
   1.201 +        /** Return true is file identifies a directory. */
   1.202 +        public boolean isDirectory() {
   1.203 +            return Files.isDirectory(file);
   1.204 +        }
   1.205 +
   1.206 +        /** Return true is file identifies a file. */
   1.207 +        public boolean isFile() {
   1.208 +            return Files.isRegularFile(file);
   1.209 +        }
   1.210 +
   1.211 +        /** Return true if this file is the same as another. */
   1.212 +        public boolean isSameFile(DocFile other) {
   1.213 +            if (!(other instanceof StandardDocFile))
   1.214 +                return false;
   1.215 +
   1.216 +            try {
   1.217 +                return Files.isSameFile(file, ((StandardDocFile) other).file);
   1.218 +            } catch (IOException e) {
   1.219 +                return false;
   1.220 +            }
   1.221 +        }
   1.222 +
   1.223 +        /** If the file is a directory, list its contents. */
   1.224 +        public Iterable<DocFile> list() throws IOException {
   1.225 +            List<DocFile> files = new ArrayList<DocFile>();
   1.226 +            for (Path f: Files.newDirectoryStream(file)) {
   1.227 +                files.add(new StandardDocFile(f));
   1.228 +            }
   1.229 +            return files;
   1.230 +        }
   1.231 +
   1.232 +        /** Create the file as a directory, including any parent directories. */
   1.233 +        public boolean mkdirs() {
   1.234 +            try {
   1.235 +                Files.createDirectories(file);
   1.236 +                return true;
   1.237 +            } catch (IOException e) {
   1.238 +                return false;
   1.239 +            }
   1.240 +        }
   1.241 +
   1.242 +        /**
   1.243 +         * Derive a new file by resolving a relative path against this file.
   1.244 +         * The new file will inherit the configuration and location of this file
   1.245 +         * If this file has a path set, the new file will have a corresponding
   1.246 +         * new path.
   1.247 +         */
   1.248 +        public DocFile resolve(DocPath p) {
   1.249 +            return resolve(p.getPath());
   1.250 +        }
   1.251 +
   1.252 +        /**
   1.253 +         * Derive a new file by resolving a relative path against this file.
   1.254 +         * The new file will inherit the configuration and location of this file
   1.255 +         * If this file has a path set, the new file will have a corresponding
   1.256 +         * new path.
   1.257 +         */
   1.258 +        public DocFile resolve(String p) {
   1.259 +            if (location == null && path == null) {
   1.260 +                return new StandardDocFile(file.resolve(p));
   1.261 +            } else {
   1.262 +                return new StandardDocFile(location, path.resolve(p));
   1.263 +            }
   1.264 +        }
   1.265 +
   1.266 +        /**
   1.267 +         * Resolve a relative file against the given output location.
   1.268 +         * @param locn Currently, only SOURCE_OUTPUT is supported.
   1.269 +         */
   1.270 +        public DocFile resolveAgainst(StandardLocation locn) {
   1.271 +            if (locn != StandardLocation.CLASS_OUTPUT)
   1.272 +                throw new IllegalArgumentException();
   1.273 +            return new StandardDocFile(destDir.resolve(file));
   1.274 +        }
   1.275 +
   1.276 +        /** Return a string to identify the contents of this object,
   1.277 +         * for debugging purposes.
   1.278 +         */
   1.279 +        @Override
   1.280 +        public String toString() {
   1.281 +            StringBuilder sb = new StringBuilder();
   1.282 +            sb.append("PathDocFile[");
   1.283 +            if (location != null)
   1.284 +                sb.append("locn:").append(location).append(",");
   1.285 +            if (path != null)
   1.286 +                sb.append("path:").append(path.getPath()).append(",");
   1.287 +            sb.append("file:").append(file);
   1.288 +            sb.append("]");
   1.289 +            return sb.toString();
   1.290 +        }
   1.291 +
   1.292 +        private JavaFileObject getJavaFileObjectForInput(Path file) {
   1.293 +            return fileManager.getJavaFileObjects(file).iterator().next();
   1.294 +        }
   1.295 +
   1.296 +        private FileObject getFileObjectForOutput(DocPath path) throws IOException {
   1.297 +            // break the path into a package-part and the rest, by finding
   1.298 +            // the position of the last '/' before an invalid character for a
   1.299 +            // package name, such as the "." before an extension or the "-"
   1.300 +            // in filenames like package-summary.html, doc-files or src-html.
   1.301 +            String p = path.getPath();
   1.302 +            int lastSep = -1;
   1.303 +            for (int i = 0; i < p.length(); i++) {
   1.304 +                char ch = p.charAt(i);
   1.305 +                if (ch == '/') {
   1.306 +                    lastSep = i;
   1.307 +                } else if (i == lastSep + 1 && !Character.isJavaIdentifierStart(ch)
   1.308 +                        || !Character.isJavaIdentifierPart(ch)) {
   1.309 +                    break;
   1.310 +                }
   1.311 +            }
   1.312 +            String pkg = (lastSep == -1) ? "" : p.substring(0, lastSep);
   1.313 +            String rest = p.substring(lastSep + 1);
   1.314 +            return fileManager.getFileForOutput(location, pkg, rest, null);
   1.315 +        }
   1.316 +    }
   1.317 +
   1.318 +}

mercurial