Implement global serialize() and deserialize() functions
This commit is contained in:
parent
4527bda2b5
commit
628cadcf49
1 changed files with 47 additions and 1 deletions
|
@ -23,6 +23,7 @@ import helma.util.HtmlEncoder;
|
||||||
import helma.util.MimePart;
|
import helma.util.MimePart;
|
||||||
import helma.util.XmlUtils;
|
import helma.util.XmlUtils;
|
||||||
import org.mozilla.javascript.*;
|
import org.mozilla.javascript.*;
|
||||||
|
import org.mozilla.javascript.serialize.*;
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.net.HttpURLConnection;
|
import java.net.HttpURLConnection;
|
||||||
|
@ -65,7 +66,8 @@ public class GlobalObject extends ImporterTopLevel implements PropertyRecorder {
|
||||||
"authenticate", "createSkin", "format", "encode",
|
"authenticate", "createSkin", "format", "encode",
|
||||||
"encodeXml", "encodeForm", "stripTags", "formatParagraphs",
|
"encodeXml", "encodeForm", "stripTags", "formatParagraphs",
|
||||||
"getXmlDocument", "getHtmlDocument", "seal",
|
"getXmlDocument", "getHtmlDocument", "seal",
|
||||||
"getDBConnection", "getURL", "write", "writeln"
|
"getDBConnection", "getURL", "write", "writeln",
|
||||||
|
"serialize", "deserialize"
|
||||||
};
|
};
|
||||||
|
|
||||||
defineFunctionProperties(globalFuncs, GlobalObject.class, 0);
|
defineFunctionProperties(globalFuncs, GlobalObject.class, 0);
|
||||||
|
@ -507,6 +509,50 @@ public class GlobalObject extends ImporterTopLevel implements PropertyRecorder {
|
||||||
return str;
|
return str;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void serialize(Context cx, Scriptable thisObj,
|
||||||
|
Object[] args, Function funObj)
|
||||||
|
throws IOException
|
||||||
|
{
|
||||||
|
if (args.length < 2) {
|
||||||
|
throw Context.reportRuntimeError(
|
||||||
|
"Expected an object to serialize and a filename to write " +
|
||||||
|
"the serialization to");
|
||||||
|
}
|
||||||
|
Object obj = args[0];
|
||||||
|
String filename = cx.toString(args[1]);
|
||||||
|
FileOutputStream fos = new FileOutputStream(filename);
|
||||||
|
Scriptable scope = ScriptableObject.getTopLevelScope(thisObj).getPrototype();
|
||||||
|
// use a ScriptableOutputStream that unwraps Wrappers
|
||||||
|
ScriptableOutputStream out = new ScriptableOutputStream(fos, scope) {
|
||||||
|
protected Object replaceObject(Object obj) throws IOException {
|
||||||
|
if (obj instanceof Wrapper)
|
||||||
|
obj = ((Wrapper) obj).unwrap();
|
||||||
|
return super.replaceObject(obj);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
out.writeObject(obj);
|
||||||
|
out.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Object deserialize(Context cx, Scriptable thisObj,
|
||||||
|
Object[] args, Function funObj)
|
||||||
|
throws IOException, ClassNotFoundException
|
||||||
|
{
|
||||||
|
if (args.length < 1) {
|
||||||
|
throw Context.reportRuntimeError(
|
||||||
|
"Expected a filename to read the serialization from");
|
||||||
|
}
|
||||||
|
String filename = cx.toString(args[0]);
|
||||||
|
FileInputStream fis = new FileInputStream(filename);
|
||||||
|
Scriptable scope = ScriptableObject.getTopLevelScope(thisObj).getPrototype();
|
||||||
|
ObjectInputStream in = new ScriptableInputStream(fis, scope);
|
||||||
|
Object deserialized = in.readObject();
|
||||||
|
in.close();
|
||||||
|
return cx.toObject(deserialized, scope);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
private static String toString(Object obj) {
|
private static String toString(Object obj) {
|
||||||
if (obj == null || obj == Undefined.instance) {
|
if (obj == null || obj == Undefined.instance) {
|
||||||
// Note: we might return "" here in order
|
// Note: we might return "" here in order
|
||||||
|
|
Loading…
Add table
Reference in a new issue