-
Notifications
You must be signed in to change notification settings - Fork 864
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Introduced a plugin-API and show usage with rhino-xml #1699
base: master
Are you sure you want to change the base?
Changes from all commits
9fcf15a
d1f6c65
8d39e60
1a97f97
742a928
5570349
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
module org.mozilla.javascript.xml { | ||
exports org.mozilla.javascript.xmlimpl; | ||
|
||
provides org.mozilla.javascript.xml.XMLLoader with | ||
org.mozilla.javascript.xmlimpl.XMLLoaderImpl; | ||
|
||
requires transitive org.mozilla.rhino; | ||
requires transitive java.xml; | ||
|
||
provides org.mozilla.javascript.Plugin with | ||
org.mozilla.javascript.xmlimpl.XmlPlugin; | ||
} |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package org.mozilla.javascript.xmlimpl; | ||
|
||
import org.mozilla.javascript.CompilerEnvirons; | ||
import org.mozilla.javascript.Context; | ||
import org.mozilla.javascript.LazilyLoadedCtor; | ||
import org.mozilla.javascript.Plugin; | ||
import org.mozilla.javascript.ScriptableObject; | ||
|
||
/** | ||
* Registers the XML objects in the scope. | ||
* | ||
* @author Roland Praml, Foconis Analytics GmbH | ||
*/ | ||
public class XmlPlugin implements Plugin { | ||
|
||
@Override | ||
public String getName() { | ||
return "xml"; | ||
} | ||
|
||
@Override | ||
public boolean isSafe() { | ||
return true; | ||
} | ||
|
||
@Override | ||
public void init(Context cx, ScriptableObject scope, boolean sealed) { | ||
if (cx.hasFeature(Context.FEATURE_E4X)) { | ||
String xmlImpl = XMLLibImpl.class.getName(); | ||
new LazilyLoadedCtor(scope, "XML", xmlImpl, sealed, true); | ||
new LazilyLoadedCtor(scope, "XMLList", xmlImpl, sealed, true); | ||
new LazilyLoadedCtor(scope, "Namespace", xmlImpl, sealed, true); | ||
new LazilyLoadedCtor(scope, "QName", xmlImpl, sealed, true); | ||
} | ||
} | ||
|
||
@Override | ||
public void initCompilerEnvirons(Context cx, CompilerEnvirons compilerEnvirons) { | ||
if (cx.hasFeature(Context.FEATURE_E4X)) { | ||
compilerEnvirons.setXmlAvailable(true); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
org.mozilla.javascript.xmlimpl.XmlPlugin |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
package org.mozilla.javascript.tests; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import org.junit.Test; | ||
import org.mozilla.javascript.Context; | ||
import org.mozilla.javascript.ContextFactory; | ||
import org.mozilla.javascript.Scriptable; | ||
|
||
/** | ||
* Test if XML is present. This test exists in "rhino-xml" where XML is present and in "rhino", | ||
* where XML is not on classpath. | ||
* | ||
* @author Roland Praml | ||
*/ | ||
public class XMLPresentTest { | ||
|
||
@Test | ||
public void testXMLPresent() { | ||
try (Context cx = ContextFactory.getGlobal().enterContext()) { | ||
Scriptable scope = cx.initStandardObjects(); | ||
Object result = | ||
cx.evaluateString( | ||
scope, "new XML('<a></a>').toXMLString();", "source", 1, null); | ||
assertEquals("<a/>", Context.toString(result)); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,7 +35,6 @@ | |
import org.mozilla.javascript.debug.DebuggableScript; | ||
import org.mozilla.javascript.debug.Debugger; | ||
import org.mozilla.javascript.xml.XMLLib; | ||
import org.mozilla.javascript.xml.XMLLoader; | ||
|
||
/** | ||
* This class represents the runtime context of an executing script. | ||
|
@@ -199,7 +198,8 @@ public class Context implements Closeable { | |
|
||
/** | ||
* Control if support for E4X(ECMAScript for XML) extension is available. If | ||
* hasFeature(FEATURE_E4X) returns true, the XML syntax is available. | ||
* hasFeature(FEATURE_E4X) returns true and rhino-xml is on classpath, the XML syntax is | ||
* available. | ||
* | ||
* <p>By default {@link #hasFeature(int)} returns true if the current JS version is set to | ||
* {@link #VERSION_DEFAULT} or is at least {@link #VERSION_1_6}. | ||
|
@@ -2327,11 +2327,8 @@ public boolean hasFeature(int featureIndex) { | |
*/ | ||
@Deprecated | ||
public XMLLib.Factory getE4xImplementationFactory() { | ||
XMLLoader loader = ScriptRuntime.loadOneServiceImplementation(XMLLoader.class); | ||
if (loader != null) { | ||
return loader.getFactory(); | ||
} | ||
return null; | ||
throw new UnsupportedOperationException( | ||
"getE4xImplementationFactory is no longer supported"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is the intention of this method? It can only provide the implementation class name. |
||
} | ||
|
||
/** | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,10 @@ | |
|
||
import java.security.AccessController; | ||
import java.security.PrivilegedAction; | ||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.ServiceLoader; | ||
|
||
/** | ||
* Factory class that Rhino runtime uses to create new {@link Context} instances. A <code> | ||
|
@@ -114,6 +118,26 @@ public class ContextFactory { | |
private volatile Object listeners; | ||
private boolean disabledListening; | ||
private ClassLoader applicationClassLoader; | ||
private final List<Plugin> plugins; | ||
|
||
/** returns a list of plugins found by the ServiceLoader */ | ||
public static List<Plugin> getDefaultPluginsFromServiceLoader() { | ||
List<Plugin> result = new ArrayList<Plugin>(); | ||
ServiceLoader.load(Plugin.class) | ||
.forEach( | ||
plugin -> { | ||
// TODO: use RhinoConfig here! | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. waiting for other PR |
||
String disabled = | ||
SecurityUtilities.getSystemProperty( | ||
"rhino.plugin." + plugin.getName() + ".disabled"); | ||
if ("1".equals(disabled) || "true".equals(disabled)) { | ||
// the plugin is disabled | ||
} else { | ||
result.add(plugin); | ||
} | ||
}); | ||
return result; | ||
} | ||
|
||
/** Listener of {@link Context} creation and release events. */ | ||
public interface Listener { | ||
|
@@ -127,6 +151,16 @@ public interface Listener { | |
public void contextReleased(Context cx); | ||
} | ||
|
||
/** Constructs a new ContextFactory with the plugins found by serviceLoader. */ | ||
public ContextFactory() { | ||
this(getDefaultPluginsFromServiceLoader()); | ||
} | ||
|
||
/** Constructs a new ContextFactory with a given list of plugins. */ | ||
public ContextFactory(List<Plugin> plugins) { | ||
this.plugins = Collections.unmodifiableList(plugins); | ||
} | ||
|
||
/** | ||
* Get global ContextFactory. | ||
* | ||
|
@@ -516,4 +550,9 @@ public final void exit() { | |
public final Context enterContext(Context cx) { | ||
return Context.enter(cx, this); | ||
} | ||
|
||
/** Returns a list of plugins. */ | ||
public List<Plugin> getPlugins() { | ||
return plugins; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package org.mozilla.javascript; | ||
|
||
/** | ||
* Plugins may be loaded with the serviceLocator. | ||
* | ||
* @author Roland Praml, Foconis Analytics GmbH | ||
*/ | ||
public interface Plugin { | ||
|
||
/** | ||
* defines, if this plugin should be initialized in safe standard objects or only in standard | ||
* objects | ||
*/ | ||
default boolean isSafe() { | ||
return false; | ||
} | ||
|
||
/** The name of the plugin. */ | ||
String getName(); | ||
|
||
/** Initializes the safe standard objects. */ | ||
default void init(Context cx, ScriptableObject scope, boolean sealed) {} | ||
|
||
/** Initialize the compiler environmnt. */ | ||
default void initCompilerEnvirons(Context cx, CompilerEnvirons compilerEnvirons) {} | ||
} |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
package org.mozilla.javascript.tests; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.fail; | ||
|
||
import org.junit.Test; | ||
import org.mozilla.javascript.Context; | ||
import org.mozilla.javascript.ContextFactory; | ||
import org.mozilla.javascript.EcmaError; | ||
import org.mozilla.javascript.Scriptable; | ||
|
||
/** | ||
* Test if XML is present. This test exists in "rhino-xml" where XML is present and in "rhino", | ||
* where XML is not on classpath. | ||
* | ||
* @author Roland Praml | ||
*/ | ||
public class XMLPresentTest { | ||
|
||
@Test | ||
public void testXMLPresent() { | ||
try (Context cx = ContextFactory.getGlobal().enterContext()) { | ||
Scriptable scope = cx.initStandardObjects(); | ||
Object result = | ||
cx.evaluateString( | ||
scope, "new XML('<a></a>').toXMLString();", "source", 1, null); | ||
fail("not expected"); | ||
} catch (EcmaError e) { | ||
assertEquals("ReferenceError: \"XML\" is not defined. (source#1)", e.getMessage()); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tried to remove all XML-Lib dependency to rhino-xml. There is still
org.mozilla.javascript.xml.XMLLib
andorg.mozilla.javascript.xml.XMLObject
in rhino base module. They are a bit difficult to remove