1 /*
2  * Link.java
3  *
4  * Copyright (C) 2021 by RStudio, PBC
5  *
6  * Unless you have received this program directly from RStudio pursuant
7  * to the terms of a commercial license agreement with RStudio, then
8  * this program is licensed to you under the terms of version 3 of the
9  * GNU Affero General Public License. This program is distributed WITHOUT
10  * ANY EXPRESS OR IMPLIED WARRANTY, INCLUDING THOSE OF NON-INFRINGEMENT,
11  * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Please refer to the
12  * AGPL (http://www.gnu.org/licenses/agpl-3.0.txt) for more details.
13  *
14  */
15 package org.rstudio.studio.client.workbench.views.help.model;
16 
17 import com.google.gwt.dom.client.Document;
18 import org.rstudio.core.client.regex.Match;
19 import org.rstudio.core.client.regex.Pattern;
20 
21 public class Link
22 {
Link(String url, String title)23    public Link(String url, String title)
24    {
25       this(url, title, false);
26    }
27 
Link(String url, String title, boolean preserveHost)28    public Link(String url, String title, boolean preserveHost)
29    {
30       if (!preserveHost)
31          url = removeHost(url);
32       url_ = url;
33       title_ = title;
34       id_ = normalizeUrl(url_);
35    }
36 
37    /**
38     * If the URL has the same scheme, hostname, and port as the current page,
39     * then drop them from the URL.
40     *
41     * For example,
42     *    http://rstudio.com/help/base/ls
43     * becomes
44     *    /help/base/ls
45     */
removeHost(String url)46    private String removeHost(String url)
47    {
48       String pageUrl = Document.get().getURL();
49       Pattern p = Pattern.create("^http(s?)://[^/]+");
50       Match m = p.match(pageUrl, 0);
51       if (m == null)
52       {
53          assert false : "Couldn't parse page URL: " + url;
54          return url;
55       }
56       String prefix = m.getValue();
57       if (!url.startsWith(prefix))
58          return url;
59       else
60          return url.substring(prefix.length());
61    }
62 
getUrl()63    public String getUrl()
64    {
65       return url_;
66    }
67 
getTitle()68    public String getTitle()
69    {
70       return title_;
71    }
72 
normalizeUrl(String url)73    private static String normalizeUrl(String url)
74    {
75       return url.indexOf('#') >= 0 ? url.substring(0, url.indexOf('#')) : url;
76    }
77 
78    @Override
hashCode()79    public int hashCode()
80    {
81       return (id_ == null) ? 0 : id_.hashCode();
82    }
83 
84    @Override
equals(Object obj)85    public boolean equals(Object obj)
86    {
87       if (this == obj)
88          return true;
89       if (obj == null)
90          return false;
91       if (getClass() != obj.getClass())
92          return false;
93       Link other = (Link) obj;
94       if (id_ == null)
95       {
96          if (other.id_ != null)
97             return false;
98       } else if (!id_.equals(other.id_))
99          return false;
100       return true;
101    }
102 
103    private final String url_;
104    private final String title_;
105    private final String id_;
106 }
107