]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_2_1-src/src/com/ibm/richtext/textapps/StringToMText.java
icu4jsrc
[Dictionary.git] / jars / icu4j-4_2_1-src / src / com / ibm / richtext / textapps / StringToMText.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 import com.ibm.richtext.styledtext.StyledText;\r
17 import com.ibm.richtext.textlayout.attributes.AttributeMap;\r
18 \r
19 import java.io.*;\r
20 \r
21 public final class StringToMText {\r
22 \r
23     static final String COPYRIGHT =\r
24                 "(C) Copyright IBM Corp. 1998-1999 - All Rights Reserved";\r
25     public static void main(String[] args) {\r
26 \r
27 \r
28         if (args.length != 2 || args[0].equals(args[1])) {\r
29             usage();\r
30         }\r
31         else {\r
32             String str = loadString(new File(args[0]));\r
33             if (str == null) {\r
34                 throw new Error("Couldn't load String from file " + args[0]);\r
35             }\r
36             MConstText text = new StyledText(str, AttributeMap.EMPTY_ATTRIBUTE_MAP);\r
37             FileUtils.saveMText(new File(args[1]), text);\r
38         }\r
39     }\r
40 \r
41     private static void usage() {\r
42 \r
43         System.out.println("Usage: StringToMText inFile outFile");\r
44         System.out.println("inFile must be a serialized String");\r
45         System.out.println("On exit, outFile will be a serialized MText ");\r
46         System.out.println("containing the characters in the string.");\r
47         System.out.println("inFile and outFile must not be the same.");\r
48         System.exit(1);\r
49     }\r
50 \r
51     public static String loadString(File file) {\r
52 \r
53         Throwable error;\r
54 \r
55         try {\r
56             FileInputStream inStream = new FileInputStream(file);\r
57             ObjectInputStream objStream = new ObjectInputStream(inStream);\r
58 \r
59             String str = (String) objStream.readObject();\r
60             inStream.close();\r
61             return str;\r
62         }\r
63         catch(IOException e) {\r
64             error = e;\r
65         }\r
66         catch(ClassNotFoundException e) {\r
67             error = e;\r
68         }\r
69         catch(ClassCastException e) {\r
70             error = e;\r
71         }\r
72 \r
73         error.printStackTrace();\r
74         return null;\r
75     }\r
76 }