1 /* 2 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 package jdk.tools.jlink.internal.plugins; 26 27 import java.io.ByteArrayInputStream; 28 import java.io.IOException; 29 import java.io.UncheckedIOException; 30 import java.util.Arrays; 31 import java.util.Collections; 32 import java.util.HashSet; 33 import java.util.IllformedLocaleException; 34 import java.util.Locale; 35 import java.util.List; 36 import java.util.Map; 37 import java.util.Set; 38 import java.util.function.Predicate; 39 import java.util.regex.Pattern; 40 import java.util.stream.Collectors; 41 import java.util.stream.IntStream; 42 import java.util.stream.Stream; 43 import jdk.internal.org.objectweb.asm.ClassReader; 44 import jdk.tools.jlink.plugin.TransformerPlugin; 45 import jdk.tools.jlink.plugin.Pool; 46 import jdk.tools.jlink.plugin.PluginException; 47 import jdk.tools.jlink.internal.ResourcePrevisitor; 48 import jdk.tools.jlink.internal.StringTable; 49 import jdk.tools.jlink.internal.Utils; 50 51 /** 52 * Plugin to explicitly specify the locale data included in jdk.localedata 53 * module. This plugin provides a jlink command line option "--include-locales" 54 * with an argument. The argument is a list of BCP 47 language tags separated 55 * by a comma. E.g., 56 * 57 * "jlink --include-locales en,ja,*-IN" 58 * 59 * This option will include locale data for all available English and Japanese 60 * languages, and ones for the country of India. All other locale data are 61 * filtered out on the image creation. 62 * 63 * Here are a few assumptions: 64 * 65 * 0. All locale data in java.base are unconditionally included. 66 * 1. All the selective locale data are in jdk.localedata module 67 * 2. Their package names are constructed by appending ".ext" to 68 * the corresponding ones in java.base module. 69 * 3. Available locales string in LocaleDataMetaInfo class should 70 * start with at least one white space character, e.g., " ar ar-EG ..." 71 * ^ 72 */ 73 public final class IncludeLocalesPlugin implements TransformerPlugin, ResourcePrevisitor { 74 75 public static final String NAME = "include-locales"; 76 private static final String MODULENAME = "jdk.localedata"; 77 private static final Set<String> LOCALEDATA_PACKAGES = Set.of( 78 "sun.text.resources.cldr.ext", 79 "sun.text.resources.ext", 80 "sun.util.resources.cldr.ext", 81 "sun.util.resources.cldr.provider", 82 "sun.util.resources.ext", 83 "sun.util.resources.provider"); 84 private static final String METAINFONAME = "LocaleDataMetaInfo"; 85 private static final String META_FILES = 86 "*module-info.class," + 87 "*LocaleDataProvider*," + 88 "*" + METAINFONAME + "*,"; 89 private static final String INCLUDE_LOCALE_FILES = 90 "*sun/text/resources/ext/[^\\/]+_%%.class," + 91 "*sun/util/resources/ext/[^\\/]+_%%.class," + 92 "*sun/text/resources/cldr/ext/[^\\/]+_%%.class," + 93 "*sun/util/resources/cldr/ext/[^\\/]+_%%.class,"; 94 private Predicate<String> predicate; 95 private List<Locale.LanguageRange> priorityList; 96 private List<Locale> available; 97 private List<String> filtered; 98 99 // Special COMPAT provider locales 100 private static final String jaJPJPTag = "ja-JP-JP"; 101 private static final String noNONYTag = "no-NO-NY"; 102 private static final String thTHTHTag = "th-TH-TH"; 103 private static final Locale jaJPJP = new Locale("ja", "JP", "JP"); 104 private static final Locale noNONY = new Locale("no", "NO", "NY"); 105 private static final Locale thTHTH = new Locale("th", "TH", "TH"); 106 107 @Override 108 public String getName() { 109 return NAME; 110 } 111 112 @Override 113 public void visit(Pool in, Pool out) { 114 in.visit((resource) -> { 115 if (resource.getModule().equals(MODULENAME)) { 116 String path = resource.getPath(); 117 resource = predicate.test(path) ? resource: null; 118 if (resource != null) { 119 byte[] bytes = resource.getBytes(); 120 ClassReader cr = new ClassReader(bytes); 121 if (Arrays.stream(cr.getInterfaces()) 122 .anyMatch(i -> i.contains(METAINFONAME)) && 123 stripUnsupportedLocales(bytes, cr)) { 124 resource = new Pool.ModuleData(MODULENAME, path, 125 resource.getType(), 126 new ByteArrayInputStream(bytes), bytes.length); 127 } 128 } 129 } 130 return resource; 131 }, out); 132 } 133 134 @Override 135 public Set<PluginType> getType() { 136 Set<PluginType> set = new HashSet<>(); 137 set.add(CATEGORY.FILTER); 138 return Collections.unmodifiableSet(set); 139 } 140 141 @Override 142 public String getDescription() { 143 return PluginsResourceBundle.getDescription(NAME); 144 } 145 146 @Override 147 public boolean hasArguments() { 148 return true; 149 } 150 151 @Override 152 public String getArgumentsDescription() { 153 return PluginsResourceBundle.getArgument(NAME); 154 } 155 156 @Override 157 public void configure(Map<String, String> config) { 158 try { 159 priorityList = Arrays.stream(config.get(NAME).split(",")) 160 .map(Locale.LanguageRange::new) 161 .collect(Collectors.toList()); 162 } catch (IllegalArgumentException iae) { 163 throw new PluginException(iae.getLocalizedMessage()); 164 } 165 } 166 167 @Override 168 public void previsit(Pool resources, StringTable strings) { 169 final Pattern p = Pattern.compile(".*((Data_)|(Names_))(?<tag>.*)\\.class"); 170 Pool.Module module = resources.getModule(MODULENAME); 171 172 // jdk.localedata module validation 173 Set<String> packages = module.getAllPackages(); 174 if (!packages.containsAll(LOCALEDATA_PACKAGES)) { 175 throw new PluginException(PluginsResourceBundle.getMessage(NAME + ".missingpackages") + 176 LOCALEDATA_PACKAGES.stream() 177 .filter(pn -> !packages.contains(pn)) 178 .collect(Collectors.joining(",\n\t"))); 179 } 180 181 available = Stream.concat(module.getContent().stream() 182 .map(md -> p.matcher(md.getPath())) 183 .filter(m -> m.matches()) 184 .map(m -> m.group("tag").replaceAll("_", "-")), 185 Stream.concat(Stream.of(jaJPJPTag), Stream.of(thTHTHTag))) 186 .distinct() 187 .sorted() 188 .map(IncludeLocalesPlugin::tagToLocale) 189 .collect(Collectors.toList()); 190 191 filtered = filterLocales(available); 192 193 if (filtered.isEmpty()) { 194 throw new PluginException(PluginsResourceBundle.getMessage(NAME + ".nomatchinglocales")); 195 } 196 197 try { 198 String value = META_FILES + filtered.stream() 199 .map(s -> includeLocaleFilePatterns(s)) 200 .collect(Collectors.joining(",")); 201 predicate = new ResourceFilter(Utils.listParser.apply(value), false); 202 } catch (IOException ex) { 203 throw new UncheckedIOException(ex); 204 } 205 } 206 207 private String includeLocaleFilePatterns(String tag) { 208 String pTag = tag.replaceAll("-", "_"); 209 String files = ""; 210 int lastDelimiter = tag.length(); 211 String isoSpecial = pTag.matches("^(he|yi|id).*") ? 212 pTag.replaceFirst("he", "iw") 213 .replaceFirst("yi", "ji") 214 .replaceFirst("id", "in") : ""; 215 216 // Add tag patterns including parents 217 while (true) { 218 pTag = pTag.substring(0, lastDelimiter); 219 files += INCLUDE_LOCALE_FILES.replaceAll("%%", pTag); 220 221 if (!isoSpecial.isEmpty()) { 222 isoSpecial = isoSpecial.substring(0, lastDelimiter); 223 files += INCLUDE_LOCALE_FILES.replaceAll("%%", isoSpecial); 224 } 225 226 lastDelimiter = pTag.lastIndexOf('_'); 227 if (lastDelimiter == -1) { 228 break; 229 } 230 } 231 232 final String lang = pTag; 233 234 // Add possible special locales of the COMPAT provider 235 files += Set.of(jaJPJPTag, noNONYTag, thTHTHTag).stream() 236 .filter(stag -> lang.equals(stag.substring(0,2))) 237 .map(t -> INCLUDE_LOCALE_FILES.replaceAll("%%", t.replaceAll("-", "_"))) 238 .collect(Collectors.joining(",")); 239 240 // Add possible UN.M49 files (unconditional for now) for each language 241 files += INCLUDE_LOCALE_FILES.replaceAll("%%", lang + "_[0-9]{3}"); 242 if (!isoSpecial.isEmpty()) { 243 files += INCLUDE_LOCALE_FILES.replaceAll("%%", isoSpecial + "_[0-9]{3}"); 244 } 245 246 // Add Thai BreakIterator related files 247 if (lang.equals("th")) { 248 files += "*sun/text/resources/thai_dict," + 249 "*sun/text/resources/[^\\/]+_th,"; 250 } 251 252 // Add Taiwan resource bundles for Hong Kong 253 if (tag.startsWith("zh-HK")) { 254 files += INCLUDE_LOCALE_FILES.replaceAll("%%", "zh_TW"); 255 } 256 257 return files; 258 } 259 260 private boolean stripUnsupportedLocales(byte[] bytes, ClassReader cr) { 261 char[] buf = new char[cr.getMaxStringLength()]; 262 boolean[] modified = new boolean[1]; 263 264 IntStream.range(1, cr.getItemCount()) 265 .map(item -> cr.getItem(item)) 266 .forEach(itemIndex -> { 267 if (bytes[itemIndex - 1] == 1 && // UTF-8 268 bytes[itemIndex + 2] == (byte)' ') { // fast check for leading space 269 int length = cr.readUnsignedShort(itemIndex); 270 byte[] b = new byte[length]; 271 System.arraycopy(bytes, itemIndex + 2, b, 0, length); 272 if (filterOutUnsupportedTags(b)) { 273 // copy back 274 System.arraycopy(b, 0, bytes, itemIndex + 2, length); 275 modified[0] = true; 276 } 277 } 278 }); 279 280 return modified[0]; 281 } 282 283 private boolean filterOutUnsupportedTags(byte[] b) { 284 List<Locale> locales; 285 286 try { 287 locales = Arrays.asList(new String(b).split(" ")).stream() 288 .filter(tag -> !tag.isEmpty()) 289 .map(IncludeLocalesPlugin::tagToLocale) 290 .collect(Collectors.toList()); 291 } catch (IllformedLocaleException ile) { 292 // Seems not an available locales string literal. 293 return false; 294 } 295 296 byte[] filteredBytes = filterLocales(locales).stream() 297 .collect(Collectors.joining(" ")) 298 .getBytes(); 299 System.arraycopy(filteredBytes, 0, b, 0, filteredBytes.length); 300 Arrays.fill(b, filteredBytes.length, b.length, (byte)' '); 301 return true; 302 } 303 304 private List<String> filterLocales(List<Locale> locales) { 305 List<String> ret = 306 Locale.filter(priorityList, locales, Locale.FilteringMode.EXTENDED_FILTERING).stream() 307 .map(loc -> 308 // Locale.filter() does not preserve the case, which is 309 // significant for "variant" equality. Retrieve the original 310 // locales from the pre-filtered list. 311 locales.stream() 312 .filter(l -> l.toString().equalsIgnoreCase(loc.toString())) 313 .findAny() 314 .orElse(Locale.ROOT) 315 .toLanguageTag()) 316 .collect(Collectors.toList()); 317 318 // no-NO-NY.toLanguageTag() returns "nn-NO", so specially handle it here 319 if (ret.contains("no-NO")) { 320 ret.add(noNONYTag); 321 } 322 323 return ret; 324 } 325 326 private static final Locale.Builder LOCALE_BUILDER = new Locale.Builder(); 327 private static Locale tagToLocale(String tag) { 328 // ISO3166 compatibility 329 tag = tag.replaceFirst("^iw", "he").replaceFirst("^ji", "yi").replaceFirst("^in", "id"); 330 331 switch (tag) { 332 case jaJPJPTag: 333 return jaJPJP; 334 case noNONYTag: 335 return noNONY; 336 case thTHTHTag: 337 return thTHTH; 338 default: 339 LOCALE_BUILDER.clear(); 340 LOCALE_BUILDER.setLanguageTag(tag); 341 return LOCALE_BUILDER.build(); 342 } 343 } 344 } --- EOF ---