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