| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 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 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
public class ScriptableOutputStream extends ObjectOutputStream { |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 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(); |
| 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 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 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 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 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 |
|
| 108 |
|
| 109 |
public boolean hasExcludedName(String name) { |
| 110 |
return table.get(name) != null; |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
public void removeExcludedName(String name) { |
| 117 |
table.remove(name); |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
public void excludeStandardObjectNames() { |
| 125 |
String[] names = { "Object", "Object.prototype", |
| 126 |
"Function", "Function.prototype", |
| 127 |
"String", "String.prototype", |
| 128 |
"Math", |
| 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(); |
| 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 |
} |