01: /*
02: * @(#)SimpleFunctionSerializer.java 1.1 05/06/21
03: *
04: * Copyright (c) 1997-2005 Sun Microsystems, Inc. All Rights Reserved.
05: *
06: * See the file "LICENSE.txt" for information on usage and redistribution
07: * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
08: */
09: package pnuts.lang;
10:
11: import java.io.*;
12:
13: class SimpleFunctionSerializer implements Runtime.FunctionSerializer {
14:
15: public void serialize(PnutsFunction pnutsFunction,
16: ObjectOutputStream s) throws IOException {
17: Function[] functions = pnutsFunction.functions;
18: int n = functions.length;
19: s.writeInt(n);
20: for (int i = 0; i < n; i++) {
21: Function f = functions[i];
22: if (f != null && f.node == null) {
23: s.writeObject(f.unparse(null));
24: f.writeAttributes(s);
25: // s.writeObject(f.pkg);
26: } else {
27: s.writeObject(f);
28: }
29: }
30: }
31:
32: public void deserialize(PnutsFunction pnutsFunction,
33: ObjectInputStream s) throws IOException,
34: ClassNotFoundException {
35: int n = s.readInt();
36: Function[] functions = new Function[n];
37: pnutsFunction.functions = functions;
38: for (int i = 0; i < n; i++) {
39: Object f = s.readObject();
40: if (f instanceof Function) {
41: functions[i] = (Function) f;
42: } else if (f instanceof SimpleNode) {
43: SimpleNode node = (SimpleNode) f;
44: Function func = new Function();
45: func.readAttributes(s);
46: // func.pkg = (Package)s.readObject();
47: func.node = node.jjtGetChild(1);
48: func.function = pnutsFunction;
49: functions[i] = func;
50: } else if (f instanceof String) {
51: try {
52: Function func = new Function();
53: func.readAttributes(s);
54: // func.pkg = (Package)s.readObject();
55: PnutsParser parser = Pnuts
56: .getParser(new StringReader((String) f));
57: SimpleNode node = parser.FunctionStatement(null);
58: func.node = node.jjtGetChild(1);
59: func.function = pnutsFunction;
60: functions[i] = func;
61: } catch (ParseException pe) {
62: pe.printStackTrace();
63: }
64: }
65: }
66: }
67:
68: }
|