• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package org.unicode.cldr.tool;
2 
3 import static org.unicode.cldr.util.PathUtilities.getNormalizedPathString;
4 
5 import java.io.BufferedReader;
6 import java.io.File;
7 import java.io.IOException;
8 import java.io.PrintWriter;
9 
10 import org.unicode.cldr.draft.FileUtilities;
11 import org.unicode.cldr.util.CLDRPaths;
12 import org.unicode.cldr.util.CldrUtility;
13 import org.unicode.cldr.util.CldrUtility.LineComparer;
14 import org.unicode.cldr.util.FileReaders;
15 import org.unicode.cldr.util.PathUtilities;
16 
17 /**
18  * Utilities for CLDR tools.
19  * Not used in Survey Tool.
20  * Moved here from CldrUtilities
21  * @author srl
22  *
23  */
24 public class ToolUtilities {
25 
registerExtraTransliterators()26     public static void registerExtraTransliterators() {
27         // NOTE: UTIL_DATA_DIR is required here only because TransliteratorUtilities
28         // requires a file path.
29 //        String tzadir = CLDRPaths.UTIL_DATA_DIR + File.separatorChar; // work around bad pattern (dir+filename)
30 //        // HACK around lack of Armenian, Ethiopic
31 //        TransliteratorUtilities.registerTransliteratorFromFile(tzadir, "Latin-Armenian");
32 //        // TransliteratorUtilities.registerTransliteratorFromFile(tzadir, "Latin-Ethiopic");
33 //        TransliteratorUtilities.registerTransliteratorFromFile(tzadir, "Cyrillic-Latin");
34 //        TransliteratorUtilities.registerTransliteratorFromFile(tzadir, "Arabic-Latin");
35 //        // needed
36 //        TransliteratorUtilities.registerTransliteratorFromFile(tzadir, "Thaana-Latin");
37 //        TransliteratorUtilities.registerTransliteratorFromFile(tzadir, "Syriac-Latin");
38 //        TransliteratorUtilities.registerTransliteratorFromFile(tzadir, "Canadian_Aboriginal-Latin");
39 //        TransliteratorUtilities.registerTransliteratorFromFile(tzadir, "Georgian-Latin");
40 //
41 //        // do nothing, too complicated to do quickly
42 //        TransliteratorUtilities.registerTransliteratorFromFile(tzadir, "Tibetan-Latin"); // needed
43 //        TransliteratorUtilities.registerTransliteratorFromFile(tzadir, "Khmer-Latin"); // needed
44 //        TransliteratorUtilities.registerTransliteratorFromFile(tzadir, "Lao-Latin");
45     }
46 
generateBat(String sourceDir, String sourceFile, String targetDir, String targetFile)47     static public void generateBat(String sourceDir, String sourceFile, String targetDir, String targetFile) {
48         generateBat(sourceDir, sourceFile, targetDir, targetFile, new CldrUtility.SimpleLineComparator(0));
49     }
50 
generateBat(String sourceDir, String sourceFile, String targetDir, String targetFile, LineComparer lineComparer)51     static public void generateBat(String sourceDir, String sourceFile, String targetDir, String targetFile,
52         LineComparer lineComparer) {
53         try {
54             String batDir = targetDir + "diff" + File.separator;
55             String batName = targetFile + ".bat";
56             String[] failureLines = new String[2];
57 
58             String fullSource = sourceDir + File.separator + sourceFile;
59             String fullTarget = targetDir + File.separator + targetFile;
60 
61             if (!new File(sourceDir, sourceFile).exists()) {
62                 File f = new File(batDir, batName);
63                 if (f.exists()) {
64                     if (DEBUG_SHOW_BAT) System.out.println("*Deleting old " + getNormalizedPathString(f));
65                     f.delete();
66                 }
67             } else if (!CldrUtility.areFileIdentical(fullSource, fullTarget, failureLines, lineComparer)) {
68                 PrintWriter bat = FileUtilities.openUTF8Writer(batDir, batName);
69                 try {
70                     bat.println(CLDRPaths.COMPARE_PROGRAM + " " +
71                             getNormalizedPathString(fullSource) + " " + getNormalizedPathString(fullTarget));
72                 } finally {
73                     bat.close();
74                 }
75             } else {
76                 File f = new File(batDir, batName);
77                 if (f.exists()) {
78                     if (DEBUG_SHOW_BAT) System.out.println("*Deleting old:\t" + getNormalizedPathString(f));
79                     f.delete();
80                 }
81                 f = new File(fullTarget);
82                 if (FileUtilities.SHOW_FILES) System.out.println("*Deleting old:\t" + getNormalizedPathString(f));
83                 f.delete();
84             }
85         } catch (IOException e) {
86             // TODO Auto-generated catch block
87             e.printStackTrace();
88         }
89     }
90 
91     /**
92      * Fetch data from jar
93      *
94      * @param name
95      *            a name residing in the org/unicode/cldr/tool/ directory, or loading from a jar will break.
96      */
getUTF8Data(String name)97     public static BufferedReader getUTF8Data(String name) {
98         if (new File(name).isAbsolute()) {
99             throw new IllegalArgumentException(
100                 "Path must be relative to org/unicode/cldr/tool  such as 'file.txt' or 'casing/file.txt', but got '"
101                     + name + "'.");
102         }
103 
104         return FileReaders.openFile(ToolUtilities.class, name);
105     }
106 
107     static final boolean DEBUG_SHOW_BAT = false;
108 
109 }
110