KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > polyglot > util > TypeOutputStream


1 package polyglot.util;
2
3 import polyglot.main.Report;
4 import polyglot.types.*;
5
6 import java.io.*;
7 import java.util.*;
8
9 /** Output stream for writing type objects. */
10 public class TypeOutputStream extends ObjectOutputStream
11 {
12   protected TypeSystem ts;
13   protected Set roots;
14   protected Map placeHolders;
15   
16   public TypeOutputStream( OutputStream out, TypeSystem ts, Type root)
17     throws IOException
18   {
19     super( out);
20
21     this.ts = ts;
22     this.roots = ts.getTypeEncoderRootSet(root);
23     this.placeHolders = new HashMap();
24
25     if (Report.should_report(Report.serialize, 2)) {
26         Report.report(2, "Began TypeOutputStream with roots: " + roots);
27     }
28     
29     enableReplaceObject( true);
30   }
31
32   protected Object JavaDoc placeHolder(TypeObject o) {
33       Object JavaDoc k = new IdentityKey(o);
34       Object JavaDoc p = placeHolders.get(k);
35       if (p == null) {
36           p = ts.placeHolder(o, roots);
37           placeHolders.put(k, p);
38       }
39       return p;
40   }
41
42   protected Object JavaDoc replaceObject(Object JavaDoc o) throws IOException
43   {
44     if (roots.contains(o)) {
45       if (Report.should_report(Report.serialize, 2)) {
46     Report.report(2, "+ In roots: " + o + " : " + o.getClass());
47       }
48       return o;
49     }
50     else if (o instanceof TypeObject) {
51       Object JavaDoc r = placeHolder((TypeObject) o);
52       if (Report.should_report(Report.serialize, 2)) {
53         if (r != o) {
54           Report.report(2, "+ Replacing: " + o + " : " + o.getClass()
55         + " with " + r);
56         }
57     else {
58       Report.report(2, "+ " + o + " : " + o.getClass());
59         }
60       }
61       return r;
62     }
63     else {
64       if (Report.should_report(Report.serialize, 2)) {
65     Report.report(2, "+ " + o + " : " + o.getClass());
66       }
67       return o;
68     }
69   }
70 }
71
Popular Tags