]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_4_2-src/tools/build/src/com/ibm/icu/dev/tool/index/IndexGenerator.java
go
[Dictionary.git] / jars / icu4j-4_4_2-src / tools / build / src / com / ibm / icu / dev / tool / index / IndexGenerator.java
1 /**\r
2 *******************************************************************************\r
3 * Copyright (C) 2005-2010, International Business Machines Corporation and    *\r
4 * others. All Rights Reserved.                                                *\r
5 *******************************************************************************\r
6 */\r
7 package com.ibm.icu.dev.tool.index;\r
8 \r
9 import java.io.BufferedWriter;\r
10 import java.io.File;\r
11 import java.io.FileWriter;\r
12 import java.io.IOException;\r
13 import java.io.PrintWriter;\r
14 import java.text.DateFormat;\r
15 import java.text.SimpleDateFormat;\r
16 import java.util.Date;\r
17 import java.util.Iterator;\r
18 import java.util.Locale;\r
19 import java.util.Set;\r
20 import java.util.TreeSet;\r
21 \r
22 public class IndexGenerator {\r
23     \r
24     private final static String stoplist = ",char.res,CurrencyData.res,invuca.res,line.res,line_th.res,pnames.res,res_index.res,sent.res,title.res,ucadata.res,ucase.res,uidna.res,unames.res,unorm.res,uprops.res,word.res,word_ja.res,word_POSIX.res,word_th.res";\r
25 \r
26     public static void main(String[] args) {\r
27         if (args.length < 1) {\r
28             usage("too few arguments");\r
29         }\r
30 \r
31         File inDir = new File(args[0]);\r
32         if (!inDir.exists()) {\r
33             System.out.println("skipping nonexistent directory " + inDir);\r
34             return;\r
35         }\r
36 \r
37         if (!inDir.isDirectory()) {\r
38             usage("first argument '" + inDir + "' must be a directory");\r
39         }\r
40 \r
41         File outDir = inDir;\r
42         if (args.length > 1) {\r
43             outDir = new File(args[1]);\r
44             if (!outDir.isDirectory() || !outDir.exists()) {\r
45                 usage("second argument must be existing directory");\r
46             }\r
47         }\r
48 \r
49         Set names = new TreeSet();\r
50         File[] files = inDir.listFiles();\r
51         if (files != null) {\r
52             for (int i = 0; i < files.length; i++){\r
53                 if (!files[i].isDirectory()) {\r
54                     String name = "," + files[i].getName(); // add ',' to get exact match\r
55                     if (name.endsWith(".res") && stoplist.indexOf(name) == -1) {\r
56                         names.add(name.substring(1, name.lastIndexOf('.'))); // 1 to trim off ','\r
57                     }\r
58                 }\r
59             }\r
60         }\r
61 \r
62         DateFormat fmt = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, Locale.US);\r
63         DateFormat copyfmt = new SimpleDateFormat("'# Copyright (C) 'yyyy' IBM Inc.  All Rights Reserved.'");\r
64 \r
65         try {\r
66             File outFile = new File(outDir, "res_index.txt");\r
67             PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter(outFile)));\r
68             Date now = new Date();\r
69             pw.println("# Generated by " + IndexGenerator.class.getName() + " on " + fmt.format(now));\r
70             pw.println("# from contents of " + inDir.getCanonicalPath());\r
71             pw.println(copyfmt.format(now));\r
72             Iterator i = names.iterator();\r
73             while (i.hasNext()) {\r
74                 pw.println(i.next());\r
75             }\r
76             int count = names.size();\r
77             pw.println("# Found " + count + " files");\r
78             pw.println("# End of file");\r
79             if (count == 0) {\r
80                 System.err.println("Warning: matched no files");\r
81             } \r
82             pw.close();\r
83         }\r
84         catch (IOException e) {\r
85             usage(e.getMessage());\r
86         }\r
87     }\r
88 \r
89     private static void usage(String msg) {\r
90         if (msg != null) {\r
91             System.err.println("Error: " + msg);\r
92         }\r
93         System.out.println("Usage: IndexGenerator inDir outDir");\r
94         System.out.println("  inDir is an existing directory whose locale-based resources are to be enumerated");\r
95         System.out.println("  outDir is an existing directory in which the res_index.txt file will be placed");\r
96         throw new IllegalStateException("Usage");\r
97     }\r
98 }\r
99 \r