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 java.io.FileOutputStream; 12 import java.io.OutputStreamWriter; 13 import java.io.PrintWriter; 14 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 import com.ibm.icu.dev.tool.UOption; 21 22 public class FixEras { 23 /** 24 * These must be kept in sync with getOptions(). 25 */ 26 private static final int HELP1 = 0; 27 private static final int HELP2 = 1; 28 private static final int DESTDIR = 2; 29 private static final int SOURCEDIR = 3; 30 private static final UOption[] options = new UOption[] { 31 UOption.HELP_H(), 32 UOption.HELP_QUESTION_MARK(), 33 UOption.DESTDIR(), 34 UOption.SOURCEDIR(), 35 36 }; 37 private String destdir = null; 38 private String sourcedir = null; 39 main(String[] args)40 public static void main(String[] args) { 41 FixEras cnv = new FixEras(); 42 cnv.processArgs(args); 43 } 44 usage()45 private void usage() { 46 System.out.println("\nUsage: FixEras [OPTIONS] [XPATH1] [XPATH2]\n\n" + 47 "This program is used to extract nodes from extract LDML file and merge \n" + 48 "the extracted nodes with the main LDML file\n" + 49 "Please refer to the following options. Options are not case sensitive.\n" + 50 "Options:\n" + 51 "-s or --sourcedir source directory followed by the path.\n" + 52 "-d or --destination destination directory, followed by the path, default is current directory.\n" + 53 "-h or -? or --help this usage text.\n" + 54 "example: com.ibm.icu.dev.tool.cldr.FixErs ar.xml\n"); 55 System.exit(-1); 56 } 57 processArgs(String[] args)58 private void processArgs(String[] args) { 59 int remainingArgc = 0; 60 try { 61 remainingArgc = UOption.parseArgs(args, options); 62 } catch (Exception e) { 63 System.err.println("ERROR: " + e.toString()); 64 e.printStackTrace(); 65 usage(); 66 } 67 if (args.length == 0 || options[HELP1].doesOccur || options[HELP2].doesOccur) { 68 usage(); 69 } 70 71 if (options[DESTDIR].doesOccur) { 72 destdir = options[DESTDIR].value; 73 } 74 if (options[SOURCEDIR].doesOccur) { 75 sourcedir = options[SOURCEDIR].value; 76 } 77 if (destdir == null) { 78 throw new RuntimeException("Destination not specified"); 79 } 80 if (remainingArgc < 1) { 81 usage(); 82 System.exit(-1); 83 } 84 for (int i = 0; i < remainingArgc; i++) { 85 try { 86 String sourcefile = null; 87 String file = args[i]; 88 if (sourcedir != null) { 89 sourcefile = sourcedir + "/" + file; 90 } else { 91 sourcefile = file; 92 } 93 Document maindoc = LDMLUtilities.parse(sourcefile, false); 94 System.out.println("INFO: Fixing eras of " + file); 95 fixEras(maindoc); 96 maindoc.normalize(); 97 String destfile = destdir + "/" + file; 98 OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(destfile), "UTF-8"); 99 PrintWriter pw = new PrintWriter(writer); 100 LDMLUtilities.printDOMTree(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