Debian Codegraph @

rhino/src/org/mozilla/javascript/serialize/ScriptableOutputStream.java

raw symbols SCIP
1 /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7 package org.mozilla.javascript.serialize;
8
9 import java.util.Map;
10 import java.util.HashMap;
11 import java.util.StringTokenizer;
12 import java.io.*;
13
14 import org.mozilla.javascript.*;
15
16 /**
17 * Class ScriptableOutputStream is an ObjectOutputStream used
18 * to serialize JavaScript objects and functions. Note that
19 * compiled functions currently cannot be serialized, only
20 * interpreted functions. The top-level scope containing the
21 * object is not written out, but is instead replaced with
22 * another top-level object when the ScriptableInputStream
23 * reads in this object. Also, object corresponding to names
24 * added to the exclude list are not written out but instead
25 * are looked up during deserialization. This approach avoids
26 * the creation of duplicate copies of standard objects
27 * during deserialization.
28 *
29 * @author Norris Boyd
30 */
31
32 // API class
33
34 public class ScriptableOutputStream extends ObjectOutputStream {
35
36 /**
37 * ScriptableOutputStream constructor.
38 * Creates a ScriptableOutputStream for use in serializing
39 * JavaScript objects. Calls excludeStandardObjectNames.
40 *
41 * @param out the OutputStream to write to.
42 * @param scope the scope containing the object.
43 */
44 public ScriptableOutputStream(OutputStream out, Scriptable scope)
45 throws IOException
46 {
47 super(out);
48 this.scope = scope;
49 table = new HashMap<Object,String>();
50 table.put(scope, "");
51 enableReplaceObject(true);
52 excludeStandardObjectNames(); // XXX
53 }
54
55 public void excludeAllIds(Object[] ids) {
56 for (Object id: ids) {
57 if (id instanceof String &&
58 (scope.get((String) id, scope) instanceof Scriptable))
59 {
60 this.addExcludedName((String)id);
61 }
62 }
63 }
64
65 /**
66 * Adds a qualified name to the list of object to be excluded from
67 * serialization. Names excluded from serialization are looked up
68 * in the new scope and replaced upon deserialization.
69 * @param name a fully qualified name (of the form "a.b.c", where
70 * "a" must be a property of the top-level object). The object
71 * need not exist, in which case the name is ignored.
72 * @throws IllegalArgumentException if the object is not a
73 * {@link Scriptable}.
74 */
75 public void addOptionalExcludedName(String name) {
76 Object obj = lookupQualifiedName(scope, name);
77 if(obj != null && obj != UniqueTag.NOT_FOUND) {
78 if (!(obj instanceof Scriptable)) {
79 throw new IllegalArgumentException(
80 "Object for excluded name " + name +
81 " is not a Scriptable, it is " +
82 obj.getClass().getName());
83 }
84 table.put(obj, name);
85 }
86 }
87
88 /**
89 * Adds a qualified name to the list of objects to be excluded from
90 * serialization. Names excluded from serialization are looked up
91 * in the new scope and replaced upon deserialization.
92 * @param name a fully qualified name (of the form "a.b.c", where
93 * "a" must be a property of the top-level object)
94 * @throws IllegalArgumentException if the object is not found or is not
95 * a {@link Scriptable}.
96 */
97 public void addExcludedName(String name) {
98 Object obj = lookupQualifiedName(scope, name);
99 if (!(obj instanceof Scriptable)) {
100 throw new IllegalArgumentException("Object for excluded name " +
101 name + " not found.");
102 }
103 table.put(obj, name);
104 }
105
106 /**
107 * Returns true if the name is excluded from serialization.
108 */
109 public boolean hasExcludedName(String name) {
110 return table.get(name) != null;
111 }
112
113 /**
114 * Removes a name from the list of names to exclude.
115 */
116 public void removeExcludedName(String name) {
117 table.remove(name);
118 }
119
120 /**
121 * Adds the names of the standard objects and their
122 * prototypes to the list of excluded names.
123 */
124 public void excludeStandardObjectNames() {
125 String[] names = { "Object", "Object.prototype",
126 "Function", "Function.prototype",
127 "String", "String.prototype",
128 "Math", // no Math.prototype
129 "Array", "Array.prototype",
130 "Error", "Error.prototype",
131 "Number", "Number.prototype",
132 "Date", "Date.prototype",
133 "RegExp", "RegExp.prototype",
134 "Script", "Script.prototype",
135 "Continuation", "Continuation.prototype",
136 };
137 for (int i=0; i < names.length; i++) {
138 addExcludedName(names[i]);
139 }
140
141 String[] optionalNames = {
142 "XML", "XML.prototype",
143 "XMLList", "XMLList.prototype",
144 };
145 for (int i=0; i < optionalNames.length; i++) {
146 addOptionalExcludedName(optionalNames[i]);
147 }
148 }
149
150 static Object lookupQualifiedName(Scriptable scope,
151 String qualifiedName)
152 {
153 StringTokenizer st = new StringTokenizer(qualifiedName, ".");
154 Object result = scope;
155 while (st.hasMoreTokens()) {
156 String s = st.nextToken();
157 result = ScriptableObject.getProperty((Scriptable)result, s);
158 if (result == null || !(result instanceof Scriptable))
159 break;
160 }
161 return result;
162 }
163
164 static class PendingLookup implements Serializable
165 {
166 static final long serialVersionUID = -2692990309789917727L;
167
168 PendingLookup(String name) { this.name = name; }
169
170 String getName() { return name; }
171
172 private String name;
173 }
174
175 @Override
176 protected Object replaceObject(Object obj) throws IOException
177 {
178 if (false) throw new IOException(); // suppress warning
179 String name = table.get(obj);
180 if (name == null)
181 return obj;
182 return new PendingLookup(name);
183 }
184
185 private Scriptable scope;
186 private Map<Object,String> table;
187 }