Added runtime support to allow custom wrapping of XML objects

This commit is contained in:
igor%mir2.org 2004-08-02 14:46:33 +00:00
parent 81e82296c8
commit 73d09ce2c9
2 changed files with 32 additions and 2 deletions

View File

@ -38,6 +38,8 @@
package org.mozilla.javascript;
import org.mozilla.javascript.xml.XMLLib;
/**
* Embeddings that wish to provide their own custom wrappings for Java
* objects may extend this class and call
@ -141,7 +143,15 @@ public class WrapFactory
public Scriptable wrapAsJavaObject(Context cx, Scriptable scope,
Object javaObject, Class staticType)
{
return new NativeJavaObject(scope, javaObject, staticType);
Scriptable wrap = null;
XMLLib lib = XMLLib.extractFromScopeOrNull(scope);
if (lib != null) {
wrap = lib.wrapAsXMLOrNull(cx, javaObject);
}
if (wrap == null) {
wrap = new NativeJavaObject(scope, javaObject, staticType);
}
return wrap;
}
/**

View File

@ -39,13 +39,22 @@ import org.mozilla.javascript.*;
public abstract class XMLLib
{
public static XMLLib extractFromScope(Scriptable scope)
public static XMLLib extractFromScopeOrNull(Scriptable scope)
{
scope = ScriptableObject.getTopLevelScope(scope);
Object testXML = ScriptableObject.getClassPrototype(scope, "XML");
if (testXML instanceof XMLObject) {
return ((XMLObject)testXML).lib();
}
return null;
}
public static XMLLib extractFromScope(Scriptable scope)
{
XMLLib lib = extractFromScopeOrNull(scope);
if (lib != null) {
return lib;
}
String msg = ScriptRuntime.getMessage0("msg.XML.not.available");
throw Context.reportRuntimeError(msg);
}
@ -97,4 +106,15 @@ public abstract class XMLLib
* Construct namespace for default xml statement.
*/
public abstract Object toDefaultXmlNamespace(Context cx, Object uriValue);
/**
* Return wrap of the given Java object as appropritate XML object or
* null if Java object has no XML representation.
* The default implementation returns null to indicate no special
* wrapping of XML objects.
*/
public Scriptable wrapAsXMLOrNull(Context cx, Object javaObject)
{
return null;
}
}