KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > fri > patterns > interpreter > parsergenerator > builder > SerializedObject


1 package fri.patterns.interpreter.parsergenerator.builder;
2
3 import java.io.*;
4 import java.util.*;
5 import fri.util.props.ConfigDir;
6 import fri.patterns.interpreter.parsergenerator.syntax.*;
7 import fri.patterns.interpreter.parsergenerator.syntax.builder.*;
8
9 /**
10     Base class for serialization and deserialisation of Java-objects.
11     
12     @author (c) 2000, Fritz Ritzberger
13 */

14
15 class SerializedObject
16 {
17     /** Do-nothing constructor. */
18     public SerializedObject() {
19     }
20     
21     /**
22         Deserializes an object from filesystem.
23         @return deserialized object.
24     */

25     protected Object JavaDoc read(String JavaDoc fileName) {
26         fileName = makeFilePath(fileName);
27         System.err.println("deserializing object from "+fileName);
28         ObjectInputStream oin = null;
29         try {
30             FileInputStream in = new FileInputStream(fileName);
31             oin = new ObjectInputStream(in);
32             return oin.readObject();
33         }
34         catch (Exception JavaDoc e) {
35             System.err.println(e.getMessage()); // tolerate non-existing object
36
return null;
37         }
38         finally {
39             try { oin.close(); } catch (Exception JavaDoc e) {}
40         }
41     }
42     
43     /**
44         Serializes an object to filesystem.
45         @return true on success.
46     */

47     protected boolean write(String JavaDoc fileName, Object JavaDoc o) {
48         fileName = makeFilePath(fileName);
49         System.err.println("serializing object to "+fileName);
50         ObjectOutputStream oout = null;
51         try {
52             ensureDirectory(fileName);
53             FileOutputStream out = new FileOutputStream(fileName);
54             oout = new ObjectOutputStream(out);
55             oout.writeObject(o);
56             return true;
57         }
58         catch (IOException e) {
59             e.printStackTrace();
60             return false;
61         }
62         finally {
63             try { oout.flush(); oout.close(); } catch (Exception JavaDoc e) {}
64         }
65     }
66
67
68     /**
69         When syntaxInput is a File, the name of the serialization file is created
70         from its basename (without any extension), else "Unknown" is assumed as basename.
71         The returned name has no path.
72     */

73     public static String JavaDoc baseNameFromSyntax(Object JavaDoc syntaxInput) {
74         String JavaDoc name;
75         if (syntaxInput instanceof File) {
76             File f = (File) syntaxInput;
77             name = f.getName();
78             int i = name.indexOf(".");
79             if (i > 0)
80                 name = name.substring(0, i);
81         }
82         else {
83             name = "Unknown";
84         }
85         return name;
86     }
87
88
89     /** Converts a File, InputStream, Reader, String, StringBuffer, List of Lists or String[][] to a Syntax. */
90     public static Syntax toSyntax(Object JavaDoc syntaxInput)
91         throws Exception JavaDoc
92     {
93         Syntax syntax;
94         if (syntaxInput instanceof Syntax)
95             syntax = (Syntax) syntaxInput;
96         else
97         if (syntaxInput instanceof String JavaDoc[][])
98             syntax = new Syntax((String JavaDoc[][]) syntaxInput);
99         else
100         if (syntaxInput instanceof List)
101             syntax = new Syntax((List)syntaxInput);
102         else
103             syntax = new SyntaxBuilder(syntaxInput).getSyntax();
104             
105         syntax.resolveSingulars();
106         return syntax;
107     }
108     
109     
110     /** Puts the file into ".friware/parsers" directory in "user.home". */
111     protected String JavaDoc makeFilePath(String JavaDoc fileName) {
112         return ConfigDir.dir()+"parsers"+File.separator+fileName;
113     }
114     
115     /** Creates the directory of passed filename if it does not exist. @return the directory name. */
116     protected String JavaDoc ensureDirectory(String JavaDoc fileName) {
117         File file = new File(fileName);
118         String JavaDoc dir = file.getParent();
119         
120         if (dir != null) {
121             File directory = new File(dir);
122             if (directory.exists() == false)
123                 directory.mkdirs();
124         }
125         
126         return dir;
127     }
128     
129 }
Popular Tags