01: /*
02: * Spoon - http://spoon.gforge.inria.fr/
03: * Copyright (C) 2006 INRIA Futurs <renaud.pawlak@inria.fr>
04: *
05: * This software is governed by the CeCILL-C License under French law and
06: * abiding by the rules of distribution of free software. You can use, modify
07: * and/or redistribute the software under the terms of the CeCILL-C license as
08: * circulated by CEA, CNRS and INRIA at http://www.cecill.info.
09: *
10: * This program is distributed in the hope that it will be useful, but WITHOUT
11: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12: * FITNESS FOR A PARTICULAR PURPOSE. See the CeCILL-C License for more details.
13: *
14: * The fact that you are presently reading this means that you have had
15: * knowledge of the CeCILL-C license and that you accept its terms.
16: */
17:
18: package spoon.support;
19:
20: import java.io.IOException;
21: import java.io.InputStream;
22: import java.io.ObjectInputStream;
23: import java.io.ObjectOutputStream;
24: import java.io.OutputStream;
25:
26: import spoon.reflect.Factory;
27: import spoon.reflect.ModelStreamer;
28: import spoon.reflect.declaration.CtElement;
29: import spoon.reflect.reference.CtReference;
30: import spoon.reflect.visitor.CtScanner;
31:
32: /**
33: * This class provides a regular Java serialization-based implementation of the
34: * model streamer.
35: */
36: public class SerializationModelStreamer implements ModelStreamer {
37:
38: /**
39: * Default constructor.
40: */
41: public SerializationModelStreamer() {
42: }
43:
44: public void save(Factory f, OutputStream out) throws IOException {
45: ObjectOutputStream oos = new ObjectOutputStream(out);
46: oos.writeObject(f);
47: oos.close();
48: }
49:
50: public Factory load(InputStream in) throws IOException {
51: try {
52: ObjectInputStream ois = new ObjectInputStream(in);
53: final Factory f = (Factory) ois.readObject();
54: new CtScanner() {
55: public void enter(CtElement e) {
56: e.setFactory(f);
57: super .enter(e);
58: }
59:
60: @Override
61: protected void enterReference(CtReference e) {
62: e.setFactory(f);
63: super .enterReference(e);
64: }
65: }.scan(f.Package().getAll());
66: ois.close();
67: return f;
68: } catch (ClassNotFoundException e) {
69: e.printStackTrace();
70: throw new IOException(e.getMessage());
71: }
72: }
73:
74: }
|