1 /* Native2ASCII.java - native2ascii program
2 Copyright (C) 2003 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package gnu.classpath.tools.native2ascii;
41 import gnu.classpath.tools.common.ClasspathToolParser;
42 import gnu.classpath.tools.getopt.FileArgumentCallback;
43 import gnu.classpath.tools.getopt.Option;
44 import gnu.classpath.tools.getopt.OptionException;
45 import gnu.classpath.tools.getopt.Parser;
47 import java.io.BufferedReader;
48 import java.io.BufferedWriter;
49 import java.io.FileInputStream;
50 import java.io.FileOutputStream;
51 import java.io.InputStream;
52 import java.io.InputStreamReader;
53 import java.io.OutputStream;
54 import java.io.OutputStreamWriter;
55 import java.io.PrintWriter;
58 * Native2ASCII main program.
59 * @author Ito Kazumitsu <kaz@maczuka.gcd.org>
61 public class Native2ASCII
69 // True for reverse operation.
72 private class HandleFile extends FileArgumentCallback
78 public void notifyFile(String fileArgument)
79 throws OptionException
83 else if (output == null)
84 output = fileArgument;
86 throw new OptionException(Messages.getString("Native2ASCII.TooManyFiles")); //$NON-NLS-1$
90 private Parser createParser()
92 Parser result = new ClasspathToolParser("native2ascii", true); //$NON-NLS-1$
93 result.setHeader(Messages.getString("Native2ASCII.Usage")); //$NON-NLS-1$
95 result.add(new Option("encoding", Messages.getString("Native2ASCII.EncodingHelp"), Messages.getString("Native2ASCII.EncodingArgName")) //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
97 public void parsed(String argument) throws OptionException
100 throw new OptionException(Messages.getString("Native2ASCII.EncodingSpecified")); //$NON-NLS-1$
104 result.add(new Option("reversed", Messages.getString("Native2ASCII.ReversedHelp")) //$NON-NLS-1$ //$NON-NLS-2$
106 public void parsed(String argument) throws OptionException
115 private void run(String[] args)
117 Parser argParser = createParser();
118 argParser.parse(args, new HandleFile());
120 if (encoding == null)
121 encoding = System.getProperty("file.encoding"); //$NON-NLS-1$
124 InputStream is = (input == null ? System.in
125 : new FileInputStream(input));
126 OutputStream os = (output == null ? (OutputStream) System.out
127 : new FileOutputStream(output));
129 BufferedReader rdr = new BufferedReader(new InputStreamReader(is,
131 PrintWriter wtr = new PrintWriter(
133 new OutputStreamWriter(
138 String s = rdr.readLine();
141 StringBuffer sb = new StringBuffer(s.length() + 80);
142 for (int i = 0; i < s.length(); i++)
144 char c = s.charAt(i);
146 && i + 6 < s.length()
147 && s.charAt(i) == '\\'
148 && s.charAt(i + 1) == 'u')
150 int num = Integer.parseInt(s.substring(i + 2, i + 6), 16);
151 sb.append((char) num);
154 else if ((int)c <= 127 || reversed)
160 sb.append("\\u"); //$NON-NLS-1$
162 sb.append("00"); //$NON-NLS-1$
163 else if ((int)c <= 0xfff)
164 sb.append("0"); //$NON-NLS-1$
165 sb.append(Integer.toHexString((int) c));
168 wtr.println(sb.toString());
180 public static void main(String[] args)
182 new Native2ASCII().run(args);
183 String encoding = System.getProperty("file.encoding"); //$NON-NLS-1$