diff --git a/js/rhino/src/org/mozilla/javascript/WrapFactory.java b/js/rhino/src/org/mozilla/javascript/WrapFactory.java index 10c482929b0c..94ae0295c4a7 100644 --- a/js/rhino/src/org/mozilla/javascript/WrapFactory.java +++ b/js/rhino/src/org/mozilla/javascript/WrapFactory.java @@ -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; } /** diff --git a/js/rhino/src/org/mozilla/javascript/xml/XMLLib.java b/js/rhino/src/org/mozilla/javascript/xml/XMLLib.java index 154dd52379b6..2cdb7b721671 100644 --- a/js/rhino/src/org/mozilla/javascript/xml/XMLLib.java +++ b/js/rhino/src/org/mozilla/javascript/xml/XMLLib.java @@ -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; + } }