1 package org.unicode.cldr.util;
2 
3 /**
4  * Generate URLs to parts of CLDR and the SurveyTool.
5  * You can change the urls used with for example,  -DCLDR_SURVEY_BASE=http://st.unicode.org/smoketest
6  *
7  * @author srl
8  *
9  */
10 public abstract class CLDRURLS {
11     public static final String DEFAULT_HOST = "st.unicode.org";
12     public static final String DEFAULT_PATH = "/cldr-apps";
13     public static final String DEFAULT_BASE = "http://" + DEFAULT_HOST + DEFAULT_PATH;
14     public static final String CLDR_NEWTICKET_URL = "http://cldr.unicode.org/index/bug-reports#TOC-Filing-a-Ticket";
15     /**
16      * Override this property if you want to change the absolute URL to the SurveyTool base from DEFAULT_BASE
17      */
18     public static final String CLDR_SURVEY_BASE = "CLDR_SURVEY_BASE";
19     /**
20      * Override this property if you want to change the relative URL to the SurveyTool base from DEFAULT_PATH (within SurveyTool only)
21      */
22     public static final String CLDR_SURVEY_PATH = "CLDR_SURVEY_PATH";
23 
24     /**
25      *  "special" pages
26      * @author srl
27      *
28      */
29     public enum Special {
30         /**
31          * The 'main' view
32          */
33         Survey(""),
34         /**
35          * The list of locales
36          */
37         Locales,
38         /**
39          * The vetting viewer (i.e. Dashboard)
40          */
41         Vetting("r_vetting_json"),
42         /**
43          * Forums.  use "id" for the numeric post id
44          */
45         Forum;
46 
Special(String s)47         Special(String s) {
48             this.id = s;
49         }
50 
51         /**
52          * Convenience - just lowercases
53          */
Special()54         Special() {
55             this.id = this.name().toLowerCase();
56         }
57 
58         private final String id;
59     }
60 
61     protected static String VPATH = "/v#";
62     /**
63      * Constant for an unknown git revision.
64      * Use the same in the builders.
65      */
66     public static final String UNKNOWN_REVISION = "(unknown)";
67 
68     /**
69      * Get the relative base URL for the SurveyTool.
70      * This may be "/cldr-apps", for example.
71      * @return example, "/cldr-apps"
72      */
base()73     public abstract String base();
74 
75     /**
76      * please use CLDRLocale instead
77      * @param locale
78      * @param xpath
79      * @return
80      */
forXpath(String locale, String xpath)81     public String forXpath(String locale, String xpath) {
82         return forXpath(CLDRLocale.getInstance(locale), xpath);
83     }
84 
85     /**
86      * Get a link to a specific xpath and locale.
87      * @param locale locale to view
88      * @param xpath the xpath to view
89      */
forXpath(CLDRLocale locale, String xpath)90     public final String forXpath(CLDRLocale locale, String xpath) {
91         assertIsXpath(xpath);
92         final String hexid = (xpath == null) ? null : StringId.getHexId(xpath);
93         return forXpathHexId(locale, hexid);
94     }
95 
96     /**
97      * please use CLDRLocale instead
98      * @param locale
99      * @param hexid
100      * @return
101      */
forXpathHexId(String locale, String hexid)102     public final String forXpathHexId(String locale, String hexid) {
103         return forXpathHexId(CLDRLocale.getInstance(locale), hexid);
104     }
105 
106     /**
107      * Get a link to a specific xpath hex ID and locale.
108      * @param locale
109      * @param hexid
110      * @return
111      */
forXpathHexId(CLDRLocale locale, String hexid)112     public final String forXpathHexId(CLDRLocale locale, String hexid) {
113         assertIsHexId(hexid);
114         return forSpecial(Special.Survey, locale, (String) null, hexid);
115     }
116 
117     /**
118      * please use CLDRLocale instead
119      * @param locale
120      * @param hexid
121      * @return
122      */
forXpathHexId(String locale, PathHeader.PageId page, String hexid)123     public final String forXpathHexId(String locale, PathHeader.PageId page, String hexid) {
124         return forXpathHexId(CLDRLocale.getInstance(locale), page, hexid);
125     }
126 
127     /**
128      * Get a link to a specific xpath hex ID and locale.
129      * @param locale
130      * @param hexid
131      * @return
132      */
forXpathHexId(CLDRLocale locale, PathHeader.PageId page, String hexid)133     public final String forXpathHexId(CLDRLocale locale, PathHeader.PageId page, String hexid) {
134         assertIsHexId(hexid);
135         return forSpecial(Special.Survey, locale, page, hexid);
136     }
137 
138     /**
139      * please use CLDRLocale instead
140      * @param locale
141      * @param page
142      * @return
143      */
forPage(String locale, PathHeader.PageId page)144     public final String forPage(String locale, PathHeader.PageId page) {
145         return forPage(CLDRLocale.getInstance(locale), page);
146     }
147 
forPage(CLDRLocale locale, PathHeader.PageId page)148     public final String forPage(CLDRLocale locale, PathHeader.PageId page) {
149         return forSpecial(Special.Survey, locale, page.name(), null);
150     }
151 
152     /**
153      * Get a link to a specific locale in the SurveyTool.
154      * @param locale
155      * @return
156      */
forLocale(CLDRLocale locale)157     public final String forLocale(CLDRLocale locale) {
158         return forXpath(locale, null);
159     }
160 
forSpecial(Special special, CLDRLocale locale, PathHeader.PageId page, String hexid)161     public final String forSpecial(Special special, CLDRLocale locale, PathHeader.PageId page, String hexid) {
162         return forSpecial(special, locale, page.name(), hexid);
163     }
164 
forSpecial(Special special)165     public final String forSpecial(Special special) {
166         return forSpecial(special, (CLDRLocale) null, (String) null, null);
167     }
168 
forSpecial(Special special, CLDRLocale locale)169     public final String forSpecial(Special special, CLDRLocale locale) {
170         return forSpecial(special, locale, (String) null, null);
171     }
172 
173     /**
174      * Get a link from all of the parts.
175      * @param special
176      * @param locale
177      * @param page
178      * @param xpath
179      * @return
180      */
forSpecial(Special special, CLDRLocale locale, String page, String hexid)181     public String forSpecial(Special special, CLDRLocale locale, String page, String hexid) {
182         StringBuilder sb = new StringBuilder(base());
183         sb.append(VPATH);
184         if (special != null) {
185             sb.append(special.id);
186         }
187         sb.append('/');
188         if (locale != null) {
189             sb.append(locale.getBaseName());
190         }
191         sb.append('/');
192         if (page != null) {
193             sb.append(page);
194         }
195         sb.append('/');
196         if (hexid != null) {
197             sb.append(hexid);
198         }
199         return sb.toString();
200     }
201 
202     /**
203      * @param hexid
204      * @throws IllegalArgumentException
205      */
assertIsHexId(String hexid)206     final public void assertIsHexId(String hexid) throws IllegalArgumentException {
207         if (hexid != null && hexid.startsWith("/")) {
208             throw new IllegalArgumentException("This function takes a hex StringID: perhaps you meant to use forXpath() instead.");
209         }
210     }
211 
212     /**
213      * @param xpath
214      * @throws IllegalArgumentException
215      */
assertIsXpath(String xpath)216     final public void assertIsXpath(String xpath) throws IllegalArgumentException {
217         if (xpath != null && !xpath.startsWith("/")) {
218             throw new IllegalArgumentException("This function takes an XPath: perhaps you meant to use forXpathHexId() instead.");
219         }
220     }
221 
222     /**
223      * please use CLDRLocale instead
224      * @param vetting
225      * @param localeID
226      * @return
227      */
forSpecial(Special special, String localeID)228     public final String forSpecial(Special special, String localeID) {
229         return forSpecial(special, CLDRLocale.getInstance(localeID));
230     }
231 
forPathHeader(String locale, PathHeader pathHeader)232     public final String forPathHeader(String locale, PathHeader pathHeader) {
233         return forPathHeader(CLDRLocale.getInstance(locale), pathHeader);
234     }
235 
236     /**
237      * This is the preferred function for jumping to an item relatively. It will reduce blinkage.
238      * @param locale
239      * @param pathHeader
240      * @return
241      */
forPathHeader(CLDRLocale locale, PathHeader pathHeader)242     public final String forPathHeader(CLDRLocale locale, PathHeader pathHeader) {
243         return forSpecial(Special.Survey, locale, pathHeader.getPageId(), StringId.getHexId(pathHeader.getOriginalPath()));
244     }
245 
246     /**
247      * For a given hash, return as a link
248      * @param hash
249      * @return
250      */
gitHashToLink(String hash)251     public static String gitHashToLink(String hash) {
252         if(!isKnownHash(hash)) return "<span class=\"githashLink\">"+hash+"</span>"; // Not linkifiable
253         return "<a class=\"githashLink\" href=\"" +
254                 CldrUtility.getProperty("CLDR_COMMIT_BASE", "https://github.com/unicode-org/cldr/commit/")
255                 + hash + "\">" + hash.substring(0, 8) + "</a>";
256     }
257 
258     /**
259      * Is this a 'known' git hash? Or unknown?
260      * @param hash
261      * @return true if known, false if (unknown)
262      */
isKnownHash(String hash)263     public static boolean isKnownHash(String hash) {
264         return !hash.equals(UNKNOWN_REVISION);
265     }
266 }
267