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