xref: /aosp_15_r20/external/cldr/tools/cldr-code/src/main/java/org/unicode/cldr/posix/GeneratePOSIX.java (revision 912701f9769bb47905792267661f0baf2b85bed5)
1 /*
2  **********************************************************************
3  * Copyright (c) 2002-2010, International Business Machines
4  * Corporation and others.  All Rights Reserved.
5  **********************************************************************
6  * Author: John Emmons & Mark Davis
7  **********************************************************************
8  */
9 package org.unicode.cldr.posix;
10 
11 import com.ibm.icu.dev.tool.shared.UOption;
12 import com.ibm.icu.text.UnicodeSet;
13 import java.io.File;
14 import java.io.PrintWriter;
15 import java.nio.charset.Charset;
16 import org.unicode.cldr.draft.FileUtilities;
17 
18 /**
19  * Class to generate POSIX format from CLDR.
20  *
21  * @author jcemmons/medavis
22  */
23 public class GeneratePOSIX {
24 
25     private static final int HELP1 = 0,
26             HELP2 = 1,
27             DESTDIR = 2,
28             MATCH = 3,
29             UNICODESET = 4,
30             COLLATESET = 5,
31             CHARSET = 6;
32 
33     private static final UOption[] options = {
34         UOption.HELP_H(),
35         UOption.HELP_QUESTION_MARK(),
36         UOption.create("destdir", 'd', UOption.REQUIRES_ARG).setDefault("."),
37         UOption.create("match", 'm', UOption.REQUIRES_ARG),
38         UOption.create("unicodeset", 'u', UOption.REQUIRES_ARG),
39         UOption.create("collateset", 'x', UOption.REQUIRES_ARG),
40         UOption.create("charset", 'c', UOption.REQUIRES_ARG).setDefault("UTF-8"),
41     };
42 
main(String[] args)43     public static void main(String[] args) throws Exception {
44         UOption.parseArgs(args, options);
45         if (!options[MATCH].doesOccur || options[HELP1].doesOccur || options[HELP2].doesOccur)
46             Usage();
47 
48         String[] matchargs = options[MATCH].value.split("@", 2);
49         String locale = matchargs[0];
50         POSIXVariant variant;
51 
52         if (options[MATCH].value.indexOf("@") > 0) variant = new POSIXVariant(matchargs[1]);
53         else variant = new POSIXVariant();
54 
55         String codeset = options[CHARSET].value;
56         UnicodeSet collate_set;
57         UnicodeSet repertoire;
58         if (options[COLLATESET].doesOccur) collate_set = new UnicodeSet(options[COLLATESET].value);
59         else collate_set = new UnicodeSet();
60 
61         if (options[UNICODESET].doesOccur) repertoire = new UnicodeSet(options[UNICODESET].value);
62         else repertoire = new UnicodeSet();
63 
64         if ((!codeset.equals("UTF-8"))
65                 && (options[COLLATESET].doesOccur || options[UNICODESET].doesOccur)) {
66             System.out.println(
67                     "Error: Specifying a non-UTF-8 codeset and repertoire or collation overrides are mutually exclusive.");
68             Usage();
69         }
70 
71         POSIXLocale pl =
72                 new POSIXLocale(
73                         locale,
74                         repertoire,
75                         Charset.forName(options[CHARSET].value),
76                         codeset,
77                         collate_set,
78                         variant);
79         PrintWriter out =
80                 FileUtilities.openUTF8Writer(
81                         options[DESTDIR].value + File.separator,
82                         options[MATCH].value + "." + codeset + ".src");
83         pl.write(out);
84         out.close();
85     }
86 
Usage()87     public static void Usage() {
88 
89         System.out.println("Usage: GeneratePOSIX [-d target_dir] -m locale_name[@variants]");
90         System.out.println(
91                 "                     { [-c codeset] | [-u repertoire_set][-x collation_set] }");
92         System.out.println("where:");
93         System.out.println(
94                 "   -d target_dir is the directory where POSIX .src files will be written");
95         System.out.println("   -m locale_name is the language/territory you want to generate");
96         System.out.println(
97                 "   -c codeset is the character set to use for the locale (Default = UTF-8)");
98         System.out.println(
99                 "   -u repertoire_set : Use to override the default repertoire set (UnicodeSet format)");
100         System.out.println(
101                 "   -x collation_set  : Use to override the default collation set (UnicodeSet format)");
102         System.exit(-1);
103     }
104 }
105