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         priorityList = Arrays.stream(config.get(NAME).split(","))
 159                 .map(Locale.LanguageRange::new)
 160                 .collect(Collectors.toList());
 161     }
 162 
 163     @Override
 164     public void previsit(Pool resources, StringTable strings) {
 165         final Pattern p = Pattern.compile(".*((Data_)|(Names_))(?<tag>.*)\\.class");
 166         Pool.Module module = resources.getModule(MODULENAME);
 167 
 168         // jdk.localedata module validation
 169         Set<String> packages = module.getAllPackages();
 170         if (!packages.containsAll(LOCALEDATA_PACKAGES)) {
 171             throw new PluginException(PluginsResourceBundle.getMessage(NAME + ".missingpackages") +
 172                 LOCALEDATA_PACKAGES.stream()
 173                     .filter(pn -> !packages.contains(pn))
 174                     .collect(Collectors.joining(",\n\t")));
 175         }
 176 
 177         available = Stream.concat(module.getContent().stream()
 178                                     .map(md -> p.matcher(md.getPath()))
 179                                     .filter(m -> m.matches())
 180                                     .map(m -> m.group("tag").replaceAll("_", "-")),
 181                                 Stream.concat(Stream.of(jaJPJPTag), Stream.of(thTHTHTag)))
 182             .distinct()
 183             .sorted()
 184             .map(IncludeLocalesPlugin::tagToLocale)
 185             .collect(Collectors.toList());
 186 
 187         filtered = filterLocales(available);
 188 
 189         if (filtered.isEmpty()) {
 190             throw new PluginException(PluginsResourceBundle.getMessage(NAME + ".nomatchinglocales"));
 191         }
 192 
 193         try {
 194             String value = META_FILES + filtered.stream()
 195                 .map(s -> includeLocaleFilePatterns(s))
 196                 .collect(Collectors.joining(","));
 197             predicate = new ResourceFilter(Utils.listParser.apply(value), false);
 198         } catch (IOException ex) {
 199             throw new UncheckedIOException(ex);
 200         }
 201     }
 202 
 203     private String includeLocaleFilePatterns(String tag) {
 204         String pTag = tag.replaceAll("-", "_");
 205         String files = "";
 206         int lastDelimiter = tag.length();
 207         String isoSpecial = pTag.matches("^(he|yi|id).*") ?
 208                             pTag.replaceFirst("he", "iw")
 209                                 .replaceFirst("yi", "ji")
 210                                 .replaceFirst("id", "in") : "";
 211 
 212         // Add tag patterns including parents
 213         while (true) {
 214             pTag = pTag.substring(0, lastDelimiter);
 215             files += INCLUDE_LOCALE_FILES.replaceAll("%%", pTag);
 216 
 217             if (!isoSpecial.isEmpty()) {
 218                 isoSpecial = isoSpecial.substring(0, lastDelimiter);
 219                 files += INCLUDE_LOCALE_FILES.replaceAll("%%", isoSpecial);
 220             }
 221 
 222             lastDelimiter = pTag.lastIndexOf('_');
 223             if (lastDelimiter == -1) {
 224                 break;
 225             }
 226         }
 227 
 228         final String lang = pTag;
 229 
 230         // Add possible special locales of the COMPAT provider
 231         files += Set.of(jaJPJPTag, noNONYTag, thTHTHTag).stream()
 232             .filter(stag -> lang.equals(stag.substring(0,2)))
 233             .map(t -> INCLUDE_LOCALE_FILES.replaceAll("%%", t.replaceAll("-", "_")))
 234             .collect(Collectors.joining(","));
 235 
 236         // Add possible UN.M49 files (unconditional for now) for each language
 237         files += INCLUDE_LOCALE_FILES.replaceAll("%%", lang + "_[0-9]{3}");
 238         if (!isoSpecial.isEmpty()) {
 239             files += INCLUDE_LOCALE_FILES.replaceAll("%%", isoSpecial + "_[0-9]{3}");
 240         }
 241 
 242         // Add Thai BreakIterator related files
 243         if (lang.equals("th")) {
 244             files += "*sun/text/resources/thai_dict," +
 245                      "*sun/text/resources/[^\\/]+_th,";
 246         }
 247 
 248         // Add Taiwan resource bundles for Hong Kong
 249         if (tag.startsWith("zh-HK")) {
 250             files += INCLUDE_LOCALE_FILES.replaceAll("%%", "zh_TW");
 251         }
 252 
 253         return files;
 254     }
 255 
 256     private boolean stripUnsupportedLocales(byte[] bytes, ClassReader cr) {
 257         char[] buf = new char[cr.getMaxStringLength()];
 258         boolean[] modified = new boolean[1];
 259 
 260         IntStream.range(1, cr.getItemCount())
 261             .map(item -> cr.getItem(item))
 262             .forEach(itemIndex -> {
 263                 if (bytes[itemIndex - 1] == 1 &&         // UTF-8
 264                     bytes[itemIndex + 2] == (byte)' ') { // fast check for leading space
 265                     int length = cr.readUnsignedShort(itemIndex);
 266                     byte[] b = new byte[length];
 267                     System.arraycopy(bytes, itemIndex + 2, b, 0, length);
 268                     if (filterOutUnsupportedTags(b)) {
 269                         // copy back
 270                         System.arraycopy(b, 0, bytes, itemIndex + 2, length);
 271                         modified[0] = true;
 272                     }
 273                 }
 274             });
 275 
 276         return modified[0];
 277     }
 278 
 279     private boolean filterOutUnsupportedTags(byte[] b) {
 280         List<Locale> locales;
 281 
 282         try {
 283             locales = Arrays.asList(new String(b).split(" ")).stream()
 284                 .filter(tag -> !tag.isEmpty())
 285                 .map(IncludeLocalesPlugin::tagToLocale)
 286                 .collect(Collectors.toList());
 287         } catch (IllformedLocaleException ile) {
 288             // Seems not an available locales string literal.
 289             return false;
 290         }
 291 
 292         byte[] filteredBytes = filterLocales(locales).stream()
 293             .collect(Collectors.joining(" "))
 294             .getBytes();
 295         System.arraycopy(filteredBytes, 0, b, 0, filteredBytes.length);
 296         Arrays.fill(b, filteredBytes.length, b.length, (byte)' ');
 297         return true;
 298     }
 299 
 300     private List<String> filterLocales(List<Locale> locales) {
 301         List<String> ret =
 302             Locale.filter(priorityList, locales, Locale.FilteringMode.EXTENDED_FILTERING).stream()
 303                 .map(loc ->
 304                     // Locale.filter() does not preserve the case, which is
 305                     // significant for "variant" equality. Retrieve the original
 306                     // locales from the pre-filtered list.
 307                     locales.stream()
 308                         .filter(l -> l.toString().equalsIgnoreCase(loc.toString()))
 309                         .findAny()
 310                         .orElse(Locale.ROOT)
 311                         .toLanguageTag())
 312                 .collect(Collectors.toList());
 313 
 314         // no-NO-NY.toLanguageTag() returns "nn-NO", so specially handle it here
 315         if (ret.contains("no-NO")) {
 316             ret.add(noNONYTag);
 317         }
 318 
 319         return ret;
 320     }
 321 
 322     private static final Locale.Builder LOCALE_BUILDER = new Locale.Builder();
 323     private static Locale tagToLocale(String tag) {
 324         // ISO3166 compatibility
 325         tag = tag.replaceFirst("^iw", "he").replaceFirst("^ji", "yi").replaceFirst("^in", "id");
 326 
 327         switch (tag) {
 328             case jaJPJPTag:
 329                 return jaJPJP;
 330             case noNONYTag:
 331                 return noNONY;
 332             case thTHTHTag:
 333                 return thTHTH;
 334             default:
 335                 LOCALE_BUILDER.clear();
 336                 LOCALE_BUILDER.setLanguageTag(tag);
 337                 return LOCALE_BUILDER.build();
 338         }
 339     }
 340 }