langtools/src/share/classes/com/sun/tools/doclets/internal/toolkit/util/SourcePath.java
author jjg
Tue, 30 Oct 2012 10:15:19 -0700
changeset 14366 af625e568667
parent 14357 faf9cde2817b
permissions -rw-r--r--
8001929: fix doclint errors in langtools doc comments Reviewed-by: darcy
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
14357
faf9cde2817b 8000741: refactor javadoc to use abstraction to handle relative paths
jjg
parents: 5520
diff changeset
     2
 * Copyright (c) 1998, 2012, Oracle and/or its affiliates. All rights reserved.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
06bc494ca11e Initial load
duke
parents:
diff changeset
     4
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
06bc494ca11e Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 10
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 10
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    10
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
06bc494ca11e Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
06bc494ca11e Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
06bc494ca11e Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
06bc494ca11e Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
06bc494ca11e Initial load
duke
parents:
diff changeset
    16
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
06bc494ca11e Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
06bc494ca11e Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
06bc494ca11e Initial load
duke
parents:
diff changeset
    20
 *
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 10
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 10
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 10
diff changeset
    23
 * questions.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    24
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    25
06bc494ca11e Initial load
duke
parents:
diff changeset
    26
package com.sun.tools.doclets.internal.toolkit.util;
06bc494ca11e Initial load
duke
parents:
diff changeset
    27
06bc494ca11e Initial load
duke
parents:
diff changeset
    28
import java.io.File;
06bc494ca11e Initial load
duke
parents:
diff changeset
    29
06bc494ca11e Initial load
duke
parents:
diff changeset
    30
/**
06bc494ca11e Initial load
duke
parents:
diff changeset
    31
 * This class is used to represent a source path which can contain only
06bc494ca11e Initial load
duke
parents:
diff changeset
    32
 * directories no zip files. If a zip file is specified in the command line it
06bc494ca11e Initial load
duke
parents:
diff changeset
    33
 * will not get reflected in the SourcePath.
06bc494ca11e Initial load
duke
parents:
diff changeset
    34
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    35
 * This code is not part of an API.
06bc494ca11e Initial load
duke
parents:
diff changeset
    36
 * It is implementation that is subject to change.
06bc494ca11e Initial load
duke
parents:
diff changeset
    37
 * Do not use it as an API
06bc494ca11e Initial load
duke
parents:
diff changeset
    38
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    39
 * @author Atul M Dambalkar
06bc494ca11e Initial load
duke
parents:
diff changeset
    40
 */
14357
faf9cde2817b 8000741: refactor javadoc to use abstraction to handle relative paths
jjg
parents: 5520
diff changeset
    41
public class SourcePath {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    42
    private final char dirSeparator = File.pathSeparatorChar;
06bc494ca11e Initial load
duke
parents:
diff changeset
    43
06bc494ca11e Initial load
duke
parents:
diff changeset
    44
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    45
     * The original class path string
06bc494ca11e Initial load
duke
parents:
diff changeset
    46
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    47
    private String pathstr;
06bc494ca11e Initial load
duke
parents:
diff changeset
    48
06bc494ca11e Initial load
duke
parents:
diff changeset
    49
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    50
     * List of source path entries. Each entry is a directory.
06bc494ca11e Initial load
duke
parents:
diff changeset
    51
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    52
    private File[] sourcePath;
06bc494ca11e Initial load
duke
parents:
diff changeset
    53
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
     * Build a source path from the specified path string on the command line.
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
    public SourcePath(String pathstr) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
        init(pathstr);
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
     * Build a default source path from the path strings specified by
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
     * the properties env.class.path.
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
    public SourcePath() {
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
        init(System.getProperty("env.class.path"));
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    71
     * Initialize the SourcePath File array, which will contain only the
06bc494ca11e Initial load
duke
parents:
diff changeset
    72
     * directory names from the given path string.
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
     * @param pathstr Path String.
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
    private void init(String pathstr) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
        if (pathstr == null ||  pathstr.length() == 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
            pathstr = ".";
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
        int noOfFileSep = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
        int index = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
        this.pathstr = pathstr; // Save original class path string
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
        // Count the number of path separators
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
        while ((index = pathstr.indexOf(dirSeparator, index)) != -1) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
            noOfFileSep++;
06bc494ca11e Initial load
duke
parents:
diff changeset
    88
            index++;
06bc494ca11e Initial load
duke
parents:
diff changeset
    89
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
    90
        // Build the source path
06bc494ca11e Initial load
duke
parents:
diff changeset
    91
        File[] tempPath = new File[noOfFileSep + 1];
06bc494ca11e Initial load
duke
parents:
diff changeset
    92
        int tempPathIndex = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
    93
        int len = pathstr.length();
06bc494ca11e Initial load
duke
parents:
diff changeset
    94
        int sepPos = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
    95
        for (index = 0; index < len; index = sepPos + 1) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    96
            sepPos = pathstr.indexOf(dirSeparator, index);
06bc494ca11e Initial load
duke
parents:
diff changeset
    97
            if (sepPos < 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    98
                sepPos = len;
06bc494ca11e Initial load
duke
parents:
diff changeset
    99
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   100
            File file = new File(pathstr.substring(index, sepPos));
06bc494ca11e Initial load
duke
parents:
diff changeset
   101
            if (file.isDirectory()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   102
                tempPath[tempPathIndex++] = file;
06bc494ca11e Initial load
duke
parents:
diff changeset
   103
            } // if it is really a file, ignore it.
06bc494ca11e Initial load
duke
parents:
diff changeset
   104
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   105
        sourcePath = new File[tempPathIndex];
06bc494ca11e Initial load
duke
parents:
diff changeset
   106
        System.arraycopy((Object)tempPath, 0, (Object)sourcePath,
06bc494ca11e Initial load
duke
parents:
diff changeset
   107
                         0, tempPathIndex);
06bc494ca11e Initial load
duke
parents:
diff changeset
   108
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   109
06bc494ca11e Initial load
duke
parents:
diff changeset
   110
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   111
     * Find the specified directory in the source path.
06bc494ca11e Initial load
duke
parents:
diff changeset
   112
     *
14366
af625e568667 8001929: fix doclint errors in langtools doc comments
jjg
parents: 14357
diff changeset
   113
     * @param p Name of the directory to be searched for in the source path.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   114
     * @return File Return the directory if found else return null.
06bc494ca11e Initial load
duke
parents:
diff changeset
   115
     */
14357
faf9cde2817b 8000741: refactor javadoc to use abstraction to handle relative paths
jjg
parents: 5520
diff changeset
   116
    public File getDirectory(DocPath p) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   117
        for (int i = 0; i < sourcePath.length; i++) {
14357
faf9cde2817b 8000741: refactor javadoc to use abstraction to handle relative paths
jjg
parents: 5520
diff changeset
   118
            File directoryNeeded = new File(sourcePath[i], p.getPath());
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   119
            if (directoryNeeded.isDirectory()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   120
                return directoryNeeded;
06bc494ca11e Initial load
duke
parents:
diff changeset
   121
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   122
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   123
        return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   124
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   125
06bc494ca11e Initial load
duke
parents:
diff changeset
   126
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   127
     * Return original source path string.
06bc494ca11e Initial load
duke
parents:
diff changeset
   128
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   129
    public String toString() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   130
        return pathstr;
06bc494ca11e Initial load
duke
parents:
diff changeset
   131
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   132
}