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://unicode.org/cldr/trac/newticket"; 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 47 Special(String s) { 48 this.id = s; 49 } 50 51 /** 52 * Convenience - just lowercases 53 */ 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 /** 64 * Get the relative base URL for the SurveyTool. 65 * This may be "/cldr-apps", for example. 66 * @return example, "/cldr-apps" 67 */ 68 public abstract String base(); 69 70 /** 71 * please use CLDRLocale instead 72 * @param locale 73 * @param xpath 74 * @return 75 */ 76 public String forXpath(String locale, String xpath) { 77 return forXpath(CLDRLocale.getInstance(locale), xpath); 78 } 79 80 /** 81 * Get a link to a specific xpath and locale. 82 * @param locale locale to view 83 * @param xpath the xpath to view 84 */ 85 public final String forXpath(CLDRLocale locale, String xpath) { 86 assertIsXpath(xpath); 87 final String hexid = (xpath == null) ? null : StringId.getHexId(xpath); 88 return forXpathHexId(locale, hexid); 89 } 90 91 /** 92 * please use CLDRLocale instead 93 * @param locale 94 * @param hexid 95 * @return 96 */ 97 public final String forXpathHexId(String locale, String hexid) { 98 return forXpathHexId(CLDRLocale.getInstance(locale), hexid); 99 } 100 101 /** 102 * Get a link to a specific xpath hex ID and locale. 103 * @param locale 104 * @param hexid 105 * @return 106 */ 107 public final String forXpathHexId(CLDRLocale locale, String hexid) { 108 assertIsHexId(hexid); 109 return forSpecial(Special.Survey, locale, (String) null, hexid); 110 } 111 112 /** 113 * please use CLDRLocale instead 114 * @param locale 115 * @param hexid 116 * @return 117 */ 118 public final String forXpathHexId(String locale, PathHeader.PageId page, String hexid) { 119 return forXpathHexId(CLDRLocale.getInstance(locale), page, hexid); 120 } 121 122 /** 123 * Get a link to a specific xpath hex ID and locale. 124 * @param locale 125 * @param hexid 126 * @return 127 */ 128 public final String forXpathHexId(CLDRLocale locale, PathHeader.PageId page, String hexid) { 129 assertIsHexId(hexid); 130 return forSpecial(Special.Survey, locale, page, hexid); 131 } 132 133 /** 134 * please use CLDRLocale instead 135 * @param locale 136 * @param page 137 * @return 138 */ 139 public final String forPage(String locale, PathHeader.PageId page) { 140 return forPage(CLDRLocale.getInstance(locale), page); 141 } 142 143 public final String forPage(CLDRLocale locale, PathHeader.PageId page) { 144 return forSpecial(Special.Survey, locale, page.name(), null); 145 } 146 147 /** 148 * Get a link to a specific locale in the SurveyTool. 149 * @param locale 150 * @return 151 */ 152 public final String forLocale(CLDRLocale locale) { 153 return forXpath(locale, null); 154 } 155 156 public final String forSpecial(Special special, CLDRLocale locale, PathHeader.PageId page, String hexid) { 157 return forSpecial(special, locale, page.name(), hexid); 158 } 159 160 public final String forSpecial(Special special) { 161 return forSpecial(special, (CLDRLocale) null, (String) null, null); 162 } 163 164 public final String forSpecial(Special special, CLDRLocale locale) { 165 return forSpecial(special, locale, (String) null, null); 166 } 167 168 /** 169 * Get a link from all of the parts. 170 * @param special 171 * @param locale 172 * @param page 173 * @param xpath 174 * @return 175 */ 176 public String forSpecial(Special special, CLDRLocale locale, String page, String hexid) { 177 StringBuilder sb = new StringBuilder(base()); 178 sb.append(VPATH); 179 if (special != null) { 180 sb.append(special.id); 181 } 182 sb.append('/'); 183 if (locale != null) { 184 sb.append(locale.getBaseName()); 185 } 186 sb.append('/'); 187 if (page != null) { 188 sb.append(page); 189 } 190 sb.append('/'); 191 if (hexid != null) { 192 sb.append(hexid); 193 } 194 return sb.toString(); 195 } 196 197 /** 198 * @param hexid 199 * @throws IllegalArgumentException 200 */ 201 final public void assertIsHexId(String hexid) throws IllegalArgumentException { 202 if (hexid != null && hexid.startsWith("/")) { 203 throw new IllegalArgumentException("This function takes a hex StringID: perhaps you meant to use forXpath() instead."); 204 } 205 } 206 207 /** 208 * @param xpath 209 * @throws IllegalArgumentException 210 */ 211 final public void assertIsXpath(String xpath) throws IllegalArgumentException { 212 if (xpath != null && !xpath.startsWith("/")) { 213 throw new IllegalArgumentException("This function takes an XPath: perhaps you meant to use forXpathHexId() instead."); 214 } 215 } 216 217 /** 218 * please use CLDRLocale instead 219 * @param vetting 220 * @param localeID 221 * @return 222 */ 223 public final String forSpecial(Special special, String localeID) { 224 return forSpecial(special, CLDRLocale.getInstance(localeID)); 225 } 226 227 public final String forPathHeader(String locale, PathHeader pathHeader) { 228 return forPathHeader(CLDRLocale.getInstance(locale), pathHeader); 229 } 230 231 /** 232 * This is the preferred function for jumping to an item relatively. It will reduce blinkage. 233 * @param locale 234 * @param pathHeader 235 * @return 236 */ 237 public final String forPathHeader(CLDRLocale locale, PathHeader pathHeader) { 238 return forSpecial(Special.Survey, locale, pathHeader.getPageId(), StringId.getHexId(pathHeader.getOriginalPath())); 239 } 240 } 241