001: package fri.patterns.interpreter.parsergenerator.builder;
002:
003: import java.io.*;
004: import java.util.*;
005: import fri.util.props.ConfigDir;
006: import fri.patterns.interpreter.parsergenerator.syntax.*;
007: import fri.patterns.interpreter.parsergenerator.syntax.builder.*;
008:
009: /**
010: Base class for serialization and deserialisation of Java-objects.
011:
012: @author (c) 2000, Fritz Ritzberger
013: */
014:
015: class SerializedObject {
016: /** Do-nothing constructor. */
017: public SerializedObject() {
018: }
019:
020: /**
021: Deserializes an object from filesystem.
022: @return deserialized object.
023: */
024: protected Object read(String fileName) {
025: fileName = makeFilePath(fileName);
026: System.err.println("deserializing object from " + fileName);
027: ObjectInputStream oin = null;
028: try {
029: FileInputStream in = new FileInputStream(fileName);
030: oin = new ObjectInputStream(in);
031: return oin.readObject();
032: } catch (Exception e) {
033: System.err.println(e.getMessage()); // tolerate non-existing object
034: return null;
035: } finally {
036: try {
037: oin.close();
038: } catch (Exception e) {
039: }
040: }
041: }
042:
043: /**
044: Serializes an object to filesystem.
045: @return true on success.
046: */
047: protected boolean write(String fileName, Object o) {
048: fileName = makeFilePath(fileName);
049: System.err.println("serializing object to " + fileName);
050: ObjectOutputStream oout = null;
051: try {
052: ensureDirectory(fileName);
053: FileOutputStream out = new FileOutputStream(fileName);
054: oout = new ObjectOutputStream(out);
055: oout.writeObject(o);
056: return true;
057: } catch (IOException e) {
058: e.printStackTrace();
059: return false;
060: } finally {
061: try {
062: oout.flush();
063: oout.close();
064: } catch (Exception e) {
065: }
066: }
067: }
068:
069: /**
070: When syntaxInput is a File, the name of the serialization file is created
071: from its basename (without any extension), else "Unknown" is assumed as basename.
072: The returned name has no path.
073: */
074: public static String baseNameFromSyntax(Object syntaxInput) {
075: String name;
076: if (syntaxInput instanceof File) {
077: File f = (File) syntaxInput;
078: name = f.getName();
079: int i = name.indexOf(".");
080: if (i > 0)
081: name = name.substring(0, i);
082: } else {
083: name = "Unknown";
084: }
085: return name;
086: }
087:
088: /** Converts a File, InputStream, Reader, String, StringBuffer, List of Lists or String[][] to a Syntax. */
089: public static Syntax toSyntax(Object syntaxInput) throws Exception {
090: Syntax syntax;
091: if (syntaxInput instanceof Syntax)
092: syntax = (Syntax) syntaxInput;
093: else if (syntaxInput instanceof String[][])
094: syntax = new Syntax((String[][]) syntaxInput);
095: else if (syntaxInput instanceof List)
096: syntax = new Syntax((List) syntaxInput);
097: else
098: syntax = new SyntaxBuilder(syntaxInput).getSyntax();
099:
100: syntax.resolveSingulars();
101: return syntax;
102: }
103:
104: /** Puts the file into ".friware/parsers" directory in "user.home". */
105: protected String makeFilePath(String fileName) {
106: return ConfigDir.dir() + "parsers" + File.separator + fileName;
107: }
108:
109: /** Creates the directory of passed filename if it does not exist. @return the directory name. */
110: protected String ensureDirectory(String fileName) {
111: File file = new File(fileName);
112: String dir = file.getParent();
113:
114: if (dir != null) {
115: File directory = new File(dir);
116: if (directory.exists() == false)
117: directory.mkdirs();
118: }
119:
120: return dir;
121: }
122:
123: }
|