File: IndexGenerator.java

package info (click to toggle)
icu4j-4.2 4.2.1.1-5
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 45,272 kB
  • ctags: 28,769
  • sloc: java: 258,969; xml: 4,191; perl: 3,097; sh: 68; makefile: 7
file content (90 lines) | stat: -rw-r--r-- 3,547 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/**
*******************************************************************************
* Copyright (C) 2005-2008, International Business Machines Corporation and    *
* others. All Rights Reserved.                                                *
*******************************************************************************
*/
package com.ibm.icu.dev.tool.index;

import java.io.*;
import java.text.*;
import java.util.*;

public class IndexGenerator {
    
    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";

    public static void main(String[] args) {
        if (args.length < 1) {
            usage("too few arguments");
        }

        File inDir = new File(args[0]);
        if (!inDir.exists()) {
            System.out.println("skipping nonexistent directory " + inDir);
            return;
        }

        if (!inDir.isDirectory()) {
            usage("first argument '" + inDir + "' must be a directory");
        }

        File outDir = inDir;
        if (args.length > 1) {
            outDir = new File(args[1]);
            if (!outDir.isDirectory() || !outDir.exists()) {
                usage("second argument must be existing directory");
            }
        }

        Set names = new TreeSet();
        File[] files = inDir.listFiles();
        if (files != null) {
            for (int i = 0; i < files.length; i++){
                if (!files[i].isDirectory()) {
                    String name = "," + files[i].getName(); // add ',' to get exact match
                    if (name.endsWith(".res") && stoplist.indexOf(name) == -1) {
                        names.add(name.substring(1, name.lastIndexOf('.'))); // 1 to trim off ','
                    }
                }
            }
        }

        DateFormat fmt = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, Locale.US);
        DateFormat copyfmt = new SimpleDateFormat("'# Copyright (C) 'yyyy' IBM Inc.  All Rights Reserved.'");

        try {
            File outFile = new File(outDir, "res_index.txt");
            PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter(outFile)));
            Date now = new Date();
            pw.println("# Generated by " + IndexGenerator.class.getName() + " on " + fmt.format(now));
            pw.println("# from contents of " + inDir.getCanonicalPath());
            pw.println(copyfmt.format(now));
            Iterator i = names.iterator();
            while (i.hasNext()) {
                pw.println(i.next());
            }
            int count = names.size();
            pw.println("# Found " + count + " files");
            pw.println("# End of file");
            if (count == 0) {
                System.err.println("Warning: matched no files");
            } 
            pw.close();
        }
        catch (IOException e) {
            usage(e.getMessage());
        }
    }

    private static void usage(String msg) {
        if (msg != null) {
            System.err.println("Error: " + msg);
        }
        System.out.println("Usage: IndexGenerator inDir outDir");
        System.out.println("  inDir is an existing directory whose locale-based resources are to be enumerated");
        System.out.println("  outDir is an existing directory in which the res_index.txt file will be placed");
        throw new IllegalStateException("Usage");
    }
}