OSDN Git Service

Merged gcj-eclipse branch to trunk.
[pf3gnuchains/gcc-fork.git] / libjava / classpath / tools / gnu / classpath / tools / native2ascii / Native2ASCII.java
1 /* Native2ASCII.java - native2ascii program
2  Copyright (C) 2003 Free Software Foundation, Inc.
3
4  This file is part of GNU Classpath.
5
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)
9  any later version.
10
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.
15
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
19  02110-1301 USA.
20
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
24  combination.
25
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. */
37
38
39 package gnu.classpath.tools.native2ascii;
40
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;
46
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;
56
57 /**
58  * Native2ASCII main program.
59  * @author Ito Kazumitsu <kaz@maczuka.gcd.org>
60  */
61 public class Native2ASCII
62 {
63   // Input file.
64   String input;
65   // Output file.
66   String output;
67   // Encoding to use.
68   String encoding;
69   // True for reverse operation.
70   boolean reversed;
71
72   private class HandleFile extends FileArgumentCallback
73   {
74     public HandleFile()
75     {
76     }
77
78     public void notifyFile(String fileArgument)
79       throws OptionException
80     {
81       if (input == null)
82         input = fileArgument;
83       else if (output == null)
84         output = fileArgument;
85       else
86         throw new OptionException(Messages.getString("Native2ASCII.TooManyFiles")); //$NON-NLS-1$
87     }
88   }
89
90   private Parser createParser()
91   {
92     Parser result = new ClasspathToolParser("native2ascii", true); //$NON-NLS-1$
93     result.setHeader(Messages.getString("Native2ASCII.Usage")); //$NON-NLS-1$
94
95     result.add(new Option("encoding", Messages.getString("Native2ASCII.EncodingHelp"), Messages.getString("Native2ASCII.EncodingArgName")) //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
96     {
97       public void parsed(String argument) throws OptionException
98       {
99         if (encoding != null)
100           throw new OptionException(Messages.getString("Native2ASCII.EncodingSpecified")); //$NON-NLS-1$
101         encoding = argument;
102       }
103     });
104     result.add(new Option("reversed", Messages.getString("Native2ASCII.ReversedHelp")) //$NON-NLS-1$ //$NON-NLS-2$
105     {
106       public void parsed(String argument) throws OptionException
107       {
108         reversed = true;
109       }
110     });
111
112     return result;
113   }
114
115   private void run(String[] args)
116   {
117     Parser argParser = createParser();
118     argParser.parse(args, new HandleFile());
119
120     if (encoding == null)
121       encoding = System.getProperty("file.encoding"); //$NON-NLS-1$
122     try
123       {
124         InputStream is = (input == null ? System.in
125                                         : new FileInputStream(input));
126         OutputStream os = (output == null ? (OutputStream) System.out
127                                           : new FileOutputStream(output));
128
129         BufferedReader rdr = new BufferedReader(new InputStreamReader(is,
130                                                                       encoding));
131         PrintWriter wtr = new PrintWriter(
132                                           new BufferedWriter(
133                                                              new OutputStreamWriter(
134                                                                                     os,
135                                                                                     encoding)));
136         while (true)
137           {
138             String s = rdr.readLine();
139             if (s == null)
140               break;
141             StringBuffer sb = new StringBuffer(s.length() + 80);
142             for (int i = 0; i < s.length(); i++)
143               {
144                 char c = s.charAt(i);
145                 if (reversed
146                     && i + 6 < s.length()
147                     && s.charAt(i) == '\\'
148                     && s.charAt(i + 1) == 'u')
149                   {
150                     int num = Integer.parseInt(s.substring(i + 2, i + 6), 16);
151                     sb.append((char) num);
152                     i += 5;
153                   }
154                 else if ((int)c <= 127 || reversed)
155                   {
156                     sb.append(c);
157                   }
158                 else
159                   {
160                     sb.append("\\u"); //$NON-NLS-1$
161                     if ((int)c <= 0xff)
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));
166                   }
167               }
168             wtr.println(sb.toString());
169           }
170         rdr.close();
171         wtr.flush();
172         wtr.close();
173       }
174     catch (Exception e)
175       {
176         e.printStackTrace();
177       }
178   }
179
180   public static void main(String[] args)
181   {
182     new Native2ASCII().run(args);
183     String encoding = System.getProperty("file.encoding"); //$NON-NLS-1$
184   }
185 }