]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-52_1/tools/misc/src/com/ibm/icu/dev/tool/translit/Trans.java
Clean up imports.
[Dictionary.git] / jars / icu4j-52_1 / tools / misc / src / com / ibm / icu / dev / tool / translit / Trans.java
1 /**
2  *******************************************************************************
3  * Copyright (C) 2001-2010, International Business Machines Corporation and    *
4  * others. All Rights Reserved.                                                *
5  *******************************************************************************
6  */
7 package com.ibm.icu.dev.tool.translit;
8
9 import java.io.BufferedReader;
10 import java.io.FileInputStream;
11 import java.io.FileOutputStream;
12 import java.io.IOException;
13 import java.io.InputStreamReader;
14 import java.io.OutputStreamWriter;
15 import java.io.PrintWriter;
16
17 import com.ibm.icu.text.Transliterator;
18
19 /**
20  * A command-line interface to the ICU4J transliterators.
21  * @author Alan Liu
22  */
23 public class Trans {
24
25     public static void main(String[] args) throws Exception {
26         boolean isHTML = false;
27         int pos = 0;
28
29         String transName = null; // first untagged string is this
30         String inText = null; // all other untagged strings are this
31         String inName = null;
32         String outName = null;
33
34         while (pos < args.length) {
35             if (args[pos].equals("-html")) {
36                 isHTML = true;
37             } else if (args[pos].equals("-i")) {
38                 if (++pos == args.length) usage();
39                 inName = args[pos];
40             } else if (args[pos].equals("-o")) {
41                 if (++pos == args.length) usage();
42                 outName = args[pos];
43             } else if (transName == null) {
44                 transName = args[pos];
45             } else {
46                 if (inText == null) {
47                     inText = args[pos];
48                 } else {
49                     inText = inText + " " + args[pos];
50                 }
51             }
52             ++pos;
53         }
54
55         if (inText != null && inName != null) {
56             usage();
57         }
58
59         Transliterator trans = Transliterator.getInstance(transName);
60         BufferedReader in = null;
61         if (inName != null) {
62             in = new BufferedReader(new InputStreamReader(new FileInputStream(inName), "UTF8"));
63         }
64         PrintWriter out = null;
65         if (outName != null) {
66             out = new PrintWriter(new OutputStreamWriter(new FileOutputStream(outName), "UTF8"));
67         } else {
68             out = new PrintWriter(System.out);
69         }
70         trans(trans, inText, in, out, isHTML);
71         out.close();
72     }
73
74     static void trans(Transliterator trans, String inText,
75                       BufferedReader in, PrintWriter out, boolean isHTML) throws IOException {
76         boolean inTag = false; // If true, we are within a <tag>
77         for (;;) {
78             String line = null;
79             if (inText != null) {
80                 line = inText;
81                 inText = null;
82             } else if (in != null) {
83                 line = in.readLine();
84             }
85             if (line == null) {
86                 break;
87             }
88             if (isHTML) {
89                 // Pass tags between < and > unchanged
90                 StringBuffer buf = new StringBuffer();
91                 int right = -1;
92                 if (inTag) {
93                     right = line.indexOf('>');
94                     if (right < 0) {
95                         right = line.length()-1;
96                     }
97                     buf.append(line.substring(0, right+1));
98                     if (DEBUG) System.out.println("*S:" + line.substring(0, right+1));
99                     inTag = false;
100                 }
101                 for (;;) {
102                     int left = line.indexOf('<', right+1);
103                     if (left < 0) {
104                         if (right < line.length()-1) {
105                             buf.append(trans.transliterate(line.substring(right+1)));
106                             if (DEBUG) System.out.println("T:" + line.substring(right+1));
107                         }
108                         break;
109                     }
110                     // Append transliterated segment right+1..left-1
111                     buf.append(trans.transliterate(line.substring(right+1, left)));
112                     if (DEBUG) System.out.println("T:" + line.substring(right+1, left));
113                     right = line.indexOf('>', left+1);
114                     if (right < 0) {
115                         inTag = true;
116                         buf.append(line.substring(left));
117                         if (DEBUG) System.out.println("S:" + line.substring(left));
118                         break;
119                     }
120                     buf.append(line.substring(left, right+1));
121                     if (DEBUG) System.out.println("S:" + line.substring(left, right+1));
122                 }
123                 line = buf.toString();
124             } else {
125                 line = trans.transliterate(line);
126             }
127             out.println(line);
128         }
129     }
130
131     static final boolean DEBUG = false;
132
133     /**
134      * Emit usage and die.
135      */
136     static void usage() {
137         System.out.println("Usage: java com.ibm.icu.dev.tool.translit.Trans [-html] <trans> ( <input> | -i <infile>) [ -o <outfile> ]");
138         System.out.println("<trans>   Name of transliterator");
139         System.out.println("<input>   Text to transliterate");
140         System.out.println("<infile>  Name of input file");
141         System.out.println("<outfile> Name of output file");
142         System.out.println("-html     Only transliterate text outside of <tags>");
143         System.out.println("Input may come from the command line or a file.\n");
144         System.out.println("Ouput may go to stdout or a file.\n");
145         System.exit(0);
146     }
147 }