]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_2_1-src/src/com/ibm/richtext/textapps/MTextToString.java
icu4jsrc
[Dictionary.git] / jars / icu4j-4_2_1-src / src / com / ibm / richtext / textapps / MTextToString.java
1 /*\r
2  * (C) Copyright IBM Corp. 1998-2004.  All Rights Reserved.\r
3  *\r
4  * The program is provided "as is" without any warranty express or\r
5  * implied, including the warranty of non-infringement and the implied\r
6  * warranties of merchantibility and fitness for a particular purpose.\r
7  * IBM will not be liable for any damages suffered by you as a result\r
8  * of using the Program. In no event will IBM be liable for any\r
9  * special, indirect or consequential damages or lost profits even if\r
10  * IBM has been advised of the possibility of their occurrence. IBM\r
11  * will not be liable for any third party claims against you.\r
12  */\r
13 package com.ibm.richtext.textapps;\r
14 \r
15 import com.ibm.richtext.styledtext.MConstText;\r
16 \r
17 import java.io.*;\r
18 \r
19 public final class MTextToString {\r
20 \r
21     static final String COPYRIGHT =\r
22                 "(C) Copyright IBM Corp. 1998-1999 - All Rights Reserved";\r
23     public static void main(String[] args) {\r
24 \r
25 \r
26         if (args.length != 2) {\r
27             usage();\r
28         }\r
29         else {\r
30             writeMTextAsString(args[0], args[1]);\r
31         }\r
32     }\r
33 \r
34     private static void usage() {\r
35 \r
36         System.out.println("Usage: MTextToString inFile outFile");\r
37         System.out.println("inFile must be a serialized MConstText");\r
38         System.out.println("On exit, outFile will be a serialized String ");\r
39         System.out.println("containing the characters in the text.");\r
40         System.out.println("inFile and outFile must not be the same.");\r
41         System.exit(1);\r
42     }\r
43 \r
44     public static void writeMTextAsString(String inFile, String outFile) {\r
45 \r
46         File file = new File(inFile);\r
47         MConstText text = FileUtils.loadMText(file);\r
48 \r
49         if (text != null) {\r
50             char[] ch = new char[text.length()];\r
51             text.extractChars(0, ch.length, ch, 0);\r
52             String str = new String(ch);\r
53             writeString(str, outFile);\r
54         }\r
55         else {\r
56             System.out.println("Can't read inFile.");\r
57         }\r
58     }\r
59 \r
60     public static void writeString(String stringToWrite, String outFile) {\r
61 \r
62         File file = new File(outFile);\r
63         Throwable error = null;\r
64 \r
65         try {\r
66             OutputStream outStream = new FileOutputStream(file);\r
67             ObjectOutputStream objStream = new ObjectOutputStream(outStream);\r
68 \r
69             objStream.writeObject(stringToWrite);\r
70             outStream.close();\r
71             return;\r
72         }\r
73         catch(IOException e) {\r
74             error = e;\r
75         }\r
76         catch(ClassCastException e) {\r
77             error = e;\r
78         }\r
79 \r
80         error.printStackTrace();\r
81     }\r
82 }