xref: /aosp_15_r20/external/cldr/tools/cldr-code/src/main/java/org/unicode/cldr/icu/FixEras.java (revision 912701f9769bb47905792267661f0baf2b85bed5)
1 /*
2  * Copyright (c) 2004, International Business Machines
3  * Corporation and others.  All Rights Reserved.
4  *
5  */
6 /**
7  * @author Ram Viswanadha
8  */
9 package org.unicode.cldr.icu;
10 
11 import com.ibm.icu.dev.tool.shared.UOption;
12 import java.io.FileOutputStream;
13 import java.io.OutputStreamWriter;
14 import java.io.PrintWriter;
15 import org.unicode.cldr.util.LDMLUtilities;
16 import org.w3c.dom.Document;
17 import org.w3c.dom.NamedNodeMap;
18 import org.w3c.dom.Node;
19 
20 public class FixEras {
21     /** These must be kept in sync with getOptions(). */
22     private static final int HELP1 = 0;
23 
24     private static final int HELP2 = 1;
25     private static final int DESTDIR = 2;
26     private static final int SOURCEDIR = 3;
27     private static final UOption[] options =
28             new UOption[] {
29                 UOption.HELP_H(),
30                 UOption.HELP_QUESTION_MARK(),
31                 UOption.DESTDIR(),
32                 UOption.SOURCEDIR(),
33             };
34     private String destdir = null;
35     private String sourcedir = null;
36 
main(String[] args)37     public static void main(String[] args) {
38         FixEras cnv = new FixEras();
39         cnv.processArgs(args);
40     }
41 
usage()42     private void usage() {
43         System.out.println(
44                 "\nUsage: FixEras [OPTIONS] [XPATH1] [XPATH2]\n\n"
45                         + "This program is used to extract nodes from extract LDML file and merge \n"
46                         + "the extracted nodes with the main LDML file\n"
47                         + "Please refer to the following options. Options are not case sensitive.\n"
48                         + "Options:\n"
49                         + "-s or --sourcedir          source directory followed by the path.\n"
50                         + "-d or --destination        destination directory, followed by the path, default is current directory.\n"
51                         + "-h or -? or --help         this usage text.\n"
52                         + "example: com.ibm.icu.dev.tool.cldr.FixErs ar.xml\n");
53         System.exit(-1);
54     }
55 
processArgs(String[] args)56     private void processArgs(String[] args) {
57         int remainingArgc = 0;
58         try {
59             remainingArgc = UOption.parseArgs(args, options);
60         } catch (Exception e) {
61             System.err.println("ERROR: " + e.toString());
62             e.printStackTrace();
63             usage();
64         }
65         if (args.length == 0 || options[HELP1].doesOccur || options[HELP2].doesOccur) {
66             usage();
67         }
68 
69         if (options[DESTDIR].doesOccur) {
70             destdir = options[DESTDIR].value;
71         }
72         if (options[SOURCEDIR].doesOccur) {
73             sourcedir = options[SOURCEDIR].value;
74         }
75         if (destdir == null) {
76             throw new RuntimeException("Destination not specified");
77         }
78         if (remainingArgc < 1) {
79             usage();
80             System.exit(-1);
81         }
82         for (int i = 0; i < remainingArgc; i++) {
83             try {
84                 String sourcefile = null;
85                 String file = args[i];
86                 if (sourcedir != null) {
87                     sourcefile = sourcedir + "/" + file;
88                 } else {
89                     sourcefile = file;
90                 }
91                 Document maindoc = LDMLUtilities.parse(sourcefile, false);
92                 System.out.println("INFO: Fixing eras of " + file);
93                 fixEras(maindoc);
94                 maindoc.normalize();
95                 String destfile = destdir + "/" + file;
96                 OutputStreamWriter writer =
97                         new OutputStreamWriter(new FileOutputStream(destfile), "UTF-8");
98                 PrintWriter pw = new PrintWriter(writer);
99                 LDMLUtilities.printDOMTree(
100                         maindoc, pw, "http://www.unicode.org/cldr/dtd/1.3/ldml.dtd", null);
101                 writer.flush();
102                 writer.close();
103             } catch (Exception e) {
104                 e.printStackTrace();
105                 System.exit(-1);
106             }
107         }
108     }
109 
fixEras(Document doc)110     private void fixEras(Document doc) {
111         Node[] nodes = LDMLUtilities.getElementsByTagName(doc, LDMLConstants.ERA);
112         if (nodes != null) {
113             for (int i = 0; i < nodes.length; i++) {
114                 NamedNodeMap attr = nodes[i].getAttributes();
115                 Node type = attr.getNamedItem(LDMLConstants.TYPE);
116                 if (type != null) {
117                     String val = type.getNodeValue();
118                     int j = Integer.parseInt(val);
119                     if (j > 0) {
120                         j--;
121                     }
122                     type.setNodeValue(Integer.toString(j));
123                 }
124             }
125         }
126     }
127 }
128