src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/toolkit/util/DocPath.java
author jjg
Tue, 10 Apr 2018 15:05:10 -0700
changeset 49569 d4d2f634b72f
parent 48759 ffa68af7da87
permissions -rw-r--r--
8201396: fix broken links generated by javadoc doclet Reviewed-by: ksrini
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
14357
faf9cde2817b 8000741: refactor javadoc to use abstraction to handle relative paths
jjg
parents:
diff changeset
     1
/*
48654
36f58bd6269f 8195796: Reduce the size of relative URLs in generated docs
jjg
parents: 48029
diff changeset
     2
 * Copyright (c) 1998, 2018, Oracle and/or its affiliates. All rights reserved.
14357
faf9cde2817b 8000741: refactor javadoc to use abstraction to handle relative paths
jjg
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
faf9cde2817b 8000741: refactor javadoc to use abstraction to handle relative paths
jjg
parents:
diff changeset
     4
 *
faf9cde2817b 8000741: refactor javadoc to use abstraction to handle relative paths
jjg
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
faf9cde2817b 8000741: refactor javadoc to use abstraction to handle relative paths
jjg
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
faf9cde2817b 8000741: refactor javadoc to use abstraction to handle relative paths
jjg
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
faf9cde2817b 8000741: refactor javadoc to use abstraction to handle relative paths
jjg
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
faf9cde2817b 8000741: refactor javadoc to use abstraction to handle relative paths
jjg
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
faf9cde2817b 8000741: refactor javadoc to use abstraction to handle relative paths
jjg
parents:
diff changeset
    10
 *
faf9cde2817b 8000741: refactor javadoc to use abstraction to handle relative paths
jjg
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
faf9cde2817b 8000741: refactor javadoc to use abstraction to handle relative paths
jjg
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
faf9cde2817b 8000741: refactor javadoc to use abstraction to handle relative paths
jjg
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
faf9cde2817b 8000741: refactor javadoc to use abstraction to handle relative paths
jjg
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
faf9cde2817b 8000741: refactor javadoc to use abstraction to handle relative paths
jjg
parents:
diff changeset
    15
 * accompanied this code).
faf9cde2817b 8000741: refactor javadoc to use abstraction to handle relative paths
jjg
parents:
diff changeset
    16
 *
faf9cde2817b 8000741: refactor javadoc to use abstraction to handle relative paths
jjg
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
faf9cde2817b 8000741: refactor javadoc to use abstraction to handle relative paths
jjg
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
faf9cde2817b 8000741: refactor javadoc to use abstraction to handle relative paths
jjg
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
faf9cde2817b 8000741: refactor javadoc to use abstraction to handle relative paths
jjg
parents:
diff changeset
    20
 *
faf9cde2817b 8000741: refactor javadoc to use abstraction to handle relative paths
jjg
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
faf9cde2817b 8000741: refactor javadoc to use abstraction to handle relative paths
jjg
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
faf9cde2817b 8000741: refactor javadoc to use abstraction to handle relative paths
jjg
parents:
diff changeset
    23
 * questions.
faf9cde2817b 8000741: refactor javadoc to use abstraction to handle relative paths
jjg
parents:
diff changeset
    24
 */
faf9cde2817b 8000741: refactor javadoc to use abstraction to handle relative paths
jjg
parents:
diff changeset
    25
35426
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 25874
diff changeset
    26
package jdk.javadoc.internal.doclets.toolkit.util;
14357
faf9cde2817b 8000741: refactor javadoc to use abstraction to handle relative paths
jjg
parents:
diff changeset
    27
48654
36f58bd6269f 8195796: Reduce the size of relative URLs in generated docs
jjg
parents: 48029
diff changeset
    28
import java.util.ArrayList;
36f58bd6269f 8195796: Reduce the size of relative URLs in generated docs
jjg
parents: 48029
diff changeset
    29
import java.util.Arrays;
36f58bd6269f 8195796: Reduce the size of relative URLs in generated docs
jjg
parents: 48029
diff changeset
    30
import java.util.Collections;
36f58bd6269f 8195796: Reduce the size of relative URLs in generated docs
jjg
parents: 48029
diff changeset
    31
import java.util.List;
36f58bd6269f 8195796: Reduce the size of relative URLs in generated docs
jjg
parents: 48029
diff changeset
    32
14357
faf9cde2817b 8000741: refactor javadoc to use abstraction to handle relative paths
jjg
parents:
diff changeset
    33
/**
faf9cde2817b 8000741: refactor javadoc to use abstraction to handle relative paths
jjg
parents:
diff changeset
    34
 * Abstraction for immutable relative paths.
faf9cde2817b 8000741: refactor javadoc to use abstraction to handle relative paths
jjg
parents:
diff changeset
    35
 * Paths always use '/' as a separator, and never begin or end with '/'.
14358
9eda9239cba0 8000416: refactor javadoc to provide and use an abstraction for relative URIs
jjg
parents: 14357
diff changeset
    36
 *
9eda9239cba0 8000416: refactor javadoc to provide and use an abstraction for relative URIs
jjg
parents: 14357
diff changeset
    37
 *  <p><b>This is NOT part of any supported API.
9eda9239cba0 8000416: refactor javadoc to provide and use an abstraction for relative URIs
jjg
parents: 14357
diff changeset
    38
 *  If you write code that depends on this, you do so at your own risk.
9eda9239cba0 8000416: refactor javadoc to provide and use an abstraction for relative URIs
jjg
parents: 14357
diff changeset
    39
 *  This code and its internal interfaces are subject to change or
9eda9239cba0 8000416: refactor javadoc to provide and use an abstraction for relative URIs
jjg
parents: 14357
diff changeset
    40
 *  deletion without notice.</b>
14357
faf9cde2817b 8000741: refactor javadoc to use abstraction to handle relative paths
jjg
parents:
diff changeset
    41
 */
faf9cde2817b 8000741: refactor javadoc to use abstraction to handle relative paths
jjg
parents:
diff changeset
    42
public class DocPath {
faf9cde2817b 8000741: refactor javadoc to use abstraction to handle relative paths
jjg
parents:
diff changeset
    43
    private final String path;
faf9cde2817b 8000741: refactor javadoc to use abstraction to handle relative paths
jjg
parents:
diff changeset
    44
faf9cde2817b 8000741: refactor javadoc to use abstraction to handle relative paths
jjg
parents:
diff changeset
    45
    /** The empty path. */
faf9cde2817b 8000741: refactor javadoc to use abstraction to handle relative paths
jjg
parents:
diff changeset
    46
    public static final DocPath empty = new DocPath("");
faf9cde2817b 8000741: refactor javadoc to use abstraction to handle relative paths
jjg
parents:
diff changeset
    47
faf9cde2817b 8000741: refactor javadoc to use abstraction to handle relative paths
jjg
parents:
diff changeset
    48
    /** The empty path. */
faf9cde2817b 8000741: refactor javadoc to use abstraction to handle relative paths
jjg
parents:
diff changeset
    49
    public static final DocPath parent = new DocPath("..");
faf9cde2817b 8000741: refactor javadoc to use abstraction to handle relative paths
jjg
parents:
diff changeset
    50
faf9cde2817b 8000741: refactor javadoc to use abstraction to handle relative paths
jjg
parents:
diff changeset
    51
    /**
48654
36f58bd6269f 8195796: Reduce the size of relative URLs in generated docs
jjg
parents: 48029
diff changeset
    52
     * Creates a path from a string.
36f58bd6269f 8195796: Reduce the size of relative URLs in generated docs
jjg
parents: 48029
diff changeset
    53
     * @param p the string
36f58bd6269f 8195796: Reduce the size of relative URLs in generated docs
jjg
parents: 48029
diff changeset
    54
     * @return the path
14357
faf9cde2817b 8000741: refactor javadoc to use abstraction to handle relative paths
jjg
parents:
diff changeset
    55
     */
faf9cde2817b 8000741: refactor javadoc to use abstraction to handle relative paths
jjg
parents:
diff changeset
    56
    public static DocPath create(String p) {
faf9cde2817b 8000741: refactor javadoc to use abstraction to handle relative paths
jjg
parents:
diff changeset
    57
        return (p == null) || p.isEmpty() ? empty : new DocPath(p);
faf9cde2817b 8000741: refactor javadoc to use abstraction to handle relative paths
jjg
parents:
diff changeset
    58
    }
faf9cde2817b 8000741: refactor javadoc to use abstraction to handle relative paths
jjg
parents:
diff changeset
    59
faf9cde2817b 8000741: refactor javadoc to use abstraction to handle relative paths
jjg
parents:
diff changeset
    60
    protected DocPath(String p) {
faf9cde2817b 8000741: refactor javadoc to use abstraction to handle relative paths
jjg
parents:
diff changeset
    61
        path = (p.endsWith("/") ? p.substring(0, p.length() - 1) : p);
faf9cde2817b 8000741: refactor javadoc to use abstraction to handle relative paths
jjg
parents:
diff changeset
    62
    }
faf9cde2817b 8000741: refactor javadoc to use abstraction to handle relative paths
jjg
parents:
diff changeset
    63
14366
af625e568667 8001929: fix doclint errors in langtools doc comments
jjg
parents: 14358
diff changeset
    64
    /** {@inheritDoc} */
14357
faf9cde2817b 8000741: refactor javadoc to use abstraction to handle relative paths
jjg
parents:
diff changeset
    65
    @Override
faf9cde2817b 8000741: refactor javadoc to use abstraction to handle relative paths
jjg
parents:
diff changeset
    66
    public boolean equals(Object other) {
faf9cde2817b 8000741: refactor javadoc to use abstraction to handle relative paths
jjg
parents:
diff changeset
    67
        return (other instanceof DocPath) && path.equals(((DocPath)other).path);
faf9cde2817b 8000741: refactor javadoc to use abstraction to handle relative paths
jjg
parents:
diff changeset
    68
    }
faf9cde2817b 8000741: refactor javadoc to use abstraction to handle relative paths
jjg
parents:
diff changeset
    69
14366
af625e568667 8001929: fix doclint errors in langtools doc comments
jjg
parents: 14358
diff changeset
    70
    /** {@inheritDoc} */
14357
faf9cde2817b 8000741: refactor javadoc to use abstraction to handle relative paths
jjg
parents:
diff changeset
    71
    @Override
faf9cde2817b 8000741: refactor javadoc to use abstraction to handle relative paths
jjg
parents:
diff changeset
    72
    public int hashCode() {
faf9cde2817b 8000741: refactor javadoc to use abstraction to handle relative paths
jjg
parents:
diff changeset
    73
        return path.hashCode();
faf9cde2817b 8000741: refactor javadoc to use abstraction to handle relative paths
jjg
parents:
diff changeset
    74
    }
faf9cde2817b 8000741: refactor javadoc to use abstraction to handle relative paths
jjg
parents:
diff changeset
    75
faf9cde2817b 8000741: refactor javadoc to use abstraction to handle relative paths
jjg
parents:
diff changeset
    76
    public DocPath basename() {
faf9cde2817b 8000741: refactor javadoc to use abstraction to handle relative paths
jjg
parents:
diff changeset
    77
        int sep = path.lastIndexOf("/");
faf9cde2817b 8000741: refactor javadoc to use abstraction to handle relative paths
jjg
parents:
diff changeset
    78
        return (sep == -1) ? this : new DocPath(path.substring(sep + 1));
faf9cde2817b 8000741: refactor javadoc to use abstraction to handle relative paths
jjg
parents:
diff changeset
    79
    }
faf9cde2817b 8000741: refactor javadoc to use abstraction to handle relative paths
jjg
parents:
diff changeset
    80
faf9cde2817b 8000741: refactor javadoc to use abstraction to handle relative paths
jjg
parents:
diff changeset
    81
    public DocPath parent() {
faf9cde2817b 8000741: refactor javadoc to use abstraction to handle relative paths
jjg
parents:
diff changeset
    82
        int sep = path.lastIndexOf("/");
faf9cde2817b 8000741: refactor javadoc to use abstraction to handle relative paths
jjg
parents:
diff changeset
    83
        return (sep == -1) ? empty : new DocPath(path.substring(0, sep));
faf9cde2817b 8000741: refactor javadoc to use abstraction to handle relative paths
jjg
parents:
diff changeset
    84
    }
faf9cde2817b 8000741: refactor javadoc to use abstraction to handle relative paths
jjg
parents:
diff changeset
    85
faf9cde2817b 8000741: refactor javadoc to use abstraction to handle relative paths
jjg
parents:
diff changeset
    86
    /**
48654
36f58bd6269f 8195796: Reduce the size of relative URLs in generated docs
jjg
parents: 48029
diff changeset
    87
     * Returns the path formed by appending the specified string to the current path.
36f58bd6269f 8195796: Reduce the size of relative URLs in generated docs
jjg
parents: 48029
diff changeset
    88
     * @param p the string
36f58bd6269f 8195796: Reduce the size of relative URLs in generated docs
jjg
parents: 48029
diff changeset
    89
     * @return the path
14357
faf9cde2817b 8000741: refactor javadoc to use abstraction to handle relative paths
jjg
parents:
diff changeset
    90
     */
faf9cde2817b 8000741: refactor javadoc to use abstraction to handle relative paths
jjg
parents:
diff changeset
    91
    public DocPath resolve(String p) {
faf9cde2817b 8000741: refactor javadoc to use abstraction to handle relative paths
jjg
parents:
diff changeset
    92
        if (p == null || p.isEmpty())
faf9cde2817b 8000741: refactor javadoc to use abstraction to handle relative paths
jjg
parents:
diff changeset
    93
            return this;
faf9cde2817b 8000741: refactor javadoc to use abstraction to handle relative paths
jjg
parents:
diff changeset
    94
        if (path.isEmpty())
faf9cde2817b 8000741: refactor javadoc to use abstraction to handle relative paths
jjg
parents:
diff changeset
    95
            return new DocPath(p);
faf9cde2817b 8000741: refactor javadoc to use abstraction to handle relative paths
jjg
parents:
diff changeset
    96
        return new DocPath(path + "/" + p);
faf9cde2817b 8000741: refactor javadoc to use abstraction to handle relative paths
jjg
parents:
diff changeset
    97
    }
faf9cde2817b 8000741: refactor javadoc to use abstraction to handle relative paths
jjg
parents:
diff changeset
    98
faf9cde2817b 8000741: refactor javadoc to use abstraction to handle relative paths
jjg
parents:
diff changeset
    99
    /**
48654
36f58bd6269f 8195796: Reduce the size of relative URLs in generated docs
jjg
parents: 48029
diff changeset
   100
     * Returns the path by appending the specified path to the current path.
36f58bd6269f 8195796: Reduce the size of relative URLs in generated docs
jjg
parents: 48029
diff changeset
   101
     * @param p the path
36f58bd6269f 8195796: Reduce the size of relative URLs in generated docs
jjg
parents: 48029
diff changeset
   102
     * @return the path
14357
faf9cde2817b 8000741: refactor javadoc to use abstraction to handle relative paths
jjg
parents:
diff changeset
   103
     */
faf9cde2817b 8000741: refactor javadoc to use abstraction to handle relative paths
jjg
parents:
diff changeset
   104
    public DocPath resolve(DocPath p) {
faf9cde2817b 8000741: refactor javadoc to use abstraction to handle relative paths
jjg
parents:
diff changeset
   105
        if (p == null || p.isEmpty())
faf9cde2817b 8000741: refactor javadoc to use abstraction to handle relative paths
jjg
parents:
diff changeset
   106
            return this;
faf9cde2817b 8000741: refactor javadoc to use abstraction to handle relative paths
jjg
parents:
diff changeset
   107
        if (path.isEmpty())
faf9cde2817b 8000741: refactor javadoc to use abstraction to handle relative paths
jjg
parents:
diff changeset
   108
            return p;
faf9cde2817b 8000741: refactor javadoc to use abstraction to handle relative paths
jjg
parents:
diff changeset
   109
        return new DocPath(path + "/" + p.getPath());
faf9cde2817b 8000741: refactor javadoc to use abstraction to handle relative paths
jjg
parents:
diff changeset
   110
    }
faf9cde2817b 8000741: refactor javadoc to use abstraction to handle relative paths
jjg
parents:
diff changeset
   111
faf9cde2817b 8000741: refactor javadoc to use abstraction to handle relative paths
jjg
parents:
diff changeset
   112
    /**
faf9cde2817b 8000741: refactor javadoc to use abstraction to handle relative paths
jjg
parents:
diff changeset
   113
     * Return the inverse path for this path.
faf9cde2817b 8000741: refactor javadoc to use abstraction to handle relative paths
jjg
parents:
diff changeset
   114
     * For example, if the path is a/b/c, the inverse path is ../../..
48654
36f58bd6269f 8195796: Reduce the size of relative URLs in generated docs
jjg
parents: 48029
diff changeset
   115
     * @return the path
14357
faf9cde2817b 8000741: refactor javadoc to use abstraction to handle relative paths
jjg
parents:
diff changeset
   116
     */
faf9cde2817b 8000741: refactor javadoc to use abstraction to handle relative paths
jjg
parents:
diff changeset
   117
    public DocPath invert() {
faf9cde2817b 8000741: refactor javadoc to use abstraction to handle relative paths
jjg
parents:
diff changeset
   118
        return new DocPath(path.replaceAll("[^/]+", ".."));
faf9cde2817b 8000741: refactor javadoc to use abstraction to handle relative paths
jjg
parents:
diff changeset
   119
    }
faf9cde2817b 8000741: refactor javadoc to use abstraction to handle relative paths
jjg
parents:
diff changeset
   120
faf9cde2817b 8000741: refactor javadoc to use abstraction to handle relative paths
jjg
parents:
diff changeset
   121
    /**
48654
36f58bd6269f 8195796: Reduce the size of relative URLs in generated docs
jjg
parents: 48029
diff changeset
   122
     * Returns the path formed by eliminating empty components,
36f58bd6269f 8195796: Reduce the size of relative URLs in generated docs
jjg
parents: 48029
diff changeset
   123
     * '.' components, and redundant name/.. components.
36f58bd6269f 8195796: Reduce the size of relative URLs in generated docs
jjg
parents: 48029
diff changeset
   124
     * @return the path
36f58bd6269f 8195796: Reduce the size of relative URLs in generated docs
jjg
parents: 48029
diff changeset
   125
     */
36f58bd6269f 8195796: Reduce the size of relative URLs in generated docs
jjg
parents: 48029
diff changeset
   126
    public DocPath normalize() {
36f58bd6269f 8195796: Reduce the size of relative URLs in generated docs
jjg
parents: 48029
diff changeset
   127
        return path.isEmpty()
36f58bd6269f 8195796: Reduce the size of relative URLs in generated docs
jjg
parents: 48029
diff changeset
   128
                ? this
36f58bd6269f 8195796: Reduce the size of relative URLs in generated docs
jjg
parents: 48029
diff changeset
   129
                : new DocPath(String.join("/", normalize(path)));
36f58bd6269f 8195796: Reduce the size of relative URLs in generated docs
jjg
parents: 48029
diff changeset
   130
    }
36f58bd6269f 8195796: Reduce the size of relative URLs in generated docs
jjg
parents: 48029
diff changeset
   131
36f58bd6269f 8195796: Reduce the size of relative URLs in generated docs
jjg
parents: 48029
diff changeset
   132
    private static List<String> normalize(String path) {
36f58bd6269f 8195796: Reduce the size of relative URLs in generated docs
jjg
parents: 48029
diff changeset
   133
        return normalize(Arrays.asList(path.split("/")));
36f58bd6269f 8195796: Reduce the size of relative URLs in generated docs
jjg
parents: 48029
diff changeset
   134
    }
36f58bd6269f 8195796: Reduce the size of relative URLs in generated docs
jjg
parents: 48029
diff changeset
   135
36f58bd6269f 8195796: Reduce the size of relative URLs in generated docs
jjg
parents: 48029
diff changeset
   136
    private static List<String> normalize(List<String> parts) {
36f58bd6269f 8195796: Reduce the size of relative URLs in generated docs
jjg
parents: 48029
diff changeset
   137
        if (parts.stream().noneMatch(s -> s.isEmpty() || s.equals(".") || s.equals(".."))) {
36f58bd6269f 8195796: Reduce the size of relative URLs in generated docs
jjg
parents: 48029
diff changeset
   138
            return parts;
36f58bd6269f 8195796: Reduce the size of relative URLs in generated docs
jjg
parents: 48029
diff changeset
   139
        }
36f58bd6269f 8195796: Reduce the size of relative URLs in generated docs
jjg
parents: 48029
diff changeset
   140
        List<String> normalized = new ArrayList<>();
36f58bd6269f 8195796: Reduce the size of relative URLs in generated docs
jjg
parents: 48029
diff changeset
   141
        for (String part : parts) {
36f58bd6269f 8195796: Reduce the size of relative URLs in generated docs
jjg
parents: 48029
diff changeset
   142
            switch (part) {
36f58bd6269f 8195796: Reduce the size of relative URLs in generated docs
jjg
parents: 48029
diff changeset
   143
                case "":
36f58bd6269f 8195796: Reduce the size of relative URLs in generated docs
jjg
parents: 48029
diff changeset
   144
                case ".":
36f58bd6269f 8195796: Reduce the size of relative URLs in generated docs
jjg
parents: 48029
diff changeset
   145
                    break;
36f58bd6269f 8195796: Reduce the size of relative URLs in generated docs
jjg
parents: 48029
diff changeset
   146
                case "..":
36f58bd6269f 8195796: Reduce the size of relative URLs in generated docs
jjg
parents: 48029
diff changeset
   147
                    int n = normalized.size();
36f58bd6269f 8195796: Reduce the size of relative URLs in generated docs
jjg
parents: 48029
diff changeset
   148
                    if (n > 0 && !normalized.get(n - 1).equals("..")) {
36f58bd6269f 8195796: Reduce the size of relative URLs in generated docs
jjg
parents: 48029
diff changeset
   149
                        normalized.remove(n - 1);
36f58bd6269f 8195796: Reduce the size of relative URLs in generated docs
jjg
parents: 48029
diff changeset
   150
                    } else {
36f58bd6269f 8195796: Reduce the size of relative URLs in generated docs
jjg
parents: 48029
diff changeset
   151
                        normalized.add(part);
36f58bd6269f 8195796: Reduce the size of relative URLs in generated docs
jjg
parents: 48029
diff changeset
   152
                    }
36f58bd6269f 8195796: Reduce the size of relative URLs in generated docs
jjg
parents: 48029
diff changeset
   153
                    break;
36f58bd6269f 8195796: Reduce the size of relative URLs in generated docs
jjg
parents: 48029
diff changeset
   154
                default:
36f58bd6269f 8195796: Reduce the size of relative URLs in generated docs
jjg
parents: 48029
diff changeset
   155
                    normalized.add(part);
36f58bd6269f 8195796: Reduce the size of relative URLs in generated docs
jjg
parents: 48029
diff changeset
   156
            }
36f58bd6269f 8195796: Reduce the size of relative URLs in generated docs
jjg
parents: 48029
diff changeset
   157
        }
36f58bd6269f 8195796: Reduce the size of relative URLs in generated docs
jjg
parents: 48029
diff changeset
   158
        return normalized;
36f58bd6269f 8195796: Reduce the size of relative URLs in generated docs
jjg
parents: 48029
diff changeset
   159
    }
36f58bd6269f 8195796: Reduce the size of relative URLs in generated docs
jjg
parents: 48029
diff changeset
   160
36f58bd6269f 8195796: Reduce the size of relative URLs in generated docs
jjg
parents: 48029
diff changeset
   161
    /**
36f58bd6269f 8195796: Reduce the size of relative URLs in generated docs
jjg
parents: 48029
diff changeset
   162
     * Normalize and relativize a path against this path,
36f58bd6269f 8195796: Reduce the size of relative URLs in generated docs
jjg
parents: 48029
diff changeset
   163
     * assuming that this path is for a file (not a directory),
36f58bd6269f 8195796: Reduce the size of relative URLs in generated docs
jjg
parents: 48029
diff changeset
   164
     * in which the other path will appear.
36f58bd6269f 8195796: Reduce the size of relative URLs in generated docs
jjg
parents: 48029
diff changeset
   165
     *
36f58bd6269f 8195796: Reduce the size of relative URLs in generated docs
jjg
parents: 48029
diff changeset
   166
     * @param other the path to be relativized.
36f58bd6269f 8195796: Reduce the size of relative URLs in generated docs
jjg
parents: 48029
diff changeset
   167
     * @return the simplified path
36f58bd6269f 8195796: Reduce the size of relative URLs in generated docs
jjg
parents: 48029
diff changeset
   168
     */
36f58bd6269f 8195796: Reduce the size of relative URLs in generated docs
jjg
parents: 48029
diff changeset
   169
    public DocPath relativize(DocPath other) {
36f58bd6269f 8195796: Reduce the size of relative URLs in generated docs
jjg
parents: 48029
diff changeset
   170
        if (other == null || other.path.isEmpty()) {
36f58bd6269f 8195796: Reduce the size of relative URLs in generated docs
jjg
parents: 48029
diff changeset
   171
            return this;
36f58bd6269f 8195796: Reduce the size of relative URLs in generated docs
jjg
parents: 48029
diff changeset
   172
        }
36f58bd6269f 8195796: Reduce the size of relative URLs in generated docs
jjg
parents: 48029
diff changeset
   173
36f58bd6269f 8195796: Reduce the size of relative URLs in generated docs
jjg
parents: 48029
diff changeset
   174
        if (path.isEmpty()) {
36f58bd6269f 8195796: Reduce the size of relative URLs in generated docs
jjg
parents: 48029
diff changeset
   175
            return other;
36f58bd6269f 8195796: Reduce the size of relative URLs in generated docs
jjg
parents: 48029
diff changeset
   176
        }
36f58bd6269f 8195796: Reduce the size of relative URLs in generated docs
jjg
parents: 48029
diff changeset
   177
36f58bd6269f 8195796: Reduce the size of relative URLs in generated docs
jjg
parents: 48029
diff changeset
   178
        List<String> originParts = normalize(path);
36f58bd6269f 8195796: Reduce the size of relative URLs in generated docs
jjg
parents: 48029
diff changeset
   179
        int sep = path.lastIndexOf("/");
36f58bd6269f 8195796: Reduce the size of relative URLs in generated docs
jjg
parents: 48029
diff changeset
   180
        List<String> destParts = sep == -1
36f58bd6269f 8195796: Reduce the size of relative URLs in generated docs
jjg
parents: 48029
diff changeset
   181
                ? normalize(other.path)
36f58bd6269f 8195796: Reduce the size of relative URLs in generated docs
jjg
parents: 48029
diff changeset
   182
                : normalize(path.substring(0, sep + 1) + other.path);
36f58bd6269f 8195796: Reduce the size of relative URLs in generated docs
jjg
parents: 48029
diff changeset
   183
        int common = 0;
36f58bd6269f 8195796: Reduce the size of relative URLs in generated docs
jjg
parents: 48029
diff changeset
   184
        while (common < originParts.size()
36f58bd6269f 8195796: Reduce the size of relative URLs in generated docs
jjg
parents: 48029
diff changeset
   185
                && common < destParts.size()
36f58bd6269f 8195796: Reduce the size of relative URLs in generated docs
jjg
parents: 48029
diff changeset
   186
                && originParts.get(common).equals(destParts.get(common))) {
36f58bd6269f 8195796: Reduce the size of relative URLs in generated docs
jjg
parents: 48029
diff changeset
   187
            common++;
36f58bd6269f 8195796: Reduce the size of relative URLs in generated docs
jjg
parents: 48029
diff changeset
   188
        }
36f58bd6269f 8195796: Reduce the size of relative URLs in generated docs
jjg
parents: 48029
diff changeset
   189
36f58bd6269f 8195796: Reduce the size of relative URLs in generated docs
jjg
parents: 48029
diff changeset
   190
        List<String> newParts;
36f58bd6269f 8195796: Reduce the size of relative URLs in generated docs
jjg
parents: 48029
diff changeset
   191
        if (common == originParts.size()) {
36f58bd6269f 8195796: Reduce the size of relative URLs in generated docs
jjg
parents: 48029
diff changeset
   192
            newParts = destParts.subList(common, destParts.size());
36f58bd6269f 8195796: Reduce the size of relative URLs in generated docs
jjg
parents: 48029
diff changeset
   193
        } else {
36f58bd6269f 8195796: Reduce the size of relative URLs in generated docs
jjg
parents: 48029
diff changeset
   194
            newParts = new ArrayList<>();
36f58bd6269f 8195796: Reduce the size of relative URLs in generated docs
jjg
parents: 48029
diff changeset
   195
            newParts.addAll(Collections.nCopies(originParts.size() - common - 1, ".."));
36f58bd6269f 8195796: Reduce the size of relative URLs in generated docs
jjg
parents: 48029
diff changeset
   196
            newParts.addAll(destParts.subList(common, destParts.size()));
36f58bd6269f 8195796: Reduce the size of relative URLs in generated docs
jjg
parents: 48029
diff changeset
   197
        }
36f58bd6269f 8195796: Reduce the size of relative URLs in generated docs
jjg
parents: 48029
diff changeset
   198
        return new DocPath(String.join("/", newParts));
36f58bd6269f 8195796: Reduce the size of relative URLs in generated docs
jjg
parents: 48029
diff changeset
   199
    }
36f58bd6269f 8195796: Reduce the size of relative URLs in generated docs
jjg
parents: 48029
diff changeset
   200
36f58bd6269f 8195796: Reduce the size of relative URLs in generated docs
jjg
parents: 48029
diff changeset
   201
    /**
14357
faf9cde2817b 8000741: refactor javadoc to use abstraction to handle relative paths
jjg
parents:
diff changeset
   202
     * Return true if this path is empty.
48654
36f58bd6269f 8195796: Reduce the size of relative URLs in generated docs
jjg
parents: 48029
diff changeset
   203
     * @return true if this path is empty
14357
faf9cde2817b 8000741: refactor javadoc to use abstraction to handle relative paths
jjg
parents:
diff changeset
   204
     */
faf9cde2817b 8000741: refactor javadoc to use abstraction to handle relative paths
jjg
parents:
diff changeset
   205
    public boolean isEmpty() {
faf9cde2817b 8000741: refactor javadoc to use abstraction to handle relative paths
jjg
parents:
diff changeset
   206
        return path.isEmpty();
faf9cde2817b 8000741: refactor javadoc to use abstraction to handle relative paths
jjg
parents:
diff changeset
   207
    }
faf9cde2817b 8000741: refactor javadoc to use abstraction to handle relative paths
jjg
parents:
diff changeset
   208
48654
36f58bd6269f 8195796: Reduce the size of relative URLs in generated docs
jjg
parents: 48029
diff changeset
   209
    /**
36f58bd6269f 8195796: Reduce the size of relative URLs in generated docs
jjg
parents: 48029
diff changeset
   210
     * Creates a DocLink formed from this path and a fragment identifier.
36f58bd6269f 8195796: Reduce the size of relative URLs in generated docs
jjg
parents: 48029
diff changeset
   211
     * @param fragment the fragment
36f58bd6269f 8195796: Reduce the size of relative URLs in generated docs
jjg
parents: 48029
diff changeset
   212
     * @return the link
36f58bd6269f 8195796: Reduce the size of relative URLs in generated docs
jjg
parents: 48029
diff changeset
   213
     */
14358
9eda9239cba0 8000416: refactor javadoc to provide and use an abstraction for relative URIs
jjg
parents: 14357
diff changeset
   214
    public DocLink fragment(String fragment) {
9eda9239cba0 8000416: refactor javadoc to provide and use an abstraction for relative URIs
jjg
parents: 14357
diff changeset
   215
        return new DocLink(path, null, fragment);
9eda9239cba0 8000416: refactor javadoc to provide and use an abstraction for relative URIs
jjg
parents: 14357
diff changeset
   216
    }
9eda9239cba0 8000416: refactor javadoc to provide and use an abstraction for relative URIs
jjg
parents: 14357
diff changeset
   217
48654
36f58bd6269f 8195796: Reduce the size of relative URLs in generated docs
jjg
parents: 48029
diff changeset
   218
    /**
36f58bd6269f 8195796: Reduce the size of relative URLs in generated docs
jjg
parents: 48029
diff changeset
   219
     * Creates a DocLink formed from this path and a query string.
36f58bd6269f 8195796: Reduce the size of relative URLs in generated docs
jjg
parents: 48029
diff changeset
   220
     * @param query the query string
36f58bd6269f 8195796: Reduce the size of relative URLs in generated docs
jjg
parents: 48029
diff changeset
   221
     * @return the link
36f58bd6269f 8195796: Reduce the size of relative URLs in generated docs
jjg
parents: 48029
diff changeset
   222
     */
14358
9eda9239cba0 8000416: refactor javadoc to provide and use an abstraction for relative URIs
jjg
parents: 14357
diff changeset
   223
    public DocLink query(String query) {
9eda9239cba0 8000416: refactor javadoc to provide and use an abstraction for relative URIs
jjg
parents: 14357
diff changeset
   224
        return new DocLink(path, query, null);
9eda9239cba0 8000416: refactor javadoc to provide and use an abstraction for relative URIs
jjg
parents: 14357
diff changeset
   225
    }
9eda9239cba0 8000416: refactor javadoc to provide and use an abstraction for relative URIs
jjg
parents: 14357
diff changeset
   226
14357
faf9cde2817b 8000741: refactor javadoc to use abstraction to handle relative paths
jjg
parents:
diff changeset
   227
    /**
48654
36f58bd6269f 8195796: Reduce the size of relative URLs in generated docs
jjg
parents: 48029
diff changeset
   228
     * Returns this path as a string.
36f58bd6269f 8195796: Reduce the size of relative URLs in generated docs
jjg
parents: 48029
diff changeset
   229
     * @return the path
14357
faf9cde2817b 8000741: refactor javadoc to use abstraction to handle relative paths
jjg
parents:
diff changeset
   230
     */
faf9cde2817b 8000741: refactor javadoc to use abstraction to handle relative paths
jjg
parents:
diff changeset
   231
    // This is provided instead of using toString() to help catch
faf9cde2817b 8000741: refactor javadoc to use abstraction to handle relative paths
jjg
parents:
diff changeset
   232
    // unintended use of toString() in string concatenation sequences.
faf9cde2817b 8000741: refactor javadoc to use abstraction to handle relative paths
jjg
parents:
diff changeset
   233
    public String getPath() {
faf9cde2817b 8000741: refactor javadoc to use abstraction to handle relative paths
jjg
parents:
diff changeset
   234
        return path;
faf9cde2817b 8000741: refactor javadoc to use abstraction to handle relative paths
jjg
parents:
diff changeset
   235
    }
faf9cde2817b 8000741: refactor javadoc to use abstraction to handle relative paths
jjg
parents:
diff changeset
   236
}