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