1 /* |
|
2 * Copyright (c) 1996, 2012, Oracle and/or its affiliates. All rights reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. Oracle designates this |
|
8 * particular file as subject to the "Classpath" exception as provided |
|
9 * by Oracle in the LICENSE file that accompanied this code. |
|
10 * |
|
11 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 * version 2 for more details (a copy is included in the LICENSE file that |
|
15 * accompanied this code). |
|
16 * |
|
17 * You should have received a copy of the GNU General Public License version |
|
18 * 2 along with this work; if not, write to the Free Software Foundation, |
|
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 * |
|
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 * or visit www.oracle.com if you need additional information or have any |
|
23 * questions. |
|
24 */ |
|
25 |
|
26 /* |
|
27 */ |
|
28 |
|
29 /* |
|
30 Currently javac and load() method in java.util.Properties |
|
31 supports only Latin1 encoding input. |
|
32 But in Asian platforms programmer or message translator |
|
33 uses the editor which support othere than latin1 encoding |
|
34 to specify their native language string. |
|
35 So if programmer or message translator wants to use other than |
|
36 Latin1 character in his/her program source or properties file |
|
37 they must convert the file to ASCII plus \udddd notation. |
|
38 (javac/load() modification is not appropriate due to |
|
39 time constraints for JDK1.1) |
|
40 This utility is for the purpose of that conversion. |
|
41 |
|
42 NAME |
|
43 native2ascii - convert native encoding file to ascii file |
|
44 include \udddd Unicode notation |
|
45 |
|
46 SYNOPSIS |
|
47 native2ascii [options] [inputfile [outputfile]] |
|
48 |
|
49 DESCRIPTION |
|
50 If outputfile is not described standard output is used as |
|
51 output file, and if inputfile is not also described |
|
52 stardard input is used as input file. |
|
53 |
|
54 Options |
|
55 |
|
56 -reverse |
|
57 convert ascii with \udddd notation to native encoding |
|
58 |
|
59 -encoding encoding_name |
|
60 Specify the encoding name which is used by conversion. |
|
61 8859_[1 - 9], JIS, EUCJIS, SJIS is currently supported. |
|
62 Default encoding is taken from System property "file.encoding". |
|
63 |
|
64 */ |
|
65 |
|
66 package sun.tools.native2ascii; |
|
67 |
|
68 import java.io.*; |
|
69 import java.util.*; |
|
70 import java.text.MessageFormat; |
|
71 import java.nio.charset.CharsetEncoder; |
|
72 import java.nio.charset.Charset; |
|
73 import java.nio.charset.IllegalCharsetNameException; |
|
74 import java.io.UnsupportedEncodingException; |
|
75 import java.nio.charset.UnsupportedCharsetException; |
|
76 |
|
77 /** |
|
78 * Main program of the native2ascii |
|
79 */ |
|
80 |
|
81 public class Main { |
|
82 |
|
83 String inputFileName = null; |
|
84 String outputFileName = null; |
|
85 File tempFile = null; |
|
86 boolean reverse = false; |
|
87 static String encodingString = null; |
|
88 static String defaultEncoding = null; |
|
89 static CharsetEncoder encoder = null; |
|
90 |
|
91 /** |
|
92 * Run the converter |
|
93 */ |
|
94 public synchronized boolean convert(String argv[]) { |
|
95 List<String> v = new ArrayList<>(2); |
|
96 File outputFile = null; |
|
97 boolean createOutputFile = false; |
|
98 |
|
99 // Parse arguments |
|
100 for (int i = 0; i < argv.length; i++) { |
|
101 if (argv[i].equals("-encoding")) { |
|
102 if ((i + 1) < argv.length) { |
|
103 encodingString = argv[++i]; |
|
104 } else { |
|
105 error(getMsg("err.bad.arg")); |
|
106 usage(); |
|
107 return false; |
|
108 } |
|
109 } else if (argv[i].equals("-reverse")) { |
|
110 reverse = true; |
|
111 } else { |
|
112 if (v.size() > 1) { |
|
113 usage(); |
|
114 return false; |
|
115 } |
|
116 v.add(argv[i]); |
|
117 } |
|
118 } |
|
119 |
|
120 if (encodingString == null) { |
|
121 defaultEncoding = Charset.defaultCharset().name(); |
|
122 } |
|
123 char[] lineBreak = System.getProperty("line.separator").toCharArray(); |
|
124 |
|
125 try { |
|
126 initializeConverter(); |
|
127 |
|
128 if (v.size() == 1) { |
|
129 inputFileName = v.get(0); |
|
130 } |
|
131 |
|
132 if (v.size() == 2) { |
|
133 inputFileName = v.get(0); |
|
134 outputFileName = v.get(1); |
|
135 createOutputFile = true; |
|
136 } |
|
137 |
|
138 if (createOutputFile) { |
|
139 outputFile = new File(outputFileName); |
|
140 if (outputFile.exists() && !outputFile.canWrite()) { |
|
141 throw new Exception(formatMsg("err.cannot.write", outputFileName)); |
|
142 } |
|
143 } |
|
144 |
|
145 if (reverse) { |
|
146 try (BufferedReader reader = getA2NInput(inputFileName); |
|
147 Writer osw = getA2NOutput(outputFileName);) { |
|
148 String line; |
|
149 while ((line = reader.readLine()) != null) { |
|
150 osw.write(line.toCharArray()); |
|
151 osw.write(lineBreak); |
|
152 if (outputFileName == null) { // flush stdout |
|
153 osw.flush(); |
|
154 } |
|
155 } |
|
156 } |
|
157 } else { |
|
158 // N2A |
|
159 try (BufferedReader in = getN2AInput(inputFileName); |
|
160 BufferedWriter out = getN2AOutput(outputFileName);) { |
|
161 String inLine; |
|
162 while ((inLine = in.readLine()) != null) { |
|
163 out.write(inLine.toCharArray()); |
|
164 out.write(lineBreak); |
|
165 if (outputFileName == null) { // flush stdout |
|
166 out.flush(); |
|
167 } |
|
168 } |
|
169 } |
|
170 } |
|
171 |
|
172 // Since we are done rename temporary file to desired output file |
|
173 if (createOutputFile) { |
|
174 if (outputFile.exists()) { |
|
175 // Some win32 platforms can't handle atomic |
|
176 // rename if source and target file paths are |
|
177 // identical. To make things simple we just unconditionally |
|
178 // delete the target file before calling renameTo() |
|
179 outputFile.delete(); |
|
180 } |
|
181 tempFile.renameTo(outputFile); |
|
182 } |
|
183 } catch (Exception e) { |
|
184 error(e.toString()); |
|
185 return false; |
|
186 } |
|
187 |
|
188 return true; |
|
189 } |
|
190 |
|
191 private void error(String msg){ |
|
192 System.out.println(msg); |
|
193 } |
|
194 |
|
195 private void usage(){ |
|
196 System.out.println(getMsg("usage")); |
|
197 } |
|
198 |
|
199 |
|
200 private BufferedReader getN2AInput(String inFile) throws Exception { |
|
201 |
|
202 InputStream forwardIn; |
|
203 if (inFile == null) |
|
204 forwardIn = System.in; |
|
205 else { |
|
206 File f = new File(inFile); |
|
207 if (!f.canRead()){ |
|
208 throw new Exception(formatMsg("err.cannot.read", f.getName())); |
|
209 } |
|
210 |
|
211 try { |
|
212 forwardIn = new FileInputStream(inFile); |
|
213 } catch (IOException e) { |
|
214 throw new Exception(formatMsg("err.cannot.read", f.getName())); |
|
215 } |
|
216 } |
|
217 |
|
218 BufferedReader r = (encodingString != null) ? |
|
219 new BufferedReader(new InputStreamReader(forwardIn, |
|
220 encodingString)) : |
|
221 new BufferedReader(new InputStreamReader(forwardIn)); |
|
222 return r; |
|
223 } |
|
224 |
|
225 |
|
226 private BufferedWriter getN2AOutput(String outFile) throws Exception { |
|
227 Writer output; |
|
228 BufferedWriter n2aOut; |
|
229 |
|
230 if (outFile == null) |
|
231 output = new OutputStreamWriter(System.out,"US-ASCII"); |
|
232 |
|
233 else { |
|
234 File f = new File(outFile); |
|
235 |
|
236 File tempDir = f.getParentFile(); |
|
237 |
|
238 if (tempDir == null) |
|
239 tempDir = new File(System.getProperty("user.dir")); |
|
240 |
|
241 tempFile = File.createTempFile("_N2A", |
|
242 ".TMP", |
|
243 tempDir); |
|
244 tempFile.deleteOnExit(); |
|
245 |
|
246 try { |
|
247 output = new FileWriter(tempFile); |
|
248 } catch (IOException e){ |
|
249 throw new Exception(formatMsg("err.cannot.write", tempFile.getName())); |
|
250 } |
|
251 } |
|
252 |
|
253 n2aOut = new BufferedWriter(new N2AFilter(output)); |
|
254 return n2aOut; |
|
255 } |
|
256 |
|
257 private BufferedReader getA2NInput(String inFile) throws Exception { |
|
258 Reader in; |
|
259 BufferedReader reader; |
|
260 |
|
261 if (inFile == null) |
|
262 in = new InputStreamReader(System.in, "US-ASCII"); |
|
263 else { |
|
264 File f = new File(inFile); |
|
265 if (!f.canRead()){ |
|
266 throw new Exception(formatMsg("err.cannot.read", f.getName())); |
|
267 } |
|
268 |
|
269 try { |
|
270 in = new FileReader(inFile); |
|
271 } catch (Exception e) { |
|
272 throw new Exception(formatMsg("err.cannot.read", f.getName())); |
|
273 } |
|
274 } |
|
275 |
|
276 reader = new BufferedReader(new A2NFilter(in)); |
|
277 return reader; |
|
278 } |
|
279 |
|
280 private Writer getA2NOutput(String outFile) throws Exception { |
|
281 |
|
282 OutputStreamWriter w = null; |
|
283 OutputStream output = null; |
|
284 |
|
285 if (outFile == null) |
|
286 output = System.out; |
|
287 else { |
|
288 File f = new File(outFile); |
|
289 |
|
290 File tempDir = f.getParentFile(); |
|
291 if (tempDir == null) |
|
292 tempDir = new File(System.getProperty("user.dir")); |
|
293 tempFile = File.createTempFile("_N2A", |
|
294 ".TMP", |
|
295 tempDir); |
|
296 tempFile.deleteOnExit(); |
|
297 |
|
298 try { |
|
299 output = new FileOutputStream(tempFile); |
|
300 } catch (IOException e){ |
|
301 throw new Exception(formatMsg("err.cannot.write", tempFile.getName())); |
|
302 } |
|
303 } |
|
304 |
|
305 w = (encodingString != null) ? |
|
306 new OutputStreamWriter(output, encodingString) : |
|
307 new OutputStreamWriter(output); |
|
308 |
|
309 return (w); |
|
310 } |
|
311 |
|
312 private static Charset lookupCharset(String csName) { |
|
313 if (Charset.isSupported(csName)) { |
|
314 try { |
|
315 return Charset.forName(csName); |
|
316 } catch (UnsupportedCharsetException x) { |
|
317 throw new Error(x); |
|
318 } |
|
319 } |
|
320 return null; |
|
321 } |
|
322 |
|
323 public static boolean canConvert(char ch) { |
|
324 return (encoder != null && encoder.canEncode(ch)); |
|
325 } |
|
326 |
|
327 private static void initializeConverter() throws UnsupportedEncodingException { |
|
328 Charset cs = null; |
|
329 |
|
330 try { |
|
331 cs = (encodingString == null) ? |
|
332 lookupCharset(defaultEncoding): |
|
333 lookupCharset(encodingString); |
|
334 |
|
335 encoder = (cs != null) ? |
|
336 cs.newEncoder() : |
|
337 null; |
|
338 } catch (IllegalCharsetNameException e) { |
|
339 throw new Error(e); |
|
340 } |
|
341 } |
|
342 |
|
343 private static ResourceBundle rsrc; |
|
344 |
|
345 static { |
|
346 try { |
|
347 rsrc = ResourceBundle.getBundle( |
|
348 "sun.tools.native2ascii.resources.MsgNative2ascii"); |
|
349 } catch (MissingResourceException e) { |
|
350 throw new Error("Missing message file."); |
|
351 } |
|
352 } |
|
353 |
|
354 private String getMsg(String key) { |
|
355 try { |
|
356 return (rsrc.getString(key)); |
|
357 } catch (MissingResourceException e) { |
|
358 throw new Error("Error in message file format."); |
|
359 } |
|
360 } |
|
361 |
|
362 private String formatMsg(String key, String arg) { |
|
363 String msg = getMsg(key); |
|
364 return MessageFormat.format(msg, arg); |
|
365 } |
|
366 |
|
367 |
|
368 /** |
|
369 * Main program |
|
370 */ |
|
371 public static void main(String argv[]){ |
|
372 Main converter = new Main(); |
|
373 System.exit(converter.convert(argv) ? 0 : 1); |
|
374 } |
|
375 } |
|