01: /*
02: * (C) Copyright IBM Corp. 1998-2004. All Rights Reserved.
03: *
04: * The program is provided "as is" without any warranty express or
05: * implied, including the warranty of non-infringement and the implied
06: * warranties of merchantibility and fitness for a particular purpose.
07: * IBM will not be liable for any damages suffered by you as a result
08: * of using the Program. In no event will IBM be liable for any
09: * special, indirect or consequential damages or lost profits even if
10: * IBM has been advised of the possibility of their occurrence. IBM
11: * will not be liable for any third party claims against you.
12: */
13: package com.ibm.richtext.textapps;
14:
15: import com.ibm.richtext.styledtext.MConstText;
16: import com.ibm.richtext.styledtext.StyledText;
17: import com.ibm.richtext.textlayout.attributes.AttributeMap;
18:
19: import java.io.*;
20:
21: public final class StringToMText {
22:
23: static final String COPYRIGHT = "(C) Copyright IBM Corp. 1998-1999 - All Rights Reserved";
24:
25: public static void main(String[] args) {
26:
27: if (args.length != 2 || args[0].equals(args[1])) {
28: usage();
29: } else {
30: String str = loadString(new File(args[0]));
31: if (str == null) {
32: throw new Error("Couldn't load String from file "
33: + args[0]);
34: }
35: MConstText text = new StyledText(str,
36: AttributeMap.EMPTY_ATTRIBUTE_MAP);
37: FileUtils.saveMText(new File(args[1]), text);
38: }
39: }
40:
41: private static void usage() {
42:
43: System.out.println("Usage: StringToMText inFile outFile");
44: System.out.println("inFile must be a serialized String");
45: System.out
46: .println("On exit, outFile will be a serialized MText ");
47: System.out.println("containing the characters in the string.");
48: System.out.println("inFile and outFile must not be the same.");
49: System.exit(1);
50: }
51:
52: public static String loadString(File file) {
53:
54: Throwable error;
55:
56: try {
57: FileInputStream inStream = new FileInputStream(file);
58: ObjectInputStream objStream = new ObjectInputStream(
59: inStream);
60:
61: String str = (String) objStream.readObject();
62: inStream.close();
63: return str;
64: } catch (IOException e) {
65: error = e;
66: } catch (ClassNotFoundException e) {
67: error = e;
68: } catch (ClassCastException e) {
69: error = e;
70: }
71:
72: error.printStackTrace();
73: return null;
74: }
75: }
|