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

changeset 0
959103a6100f
child 2525
2eb010b6cb22
     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/SimpleDocFileFactory.java	Wed Apr 27 01:34:52 2016 +0800
     1.3 @@ -0,0 +1,292 @@
     1.4 +/*
     1.5 + * Copyright (c) 1998, 2013, 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 +
    1.29 +package com.sun.tools.doclets.internal.toolkit.util;
    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.File;
    1.35 +import java.io.FileInputStream;
    1.36 +import java.io.FileNotFoundException;
    1.37 +import java.io.FileOutputStream;
    1.38 +import java.io.IOException;
    1.39 +import java.io.InputStream;
    1.40 +import java.io.OutputStream;
    1.41 +import java.io.OutputStreamWriter;
    1.42 +import java.io.UnsupportedEncodingException;
    1.43 +import java.io.Writer;
    1.44 +import java.util.ArrayList;
    1.45 +import java.util.LinkedHashSet;
    1.46 +import java.util.List;
    1.47 +import java.util.Set;
    1.48 +
    1.49 +import javax.tools.DocumentationTool;
    1.50 +import javax.tools.JavaFileManager.Location;
    1.51 +import javax.tools.StandardLocation;
    1.52 +
    1.53 +import com.sun.tools.doclets.internal.toolkit.Configuration;
    1.54 +
    1.55 +/**
    1.56 + * Implementation of DocFileFactory that just uses java.io.File API,
    1.57 + * and does not use a JavaFileManager..
    1.58 + *
    1.59 + *  <p><b>This is NOT part of any supported API.
    1.60 + *  If you write code that depends on this, you do so at your own risk.
    1.61 + *  This code and its internal interfaces are subject to change or
    1.62 + *  deletion without notice.</b>
    1.63 + *
    1.64 + * @since 1.8
    1.65 + */
    1.66 +class SimpleDocFileFactory extends DocFileFactory {
    1.67 +
    1.68 +    public SimpleDocFileFactory(Configuration configuration) {
    1.69 +        super(configuration);
    1.70 +    }
    1.71 +
    1.72 +    public DocFile createFileForDirectory(String file) {
    1.73 +        return new SimpleDocFile(new File(file));
    1.74 +    }
    1.75 +
    1.76 +    public DocFile createFileForInput(String file) {
    1.77 +        return new SimpleDocFile(new File(file));
    1.78 +    }
    1.79 +
    1.80 +    public DocFile createFileForOutput(DocPath path) {
    1.81 +        return new SimpleDocFile(DocumentationTool.Location.DOCUMENTATION_OUTPUT, path);
    1.82 +    }
    1.83 +
    1.84 +    @Override
    1.85 +    Iterable<DocFile> list(Location location, DocPath path) {
    1.86 +        if (location != StandardLocation.SOURCE_PATH)
    1.87 +            throw new IllegalArgumentException();
    1.88 +
    1.89 +        Set<DocFile> files = new LinkedHashSet<DocFile>();
    1.90 +        for (String s : configuration.sourcepath.split(File.pathSeparator)) {
    1.91 +            if (s.isEmpty())
    1.92 +                continue;
    1.93 +            File f = new File(s);
    1.94 +            if (f.isDirectory()) {
    1.95 +                f = new File(f, path.getPath());
    1.96 +                if (f.exists())
    1.97 +                    files.add(new SimpleDocFile(f));
    1.98 +            }
    1.99 +        }
   1.100 +        return files;
   1.101 +    }
   1.102 +
   1.103 +    class SimpleDocFile extends DocFile {
   1.104 +        private File file;
   1.105 +
   1.106 +        /** Create a DocFile for a given file. */
   1.107 +        private SimpleDocFile(File file) {
   1.108 +            super(configuration);
   1.109 +            this.file = file;
   1.110 +        }
   1.111 +
   1.112 +        /** Create a DocFile for a given location and relative path. */
   1.113 +        private SimpleDocFile(Location location, DocPath path) {
   1.114 +            super(configuration, location, path);
   1.115 +            String destDirName = configuration.destDirName;
   1.116 +            this.file = destDirName.isEmpty() ? new File(path.getPath())
   1.117 +                    : new File(destDirName, path.getPath());
   1.118 +        }
   1.119 +
   1.120 +        /** Open an input stream for the file. */
   1.121 +        public InputStream openInputStream() throws FileNotFoundException {
   1.122 +            return new BufferedInputStream(new FileInputStream(file));
   1.123 +        }
   1.124 +
   1.125 +        /**
   1.126 +         * Open an output stream for the file.
   1.127 +         * The file must have been created with a location of
   1.128 +         * {@link DocumentationTool.Location#DOCUMENTATION_OUTPUT} and a corresponding relative path.
   1.129 +         */
   1.130 +        public OutputStream openOutputStream() throws IOException, UnsupportedEncodingException {
   1.131 +            if (location != DocumentationTool.Location.DOCUMENTATION_OUTPUT)
   1.132 +                throw new IllegalStateException();
   1.133 +
   1.134 +            createDirectoryForFile(file);
   1.135 +            return new BufferedOutputStream(new FileOutputStream(file));
   1.136 +        }
   1.137 +
   1.138 +        /**
   1.139 +         * Open an writer for the file, using the encoding (if any) given in the
   1.140 +         * doclet configuration.
   1.141 +         * The file must have been created with a location of
   1.142 +         * {@link DocumentationTool.Location#DOCUMENTATION_OUTPUT} and a corresponding relative path.
   1.143 +         */
   1.144 +        public Writer openWriter() throws IOException, UnsupportedEncodingException {
   1.145 +            if (location != DocumentationTool.Location.DOCUMENTATION_OUTPUT)
   1.146 +                throw new IllegalStateException();
   1.147 +
   1.148 +            createDirectoryForFile(file);
   1.149 +            FileOutputStream fos = new FileOutputStream(file);
   1.150 +            if (configuration.docencoding == null) {
   1.151 +                return new BufferedWriter(new OutputStreamWriter(fos));
   1.152 +            } else {
   1.153 +                return new BufferedWriter(new OutputStreamWriter(fos, configuration.docencoding));
   1.154 +            }
   1.155 +        }
   1.156 +
   1.157 +        /** Return true if the file can be read. */
   1.158 +        public boolean canRead() {
   1.159 +            return file.canRead();
   1.160 +        }
   1.161 +
   1.162 +        /** Return true if the file can be written. */
   1.163 +        public boolean canWrite() {
   1.164 +            return file.canRead();
   1.165 +        }
   1.166 +
   1.167 +        /** Return true if the file exists. */
   1.168 +        public boolean exists() {
   1.169 +            return file.exists();
   1.170 +        }
   1.171 +
   1.172 +        /** Return the base name (last component) of the file name. */
   1.173 +        public String getName() {
   1.174 +            return file.getName();
   1.175 +        }
   1.176 +
   1.177 +        /** Return the file system path for this file. */
   1.178 +        public String getPath() {
   1.179 +            return file.getPath();
   1.180 +        }
   1.181 +
   1.182 +        /** Return true is file has an absolute path name. */
   1.183 +        public boolean isAbsolute() {
   1.184 +            return file.isAbsolute();
   1.185 +        }
   1.186 +
   1.187 +        /** Return true is file identifies a directory. */
   1.188 +        public boolean isDirectory() {
   1.189 +            return file.isDirectory();
   1.190 +        }
   1.191 +
   1.192 +        /** Return true is file identifies a file. */
   1.193 +        public boolean isFile() {
   1.194 +            return file.isFile();
   1.195 +        }
   1.196 +
   1.197 +        /** Return true if this file is the same as another. */
   1.198 +        public boolean isSameFile(DocFile other) {
   1.199 +            if (!(other instanceof SimpleDocFile))
   1.200 +                return false;
   1.201 +
   1.202 +            try {
   1.203 +                return file.exists()
   1.204 +                        && file.getCanonicalFile().equals(((SimpleDocFile)other).file.getCanonicalFile());
   1.205 +            } catch (IOException e) {
   1.206 +                return false;
   1.207 +            }
   1.208 +        }
   1.209 +
   1.210 +        /** If the file is a directory, list its contents. */
   1.211 +        public Iterable<DocFile> list() {
   1.212 +            List<DocFile> files = new ArrayList<DocFile>();
   1.213 +            for (File f: file.listFiles()) {
   1.214 +                files.add(new SimpleDocFile(f));
   1.215 +            }
   1.216 +            return files;
   1.217 +        }
   1.218 +
   1.219 +        /** Create the file as a directory, including any parent directories. */
   1.220 +        public boolean mkdirs() {
   1.221 +            return file.mkdirs();
   1.222 +        }
   1.223 +
   1.224 +        /**
   1.225 +         * Derive a new file by resolving a relative path against this file.
   1.226 +         * The new file will inherit the configuration and location of this file
   1.227 +         * If this file has a path set, the new file will have a corresponding
   1.228 +         * new path.
   1.229 +         */
   1.230 +        public DocFile resolve(DocPath p) {
   1.231 +            return resolve(p.getPath());
   1.232 +        }
   1.233 +
   1.234 +        /**
   1.235 +         * Derive a new file by resolving a relative path against this file.
   1.236 +         * The new file will inherit the configuration and location of this file
   1.237 +         * If this file has a path set, the new file will have a corresponding
   1.238 +         * new path.
   1.239 +         */
   1.240 +        public DocFile resolve(String p) {
   1.241 +            if (location == null && path == null) {
   1.242 +                return new SimpleDocFile(new File(file, p));
   1.243 +            } else {
   1.244 +                return new SimpleDocFile(location, path.resolve(p));
   1.245 +            }
   1.246 +        }
   1.247 +
   1.248 +        /**
   1.249 +         * Resolve a relative file against the given output location.
   1.250 +         * @param locn Currently, only
   1.251 +         * {@link DocumentationTool.Location#DOCUMENTATION_OUTPUT} is supported.
   1.252 +         */
   1.253 +        public DocFile resolveAgainst(Location locn) {
   1.254 +            if (locn != DocumentationTool.Location.DOCUMENTATION_OUTPUT)
   1.255 +                throw new IllegalArgumentException();
   1.256 +            return new SimpleDocFile(
   1.257 +                    new File(configuration.destDirName, file.getPath()));
   1.258 +        }
   1.259 +
   1.260 +        /**
   1.261 +         * Given a path string create all the directories in the path. For example,
   1.262 +         * if the path string is "java/applet", the method will create directory
   1.263 +         * "java" and then "java/applet" if they don't exist. The file separator
   1.264 +         * string "/" is platform dependent system property.
   1.265 +         *
   1.266 +         * @param path Directory path string.
   1.267 +         */
   1.268 +        private void createDirectoryForFile(File file) {
   1.269 +            File dir = file.getParentFile();
   1.270 +            if (dir == null || dir.exists() || dir.mkdirs())
   1.271 +                return;
   1.272 +
   1.273 +            configuration.message.error(
   1.274 +                   "doclet.Unable_to_create_directory_0", dir.getPath());
   1.275 +            throw new DocletAbortException("can't create directory");
   1.276 +        }
   1.277 +
   1.278 +        /** Return a string to identify the contents of this object,
   1.279 +         * for debugging purposes.
   1.280 +         */
   1.281 +        @Override
   1.282 +        public String toString() {
   1.283 +            StringBuilder sb = new StringBuilder();
   1.284 +            sb.append("DocFile[");
   1.285 +            if (location != null)
   1.286 +                sb.append("locn:").append(location).append(",");
   1.287 +            if (path != null)
   1.288 +                sb.append("path:").append(path.getPath()).append(",");
   1.289 +            sb.append("file:").append(file);
   1.290 +            sb.append("]");
   1.291 +            return sb.toString();
   1.292 +        }
   1.293 +
   1.294 +    }
   1.295 +}

mercurial